Skip to content
Closed
Show file tree
Hide file tree
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
Expand Up @@ -4008,11 +4008,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;
}

/* ***************************************************************************
Expand Down
14 changes: 13 additions & 1 deletion test/langtools/tools/javac/SuperInit/SuperInitGood.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
/*
* @test
* @bug 8194743 8345438 8356551
* @bug 8194743 8345438 8356551 8349754
* @summary Test valid placements of super()/this() in constructors
*/

Expand Down Expand Up @@ -501,6 +501,17 @@ class Local {
}
}

// Test for JDK-8349754
public static class Test24 {
private int i;
class Sub extends Test24 {
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();
Expand Down Expand Up @@ -547,5 +558,6 @@ public static void main(String[] args) {
new Test21((float)123);
new Test22('x');
new Test23();
new Test24();
}
}