From 12a61bce8db5e6b152eb101de1662847bebb7997 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Wed, 19 Jun 2024 04:00:58 +0000 Subject: [PATCH] 8334043: VerifyError when inner class is accessed in prologue Reviewed-by: jpai Backport-of: d4c13737171b7ab7a8a29a69fa9965f8363c5aee --- .../classes/com/sun/tools/javac/comp/Resolve.java | 15 ++++++++++++++- .../tools/javac/SuperInit/EarlyAssignments.java | 11 +++++++++++ .../tools/javac/SuperInit/EarlyAssignments.out | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 5b60a7383aa..079a0bc5c6a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -3863,7 +3863,20 @@ private boolean isAllowedEarlyReference(DiagnosticPosition pos, Env // Get the symbol's qualifier, if any JCExpression lhs = TreeInfo.skipParens(assign.lhs); - JCExpression base = lhs instanceof JCFieldAccess select ? select.selected : null; + JCExpression base; + switch (lhs.getTag()) { + case IDENT: + base = null; + break; + case SELECT: + JCFieldAccess select = (JCFieldAccess)lhs; + base = select.selected; + if (!TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base)) + return false; + break; + default: + return false; + } // If an early reference, the field must not be declared in a superclass if (isEarlyReference(env, base, v) && v.owner != env.enclClass.sym) diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignments.java b/test/langtools/tools/javac/SuperInit/EarlyAssignments.java index 89a13ccf0ff..c3cad5d7016 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignments.java +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignments.java @@ -158,4 +158,15 @@ public Inner7() { super(); } } + + public static class Inner8 { + class Inner8a { + int x; + } + + public Inner8() { + this.new Inner8a().x = 1; // FAIL - illegal early access + super(); + } + } } diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignments.out b/test/langtools/tools/javac/SuperInit/EarlyAssignments.out index 81b4f20b505..38182c2d312 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignments.out +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignments.out @@ -23,6 +23,7 @@ EarlyAssignments.java:134:17: compiler.err.cant.ref.before.ctor.called: super EarlyAssignments.java:139:23: compiler.err.cant.ref.before.ctor.called: this EarlyAssignments.java:148:13: compiler.err.cant.assign.initialized.before.ctor.called: x EarlyAssignments.java:157:13: compiler.err.cant.assign.val.to.var: final, x +EarlyAssignments.java:168:13: compiler.err.cant.ref.before.ctor.called: this - compiler.note.preview.filename: EarlyAssignments.java, DEFAULT - compiler.note.preview.recompile -25 errors +26 errors