Skip to content

Commit

Permalink
[23] ECJ rejects local instantiation in early construction context (#…
Browse files Browse the repository at this point in the history
…3165)

Avoid allocating enclosing instance beyond a static method scope

Fixes #3153
  • Loading branch information
stephan-herrmann authored Oct 25, 2024
1 parent b9e535a commit becc3cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ public void manageEnclosingInstanceAccessIfNecessary(BlockScope currentScope, Fl
Scope outerScope = currentScope.parent;
if (!methodScope.isConstructorCall) {
nestedType.addSyntheticArgumentAndField(nestedType.enclosingType());
outerScope = outerScope.enclosingClassScope();
outerScope = outerScope.enclosingInstanceScope();
earlySeen = methodScope.isInsideEarlyConstructionContext(nestedType.enclosingType(), false);
}
if (JavaFeature.FLEXIBLE_CONSTRUCTOR_BODIES.isSupported(currentScope.compilerOptions())) {
Expand All @@ -1087,6 +1087,8 @@ public void manageEnclosingInstanceAccessIfNecessary(BlockScope currentScope, Fl
earlySeen = cs.insideEarlyConstructionContext;
}
outerScope = outerScope.parent;
if (outerScope instanceof MethodScope ms && ms.isStatic)
break;
}
}
}
Expand Down Expand Up @@ -1124,6 +1126,7 @@ public void manageEnclosingInstanceAccessIfNecessary(BlockScope currentScope, Fl
}
}


/**
* Access emulation for a local member type
* force to emulation of access to direct enclosing instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,18 @@ public final ClassScope enclosingClassScope() {
return null; // may answer null if no type around
}

public ClassScope enclosingInstanceScope() {
Scope scope = this;
while (true) {
scope = scope.parent;
if (scope == null || scope instanceof MethodScope ms && ms.isStatic) {
return null;
} else if (scope instanceof ClassScope cs) {
return cs;
}
}
}

public final ClassScope enclosingTopMostClassScope() {
Scope scope = this;
while (scope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2330,4 +2330,25 @@ public static void main(String... args) {
};
runner.runConformTest();
}

public void testGH3153() {
runConformTest(new String[] {
"X.java",
"""
public class X {
public static void main(String[] argv) {
class Inner {
Inner() {
class Local {}
new Local() {}; // Error: No enclosing instance of the type X is accessible in scope
super();
}
}
new Inner();
}
}
""" },
"");
}

}

0 comments on commit becc3cd

Please sign in to comment.