Skip to content

Commit

Permalink
Merge pull request jruby#7857 from enebo/fix_7855
Browse files Browse the repository at this point in the history
Fixes jruby#7855. pattern assoc assigns should scope
  • Loading branch information
enebo authored Jul 14, 2023
2 parents 9151727 + 8d35213 commit c0de12d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/parser/RubyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5060,7 +5060,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
};
states[522] = (RubyParser p, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
/*%%%*/
yyVal = p.logop(((Node)yyVals[-2+yyTop].value), OR_OR, ((Node)yyVals[0+yyTop].value));
yyVal = p.logop(((Node)yyVals[-2+yyTop].value), OR, ((Node)yyVals[0+yyTop].value));
/*% %*/
/*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
return yyVal;
Expand Down Expand Up @@ -5484,7 +5484,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
states[603] = (RubyParser p, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
/*%%%*/
p.error_duplicate_pattern_variable(((ByteList)yyVals[0+yyTop].value));
yyVal = p.assignableInCurr(((ByteList)yyVals[0+yyTop].value), null);
yyVal = p.assignableLabelOrIdentifier(((ByteList)yyVals[0+yyTop].value), null);
/*%
$$ = p.assignable(yyVals[yyTop - count + 1].id, p.var_field($1));
%*/
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/parser/RubyParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3591,7 +3591,7 @@ p_primitive : literal
p_variable : tIDENTIFIER {
/*%%%*/
p.error_duplicate_pattern_variable($1);
$$ = p.assignableInCurr($1, null);
$$ = p.assignableLabelOrIdentifier($1, null);
/*%
$$ = p.assignable(@1.id, p.var_field($1));
%*/
Expand Down
26 changes: 20 additions & 6 deletions spec/ruby/language/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@
ScratchPad.record []
end

it "can be standalone assoc operator that deconstructs value" do
suppress_warning do
eval(<<-RUBY).should == [0, 1]
[0, 1] => [a, b]
[a, b]
RUBY
describe "can be standalone assoc operator that" do
it "deconstructs value" do
suppress_warning do
eval(<<-RUBY).should == [0, 1]
[0, 1] => [a, b]
[a, b]
RUBY
end
end

it "deconstructs value and properly scopes variables" do
suppress_warning do
eval(<<-RUBY).should == [0, nil]
a = nil
eval(<<-PATTERN)
[0, 1] => [a, b]
PATTERN
[a, defined?(b)]
RUBY
end
end
end

Expand Down

0 comments on commit c0de12d

Please sign in to comment.