diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 5a6e0fb8556..65da42f3fbc 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -954,10 +954,10 @@ public class Attr extends JCTree.Visitor { // make sure class has been completed: c.complete(); - // If this class appears as an anonymous class in a constructor - // prologue, disable implicit outer instance from being passed. - // (This would be an illegal access to "this before super"). - if (ctorProloguePrev && env.tree.hasTag(NEWCLASS)) { + // If a class declaration appears in a constructor prologue, + // that means it's either a local class or an anonymous class. + // Either way, there is no immediately enclosing instance. + if (ctorProloguePrev) { c.flags_field |= NOOUTERTHIS; } attribClass(tree.pos(), c); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java index 3ad0188ee0c..f1c897a917e 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java @@ -1843,7 +1843,6 @@ public class Lower extends TreeTranslator { List ots = outerThisStack; if (ots.isEmpty()) { log.error(pos, Errors.NoEnclInstanceOfTypeInScope(c)); - Assert.error(); return makeNull(); } VarSymbol ot = ots.head; diff --git a/test/langtools/tools/javac/LocalClassCtorPrologue.java b/test/langtools/tools/javac/LocalClassCtorPrologue.java new file mode 100644 index 00000000000..2a7178ed857 --- /dev/null +++ b/test/langtools/tools/javac/LocalClassCtorPrologue.java @@ -0,0 +1,32 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8328649 + * @summary Verify local classes in constructor prologues don't have enclosing instances + * @compile/fail/ref=LocalClassCtorPrologue.out -XDrawDiagnostics LocalClassCtorPrologue.java + * @enablePreview + */ + +class LocalClassCtorPrologue { + + int x; + + LocalClassCtorPrologue() { + class Local { + { + x++; // this should fail + } + } + super(); + } + + public class Inner { + public Inner() { + class Local { + { + x++; // this should work + } + }; + super(); + } + } +} diff --git a/test/langtools/tools/javac/LocalClassCtorPrologue.out b/test/langtools/tools/javac/LocalClassCtorPrologue.out new file mode 100644 index 00000000000..f1a999af491 --- /dev/null +++ b/test/langtools/tools/javac/LocalClassCtorPrologue.out @@ -0,0 +1,4 @@ +LocalClassCtorPrologue.java:16:17: compiler.err.no.encl.instance.of.type.in.scope: LocalClassCtorPrologue +- compiler.note.preview.filename: LocalClassCtorPrologue.java, DEFAULT +- compiler.note.preview.recompile +1 error diff --git a/test/langtools/tools/javac/SuperInit/SuperInitGood.java b/test/langtools/tools/javac/SuperInit/SuperInitGood.java index cbcbee8fef0..f82ee6188f1 100644 --- a/test/langtools/tools/javac/SuperInit/SuperInitGood.java +++ b/test/langtools/tools/javac/SuperInit/SuperInitGood.java @@ -407,32 +407,6 @@ public class SuperInitGood { } } - // local class declared before super(), but not used until after super() - public static class Test20 { - public Test20() { - class Foo { - Foo() { - Test20.this.hashCode(); - } - } - super(); - new Foo(); - } - } - - // local class inside super() parameter list - public static class Test21 extends AtomicReference { - private int x; - public Test21() { - super(switch ("foo".hashCode()) { - default -> { - class Nested {{ System.out.println(x); }} // class is NOT instantiated - OK - yield "bar"; - } - }); - } - } - public static void main(String[] args) { new Test0(); new Test1(); @@ -474,7 +448,5 @@ public class SuperInitGood { assert false : "unexpected exception: " + e; } new Test19(123); - new Test20(); - new Test21(); } }