Skip to content

Commit 54316fd

Browse files
committed
Update duplicated when error message
1 parent c3ce858 commit 54316fd

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
@@ -13633,7 +13633,7 @@ parse_statements(pm_parser_t *parser, pm_context_t context) {
1363313633
*/
1363413634
static void
1363513635
pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) {
13636-
const pm_node_t *duplicated = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node);
13636+
const pm_node_t *duplicated = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node, true);
1363713637

1363813638
if (duplicated != NULL) {
1363913639
pm_buffer_t buffer = { 0 };
@@ -13659,13 +13659,16 @@ pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *liter
1365913659
*/
1366013660
static void
1366113661
pm_when_clause_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) {
13662-
if (pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node) != NULL) {
13662+
pm_node_t *previous;
13663+
13664+
if ((previous = pm_static_literals_add(&parser->newline_list, parser->start_line, literals, node, false)) != NULL) {
1366313665
pm_diagnostic_list_append_format(
1366413666
&parser->warning_list,
1366513667
node->location.start,
1366613668
node->location.end,
1366713669
PM_WARN_DUPLICATED_WHEN_CLAUSE,
13668-
pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line).line
13670+
pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line).line,
13671+
pm_newline_list_line_column(&parser->newline_list, previous->location.start, parser->start_line).line
1366913672
);
1367013673
}
1367113674
}
@@ -16215,7 +16218,7 @@ parse_pattern_hash_implicit_value(pm_parser_t *parser, pm_constant_id_list_t *ca
1621516218
*/
1621616219
static void
1621716220
parse_pattern_hash_key(pm_parser_t *parser, pm_static_literals_t *keys, pm_node_t *node) {
16218-
if (pm_static_literals_add(&parser->newline_list, parser->start_line, keys, node) != NULL) {
16221+
if (pm_static_literals_add(&parser->newline_list, parser->start_line, keys, node, true) != NULL) {
1621916222
pm_parser_err_node(parser, node, PM_ERR_PATTERN_HASH_KEY_DUPLICATE);
1622016223
}
1622116224
}
@@ -17973,6 +17976,8 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1797317976
// as frozen because when clause strings are frozen.
1797417977
if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
1797517978
pm_node_flag_set(condition, PM_STRING_FLAGS_FROZEN | PM_NODE_FLAG_STATIC_LITERAL);
17979+
} else if (PM_NODE_TYPE_P(condition, PM_SOURCE_FILE_NODE)) {
17980+
pm_node_flag_set(condition, PM_NODE_FLAG_STATIC_LITERAL);
1797617981
}
1797717982

1797817983
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
@@ -148,7 +148,7 @@ node_hash(const pm_static_literals_metadata_t *metadata, const pm_node_t *node)
148148
* and must be able to compare all node types that will be stored in this hash.
149149
*/
150150
static pm_node_t *
151-
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)) {
151+
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)) {
152152
// If we are out of space, we need to resize the hash. This will cause all
153153
// of the nodes to be rehashed and reinserted into the new hash.
154154
if (hash->size * 2 >= hash->capacity) {
@@ -196,9 +196,14 @@ pm_node_hash_insert(pm_node_hash_t *hash, const pm_static_literals_metadata_t *m
196196
// already in the hash. Otherwise, we can just increment the size and insert
197197
// the new node.
198198
pm_node_t *result = hash->nodes[index];
199-
if (result == NULL) hash->size++;
200199

201-
hash->nodes[index] = node;
200+
if (result == NULL) {
201+
hash->size++;
202+
hash->nodes[index] = node;
203+
} else if (replace) {
204+
hash->nodes[index] = node;
205+
}
206+
202207
return result;
203208
}
204209

@@ -335,7 +340,7 @@ pm_compare_regular_expression_nodes(PRISM_ATTRIBUTE_UNUSED const pm_static_liter
335340
* Add a node to the set of static literals.
336341
*/
337342
pm_node_t *
338-
pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node) {
343+
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) {
339344
switch (PM_NODE_TYPE(node)) {
340345
case PM_INTEGER_NODE:
341346
case PM_SOURCE_LINE_NODE:
@@ -347,6 +352,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
347352
.encoding_name = NULL
348353
},
349354
node,
355+
replace,
350356
pm_compare_integer_nodes
351357
);
352358
case PM_FLOAT_NODE:
@@ -358,6 +364,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
358364
.encoding_name = NULL
359365
},
360366
node,
367+
replace,
361368
pm_compare_float_nodes
362369
);
363370
case PM_RATIONAL_NODE:
@@ -370,6 +377,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
370377
.encoding_name = NULL
371378
},
372379
node,
380+
replace,
373381
pm_compare_number_nodes
374382
);
375383
case PM_STRING_NODE:
@@ -382,6 +390,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
382390
.encoding_name = NULL
383391
},
384392
node,
393+
replace,
385394
pm_compare_string_nodes
386395
);
387396
case PM_REGULAR_EXPRESSION_NODE:
@@ -393,6 +402,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
393402
.encoding_name = NULL
394403
},
395404
node,
405+
replace,
396406
pm_compare_regular_expression_nodes
397407
);
398408
case PM_SYMBOL_NODE:
@@ -404,26 +414,27 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line
404414
.encoding_name = NULL
405415
},
406416
node,
417+
replace,
407418
pm_compare_string_nodes
408419
);
409420
case PM_TRUE_NODE: {
410421
pm_node_t *duplicated = literals->true_node;
411-
literals->true_node = node;
422+
if ((duplicated == NULL) || replace) literals->true_node = node;
412423
return duplicated;
413424
}
414425
case PM_FALSE_NODE: {
415426
pm_node_t *duplicated = literals->false_node;
416-
literals->false_node = node;
427+
if ((duplicated == NULL) || replace) literals->false_node = node;
417428
return duplicated;
418429
}
419430
case PM_NIL_NODE: {
420431
pm_node_t *duplicated = literals->nil_node;
421-
literals->nil_node = node;
432+
if ((duplicated == NULL) || replace) literals->nil_node = node;
422433
return duplicated;
423434
}
424435
case PM_SOURCE_ENCODING_NODE: {
425436
pm_node_t *duplicated = literals->source_encoding_node;
426-
literals->source_encoding_node = node;
437+
if ((duplicated == NULL) || replace) literals->source_encoding_node = node;
427438
return duplicated;
428439
}
429440
default:

