Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8349754: Invalid "early reference" error when class extends an outer class #23545

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}

/* ***************************************************************************
15 changes: 13 additions & 2 deletions test/langtools/tools/javac/SuperInit/SuperInitGood.java
Original file line number Diff line number Diff line change
@@ -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();