Skip to content

Commit 92bab04

Browse files
makenowjustkddnewton
authored andcommitted
Fix and reuse pm_call_node_index_p
Fix #1925 Fix #1927 Previously pm_call_node_index_p does not check about a block argument correctly and is not used in parse_write to check an index call node. This commit fixes these problems.
1 parent 9a5bc4a commit 92bab04

File tree

4 files changed

+394
-223
lines changed

4 files changed

+394
-223
lines changed

src/prism.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,16 +1840,17 @@ pm_call_node_variable_call_p(pm_call_node_t *node) {
18401840
}
18411841

18421842
/**
1843-
* Returns whether or not this call is to the [] method in the index form (as
1844-
* opposed to `foo.[]`).
1843+
* Returns whether or not this call is to the [] method in the index form without a block (as
1844+
* opposed to `foo.[]` and `foo[] { }`).
18451845
*/
18461846
static inline bool
18471847
pm_call_node_index_p(pm_call_node_t *node) {
18481848
return (
18491849
(node->call_operator_loc.start == NULL) &&
18501850
(node->message_loc.start != NULL) &&
18511851
(node->message_loc.start[0] == '[') &&
1852-
(node->message_loc.end[-1] == ']')
1852+
(node->message_loc.end[-1] == ']') &&
1853+
(node->block == NULL || PM_NODE_TYPE_P(node->block, PM_BLOCK_ARGUMENT_NODE))
18531854
);
18541855
}
18551856

@@ -10827,13 +10828,7 @@ parse_write(pm_parser_t *parser, pm_node_t *target, pm_token_t *operator, pm_nod
1082710828
// If there is no call operator and the message is "[]" then this is
1082810829
// an aref expression, and we can transform it into an aset
1082910830
// expression.
10830-
if (
10831-
(call->call_operator_loc.start == NULL) &&
10832-
(call->message_loc.start != NULL) &&
10833-
(call->message_loc.start[0] == '[') &&
10834-
(call->message_loc.end[-1] == ']') &&
10835-
(call->block == NULL)
10836-
) {
10831+
if (pm_call_node_index_p(call)) {
1083710832
if (call->arguments == NULL) {
1083810833
call->arguments = pm_arguments_node_create(parser);
1083910834
}

test/prism/errors_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,33 @@ def test_call_with_block_operator_write
12561256
]
12571257
end
12581258

1259+
def test_index_call_with_block_and_write
1260+
source = "foo[1] {} &&= 1"
1261+
assert_errors expression(source), source, [
1262+
["Unexpected write target", 0..9],
1263+
["Unexpected operator after a call with arguments", 10..13],
1264+
["Unexpected operator after a call with a block", 10..13]
1265+
]
1266+
end
1267+
1268+
def test_index_call_with_block_or_write
1269+
source = "foo[1] {} ||= 1"
1270+
assert_errors expression(source), source, [
1271+
["Unexpected write target", 0..9],
1272+
["Unexpected operator after a call with arguments", 10..13],
1273+
["Unexpected operator after a call with a block", 10..13]
1274+
]
1275+
end
1276+
1277+
def test_index_call_with_block_operator_write
1278+
source = "foo[1] {} += 1"
1279+
assert_errors expression(source), source, [
1280+
["Unexpected write target", 0..9],
1281+
["Unexpected operator after a call with arguments", 10..12],
1282+
["Unexpected operator after a call with a block", 10..12]
1283+
]
1284+
end
1285+
12591286
def test_writing_numbered_parameter
12601287
assert_errors expression("-> { _1 = 0 }"), "-> { _1 = 0 }", [
12611288
["_1 is reserved for a numbered parameter", 5..7]

test/prism/fixtures/arrays.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ foo[bar] = baz
8181

8282
%w[\C:]
8383

84+
foo[&bar] = 1
85+
86+
foo.foo[&bar] = 1
87+
88+
def foo(&)
89+
bar[&] = 1
90+
end
91+
8492
foo[] += 1
8593

8694
foo[] ||= 1

0 commit comments

Comments
 (0)