Skip to content

Commit

Permalink
Showing 3 changed files with 29 additions and 3 deletions.
7 changes: 5 additions & 2 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
@@ -1220,11 +1220,14 @@ public Operand classVarContainer(boolean declContext) {
int n = 0;
IRScope cvarScope = scope;
while (cvarScope != null && !(cvarScope instanceof IREvalScript) && !cvarScope.isNonSingletonClassBody()) {
// For loops don't get their own static scope
if (!(cvarScope instanceof IRFor)) {
n++;
}
cvarScope = cvarScope.getLexicalParent();
n++;
}

if ((cvarScope != null) && cvarScope.isNonSingletonClassBody()) {
if (cvarScope != null && cvarScope.isNonSingletonClassBody()) {
return ScopeModule.ModuleFor(n);
} else {
return addResultInstr(new GetClassVarContainerModuleInstr(createTemporaryVariable(),
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ir/operands/ScopeModule.java
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
public class ScopeModule extends Operand {
// First four scopes are so common and this operand is immutable so we share them.
public static final ScopeModule[] SCOPE_MODULE = {
new ScopeModule(0), new ScopeModule(1), new ScopeModule(2), new ScopeModule(3), new ScopeModule(4)
new ScopeModule(0), new ScopeModule(1), new ScopeModule(2), new ScopeModule(3), new ScopeModule(4)
};

public static ScopeModule ModuleFor(int depth) {
23 changes: 23 additions & 0 deletions spec/regression/GH-3042_cvar_access_in_for_loop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Foo
@@A = 42

def self.test_it
a = []
a << @@A
for name in [1] do
a << @@A
for value in [1] do
a << @@A
end
end

a
end
end

# https://github.com/jruby/jruby/issues/3042
describe 'cvar access in for loop' do
it 'should use correct module depth' do
expect(Foo.test_it).to eq([42, 42, 42])
end
end

0 comments on commit d2979e3

Please sign in to comment.