Skip to content

Commit

Permalink
[23] super reference disallowed in the context of a field access
Browse files Browse the repository at this point in the history
+ distinguish super vs. outer type for early constr. check

fixes eclipse-jdt#3094
  • Loading branch information
stephan-herrmann committed Oct 21, 2024
1 parent 1511df2 commit 74cb1ef
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ public TypeBinding resolveType(BlockScope scope) {
ReferenceBinding enclosingReceiverType = scope.enclosingReceiverType();
// interface-qualified this selects a default method in a super interface,
// but here we are only interested in supers of *enclosing instances*:
if (enclosingReceiverType != null && !enclosingReceiverType.isInterface() && scope.isInsideEarlyConstructionContext(enclosingReceiverType, false))
scope.problemReporter().errorExpressionInEarlyConstructionContext(this);
if (enclosingReceiverType != null && !enclosingReceiverType.isInterface()) {
TypeBinding typeToCheck = (enclosingReceiverType.isCompatibleWith(this.resolvedType))
? enclosingReceiverType // cannot reference super of the current type
: this.resolvedType; // assumeably not a super but an outer type
if (scope.isInsideEarlyConstructionContext(typeToCheck, false))
scope.problemReporter().errorExpressionInEarlyConstructionContext(this);
}
return this.resolvedType = (this.currentCompatibleType.isInterface()
? this.currentCompatibleType
: this.currentCompatibleType.superclass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2182,4 +2182,103 @@ public class TestFlow {
""";
runner.runNegativeTest();
}

public void testGH3094() {
Runner runner = new Runner(false);
runner.testFiles = new String[] {
"X.java",
"""
public class X {
class Nested extends X1 {
X1 xy;
class DeeplyNested extends NestedInX1 {
DeeplyNested(float f) {
Nested.super.x1.super(); // Error here
}
}
}
public static void main(String... args) {
Nested nest = new X().new Nested();
nest.x1 = new X1();
nest.new DeeplyNested(1.1f);
}
}
class X1 {
X1 x1;
class NestedInX1 {}
}
"""
};
runner.runConformTest();
}

public void testGH3094_2() {
Runner runner = new Runner(false);
runner.testFiles = new String[] {
"X.java",
"""
public class X {
class Nested extends X1 {
X1 xy;
class DeeplyNested extends NestedInX1 {
DeeplyNested(float f) {
Nested.this.x1.super();
}
}
}
public static void main(String... args) {
Nested nest = new X().new Nested();
nest.x1 = new X1();
nest.new DeeplyNested(1.1f);
}
}
class X1 {
X1 x1;
class NestedInX1 {}
}
"""
};
runner.runConformTest();
}

public void testGH3094_3() {
Runner runner = new Runner(false);
runner.testFiles = new String[] {
"X.java",
"""
public class X {
class Nested extends X1 {
X1 xy;
Nested() {
class DeeplyNested extends NestedInX1 {
DeeplyNested(float f) {
Nested.this.x1.super();
}
}
super();
}
}
}
class X1 {
X1 x1;
class NestedInX1 {}
}
"""
};
runner.expectedCompilerLog =
"""
----------
1. WARNING in X.java (at line 5)
class DeeplyNested extends NestedInX1 {
^^^^^^^^^^^^
The type DeeplyNested is never used locally
----------
2. ERROR in X.java (at line 7)
Nested.this.x1.super();
^^^^^^^^^^^^^^
Cannot read field x1 in an early construction context
----------
""";
runner.runNegativeTest();
}
}

0 comments on commit 74cb1ef

Please sign in to comment.