Skip to content

Commit

Permalink
Fix #2574: Part 3: Fix IR building of hash literals
Browse files Browse the repository at this point in the history
* Hash literals were broken similarly as array literals.
* Updated the spec for #2574.
  • Loading branch information
subbuss committed Feb 8, 2015
1 parent cc00fd4 commit 9b3d23a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2393,10 +2393,10 @@ public Operand buildHash(HashNode hashNode) {
splatKeywordArgument = build(pair.getValue());
break;
} else {
keyOperand = build(key);
keyOperand = copyAndReturnValue(build(key));
}

args.add(new KeyValuePair<>(keyOperand, build(pair.getValue())));
args.add(new KeyValuePair<Operand, Operand>(keyOperand, copyAndReturnValue(build(pair.getValue()))));
}

if (splatKeywordArgument != null) { // splat kwargs merge with any explicit kwargs
Expand Down
10 changes: 9 additions & 1 deletion spec/regression/GH-2574_dont_clobber_variable.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# https://github.com/jruby/jruby/issues/2574
describe 'Local variable assignments should not get clobbered' do
it 'returns the right value' do
it 'returns the right value for array literals' do
a = 0
b = [a,a=1]
expect(b).to eq([0,1])
end

it 'returns the right value for hash literals' do
a = 0
b = { a => a, (a = 1) => a } # => { 1 => 1 } (MRI: {0=>0, 1=>1})
c = { a => a, a => (a = 2) } # => { 2 => 2 } (MRI: {1=>2})
expect(b).to eq({0=>0, 1=>1})
expect(c).to eq({1=>2})
end
end

0 comments on commit 9b3d23a

Please sign in to comment.