Skip to content

Commit 1a57fee

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Fix multi write with modifier rescue
ruby/prism@8ea7a3270f
1 parent 815db5c commit 1a57fee

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

prism/prism.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17051,10 +17051,10 @@ parse_assignment_value(pm_parser_t *parser, pm_binding_power_t previous_binding_
1705117051
static inline pm_node_t *
1705217052
parse_assignment_values(pm_parser_t *parser, pm_binding_power_t previous_binding_power, pm_binding_power_t binding_power, bool accepts_command_call, pm_diagnostic_id_t diag_id) {
1705317053
pm_node_t *value = parse_starred_expression(parser, binding_power, previous_binding_power == PM_BINDING_POWER_ASSIGNMENT ? accepts_command_call : previous_binding_power < PM_BINDING_POWER_MATCH, diag_id);
17054+
bool single_value = true;
1705417055

17055-
bool is_single_value = true;
1705617056
if (previous_binding_power == PM_BINDING_POWER_STATEMENT && (PM_NODE_TYPE_P(value, PM_SPLAT_NODE) || match1(parser, PM_TOKEN_COMMA))) {
17057-
is_single_value = false;
17057+
single_value = false;
1705817058
pm_token_t opening = not_provided(parser);
1705917059
pm_array_node_t *array = pm_array_node_create(parser, &opening);
1706017060

@@ -17068,16 +17068,18 @@ parse_assignment_values(pm_parser_t *parser, pm_binding_power_t previous_binding
1706817068
}
1706917069
}
1707017070

17071-
// Contradicting binding powers, the right-hand-side value of the assignment allows the `rescue` modifier.
17072-
if (is_single_value && match1(parser, PM_TOKEN_KEYWORD_RESCUE_MODIFIER)) {
17071+
// Contradicting binding powers, the right-hand-side value of the assignment
17072+
// allows the `rescue` modifier.
17073+
if ((single_value || (binding_power == (PM_BINDING_POWER_MULTI_ASSIGNMENT + 1))) && match1(parser, PM_TOKEN_KEYWORD_RESCUE_MODIFIER)) {
1707317074
pm_token_t rescue = parser->current;
1707417075
parser_lex(parser);
1707517076

1707617077
bool accepts_command_call_inner = false;
1707717078

17078-
// RHS can accept command call iff the value is a call with arguments but without paranthesis.
17079+
// RHS can accept command call iff the value is a call with arguments
17080+
// but without paranthesis.
1707917081
if (PM_NODE_TYPE_P(value, PM_CALL_NODE)) {
17080-
pm_call_node_t *call_node = (pm_call_node_t *)value;
17082+
pm_call_node_t *call_node = (pm_call_node_t *) value;
1708117083
if ((call_node->arguments != NULL) && (call_node->opening_loc.start == NULL)) {
1708217084
accepts_command_call_inner = true;
1708317085
}

test/prism/fixtures/multi_write.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo = 1 rescue nil
2+
foo, bar = 1 rescue nil
3+
foo = 1, 2 rescue nil
4+
foo, bar = 1, 2 rescue nil

test/prism/ruby_parser_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class RubyParserTest < TestCase
7373
alias.txt
7474
method_calls.txt
7575
methods.txt
76+
multi_write.txt
7677
not.txt
7778
patterns.txt
7879
regex.txt

test/prism/snapshots/multi_write.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
@ ProgramNode (location: (1,0)-(4,26))
2+
├── locals: [:foo, :bar]
3+
└── statements:
4+
@ StatementsNode (location: (1,0)-(4,26))
5+
└── body: (length: 4)
6+
├── @ LocalVariableWriteNode (location: (1,0)-(1,18))
7+
│ ├── name: :foo
8+
│ ├── depth: 0
9+
│ ├── name_loc: (1,0)-(1,3) = "foo"
10+
│ ├── value:
11+
│ │ @ RescueModifierNode (location: (1,6)-(1,18))
12+
│ │ ├── expression:
13+
│ │ │ @ IntegerNode (location: (1,6)-(1,7))
14+
│ │ │ ├── flags: decimal
15+
│ │ │ └── value: 1
16+
│ │ ├── keyword_loc: (1,8)-(1,14) = "rescue"
17+
│ │ └── rescue_expression:
18+
│ │ @ NilNode (location: (1,15)-(1,18))
19+
│ └── operator_loc: (1,4)-(1,5) = "="
20+
├── @ MultiWriteNode (location: (2,0)-(2,23))
21+
│ ├── lefts: (length: 2)
22+
│ │ ├── @ LocalVariableTargetNode (location: (2,0)-(2,3))
23+
│ │ │ ├── name: :foo
24+
│ │ │ └── depth: 0
25+
│ │ └── @ LocalVariableTargetNode (location: (2,5)-(2,8))
26+
│ │ ├── name: :bar
27+
│ │ └── depth: 0
28+
│ ├── rest: ∅
29+
│ ├── rights: (length: 0)
30+
│ ├── lparen_loc: ∅
31+
│ ├── rparen_loc: ∅
32+
│ ├── operator_loc: (2,9)-(2,10) = "="
33+
│ └── value:
34+
│ @ RescueModifierNode (location: (2,11)-(2,23))
35+
│ ├── expression:
36+
│ │ @ IntegerNode (location: (2,11)-(2,12))
37+
│ │ ├── flags: decimal
38+
│ │ └── value: 1
39+
│ ├── keyword_loc: (2,13)-(2,19) = "rescue"
40+
│ └── rescue_expression:
41+
│ @ NilNode (location: (2,20)-(2,23))
42+
├── @ RescueModifierNode (location: (3,0)-(3,21))
43+
│ ├── expression:
44+
│ │ @ LocalVariableWriteNode (location: (3,0)-(3,10))
45+
│ │ ├── name: :foo
46+
│ │ ├── depth: 0
47+
│ │ ├── name_loc: (3,0)-(3,3) = "foo"
48+
│ │ ├── value:
49+
│ │ │ @ ArrayNode (location: (3,6)-(3,10))
50+
│ │ │ ├── flags: ∅
51+
│ │ │ ├── elements: (length: 2)
52+
│ │ │ │ ├── @ IntegerNode (location: (3,6)-(3,7))
53+
│ │ │ │ │ ├── flags: decimal
54+
│ │ │ │ │ └── value: 1
55+
│ │ │ │ └── @ IntegerNode (location: (3,9)-(3,10))
56+
│ │ │ │ ├── flags: decimal
57+
│ │ │ │ └── value: 2
58+
│ │ │ ├── opening_loc: ∅
59+
│ │ │ └── closing_loc: ∅
60+
│ │ └── operator_loc: (3,4)-(3,5) = "="
61+
│ ├── keyword_loc: (3,11)-(3,17) = "rescue"
62+
│ └── rescue_expression:
63+
│ @ NilNode (location: (3,18)-(3,21))
64+
└── @ MultiWriteNode (location: (4,0)-(4,26))
65+
├── lefts: (length: 2)
66+
│ ├── @ LocalVariableTargetNode (location: (4,0)-(4,3))
67+
│ │ ├── name: :foo
68+
│ │ └── depth: 0
69+
│ └── @ LocalVariableTargetNode (location: (4,5)-(4,8))
70+
│ ├── name: :bar
71+
│ └── depth: 0
72+
├── rest: ∅
73+
├── rights: (length: 0)
74+
├── lparen_loc: ∅
75+
├── rparen_loc: ∅
76+
├── operator_loc: (4,9)-(4,10) = "="
77+
└── value:
78+
@ RescueModifierNode (location: (4,11)-(4,26))
79+
├── expression:
80+
│ @ ArrayNode (location: (4,11)-(4,15))
81+
│ ├── flags: ∅
82+
│ ├── elements: (length: 2)
83+
│ │ ├── @ IntegerNode (location: (4,11)-(4,12))
84+
│ │ │ ├── flags: decimal
85+
│ │ │ └── value: 1
86+
│ │ └── @ IntegerNode (location: (4,14)-(4,15))
87+
│ │ ├── flags: decimal
88+
│ │ └── value: 2
89+
│ ├── opening_loc: ∅
90+
│ └── closing_loc: ∅
91+
├── keyword_loc: (4,16)-(4,22) = "rescue"
92+
└── rescue_expression:
93+
@ NilNode (location: (4,23)-(4,26))

0 commit comments

Comments
 (0)