Skip to content

Commit

Permalink
[23] anonymous creation in super call allowed?
Browse files Browse the repository at this point in the history
For allocation expressions:
 + only check enclosing types
 + but never travel out past static / local types

Fixes eclipse-jdt#3132
  • Loading branch information
stephan-herrmann committed Oct 21, 2024
1 parent 251af63 commit 60848d8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,13 @@ public TypeBinding resolveType(BlockScope scope) {
protected void checkEarlyConstructionContext(BlockScope scope) {
if (JavaFeature.FLEXIBLE_CONSTRUCTOR_BODIES.isSupported(scope.compilerOptions())
&& this.type != null && this.type.resolvedType instanceof ReferenceBinding currentType) {
TypeBinding uninitialized = scope.getMatchingUninitializedType(currentType, !currentType.isLocalType());
// only enclosing types of non-static member types are relevant
if (currentType.isStatic() || currentType.isLocalType())
return;
currentType = currentType.enclosingType();
if (currentType == null)
return;
TypeBinding uninitialized = scope.getMatchingUninitializedType(currentType, true);
if (uninitialized != null)
scope.problemReporter().allocationInEarlyConstructionContext(this, this.resolvedType, uninitialized);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5756,6 +5756,8 @@ public TypeBinding getMatchingUninitializedType(TypeBinding targetClass, boolean
|| (currentTarget instanceof ReferenceBinding currentRefBind && !currentRefBind.hasEnclosingInstanceContext())) {
break;
}
if (currentTarget.isStatic() || currentTarget.isLocalType())
break;
currentTarget = currentTarget.enclosingType();
}
currentEnclosing = currentEnclosing.parent.classScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2281,4 +2281,53 @@ class DeeplyNested extends NestedInX1 {
""";
runner.runNegativeTest();
}

public void testGH3132() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
public class X {
class Nested {
Nested(Object o) {}
}
class AnotherNested extends Nested {
AnotherNested() {
super(new Object() { // Cannot instantiate class new Object(){} in an early construction context of class X.AnotherNested
});
}
}
public static void main(String... args) {
new X().new AnotherNested();
}
}
"""
};
runner.runConformTest();
}

public void testGH3132_2() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
class O {} // demonstrates the the bug was not specific to j.l.Object
public class X {
class Nested extends O {
Nested(Object o) {}
}
class AnotherNested extends Nested {
AnotherNested() {
super(new O() {
});
}
}
public static void main(String... args) {
new X().new AnotherNested();
}
}
"""
};
runner.runConformTest();
}
}

0 comments on commit 60848d8

Please sign in to comment.