Skip to content

Commit b863f9a

Browse files
authored
Merge pull request #2812 from ruby/update-when-message
Update duplicated when error message
2 parents ca62c3f + 54316fd commit b863f9a

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

include/prism/static_literals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ typedef struct {
9595
* @param start_line The line number that the parser starts on.
9696
* @param literals The set of static literals to add the node to.
9797
* @param node The node to add to the set.
98+
* @param replace Whether to replace the previous node if one already exists.
9899
* @return A pointer to the node that is being overwritten, if there is one.
99100
*/
100-
pm_node_t * pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node);
101+
pm_node_t * pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace);
101102

102103
/**
103104
* Free the internal memory associated with the given static literals set.

src/prism.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13737,7 +13737,7 @@ parse_statements(pm_parser_t *parser, pm_context_t context) {
1373713737
*/
1373813738
static void
1373913739
pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) {
13740-
const pm_node_t *duplicated = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node);
13740+
const pm_node_t *duplicated = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node, true);
1374113741

1374213742
if (duplicated != NULL) {
1374313743
pm_buffer_t buffer = { 0 };
@@ -13763,13 +13763,16 @@ pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *liter
1376313763
*/
1376413764
static void
1376513765
pm_when_clause_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) {
13766-
if (pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node) != NULL) {
13766+
pm_node_t *previous;
13767+
13768+
if ((previous = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node, false)) != NULL) {
1376713769
pm_diagnostic_list_append_format(
1376813770
&parser->warning_list,
1376913771
node->location.start,
1377013772
node->location.end,
1377113773
PM_WARN_DUPLICATED_WHEN_CLAUSE,
13772-
pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line).line
13774+
pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line).line,
13775+
pm_newline_list_line_column(&parser->newline_list, previous->location.start, parser->start_line).line
1377313776
);
1377413777
}
1377513778
}
@@ -16370,7 +16373,7 @@ parse_pattern_hash_implicit_value(pm_parser_t *parser, pm_constant_id_list_t *ca
1637016373
*/
1637116374
static void
1637216375
parse_pattern_hash_key(pm_parser_t *parser, pm_static_literals_t *keys, pm_node_t *node) {
16373-
if (pm_static_literals_add(&parser->newline_list, parser->start_line, keys, node) != NULL) {
16376+
if (pm_static_literals_add(&parser->newline_list, parser->start_line, keys, node, true) != NULL) {
1637416377
pm_parser_err_node(parser, node, PM_ERR_PATTERN_HASH_KEY_DUPLICATE);
1637516378
}
1637616379
}
@@ -18132,6 +18135,8 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1813218135
// as frozen because when clause strings are frozen.
1813318136
if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
1813418137
pm_node_flag_set(condition, PM_STRING_FLAGS_FROZEN | PM_NODE_FLAG_STATIC_LITERAL);
18138+
} else if (PM_NODE_TYPE_P(condition, PM_SOURCE_FILE_NODE)) {
18139+
pm_node_flag_set(condition, PM_NODE_FLAG_STATIC_LITERAL);
1813518140
}
1813618141

1813718142
pm_when_clause_static_literals_add(parser, &literals, condition);

src/static_literals.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ node_hash(const pm_static_literals_metadata_t *metadata, const pm_node_t *node)
154154
* and must be able to compare all node types that will be stored in this hash.
155155
*/
156156
static pm_node_t *
157-
pm_node_hash_insert(pm_node_hash_t *hash, const pm_static_literals_metadata_t *metadata, pm_node_t *node, int (*compare)(const pm_static_literals_metadata_t *metadata, const pm_node_t *left, const pm_node_t *right)) {
157+
pm_node_hash_insert(pm_node_hash_t *hash, const pm_static_literals_metadata_t *metadata, pm_node_t *node, bool replace, int (*compare)(const pm_static_literals_metadata_t *metadata, const pm_node_t *left, const pm_node_t *right)) {
158158
// If we are out of space, we need to resize the hash. This will cause all
159159
// of the nodes to be rehashed and reinserted into the new hash.
160160
if (hash->size * 2 >= hash->capacity) {
@@ -202,9 +202,14 @@ pm_node_hash_insert(pm_node_hash_t *hash, const pm_static_literals_metadata_t *m
202202
// already in the hash. Otherwise, we can just increment the size and insert
203203
// the new node.
204204
pm_node_t *result = hash->nodes[index];
205-
if (result == NULL) hash->size++;
206205

207-
hash->nodes[index] = node;
206+
if (result == NULL) {
207+
hash->size++;
208+
hash->nodes[index] = node;
209+
} else if (replace) {
210+
hash->nodes[index] = node;
211+
}
212+
208213
return result;
209214
}
210215

@@ -348,7 +353,7 @@ pm_compare_regular_expression_nodes(PRISM_ATTRIBUTE_UNUSED const pm_static_liter
348353
* Add a node to the set of static literals.
349354
*/
350355
pm_node_t *
351-
pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node) {
356+
pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace) {
352357
switch (PM_NODE_TYPE(node)) {
353358
case PM_INTEGER_NODE:
354359
case PM_SOURCE_LINE_NODE:
@@ -360,6 +365,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
360365
.encoding_name = NULL
361366
},
362367
node,
368+
replace,
363369
pm_compare_integer_nodes
364370
);
365371
case PM_FLOAT_NODE:
@@ -371,6 +377,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
371377
.encoding_name = NULL
372378
},
373379
node,
380+
replace,
374381
pm_compare_float_nodes
375382
);
376383
case PM_RATIONAL_NODE:
@@ -383,6 +390,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
383390
.encoding_name = NULL
384391
},
385392
node,
393+
replace,
386394
pm_compare_number_nodes
387395
);
388396
case PM_STRING_NODE:
@@ -395,6 +403,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
395403
.encoding_name = NULL
396404
},
397405
node,
406+
replace,
398407
pm_compare_string_nodes
399408
);
400409
case PM_REGULAR_EXPRESSION_NODE:
@@ -406,6 +415,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
406415
.encoding_name = NULL
407416
},
408417
node,
418+
replace,
409419
pm_compare_regular_expression_nodes
410420
);
411421
case PM_SYMBOL_NODE:
@@ -417,26 +427,27 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
417427
.encoding_name = NULL
418428
},
419429
node,
430+
replace,
420431
pm_compare_string_nodes
421432
);
422433
case PM_TRUE_NODE: {
423434
pm_node_t *duplicated = literals->true_node;
424-
literals->true_node = node;
435+
if ((duplicated == NULL) || replace) literals->true_node = node;
425436
return duplicated;
426437
}
427438
case PM_FALSE_NODE: {
428439
pm_node_t *duplicated = literals->false_node;
429-
literals->false_node = node;
440+
if ((duplicated == NULL) || replace) literals->false_node = node;
430441
return duplicated;
431442
}
432443
case PM_NIL_NODE: {
433444
pm_node_t *duplicated = literals->nil_node;
434-
literals->nil_node = node;
445+
if ((duplicated == NULL) || replace) literals->nil_node = node;
435446
return duplicated;
436447
}
437448
case PM_SOURCE_ENCODING_NODE: {
438449
pm_node_t *duplicated = literals->source_encoding_node;
439-
literals->source_encoding_node = node;
450+
if ((duplicated == NULL) || replace) literals->source_encoding_node = node;
440451
return duplicated;
441452
}
442453
default:

