Skip to content

Commit

Permalink
challenge more nesting situations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-herrmann committed Jul 8, 2024
1 parent 0ff2dcd commit 93ff92d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void resolve(BlockScope scope) {
if (methodDeclaration == null
|| !methodDeclaration.isConstructor()
|| (((ConstructorDeclaration) methodDeclaration).constructorCall != this
&& !scope.isInsideEarlyConstructionContext(null, true))) {
&& !scope.isInsideEarlyConstructionContext(null, false))) {
if (!(methodDeclaration instanceof CompactConstructorDeclaration)) {// already flagged for CCD
scope.problemReporter().invalidExplicitConstructorCall(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ public TypeBinding resolveType(BlockScope scope) {
}
} else {
if ((this.bits & ASTNode.IsStrictlyAssigned) == 0
&& this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, true)
&& this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, false)
&& (this.receiver instanceof ThisReference thisReference && thisReference.isImplicitThis())) { // explicit thisReference error flagging taken care in ThisReference
scope.problemReporter().errorExpressionInPreConstructorContext(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ public TypeBinding resolveType(BlockScope scope) {
this.bits |= NeedReceiverGenericCast;
}
}
if (this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, true) &&
if (this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, false) &&
(this.receiver instanceof ThisReference thisReference && thisReference.isImplicitThis())) {
scope.problemReporter().errorExpressionInPreConstructorContext(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ public TypeBinding resolveType(BlockScope scope) {
}
} else {
boolean inStaticContext = scope.methodScope().isStatic;
if (scope.isInsideEarlyConstructionContext(fieldBinding.declaringClass, true))
if (scope.isInsideEarlyConstructionContext(fieldBinding.declaringClass, false))
scope.problemReporter().errorExpressionInPreConstructorContext(this);
if (this.indexOfFirstFieldBinding == 1) {
if (scope.compilerOptions().getSeverity(CompilerOptions.UnqualifiedFieldAccess) != ProblemSeverities.Ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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, true))
if (enclosingReceiverType != null && !enclosingReceiverType.isInterface() && scope.isInsideEarlyConstructionContext(enclosingReceiverType, false))
scope.problemReporter().errorExpressionInPreConstructorContext(this);
return this.resolvedType = (this.currentCompatibleType.isInterface()
? this.currentCompatibleType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ else if (scope.compilerOptions().complianceLevel >= ClassFileConstants.JDK16) {
if (ms.isStatic)
ms.problemReporter().errorThisSuperInStatic(this);
}
if (scope.isInsideEarlyConstructionContext(this.resolvedType, true)) {
if (scope.isInsideEarlyConstructionContext(this.resolvedType, false)) {
scope.problemReporter().errorExpressionInPreConstructorContext(this);
}
MethodScope methodScope = scope.namedMethodScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public TypeBinding checkFieldAccess(BlockScope scope) {
if (scope.compilerOptions().getSeverity(CompilerOptions.UnqualifiedFieldAccess) != ProblemSeverities.Ignore) {
scope.problemReporter().unqualifiedFieldAccess(this, fieldBinding);
}
if (this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, true)) {
if (this.actualReceiverType != null && scope.isInsideEarlyConstructionContext(this.actualReceiverType, false)) {
scope.problemReporter().errorExpressionInPreConstructorContext(this);
}
// must check for the static status....
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5699,8 +5699,10 @@ public boolean isInsideEarlyConstructionContext(TypeBinding targetClass, boolean
if (currentEnclosing.insideEarlyConstructionContext)
return true;
}
if (!considerEnclosings || currentTarget.isStatic())
if (!considerEnclosings
|| (currentTarget instanceof ReferenceBinding currentRefBind && !currentRefBind.hasEnclosingInstanceContext())) {
break;
}
currentTarget = currentTarget.enclosingType();
}
currentEnclosing = currentEnclosing.parent.classScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,39 @@ void m() {
""";
runner.runNegativeTest();
}
public void test007d() {
// early construction context of far outer, while inners happily use 'this'
Runner runner = new Runner(false);
runner.testFiles = new String[] {
"X.java",
"""
class X {
X() {
class E {
E() {
this.i = new Inner();
this.i.j++;
}
class Inner {
int j = 3;;
void m() {
E.this.i = this;
}
}
Inner i;
}
super();
System.out.print(new E().i.j);
}
public static void main(String... args) {
new X();
}
}
"""
};
runner.expectedOutputString = "4";
runner.runConformTest();
}
// an illegal access does not need to contain a this or super keyword:
public void test008() {
runNegativeTest(new String[] {
Expand Down Expand Up @@ -428,6 +461,32 @@ class A {
"You are using a preview language feature that may or may not be supported in a future release\n" +
"----------\n");
}
public void test008_OK() {
// early construction context of outer
runConformTest(new String[] {
"A.java",
"""
class A {
A() {
class Local {
int i;
int getI() { return i; }
Local() {
i++;
System.out.print(getI());
}
}
new Local();
super();
}
public static void main(String... args) {
new A();
}
}
"""
},
"1");
}
//an expression involving this does not refer to the current instance but,
// rather, to the enclosing instance of an inner class:
public void test009_NOK() {
Expand Down Expand Up @@ -535,6 +594,31 @@ class Inner {}
"You are using a preview language feature that may or may not be supported in a future release\n" +
"----------\n");
}
public void test011_nested() {
runConformTest(new String[] {
"Outer.java",
"""
class Outer {
Outer() {
class Local {
class Inner { }
Local() {
Object o = new Inner(); // No Error - enclosing Local is not in early construction
System.out.print(o.getClass().getName());
}
};
Local l = new Local();
Local.Inner i = l.new Inner();
super();
}
public static void main(String... args) {
new Outer();
}
}
"""
},
"Outer$1Local$Inner");
}
/* in a pre-construction context, class instance creation expressions that declare
* anonymous classes cannot have the newly created object as the implicit enclosing
* instance
Expand Down

0 comments on commit 93ff92d

Please sign in to comment.