Skip to content

Commit 76d01ae

Browse files
committed
Fix parsing rescued exception via indexed assignment
Given this code ```ruby begin raise '42' rescue => A[] end ``` Prism fails with this backtrace ``` Error: test_unparser/corpus/literal/rescue.txt(Prism::ParserTest): NoMethodError: undefined method `arguments' for nil prism/lib/prism/translation/parser/compiler.rb:1055:in `visit_index_target_node' prism/lib/prism/node.rb:9636:in `accept' prism/lib/prism/compiler.rb:30:in `visit' prism/lib/prism/translation/parser/compiler.rb:218:in `visit_begin_node' ``` Seems like ```diff - visit_all(node.arguments.arguments), + visit_all(node.arguments&.arguments || []), ``` fixes the problem.
1 parent a3326c0 commit 76d01ae

File tree

3 files changed

+89
-58
lines changed

3 files changed

+89
-58
lines changed

lib/prism/translation/parser/compiler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ def visit_index_target_node(node)
10521052
builder.index_asgn(
10531053
visit(node.receiver),
10541054
token(node.opening_loc),
1055-
visit_all(node.arguments.arguments),
1055+
visit_all(node.arguments&.arguments || []),
10561056
token(node.closing_loc),
10571057
)
10581058
end

snapshots/rescue.txt

+84-57
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
@ ProgramNode (location: (1,0)-(35,18))
1+
@ ProgramNode (location: (1,0)-(39,3))
22
├── flags: ∅
33
├── locals: [:a, :z]
44
└── statements:
5-
@ StatementsNode (location: (1,0)-(35,18))
5+
@ StatementsNode (location: (1,0)-(39,3))
66
├── flags: ∅
7-
└── body: (length: 14)
7+
└── body: (length: 15)
88
├── @ RescueModifierNode (location: (1,0)-(1,14))
99
│ ├── flags: newline
1010
│ ├── expression:
@@ -526,61 +526,88 @@
526526
│ ├── arguments: ∅
527527
│ ├── closing_loc: ∅
528528
│ └── block: ∅
529-
└── @ LocalVariableWriteNode (location: (35,0)-(35,18))
529+
├── @ LocalVariableWriteNode (location: (35,0)-(35,18))
530+
│ ├── flags: newline
531+
│ ├── name: :z
532+
│ ├── depth: 0
533+
│ ├── name_loc: (35,0)-(35,1) = "z"
534+
│ ├── value:
535+
│ │ @ RescueModifierNode (location: (35,4)-(35,18))
536+
│ │ ├── flags: ∅
537+
│ │ ├── expression:
538+
│ │ │ @ CallNode (location: (35,4)-(35,7))
539+
│ │ │ ├── flags: ignore_visibility
540+
│ │ │ ├── receiver: ∅
541+
│ │ │ ├── call_operator_loc: ∅
542+
│ │ │ ├── name: :x
543+
│ │ │ ├── message_loc: (35,4)-(35,5) = "x"
544+
│ │ │ ├── opening_loc: ∅
545+
│ │ │ ├── arguments:
546+
│ │ │ │ @ ArgumentsNode (location: (35,6)-(35,7))
547+
│ │ │ │ ├── flags: ∅
548+
│ │ │ │ └── arguments: (length: 1)
549+
│ │ │ │ └── @ CallNode (location: (35,6)-(35,7))
550+
│ │ │ │ ├── flags: variable_call, ignore_visibility
551+
│ │ │ │ ├── receiver: ∅
552+
│ │ │ │ ├── call_operator_loc: ∅
553+
│ │ │ │ ├── name: :y
554+
│ │ │ │ ├── message_loc: (35,6)-(35,7) = "y"
555+
│ │ │ │ ├── opening_loc: ∅
556+
│ │ │ │ ├── arguments: ∅
557+
│ │ │ │ ├── closing_loc: ∅
558+
│ │ │ │ └── block: ∅
559+
│ │ │ ├── closing_loc: ∅
560+
│ │ │ └── block: ∅
561+
│ │ ├── keyword_loc: (35,8)-(35,14) = "rescue"
562+
│ │ └── rescue_expression:
563+
│ │ @ CallNode (location: (35,15)-(35,18))
564+
│ │ ├── flags: ignore_visibility
565+
│ │ ├── receiver: ∅
566+
│ │ ├── call_operator_loc: ∅
567+
│ │ ├── name: :c
568+
│ │ ├── message_loc: (35,15)-(35,16) = "c"
569+
│ │ ├── opening_loc: ∅
570+
│ │ ├── arguments:
571+
│ │ │ @ ArgumentsNode (location: (35,17)-(35,18))
572+
│ │ │ ├── flags: ∅
573+
│ │ │ └── arguments: (length: 1)
574+
│ │ │ └── @ CallNode (location: (35,17)-(35,18))
575+
│ │ │ ├── flags: variable_call, ignore_visibility
576+
│ │ │ ├── receiver: ∅
577+
│ │ │ ├── call_operator_loc: ∅
578+
│ │ │ ├── name: :d
579+
│ │ │ ├── message_loc: (35,17)-(35,18) = "d"
580+
│ │ │ ├── opening_loc: ∅
581+
│ │ │ ├── arguments: ∅
582+
│ │ │ ├── closing_loc: ∅
583+
│ │ │ └── block: ∅
584+
│ │ ├── closing_loc: ∅
585+
│ │ └── block: ∅
586+
│ └── operator_loc: (35,2)-(35,3) = "="
587+
└── @ BeginNode (location: (37,0)-(39,3))
530588
├── flags: newline
531-
├── name: :z
532-
├── depth: 0
533-
├── name_loc: (35,0)-(35,1) = "z"
534-
├── value:
535-
│ @ RescueModifierNode (location: (35,4)-(35,18))
589+
├── begin_keyword_loc: (37,0)-(37,5) = "begin"
590+
├── statements: ∅
591+
├── rescue_clause:
592+
│ @ RescueNode (location: (38,0)-(38,13))
536593
│ ├── flags: ∅
537-
│ ├── expression:
538-
│ │ @ CallNode (location: (35,4)-(35,7))
539-
│ │ ├── flags: ignore_visibility
540-
│ │ ├── receiver: ∅
541-
│ │ ├── call_operator_loc: ∅
542-
│ │ ├── name: :x
543-
│ │ ├── message_loc: (35,4)-(35,5) = "x"
544-
│ │ ├── opening_loc: ∅
545-
│ │ ├── arguments:
546-
│ │ │ @ ArgumentsNode (location: (35,6)-(35,7))
594+
│ ├── keyword_loc: (38,0)-(38,6) = "rescue"
595+
│ ├── exceptions: (length: 0)
596+
│ ├── operator_loc: (38,7)-(38,9) = "=>"
597+
│ ├── reference:
598+
│ │ @ IndexTargetNode (location: (38,10)-(38,13))
599+
│ │ ├── flags: attribute_write
600+
│ │ ├── receiver:
601+
│ │ │ @ ConstantReadNode (location: (38,10)-(38,11))
547602
│ │ │ ├── flags: ∅
548-
│ │ │ └── arguments: (length: 1)
549-
│ │ │ └── @ CallNode (location: (35,6)-(35,7))
550-
│ │ │ ├── flags: variable_call, ignore_visibility
551-
│ │ │ ├── receiver: ∅
552-
│ │ │ ├── call_operator_loc: ∅
553-
│ │ │ ├── name: :y
554-
│ │ │ ├── message_loc: (35,6)-(35,7) = "y"
555-
│ │ │ ├── opening_loc: ∅
556-
│ │ │ ├── arguments: ∅
557-
│ │ │ ├── closing_loc: ∅
558-
│ │ │ └── block: ∅
559-
│ │ ├── closing_loc: ∅
603+
│ │ │ └── name: :A
604+
│ │ ├── opening_loc: (38,11)-(38,12) = "["
605+
│ │ ├── arguments: ∅
606+
│ │ ├── closing_loc: (38,12)-(38,13) = "]"
560607
│ │ └── block: ∅
561-
│ ├── keyword_loc: (35,8)-(35,14) = "rescue"
562-
│ └── rescue_expression:
563-
│ @ CallNode (location: (35,15)-(35,18))
564-
│ ├── flags: ignore_visibility
565-
│ ├── receiver: ∅
566-
│ ├── call_operator_loc: ∅
567-
│ ├── name: :c
568-
│ ├── message_loc: (35,15)-(35,16) = "c"
569-
│ ├── opening_loc: ∅
570-
│ ├── arguments:
571-
│ │ @ ArgumentsNode (location: (35,17)-(35,18))
572-
│ │ ├── flags: ∅
573-
│ │ └── arguments: (length: 1)
574-
│ │ └── @ CallNode (location: (35,17)-(35,18))
575-
│ │ ├── flags: variable_call, ignore_visibility
576-
│ │ ├── receiver: ∅
577-
│ │ ├── call_operator_loc: ∅
578-
│ │ ├── name: :d
579-
│ │ ├── message_loc: (35,17)-(35,18) = "d"
580-
│ │ ├── opening_loc: ∅
581-
│ │ ├── arguments: ∅
582-
│ │ ├── closing_loc: ∅
583-
│ │ └── block: ∅
584-
│ ├── closing_loc: ∅
585-
│ └── block: ∅
586-
└── operator_loc: (35,2)-(35,3) = "="
608+
│ ├── then_keyword_loc: ∅
609+
│ ├── statements: ∅
610+
│ └── subsequent: ∅
611+
├── else_clause: ∅
612+
├── ensure_clause: ∅
613+
└── end_keyword_loc: (39,0)-(39,3) = "end"

test/prism/fixtures/rescue.txt

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ end
3333
foo if bar rescue baz
3434

3535
z = x y rescue c d
36+
37+
begin
38+
rescue => A[]
39+
end

0 commit comments

Comments
 (0)