Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-herrmann committed Jul 8, 2024
1 parent f5cc3dc commit 0ff2dcd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,10 @@ public void resolve(BlockScope scope) {
}
if (methodDeclaration == null
|| !methodDeclaration.isConstructor()
|| (!scope.isInsideEarlyConstructionContext(null, true) &&
((ConstructorDeclaration) methodDeclaration).constructorCall != this)) {
|| (((ConstructorDeclaration) methodDeclaration).constructorCall != this
&& !scope.isInsideEarlyConstructionContext(null, true))) {
if (!(methodDeclaration instanceof CompactConstructorDeclaration)) {// already flagged for CCD
if (!scope.isInsideEarlyConstructionContext(null, true))
scope.problemReporter().invalidExplicitConstructorCall(this);
scope.problemReporter().invalidExplicitConstructorCall(this);
}
// fault-tolerance
if (this.qualification != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,8 @@
2001 = Unnamed variables must have an initializer

# Statements before Super
2022 = Cannot use {0} in a pre-construction context
2023 = {0} statement not allowed in prologue
2022 = Cannot use {0} in an early construction context
2023 = {0} statement not allowed in an early construction context


### ELABORATIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,17 @@ class A {
"1. ERROR in X.java (at line 4)\n" +
" this.i++; // Error\n" +
" ^^^^\n" +
"Cannot use this in a pre-construction context\n" +
"Cannot use this in an early construction context\n" +
"----------\n" +
"2. ERROR in X.java (at line 5)\n" +
" this.hashCode(); // Error\n" +
" ^^^^\n" +
"Cannot use this in a pre-construction context\n" +
"Cannot use this in an early construction context\n" +
"----------\n" +
"3. ERROR in X.java (at line 6)\n" +
" System.out.print(this); // Error\n" +
" ^^^^\n" +
"Cannot use this in a pre-construction context\n" +
"Cannot use this in an early construction context\n" +
"----------\n" +
"4. WARNING in X.java (at line 7)\n" +
" super();\n" +
Expand Down Expand Up @@ -330,7 +330,7 @@ class E extends D {
"1. ERROR in X.java (at line 6)\n" +
" super.i++; // Error\n" +
" ^^^^^\n" +
"Cannot use super in a pre-construction context\n" +
"Cannot use super in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 7)\n" +
" super();\n" +
Expand Down Expand Up @@ -364,6 +364,7 @@ public static void main(String... args) {
}
"""
};
runner.expectedOutputString = "1";
runner.runConformTest();
}
public void test007c() {
Expand All @@ -390,7 +391,7 @@ void m() {
1. ERROR in X.java (at line 5)
System.out.print(X.this);
^^^^^^
Cannot use X.this in a pre-construction context
Cannot use X.this in an early construction context
----------
""";
runner.runNegativeTest();
Expand All @@ -414,12 +415,12 @@ class A {
"1. ERROR in X.java (at line 4)\n" +
" i++; // Error\n" +
" ^\n" +
"Cannot use i in a pre-construction context\n" +
"Cannot use i in an early construction context\n" +
"----------\n" +
"2. ERROR in X.java (at line 5)\n" +
" hashCode(); // Error\n" +
" ^^^^^^^^^^\n" +
"Cannot use hashCode() in a pre-construction context\n" +
"Cannot use hashCode() in an early construction context\n" +
"----------\n" +
"3. WARNING in X.java (at line 6)\n" +
" super();\n" +
Expand All @@ -429,16 +430,14 @@ class A {
}
//an expression involving this does not refer to the current instance but,
// rather, to the enclosing instance of an inner class:
public void test009() {
public void test009_NOK() {
runNegativeTest(new String[] {
"X.java",
"B.java",
"""
class B {
int b;
class C {
int c;
C() {
B.this.b++; // Allowed - enclosing instance
C.this.c++; // Error - same instance
super();
}
Expand All @@ -447,17 +446,39 @@ class C {
"""
},
"----------\n" +
"1. ERROR in X.java (at line 7)\n" +
"1. ERROR in B.java (at line 5)\n" +
" C.this.c++; // Error - same instance\n" +
" ^^^^^^\n" +
"Cannot use C.this in a pre-construction context\n" +
"Cannot use C.this in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 8)\n" +
"2. WARNING in B.java (at line 6)\n" +
" super();\n" +
" ^^^^^^^^\n" +
"You are using a preview language feature that may or may not be supported in a future release\n" +
"----------\n");
}
public void test009_OK() {
runConformTest(new String[] {
"B.java",
"""
class B {
int b;
class C {
C() {
B.this.b++; // Allowed - enclosing instance
super();
}
}
public static void main(String... args) {
B b = new B();
C c = b.new C();
System.out.print(b.b);
}
}
"""
},
"1");
}
/* The invocation hello() that appears in the pre-construction context of the
* Inner constructor is allowed because it refers to the enclosing instance of
* Inner (which, in this case, has the type Outer), not the instance of Inner
Expand Down Expand Up @@ -506,7 +527,7 @@ class Inner {}
"1. ERROR in X.java (at line 4)\n" +
" new Inner(); // Error - \'this\' is enclosing instance\n" +
" ^^^^^^^^^^^\n" +
"Cannot use new Inner() in a pre-construction context\n" +
"Cannot use new Inner() in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 5)\n" +
" super();\n" +
Expand Down Expand Up @@ -539,7 +560,7 @@ class S {}
" x() {\n" +
" super();\n" +
" }\n" +
"} in a pre-construction context\n" +
"} in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 5)\n" +
" super();\n" +
Expand Down Expand Up @@ -727,7 +748,7 @@ public static void main(String[] args) {
"1. ERROR in X.java (at line 9)\n" +
" return; // Error - return not allowed here\n" +
" ^^^^^^^\n" +
"return ; statement not allowed in prologue\n" +
"return ; statement not allowed in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 10)\n" +
" super(i);\n" +
Expand Down Expand Up @@ -761,7 +782,7 @@ public static void main(String[] args) {
"1. ERROR in X.java (at line 4)\n" +
" return; // Error - return not allowed here\n" +
" ^^^^^^^\n" +
"return ; statement not allowed in prologue\n" +
"return ; statement not allowed in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 5)\n" +
" this(i, 0);\n" +
Expand Down Expand Up @@ -882,7 +903,7 @@ public static void main(String[] argv) {
"1. ERROR in X.java (at line 12)\n" +
" int j = a.i;\n" +
" ^^^\n" +
"Cannot use a.i in a pre-construction context\n" +
"Cannot use a.i in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 14)\n" +
" super();\n" +
Expand Down Expand Up @@ -949,7 +970,7 @@ public static void main(String[] args) {
"1. ERROR in X.java (at line 8)\n" +
" I tos = super::toString;\n" +
" ^^^^^\n" +
"Cannot use super in a pre-construction context\n" +
"Cannot use super in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 9)\n" +
" this(i, 0);\n" +
Expand Down Expand Up @@ -987,7 +1008,7 @@ public static void main(String[] argv) {
"1. ERROR in X.java (at line 13)\n" +
" int j = a.getI();\n" +
" ^\n" +
"Cannot use a in a pre-construction context\n" +
"Cannot use a in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 15)\n" +
" super();\n" +
Expand Down Expand Up @@ -1022,7 +1043,7 @@ public static void main(String argv[]) {
"1. ERROR in X.java (at line 8)\n" +
" int j = J.super.getI();\n" +
" ^^^^^^^\n" +
"Cannot use J.super in a pre-construction context\n" +
"Cannot use J.super in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 9)\n" +
" super();\n" +
Expand Down Expand Up @@ -1057,7 +1078,7 @@ public static void main(String argv[]) {
"1. ERROR in X.java (at line 8)\n" +
" int j = J.super.getI();\n" +
" ^^^^^^^\n" +
"Cannot use J.super in a pre-construction context\n" +
"Cannot use J.super in an early construction context\n" +
"----------\n" +
"2. WARNING in X.java (at line 9)\n" +
" this(j);\n" +
Expand Down Expand Up @@ -1427,7 +1448,7 @@ void foo() {}
1. ERROR in Test3.java (at line 5)
foo(); // error is correct
^^^^^
Cannot use foo() in a pre-construction context
Cannot use foo() in an early construction context
----------
""";
runner.runNegativeTest();
Expand Down Expand Up @@ -1462,7 +1483,7 @@ public static void main(String... args) {
1. ERROR in Test.java (at line 5)
foo(this);
^^^^^^^^^
Cannot use foo(this) in a pre-construction context
Cannot use foo(this) in an early construction context
----------
""";
runner.runNegativeTest();
Expand Down

0 comments on commit 0ff2dcd

Please sign in to comment.