Skip to content

Commit 9b3d23a

Browse files
committed
Fix #2574: Part 3: Fix IR building of hash literals
* Hash literals were broken similarly as array literals. * Updated the spec for #2574.
1 parent cc00fd4 commit 9b3d23a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

core/src/main/java/org/jruby/ir/IRBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2393,10 +2393,10 @@ public Operand buildHash(HashNode hashNode) {
23932393
splatKeywordArgument = build(pair.getValue());
23942394
break;
23952395
} else {
2396-
keyOperand = build(key);
2396+
keyOperand = copyAndReturnValue(build(key));
23972397
}
23982398

2399-
args.add(new KeyValuePair<>(keyOperand, build(pair.getValue())));
2399+
args.add(new KeyValuePair<Operand, Operand>(keyOperand, copyAndReturnValue(build(pair.getValue()))));
24002400
}
24012401

24022402
if (splatKeywordArgument != null) { // splat kwargs merge with any explicit kwargs
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# https://github.com/jruby/jruby/issues/2574
22
describe 'Local variable assignments should not get clobbered' do
3-
it 'returns the right value' do
3+
it 'returns the right value for array literals' do
44
a = 0
55
b = [a,a=1]
66
expect(b).to eq([0,1])
77
end
8+
9+
it 'returns the right value for hash literals' do
10+
a = 0
11+
b = { a => a, (a = 1) => a } # => { 1 => 1 } (MRI: {0=>0, 1=>1})
12+
c = { a => a, a => (a = 2) } # => { 2 => 2 } (MRI: {1=>2})
13+
expect(b).to eq({0=>0, 1=>1})
14+
expect(c).to eq({1=>2})
15+
end
816
end
917

0 commit comments

Comments
 (0)