templates/src/diagnostic.c.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
364364
[PM_WARN_COMPARISON_AFTER_COMPARISON] = { "comparison '%.*s' after comparison", PM_WARNING_LEVEL_VERBOSE },
365365
[PM_WARN_DOT_DOT_DOT_EOL] = { "... at EOL, should be parenthesized?", PM_WARNING_LEVEL_DEFAULT },
366366
[PM_WARN_DUPLICATED_HASH_KEY] = { "key %.*s is duplicated and overwritten on line %" PRIi32, PM_WARNING_LEVEL_DEFAULT },
367-
[PM_WARN_DUPLICATED_WHEN_CLAUSE] = { "duplicated 'when' clause with line %" PRIi32 " is ignored", PM_WARNING_LEVEL_VERBOSE },
367+
[PM_WARN_DUPLICATED_WHEN_CLAUSE] = { "'when' clause on line %" PRIi32 " duplicates 'when' clause on line %" PRIi32 " and is ignored", PM_WARNING_LEVEL_VERBOSE },
368368
[PM_WARN_EQUAL_IN_CONDITIONAL_3_3] = { "found `= literal' in conditional, should be ==", PM_WARNING_LEVEL_DEFAULT },
369369
[PM_WARN_EQUAL_IN_CONDITIONAL] = { "found '= literal' in conditional, should be ==", PM_WARNING_LEVEL_DEFAULT },
370370
[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
@@ -48,7 +48,7 @@ def test_duplicated_hash_key
4848
end
4949

5050
def test_duplicated_when_clause
51-
assert_warning("case 1; when 1, 1; end", "clause with line")
51+
assert_warning("case 1; when 1, 1; end", "when' clause")
5252
end
5353

5454
def test_float_out_of_range

0 commit comments

Comments
 (0)