Skip to content

Commit 7b3a96d

Browse files
archiecobbsmcimadamore
authored andcommittedJun 19, 2024
8334488: Improve error for illegal early access from nested class
Reviewed-by: mcimadamore
1 parent 07ebda5 commit 7b3a96d

File tree

7 files changed

+25
-5
lines changed

7 files changed

+25
-5
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ public void visitClassDef(JCClassDecl tree) {
950950
Optional.ofNullable(env.info.attributionMode.isSpeculative ?
951951
argumentAttr.withLocalCacheContext() : null);
952952
boolean ctorProloguePrev = env.info.ctorPrologue;
953-
env.info.ctorPrologue = false;
954953
try {
955954
// Local and anonymous classes have not been entered yet, so we need to
956955
// do it now.
@@ -995,7 +994,7 @@ public void visitMethodDef(JCMethodDecl tree) {
995994
Lint lint = env.info.lint.augment(m);
996995
Lint prevLint = chk.setLint(lint);
997996
boolean ctorProloguePrev = env.info.ctorPrologue;
998-
env.info.ctorPrologue = false;
997+
Assert.check(!env.info.ctorPrologue);
999998
MethodSymbol prevMethod = chk.setMethod(m);
1000999
try {
10011000
deferredLintHandler.flush(tree.pos(), lint);

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
209209
localEnv.info.lint = null; // leave this to be filled in by Attr,
210210
// when annotations have been processed
211211
localEnv.info.isAnonymousDiamond = TreeInfo.isDiamond(env.tree);
212+
localEnv.info.ctorPrologue = false;
212213
return localEnv;
213214
}
214215

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
AnonymousInSuperCallNegTest.java:23:49: compiler.err.no.encl.instance.of.type.in.scope: AnonymousInSuperCallNegTest.JavacBug
1+
AnonymousInSuperCallNegTest.java:23:49: compiler.err.cant.ref.before.ctor.called: x
22
1 error
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
LocalClassCtorPrologue.java:16:17: compiler.err.no.encl.instance.of.type.in.scope: LocalClassCtorPrologue
1+
LocalClassCtorPrologue.java:16:17: compiler.err.cant.ref.before.ctor.called: x
22
- compiler.note.preview.filename: LocalClassCtorPrologue.java, DEFAULT
33
- compiler.note.preview.recompile
44
1 error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8334488
4+
* @summary Verify the error message generated for early access from inner class
5+
* @compile/fail/ref=EarlyInnerAccessErrorMessageTest.out -XDrawDiagnostics EarlyInnerAccessErrorMessageTest.java
6+
* @enablePreview
7+
*/
8+
public class EarlyInnerAccessErrorMessageTest {
9+
int x;
10+
EarlyInnerAccessErrorMessageTest() {
11+
class Inner {
12+
{ System.out.println(x); }
13+
}
14+
super();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EarlyInnerAccessErrorMessageTest.java:12:34: compiler.err.cant.ref.before.ctor.called: x
2+
- compiler.note.preview.filename: EarlyInnerAccessErrorMessageTest.java, DEFAULT
3+
- compiler.note.preview.recompile
4+
1 error
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
EarlyLocalClass.java:12:32: compiler.err.no.encl.instance.of.type.in.scope: EarlyLocalClass
1+
EarlyLocalClass.java:12:32: compiler.err.cant.ref.before.ctor.called: this
22
- compiler.note.preview.filename: EarlyLocalClass.java, DEFAULT
33
- compiler.note.preview.recompile
44
1 error

0 commit comments

Comments
 (0)
Please sign in to comment.