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 82a419e326597..8014582fa51d9 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -3999,11 +3999,19 @@ private boolean isAllowedEarlyReference(DiagnosticPosition pos, Env<AttrContext> * @param v The variable */ public boolean isEarlyReference(Env<AttrContext> env, JCTree base, VarSymbol v) { - return env.info.ctorPrologue && - (v.flags() & STATIC) == 0 && - v.owner.kind == TYP && - types.isSubtype(env.enclClass.type, v.owner.type) && - (base == null || TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base)); + if (env.info.ctorPrologue && + (v.flags() & STATIC) == 0 && + v.isMemberOf(env.enclClass.sym, types)) { + + // Allow "Foo.this.x" when "Foo" is (also) an outer class, as this refers to the outer instance + if (base != null) { + return TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base); + } + + // It's an early reference to an instance field member of the current instance + return true; + } + return false; } /* *************************************************************************** diff --git a/test/langtools/tools/javac/SuperInit/SuperInitGood.java b/test/langtools/tools/javac/SuperInit/SuperInitGood.java index 11b845f7d328a..46f941145a8b1 100644 --- a/test/langtools/tools/javac/SuperInit/SuperInitGood.java +++ b/test/langtools/tools/javac/SuperInit/SuperInitGood.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,7 +22,7 @@ */ /* * @test - * @bug 8194743 + * @bug 8194743 8349754 * @summary Test valid placements of super()/this() in constructors * @enablePreview */ @@ -490,6 +490,17 @@ class A { } } + // Test for JDK-8349754 + public static class Test23 { + private int i; + class Sub extends Test23 { + Sub() { + i = 3; // here "i" refers to "Test23.this.i", not "this.i" - so it's OK + super(); + } + } + } + public static void main(String[] args) { new Test0(); new Test1();