templates/src/diagnostic.c.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
368368
[PM_WARN_COMPARISON_AFTER_COMPARISON] = { "comparison '%.*s' after comparison", PM_WARNING_LEVEL_VERBOSE },
369369
[PM_WARN_DOT_DOT_DOT_EOL] = { "... at EOL, should be parenthesized?", PM_WARNING_LEVEL_DEFAULT },
370370
[PM_WARN_DUPLICATED_HASH_KEY] = { "key %.*s is duplicated and overwritten on line %" PRIi32, PM_WARNING_LEVEL_DEFAULT },
371-
[PM_WARN_DUPLICATED_WHEN_CLAUSE] = { "duplicated 'when' clause with line %" PRIi32 " is ignored", PM_WARNING_LEVEL_VERBOSE },
371+
[PM_WARN_DUPLICATED_WHEN_CLAUSE] = { "'when' clause on line %" PRIi32 " duplicates 'when' clause on line %" PRIi32 " and is ignored", PM_WARNING_LEVEL_VERBOSE },
372372
[PM_WARN_EQUAL_IN_CONDITIONAL_3_3] = { "found `= literal' in conditional, should be ==", PM_WARNING_LEVEL_DEFAULT },
373373
[PM_WARN_EQUAL_IN_CONDITIONAL] = { "found '= literal' in conditional, should be ==", PM_WARNING_LEVEL_DEFAULT },
374374
[PM_WARN_END_IN_METHOD] = { "END in method; use at_exit", PM_WARNING_LEVEL_DEFAULT },

test/prism/warnings_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_duplicated_hash_key
6565
end
6666

6767
def test_duplicated_when_clause
68-
assert_warning("case 1; when 1, 1; end", "clause with line")
68+
assert_warning("case 1; when 1, 1; end", "when' clause")
6969
end
7070

7171
def test_float_out_of_range

0 commit comments

Comments
 (0)