Skip to content
Open
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 @@ -1569,9 +1569,10 @@ void analyzeSymbol(JCTree tree) {
* @param sym The symbol
*/
private boolean isEarlyReference(Env<AttrContext> env, JCTree tree, Symbol sym) {
if ((sym.flags() & STATIC) == 0 &&
(sym.kind == VAR || sym.kind == MTH) &&
sym.isMemberOf(env.enclClass.sym, types)) {
if ((sym.kind == VAR || sym.kind == MTH) &&
sym.isMemberOf(env.enclClass.sym, types) &&
((sym.flags() & STATIC) == 0 ||
(sym.kind == MTH && tree instanceof JCFieldAccess))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we only restrict MTH? We can write code like:

class A { static int a = 5; }
class B extends A { B() { IO.println(B.super.a); } }

Wonder if we should block B.super.a style stuff in early construction.

// Allow "Foo.this.x" when "Foo" is (also) an outer class, as this refers to the outer instance
if (tree instanceof JCFieldAccess fa) {
return TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, fa.selected);
Expand Down
22 changes: 22 additions & 0 deletions test/langtools/tools/javac/SuperInit/SuperInitFails.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,26 @@ class Inner9Test extends Medium {
}
}
}

static class Inner10 {
static boolean testMethod() { return true; }
Inner10() {}
Inner10(int a) {
Inner10.this.testMethod();
this();
}
}

static class Inner11 {
class Inner11_1 {
static <T> void m() {}
}

class Inner11_2 extends Inner11_1 {
Inner11_2() {
Inner11_1.super.<String>m();
super();
}
}
}
}
4 changes: 3 additions & 1 deletion test/langtools/tools/javac/SuperInit/SuperInitFails.out
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ SuperInitFails.java:253:13: compiler.err.cant.ref.before.ctor.called: x
SuperInitFails.java:266:18: compiler.err.cant.ref.before.ctor.called: x
SuperInitFails.java:282:50: compiler.err.cant.ref.before.ctor.called: check
SuperInitFails.java:283:39: compiler.err.cant.ref.before.ctor.called: check
SuperInitFails.java:293:25: compiler.err.cant.ref.before.ctor.called: testMethod()
SuperInitFails.java:305:26: compiler.err.not.encl.class: SuperInitFails.Inner11.Inner11_1
SuperInitFails.java:35:13: compiler.err.call.must.only.appear.in.ctor
SuperInitFails.java:39:14: compiler.err.call.must.only.appear.in.ctor
SuperInitFails.java:43:14: compiler.err.call.must.only.appear.in.ctor
Expand All @@ -37,4 +39,4 @@ SuperInitFails.java:55:32: compiler.err.call.must.only.appear.in.ctor
SuperInitFails.java:85:18: compiler.err.ctor.calls.not.allowed.here
SuperInitFails.java:91:13: compiler.err.return.before.superclass.initialized
SuperInitFails.java:152:18: compiler.err.call.must.only.appear.in.ctor
39 errors
41 errors
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SuperInitFails.java:161:15: compiler.err.cant.ref.before.ctor.called: SuperInitF
SuperInitFails.java:213:33: compiler.err.ctor.calls.not.allowed.here
SuperInitFails.java:253:13: compiler.err.cant.ref.before.ctor.called: x
SuperInitFails.java:266:18: compiler.err.cant.ref.before.ctor.called: x
SuperInitFails.java:305:26: compiler.err.not.encl.class: SuperInitFails.Inner11.Inner11_1
SuperInitFails.java:35:13: compiler.err.call.must.only.appear.in.ctor
SuperInitFails.java:39:14: compiler.err.call.must.only.appear.in.ctor
SuperInitFails.java:43:14: compiler.err.call.must.only.appear.in.ctor
Expand All @@ -34,3 +35,4 @@ SuperInitFails.java:232:16: compiler.warn.would.not.be.allowed.in.prologue: x
SuperInitFails.java:241:35: compiler.warn.would.not.be.allowed.in.prologue: this
SuperInitFails.java:282:50: compiler.warn.would.not.be.allowed.in.prologue: check
SuperInitFails.java:283:39: compiler.warn.would.not.be.allowed.in.prologue: check
SuperInitFails.java:293:25: compiler.warn.would.not.be.allowed.in.prologue: testMethod()