From 1f94f55fcb52bd65d2beefa350f952245f485607 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 30 Aug 2023 15:20:56 -0400 Subject: [PATCH 1/2] Fix instance variable constant names --- src/yarp.c | 2 +- test/yarp/snapshots/methods.txt | 2 +- test/yarp/snapshots/patterns.txt | 2 +- test/yarp/snapshots/seattlerb/case_in.txt | 2 +- ...interpolation_and_carriage_return_escapes.txt | 2 +- ...ation_and_carriage_return_escapes_windows.txt | 2 +- test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt | 2 +- ...e_line_call_ivar_arg_no_parens_line_break.txt | 2 +- .../parse_line_call_ivar_line_break_paren.txt | 2 +- test/yarp/snapshots/strings.txt | 2 +- .../unparser/corpus/literal/assignment.txt | 10 +++++----- .../unparser/corpus/literal/defined.txt | 2 +- .../snapshots/unparser/corpus/literal/dstr.txt | 2 +- .../unparser/corpus/literal/literal.txt | 16 ++++++++-------- .../unparser/corpus/literal/variables.txt | 2 +- .../snapshots/unparser/corpus/semantic/dstr.txt | 4 ++-- .../unparser/corpus/semantic/literal.txt | 2 +- test/yarp/snapshots/variables.txt | 10 +++++----- .../snapshots/whitequark/array_words_interp.txt | 2 +- test/yarp/snapshots/whitequark/defined.txt | 2 +- test/yarp/snapshots/whitequark/ivar.txt | 2 +- test/yarp/snapshots/whitequark/ivasgn.txt | 2 +- test/yarp/snapshots/whitequark/masgn_splat.txt | 2 +- .../yarp/snapshots/whitequark/parser_bug_272.txt | 2 +- test/yarp/snapshots/whitequark/resbody_var.txt | 2 +- test/yarp/snapshots/whitequark/string_concat.txt | 2 +- test/yarp/snapshots/whitequark/string_dvar.txt | 2 +- test/yarp/snapshots/whitequark/var_op_asgn.txt | 2 +- 28 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/yarp.c b/src/yarp.c index c9d7cd08b42..46fe7259a45 100644 --- a/src/yarp.c +++ b/src/yarp.c @@ -2702,7 +2702,7 @@ yp_instance_variable_read_node_create(yp_parser_t *parser, const yp_token_t *tok .type = YP_NODE_INSTANCE_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }, - .name = yp_parser_constant_id_location(parser, token->start + 1, token->end) + .name = yp_parser_constant_id_location(parser, token->start, token->end) }; return node; diff --git a/test/yarp/snapshots/methods.txt b/test/yarp/snapshots/methods.txt index 32a7a4e7cd9..70e030811c4 100644 --- a/test/yarp/snapshots/methods.txt +++ b/test/yarp/snapshots/methods.txt @@ -199,7 +199,7 @@ ProgramNode(0...1194)( ), DefNode(190...204)( (199...200), - InstanceVariableReadNode(194...198)(:var), + InstanceVariableReadNode(194...198)(:@var), nil, nil, [], diff --git a/test/yarp/snapshots/patterns.txt b/test/yarp/snapshots/patterns.txt index f888d1498fc..63764bca0be 100644 --- a/test/yarp/snapshots/patterns.txt +++ b/test/yarp/snapshots/patterns.txt @@ -923,7 +923,7 @@ ProgramNode(0...3743)( "foo" ), PinnedVariableNode(961...966)( - InstanceVariableReadNode(962...966)(:bar), + InstanceVariableReadNode(962...966)(:@bar), (961...962) ), (958...960) diff --git a/test/yarp/snapshots/seattlerb/case_in.txt b/test/yarp/snapshots/seattlerb/case_in.txt index 5ea3d11b78e..2e508d8c9ae 100644 --- a/test/yarp/snapshots/seattlerb/case_in.txt +++ b/test/yarp/snapshots/seattlerb/case_in.txt @@ -494,7 +494,7 @@ ProgramNode(0...747)( ArrayPatternNode(627...643)( nil, [PinnedVariableNode(628...631)( - InstanceVariableReadNode(629...631)(:a), + InstanceVariableReadNode(629...631)(:@a), (628...629) ), PinnedVariableNode(633...636)( diff --git a/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt b/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt index 5aab2888ef8..452e5b605b8 100644 --- a/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt +++ b/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt @@ -6,7 +6,7 @@ ProgramNode(0...5)( [StringNode(6...11)(nil, (6...11), nil, "foo\r"), EmbeddedVariableNode(11...16)( (11...12), - InstanceVariableReadNode(12...16)(:bar) + InstanceVariableReadNode(12...16)(:@bar) ), StringNode(16...17)(nil, (16...17), nil, "\n")], (17...21) diff --git a/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt b/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt index 04727011a2a..99b42f6122a 100644 --- a/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt +++ b/test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt @@ -6,7 +6,7 @@ ProgramNode(0...5)( [StringNode(7...12)(nil, (7...12), nil, "foo\r"), EmbeddedVariableNode(12...17)( (12...13), - InstanceVariableReadNode(13...17)(:bar) + InstanceVariableReadNode(13...17)(:@bar) ), StringNode(17...19)(nil, (17...19), nil, "\r\n")], (19...24) diff --git a/test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt b/test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt index f065032f3a2..cf47f6dc7e5 100644 --- a/test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt +++ b/test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt @@ -2,7 +2,7 @@ ProgramNode(0...7)( [], StatementsNode(0...7)( [InstanceVariableWriteNode(0...7)( - :a, + :@a, (0...2), IntegerNode(5...7)(), (3...4) diff --git a/test/yarp/snapshots/seattlerb/parse_line_call_ivar_arg_no_parens_line_break.txt b/test/yarp/snapshots/seattlerb/parse_line_call_ivar_arg_no_parens_line_break.txt index efbc413fe14..84ee2d450bd 100644 --- a/test/yarp/snapshots/seattlerb/parse_line_call_ivar_arg_no_parens_line_break.txt +++ b/test/yarp/snapshots/seattlerb/parse_line_call_ivar_arg_no_parens_line_break.txt @@ -6,7 +6,7 @@ ProgramNode(0...4)( nil, (0...1), nil, - ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:b)]), + ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:@b)]), nil, nil, 0, diff --git a/test/yarp/snapshots/seattlerb/parse_line_call_ivar_line_break_paren.txt b/test/yarp/snapshots/seattlerb/parse_line_call_ivar_line_break_paren.txt index 6466d9dd45b..1f9d067c76b 100644 --- a/test/yarp/snapshots/seattlerb/parse_line_call_ivar_line_break_paren.txt +++ b/test/yarp/snapshots/seattlerb/parse_line_call_ivar_line_break_paren.txt @@ -6,7 +6,7 @@ ProgramNode(0...6)( nil, (0...1), (1...2), - ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:b)]), + ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:@b)]), (5...6), nil, 0, diff --git a/test/yarp/snapshots/strings.txt b/test/yarp/snapshots/strings.txt index d6f624624c0..ee8bc79f17e 100644 --- a/test/yarp/snapshots/strings.txt +++ b/test/yarp/snapshots/strings.txt @@ -185,7 +185,7 @@ ProgramNode(0...498)( (414...415), [EmbeddedVariableNode(415...420)( (415...416), - InstanceVariableReadNode(416...420)(:foo) + InstanceVariableReadNode(416...420)(:@foo) )], (420...421) ), diff --git a/test/yarp/snapshots/unparser/corpus/literal/assignment.txt b/test/yarp/snapshots/unparser/corpus/literal/assignment.txt index 6166e0cd268..7e193e0e87d 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/assignment.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/assignment.txt @@ -66,8 +66,8 @@ ProgramNode(0...704)( (74...75) ), MultiWriteNode(85...102)( - [InstanceVariableTargetNode(86...88)(:a), - InstanceVariableTargetNode(90...92)(:b)], + [InstanceVariableTargetNode(86...88)(:@a), + InstanceVariableTargetNode(90...92)(:@b)], (94...95), ArrayNode(96...102)( [IntegerNode(97...98)(), IntegerNode(100...101)()], @@ -296,7 +296,7 @@ ProgramNode(0...704)( (306...307) ), InstanceVariableWriteNode(310...316)( - :a, + :@a, (310...312), IntegerNode(315...316)(), (313...314) @@ -606,7 +606,7 @@ ProgramNode(0...704)( (543...546) ), InstanceVariableOrWriteNode(551...561)( - :a, + :@a, (551...553), (554...557), StringNode(558...561)((558...560), (560...560), (560...561), "") @@ -700,7 +700,7 @@ ProgramNode(0...704)( (665...668) ), InstanceVariableOrWriteNode(687...704)( - :a, + :@a, (687...689), (690...693), InterpolatedStringNode(694...704)( diff --git a/test/yarp/snapshots/unparser/corpus/literal/defined.txt b/test/yarp/snapshots/unparser/corpus/literal/defined.txt index 1d39c45c836..9229badf44e 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/defined.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/defined.txt @@ -3,7 +3,7 @@ ProgramNode(0...56)( StatementsNode(0...56)( [DefinedNode(0...14)( (8...9), - InstanceVariableReadNode(9...13)(:foo), + InstanceVariableReadNode(9...13)(:@foo), (13...14), (0...8) ), diff --git a/test/yarp/snapshots/unparser/corpus/literal/dstr.txt b/test/yarp/snapshots/unparser/corpus/literal/dstr.txt index 49d08824bfc..1abbed681f8 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/dstr.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/dstr.txt @@ -75,7 +75,7 @@ ProgramNode(0...299)( [StringNode(160...161)(nil, (160...161), nil, "a"), EmbeddedVariableNode(161...164)( (161...162), - InstanceVariableReadNode(162...164)(:a) + InstanceVariableReadNode(162...164)(:@a) )], (164...165) ), diff --git a/test/yarp/snapshots/unparser/corpus/literal/literal.txt b/test/yarp/snapshots/unparser/corpus/literal/literal.txt index a38a7f4f888..03f9aecbd56 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/literal.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/literal.txt @@ -153,7 +153,7 @@ ProgramNode(0...916)( (206...207), [EmbeddedVariableNode(207...210)( (207...208), - InstanceVariableReadNode(208...210)(:a) + InstanceVariableReadNode(208...210)(:@a) ), StringNode(210...211)(nil, (210...211), nil, " "), EmbeddedVariableNode(211...215)( @@ -268,7 +268,7 @@ ProgramNode(0...916)( EmbeddedStatementsNode(422...429)( (422...424), StatementsNode(424...428)( - [InstanceVariableReadNode(424...428)(:bar)] + [InstanceVariableReadNode(424...428)(:@bar)] ), (428...429) )], @@ -303,7 +303,7 @@ ProgramNode(0...916)( EmbeddedStatementsNode(519...526)( (519...521), StatementsNode(521...525)( - [InstanceVariableReadNode(521...525)(:bar)] + [InstanceVariableReadNode(521...525)(:@bar)] ), (525...526) )], @@ -316,7 +316,7 @@ ProgramNode(0...916)( EmbeddedStatementsNode(532...539)( (532...534), StatementsNode(534...538)( - [InstanceVariableReadNode(534...538)(:bar)] + [InstanceVariableReadNode(534...538)(:@bar)] ), (538...539) )], @@ -507,7 +507,7 @@ ProgramNode(0...916)( [IntegerNode(693...694)(), SplatNode(696...701)( (696...697), - InstanceVariableReadNode(697...701)(:foo) + InstanceVariableReadNode(697...701)(:@foo) )], (692...693), (701...702) @@ -515,7 +515,7 @@ ProgramNode(0...916)( ArrayNode(703...713)( [SplatNode(704...709)( (704...705), - InstanceVariableReadNode(705...709)(:foo) + InstanceVariableReadNode(705...709)(:@foo) ), IntegerNode(711...712)()], (703...704), @@ -524,11 +524,11 @@ ProgramNode(0...916)( ArrayNode(714...728)( [SplatNode(715...720)( (715...716), - InstanceVariableReadNode(716...720)(:foo) + InstanceVariableReadNode(716...720)(:@foo) ), SplatNode(722...727)( (722...723), - InstanceVariableReadNode(723...727)(:baz) + InstanceVariableReadNode(723...727)(:@baz) )], (714...715), (727...728) diff --git a/test/yarp/snapshots/unparser/corpus/literal/variables.txt b/test/yarp/snapshots/unparser/corpus/literal/variables.txt index 29857ec53c5..051fb2f4989 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/variables.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/variables.txt @@ -2,7 +2,7 @@ ProgramNode(0...66)( [], StatementsNode(0...66)( [CallNode(0...1)(nil, nil, (0...1), nil, nil, nil, nil, 2, "a"), - InstanceVariableReadNode(2...4)(:a), + InstanceVariableReadNode(2...4)(:@a), ClassVariableReadNode(5...8)(), GlobalVariableReadNode(9...11)(), NumberedReferenceReadNode(12...14)(), diff --git a/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt b/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt index 490b13e0bc8..47d37b1c38c 100644 --- a/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt +++ b/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt @@ -208,7 +208,7 @@ ProgramNode(0...608)( EmbeddedStatementsNode(564...569)( (564...566), StatementsNode(566...568)( - [InstanceVariableReadNode(566...568)(:a)] + [InstanceVariableReadNode(566...568)(:@a)] ), (568...569) )], @@ -222,7 +222,7 @@ ProgramNode(0...608)( [StringNode(576...577)(nil, (576...577), nil, "a"), EmbeddedVariableNode(577...580)( (577...578), - InstanceVariableReadNode(578...580)(:a) + InstanceVariableReadNode(578...580)(:@a) )], (580...581) ), diff --git a/test/yarp/snapshots/unparser/corpus/semantic/literal.txt b/test/yarp/snapshots/unparser/corpus/semantic/literal.txt index b9df7fcbd9a..5e18c54da41 100644 --- a/test/yarp/snapshots/unparser/corpus/semantic/literal.txt +++ b/test/yarp/snapshots/unparser/corpus/semantic/literal.txt @@ -15,7 +15,7 @@ ProgramNode(0...131)( (71...74), [EmbeddedStatementsNode(74...81)( (74...76), - StatementsNode(76...80)([InstanceVariableReadNode(76...80)(:bar)]), + StatementsNode(76...80)([InstanceVariableReadNode(76...80)(:@bar)]), (80...81) ), StringNode(81...84)(nil, (81...84), nil, "baz")], diff --git a/test/yarp/snapshots/variables.txt b/test/yarp/snapshots/variables.txt index a6b6a444f9e..2dd87366c56 100644 --- a/test/yarp/snapshots/variables.txt +++ b/test/yarp/snapshots/variables.txt @@ -30,9 +30,9 @@ ProgramNode(0...293)( IntegerNode(57...58)() ), GlobalVariableReadNode(60...64)(), - InstanceVariableReadNode(66...70)(:abc), + InstanceVariableReadNode(66...70)(:@abc), InstanceVariableWriteNode(72...80)( - :abc, + :@abc, (72...76), IntegerNode(79...80)(), (77...78) @@ -63,15 +63,15 @@ ProgramNode(0...293)( ) ), MultiWriteNode(123...137)( - [InstanceVariableTargetNode(123...127)(:foo), - InstanceVariableTargetNode(129...133)(:bar)], + [InstanceVariableTargetNode(123...127)(:@foo), + InstanceVariableTargetNode(129...133)(:@bar)], (134...135), IntegerNode(136...137)(), nil, nil ), InstanceVariableWriteNode(139...150)( - :foo, + :@foo, (139...143), ArrayNode(146...150)( [IntegerNode(146...147)(), IntegerNode(149...150)()], diff --git a/test/yarp/snapshots/whitequark/array_words_interp.txt b/test/yarp/snapshots/whitequark/array_words_interp.txt index 963a83dd480..d4bc9292e6e 100644 --- a/test/yarp/snapshots/whitequark/array_words_interp.txt +++ b/test/yarp/snapshots/whitequark/array_words_interp.txt @@ -51,7 +51,7 @@ ProgramNode(0...38)( StringNode(29...32)(nil, (29...32), nil, "foo"), EmbeddedVariableNode(32...37)( (32...33), - InstanceVariableReadNode(33...37)(:baz) + InstanceVariableReadNode(33...37)(:@baz) )], nil )], diff --git a/test/yarp/snapshots/whitequark/defined.txt b/test/yarp/snapshots/whitequark/defined.txt index 9d8d997e2ba..3ddfdd84bbf 100644 --- a/test/yarp/snapshots/whitequark/defined.txt +++ b/test/yarp/snapshots/whitequark/defined.txt @@ -3,7 +3,7 @@ ProgramNode(0...42)( StatementsNode(0...42)( [DefinedNode(0...13)( nil, - InstanceVariableReadNode(9...13)(:foo), + InstanceVariableReadNode(9...13)(:@foo), nil, (0...8) ), diff --git a/test/yarp/snapshots/whitequark/ivar.txt b/test/yarp/snapshots/whitequark/ivar.txt index cf233251f28..ae2c8c34b48 100644 --- a/test/yarp/snapshots/whitequark/ivar.txt +++ b/test/yarp/snapshots/whitequark/ivar.txt @@ -1,4 +1,4 @@ ProgramNode(0...4)( [], - StatementsNode(0...4)([InstanceVariableReadNode(0...4)(:foo)]) + StatementsNode(0...4)([InstanceVariableReadNode(0...4)(:@foo)]) ) diff --git a/test/yarp/snapshots/whitequark/ivasgn.txt b/test/yarp/snapshots/whitequark/ivasgn.txt index 587dcfaa9df..8e952de2465 100644 --- a/test/yarp/snapshots/whitequark/ivasgn.txt +++ b/test/yarp/snapshots/whitequark/ivasgn.txt @@ -2,7 +2,7 @@ ProgramNode(0...9)( [], StatementsNode(0...9)( [InstanceVariableWriteNode(0...9)( - :var, + :@var, (0...4), IntegerNode(7...9)(), (5...6) diff --git a/test/yarp/snapshots/whitequark/masgn_splat.txt b/test/yarp/snapshots/whitequark/masgn_splat.txt index c6265c652e0..b093187ef62 100644 --- a/test/yarp/snapshots/whitequark/masgn_splat.txt +++ b/test/yarp/snapshots/whitequark/masgn_splat.txt @@ -51,7 +51,7 @@ ProgramNode(0...139)( nil ), MultiWriteNode(47...65)( - [InstanceVariableTargetNode(47...51)(:foo), + [InstanceVariableTargetNode(47...51)(:@foo), ClassVariableTargetNode(53...58)()], (59...60), ArrayNode(61...65)( diff --git a/test/yarp/snapshots/whitequark/parser_bug_272.txt b/test/yarp/snapshots/whitequark/parser_bug_272.txt index 50d75398071..0cb194d1ae4 100644 --- a/test/yarp/snapshots/whitequark/parser_bug_272.txt +++ b/test/yarp/snapshots/whitequark/parser_bug_272.txt @@ -6,7 +6,7 @@ ProgramNode(0...15)( nil, (0...1), nil, - ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:b)]), + ArgumentsNode(2...4)([InstanceVariableReadNode(2...4)(:@b)]), nil, BlockNode(5...15)( [:c], diff --git a/test/yarp/snapshots/whitequark/resbody_var.txt b/test/yarp/snapshots/whitequark/resbody_var.txt index 4530e6cdbd4..e88a7612dc1 100644 --- a/test/yarp/snapshots/whitequark/resbody_var.txt +++ b/test/yarp/snapshots/whitequark/resbody_var.txt @@ -10,7 +10,7 @@ ProgramNode(0...73)( (13...19), [], (20...22), - InstanceVariableTargetNode(23...26)(:ex), + InstanceVariableTargetNode(23...26)(:@ex), StatementsNode(28...31)( [CallNode(28...31)( nil, diff --git a/test/yarp/snapshots/whitequark/string_concat.txt b/test/yarp/snapshots/whitequark/string_concat.txt index fde498b39fe..a51362805e4 100644 --- a/test/yarp/snapshots/whitequark/string_concat.txt +++ b/test/yarp/snapshots/whitequark/string_concat.txt @@ -7,7 +7,7 @@ ProgramNode(0...14)( [StringNode(1...4)(nil, (1...4), nil, "foo"), EmbeddedVariableNode(4...7)( (4...5), - InstanceVariableReadNode(5...7)(:a) + InstanceVariableReadNode(5...7)(:@a) )], (7...8) ), diff --git a/test/yarp/snapshots/whitequark/string_dvar.txt b/test/yarp/snapshots/whitequark/string_dvar.txt index 2378e8fdcbc..981ea318c4b 100644 --- a/test/yarp/snapshots/whitequark/string_dvar.txt +++ b/test/yarp/snapshots/whitequark/string_dvar.txt @@ -5,7 +5,7 @@ ProgramNode(0...14)( (0...1), [EmbeddedVariableNode(1...4)( (1...2), - InstanceVariableReadNode(2...4)(:a) + InstanceVariableReadNode(2...4)(:@a) ), StringNode(4...5)(nil, (4...5), nil, " "), EmbeddedVariableNode(5...9)((5...6), ClassVariableReadNode(6...9)()), diff --git a/test/yarp/snapshots/whitequark/var_op_asgn.txt b/test/yarp/snapshots/whitequark/var_op_asgn.txt index 3aff7a6a602..5618d0944c3 100644 --- a/test/yarp/snapshots/whitequark/var_op_asgn.txt +++ b/test/yarp/snapshots/whitequark/var_op_asgn.txt @@ -8,7 +8,7 @@ ProgramNode(0...53)( :| ), InstanceVariableOperatorWriteNode(13...20)( - :a, + :@a, (13...15), (16...18), IntegerNode(19...20)(), From be5cb60c83d4fbd317071875195a0f45042e8e46 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 30 Aug 2023 15:28:10 -0400 Subject: [PATCH 2/2] Add class variables to the constant pool --- config.yml | 14 ++++++ lib/yarp/desugar_visitor.rb | 14 +++--- src/yarp.c | 44 ++++++++++++------- test/yarp/snapshots/methods.txt | 2 +- test/yarp/snapshots/patterns.txt | 2 +- test/yarp/snapshots/seattlerb/case_in.txt | 2 +- test/yarp/snapshots/strings.txt | 2 +- .../unparser/corpus/literal/assignment.txt | 5 ++- .../unparser/corpus/literal/dstr.txt | 2 +- .../unparser/corpus/literal/literal.txt | 2 +- .../unparser/corpus/literal/variables.txt | 2 +- .../unparser/corpus/semantic/dstr.txt | 2 +- test/yarp/snapshots/variables.txt | 8 ++-- test/yarp/snapshots/whitequark/cvar.txt | 5 ++- test/yarp/snapshots/whitequark/cvasgn.txt | 7 ++- .../yarp/snapshots/whitequark/masgn_splat.txt | 2 +- .../yarp/snapshots/whitequark/string_dvar.txt | 5 ++- .../yarp/snapshots/whitequark/var_op_asgn.txt | 2 + 18 files changed, 81 insertions(+), 41 deletions(-) diff --git a/config.yml b/config.yml index b4e8b479137..72c4aeb8af6 100644 --- a/config.yml +++ b/config.yml @@ -727,6 +727,8 @@ nodes: ^^^^^^^^^^^^^ - name: ClassVariableAndWriteNode child_nodes: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -740,6 +742,8 @@ nodes: ^^^^^^^^^^^^^^^^ - name: ClassVariableOperatorWriteNode child_nodes: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -755,6 +759,8 @@ nodes: ^^^^^^^^^^^^^^^^^ - name: ClassVariableOrWriteNode child_nodes: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -767,12 +773,18 @@ nodes: @@target ||= value ^^^^^^^^^^^^^^^^^^ - name: ClassVariableReadNode + child_nodes: + - name: name + type: constant comment: | Represents referencing a class variable. @@foo ^^^^^ - name: ClassVariableTargetNode + child_nodes: + - name: name + type: constant comment: | Represents writing to a class variable in a context that doesn't have an explicit value. @@ -780,6 +792,8 @@ nodes: ^^^^^ ^^^^^ - name: ClassVariableWriteNode child_nodes: + - name: name + type: constant - name: name_loc type: location - name: value diff --git a/lib/yarp/desugar_visitor.rb b/lib/yarp/desugar_visitor.rb index 8fe488368e9..d0c0494e0c0 100644 --- a/lib/yarp/desugar_visitor.rb +++ b/lib/yarp/desugar_visitor.rb @@ -8,7 +8,7 @@ class DesugarVisitor < MutationVisitor # # @@foo && @@foo = bar def visit_class_variable_and_write_node(node) - desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode) + desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name]) end # @@foo ||= bar @@ -17,7 +17,7 @@ def visit_class_variable_and_write_node(node) # # defined?(@@foo) ? @@foo : @@foo = bar def visit_class_variable_or_write_node(node) - desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode) + desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name]) end # @@foo += bar @@ -26,7 +26,7 @@ def visit_class_variable_or_write_node(node) # # @@foo = @@foo + bar def visit_class_variable_operator_write_node(node) - desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode) + desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name]) end # Foo &&= bar @@ -245,15 +245,15 @@ def desugar_or_write_node(node, read_class, write_class, arguments: []) end # Don't desugar `x ||= y` to `defined?(x) ? x : x = y` - def desugar_or_write_defined_node(node, read_class, write_class) + def desugar_or_write_defined_node(node, read_class, write_class, arguments: []) IfNode.new( node.operator_loc, - DefinedNode.new(nil, read_class.new(node.name_loc), nil, node.operator_loc, node.name_loc), - StatementsNode.new([read_class.new(node.name_loc)], node.location), + DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc), + StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location), ElseNode.new( node.operator_loc, StatementsNode.new( - [write_class.new(node.name_loc, node.value, node.operator_loc, node.location)], + [write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)], node.location ), node.operator_loc, diff --git a/src/yarp.c b/src/yarp.c index 46fe7259a45..6fd16e16fcd 100644 --- a/src/yarp.c +++ b/src/yarp.c @@ -1558,8 +1558,7 @@ yp_class_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const y // Allocate and initialize a new ClassVariableAndWriteNode node. static yp_class_variable_and_write_node_t * -yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { - assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE)); +yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL); yp_class_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_and_write_node_t); @@ -1567,11 +1566,12 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, { .type = YP_NODE_CLASS_VARIABLE_AND_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value }; @@ -1581,18 +1581,19 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, // Allocate and initialize a new ClassVariableOperatorWriteNode node. static yp_class_variable_operator_write_node_t * -yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { +yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t); *node = (yp_class_variable_operator_write_node_t) { { .type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value, .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) @@ -1603,8 +1604,7 @@ yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar // Allocate and initialize a new ClassVariableOrWriteNode node. static yp_class_variable_or_write_node_t * -yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { - assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE)); +yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL); yp_class_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_or_write_node_t); @@ -1612,11 +1612,12 @@ yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, c { .type = YP_NODE_CLASS_VARIABLE_OR_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value }; @@ -1629,13 +1630,21 @@ static yp_class_variable_read_node_t * yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) { assert(token->type == YP_TOKEN_CLASS_VARIABLE); yp_class_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_read_node_t); - *node = (yp_class_variable_read_node_t) {{ .type = YP_NODE_CLASS_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }}; + + *node = (yp_class_variable_read_node_t) { + { + .type = YP_NODE_CLASS_VARIABLE_READ_NODE, + .location = YP_LOCATION_TOKEN_VALUE(token) + }, + .name = yp_parser_constant_id_location(parser, token->start, token->end) + }; + return node; } // Initialize a new ClassVariableWriteNode node from a ClassVariableRead node. static yp_class_variable_write_node_t * -yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) { +yp_class_variable_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) { yp_class_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_write_node_t); *node = (yp_class_variable_write_node_t) { @@ -1646,6 +1655,7 @@ yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp .end = value->location.end }, }, + .name = read_node->name, .name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) read_node), .operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator), .value = value @@ -8007,7 +8017,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod case YP_NODE_MISSING_NODE: return target; case YP_NODE_CLASS_VARIABLE_READ_NODE: { - yp_class_variable_write_node_t *write_node = yp_class_variable_read_node_to_class_variable_write_node(parser, (yp_class_variable_read_node_t *) target, operator, value); + yp_class_variable_write_node_t *write_node = yp_class_variable_write_node_create(parser, (yp_class_variable_read_node_t *) target, operator, value); yp_node_destroy(parser, target); return (yp_node_t *) write_node; } @@ -12837,7 +12847,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&="); - yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; @@ -12938,7 +12948,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||="); - yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; @@ -13049,7 +13059,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator."); - yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; diff --git a/test/yarp/snapshots/methods.txt b/test/yarp/snapshots/methods.txt index 70e030811c4..b5ec66aae3d 100644 --- a/test/yarp/snapshots/methods.txt +++ b/test/yarp/snapshots/methods.txt @@ -910,7 +910,7 @@ ProgramNode(0...1194)( ), DefNode(811...826)( (821...822), - ClassVariableReadNode(815...820)(), + ClassVariableReadNode(815...820)(:@@var), nil, nil, [], diff --git a/test/yarp/snapshots/patterns.txt b/test/yarp/snapshots/patterns.txt index 63764bca0be..01292cbf3ca 100644 --- a/test/yarp/snapshots/patterns.txt +++ b/test/yarp/snapshots/patterns.txt @@ -941,7 +941,7 @@ ProgramNode(0...3743)( "foo" ), PinnedVariableNode(974...980)( - ClassVariableReadNode(975...980)(), + ClassVariableReadNode(975...980)(:@@bar), (974...975) ), (971...973) diff --git a/test/yarp/snapshots/seattlerb/case_in.txt b/test/yarp/snapshots/seattlerb/case_in.txt index 2e508d8c9ae..aaee410ef32 100644 --- a/test/yarp/snapshots/seattlerb/case_in.txt +++ b/test/yarp/snapshots/seattlerb/case_in.txt @@ -502,7 +502,7 @@ ProgramNode(0...747)( (633...634) ), PinnedVariableNode(638...642)( - ClassVariableReadNode(639...642)(), + ClassVariableReadNode(639...642)(:@@c), (638...639) )], nil, diff --git a/test/yarp/snapshots/strings.txt b/test/yarp/snapshots/strings.txt index ee8bc79f17e..86a732d85d5 100644 --- a/test/yarp/snapshots/strings.txt +++ b/test/yarp/snapshots/strings.txt @@ -20,7 +20,7 @@ ProgramNode(0...498)( (122...123), [EmbeddedVariableNode(123...129)( (123...124), - ClassVariableReadNode(124...129)() + ClassVariableReadNode(124...129)(:@@foo) )], (129...130) ), diff --git a/test/yarp/snapshots/unparser/corpus/literal/assignment.txt b/test/yarp/snapshots/unparser/corpus/literal/assignment.txt index 7e193e0e87d..78ab4d3d78d 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/assignment.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/assignment.txt @@ -54,8 +54,8 @@ ProgramNode(0...704)( (54...55) ), MultiWriteNode(65...84)( - [ClassVariableTargetNode(66...69)(), - ClassVariableTargetNode(71...74)()], + [ClassVariableTargetNode(66...69)(:@@a), + ClassVariableTargetNode(71...74)(:@@b)], (76...77), ArrayNode(78...84)( [IntegerNode(79...80)(), IntegerNode(82...83)()], @@ -291,6 +291,7 @@ ProgramNode(0...704)( ) ), ClassVariableWriteNode(302...309)( + :@@a, (302...305), IntegerNode(308...309)(), (306...307) diff --git a/test/yarp/snapshots/unparser/corpus/literal/dstr.txt b/test/yarp/snapshots/unparser/corpus/literal/dstr.txt index 1abbed681f8..facf0171e79 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/dstr.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/dstr.txt @@ -84,7 +84,7 @@ ProgramNode(0...299)( [StringNode(167...168)(nil, (167...168), nil, "a"), EmbeddedVariableNode(168...172)( (168...169), - ClassVariableReadNode(169...172)() + ClassVariableReadNode(169...172)(:@@a) )], (172...173) ), diff --git a/test/yarp/snapshots/unparser/corpus/literal/literal.txt b/test/yarp/snapshots/unparser/corpus/literal/literal.txt index 03f9aecbd56..3b85d3e95fb 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/literal.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/literal.txt @@ -158,7 +158,7 @@ ProgramNode(0...916)( StringNode(210...211)(nil, (210...211), nil, " "), EmbeddedVariableNode(211...215)( (211...212), - ClassVariableReadNode(212...215)() + ClassVariableReadNode(212...215)(:@@a) ), StringNode(215...216)(nil, (215...216), nil, " "), EmbeddedVariableNode(216...219)( diff --git a/test/yarp/snapshots/unparser/corpus/literal/variables.txt b/test/yarp/snapshots/unparser/corpus/literal/variables.txt index 051fb2f4989..dfb85ec0137 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/variables.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/variables.txt @@ -3,7 +3,7 @@ ProgramNode(0...66)( StatementsNode(0...66)( [CallNode(0...1)(nil, nil, (0...1), nil, nil, nil, nil, 2, "a"), InstanceVariableReadNode(2...4)(:@a), - ClassVariableReadNode(5...8)(), + ClassVariableReadNode(5...8)(:@@a), GlobalVariableReadNode(9...11)(), NumberedReferenceReadNode(12...14)(), BackReferenceReadNode(15...17)(), diff --git a/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt b/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt index 47d37b1c38c..8822eb53337 100644 --- a/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt +++ b/test/yarp/snapshots/unparser/corpus/semantic/dstr.txt @@ -246,7 +246,7 @@ ProgramNode(0...608)( [StringNode(598...599)(nil, (598...599), nil, "a"), EmbeddedVariableNode(599...603)( (599...600), - ClassVariableReadNode(600...603)() + ClassVariableReadNode(600...603)(:@@a) )], (603...604) ), diff --git a/test/yarp/snapshots/variables.txt b/test/yarp/snapshots/variables.txt index 2dd87366c56..419942d1eb9 100644 --- a/test/yarp/snapshots/variables.txt +++ b/test/yarp/snapshots/variables.txt @@ -1,21 +1,23 @@ ProgramNode(0...293)( [:abc, :foo, :bar, :baz], StatementsNode(0...293)( - [ClassVariableReadNode(0...5)(), + [ClassVariableReadNode(0...5)(:@@abc), ClassVariableWriteNode(7...16)( + :@@abc, (7...12), IntegerNode(15...16)(), (13...14) ), MultiWriteNode(18...34)( - [ClassVariableTargetNode(18...23)(), - ClassVariableTargetNode(25...30)()], + [ClassVariableTargetNode(18...23)(:@@foo), + ClassVariableTargetNode(25...30)(:@@bar)], (31...32), IntegerNode(33...34)(), nil, nil ), ClassVariableWriteNode(36...48)( + :@@foo, (36...41), ArrayNode(44...48)( [IntegerNode(44...45)(), IntegerNode(47...48)()], diff --git a/test/yarp/snapshots/whitequark/cvar.txt b/test/yarp/snapshots/whitequark/cvar.txt index 069ff24b17e..6a1f2e50f48 100644 --- a/test/yarp/snapshots/whitequark/cvar.txt +++ b/test/yarp/snapshots/whitequark/cvar.txt @@ -1 +1,4 @@ -ProgramNode(0...5)([], StatementsNode(0...5)([ClassVariableReadNode(0...5)()])) +ProgramNode(0...5)( + [], + StatementsNode(0...5)([ClassVariableReadNode(0...5)(:@@foo)]) +) diff --git a/test/yarp/snapshots/whitequark/cvasgn.txt b/test/yarp/snapshots/whitequark/cvasgn.txt index b972caaab79..7e07810eb6c 100644 --- a/test/yarp/snapshots/whitequark/cvasgn.txt +++ b/test/yarp/snapshots/whitequark/cvasgn.txt @@ -1,6 +1,11 @@ ProgramNode(0...10)( [], StatementsNode(0...10)( - [ClassVariableWriteNode(0...10)((0...5), IntegerNode(8...10)(), (6...7))] + [ClassVariableWriteNode(0...10)( + :@@var, + (0...5), + IntegerNode(8...10)(), + (6...7) + )] ) ) diff --git a/test/yarp/snapshots/whitequark/masgn_splat.txt b/test/yarp/snapshots/whitequark/masgn_splat.txt index b093187ef62..d21760a65b3 100644 --- a/test/yarp/snapshots/whitequark/masgn_splat.txt +++ b/test/yarp/snapshots/whitequark/masgn_splat.txt @@ -52,7 +52,7 @@ ProgramNode(0...139)( ), MultiWriteNode(47...65)( [InstanceVariableTargetNode(47...51)(:@foo), - ClassVariableTargetNode(53...58)()], + ClassVariableTargetNode(53...58)(:@@bar)], (59...60), ArrayNode(61...65)( [SplatNode(61...65)( diff --git a/test/yarp/snapshots/whitequark/string_dvar.txt b/test/yarp/snapshots/whitequark/string_dvar.txt index 981ea318c4b..12f083660bb 100644 --- a/test/yarp/snapshots/whitequark/string_dvar.txt +++ b/test/yarp/snapshots/whitequark/string_dvar.txt @@ -8,7 +8,10 @@ ProgramNode(0...14)( InstanceVariableReadNode(2...4)(:@a) ), StringNode(4...5)(nil, (4...5), nil, " "), - EmbeddedVariableNode(5...9)((5...6), ClassVariableReadNode(6...9)()), + EmbeddedVariableNode(5...9)( + (5...6), + ClassVariableReadNode(6...9)(:@@a) + ), StringNode(9...10)(nil, (9...10), nil, " "), EmbeddedVariableNode(10...13)( (10...11), diff --git a/test/yarp/snapshots/whitequark/var_op_asgn.txt b/test/yarp/snapshots/whitequark/var_op_asgn.txt index 5618d0944c3..0927e8b8f3a 100644 --- a/test/yarp/snapshots/whitequark/var_op_asgn.txt +++ b/test/yarp/snapshots/whitequark/var_op_asgn.txt @@ -2,6 +2,7 @@ ProgramNode(0...53)( [:a], StatementsNode(0...53)( [ClassVariableOperatorWriteNode(0...11)( + :@@var, (0...5), (6...8), IntegerNode(9...11)(), @@ -28,6 +29,7 @@ ProgramNode(0...53)( nil, StatementsNode(37...48)( [ClassVariableOperatorWriteNode(37...48)( + :@@var, (37...42), (43...45), IntegerNode(46...48)(),