Skip to content

Commit e50f031

Browse files
author
Vicente Romero
committed
8371356: [lworld] 'super' should be rejected in early construction context
Reviewed-by: liach
1 parent fe178a8 commit e50f031

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,17 @@ public void visitSelect(JCFieldAccess tree) {
13811381
SelectScanner ss = new SelectScanner();
13821382
ss.scan(tree);
13831383
if (ss.scanLater == null) {
1384+
Symbol sym = TreeInfo.symbolFor(tree);
1385+
// if this is a field access
1386+
if (sym.kind == VAR && sym.owner.kind == TYP) {
1387+
// Type.super.field or super.field expressions are forbidden in early construction contexts
1388+
for (JCTree subtree : ss.selectorTrees) {
1389+
if (TreeInfo.isSuperOrSelectorDotSuper(subtree)) {
1390+
reportPrologueError(tree, sym);
1391+
return;
1392+
}
1393+
}
1394+
}
13841395
analyzeSymbol(tree);
13851396
} else {
13861397
boolean prevLhs = isInLHS;

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ public static boolean isIdentOrThisDotIdent(JCTree tree) {
179179
}
180180
}
181181

182+
/** Is this tree `super`, or `Ident.super`?
183+
*/
184+
public static boolean isSuperOrSelectorDotSuper(JCTree tree) {
185+
switch (tree.getTag()) {
186+
case PARENS:
187+
return isSuperOrSelectorDotSuper(skipParens(tree));
188+
case IDENT:
189+
return ((JCIdent)tree).name == ((JCIdent)tree).name.table.names._super;
190+
case SELECT:
191+
return ((JCFieldAccess)tree).name == ((JCFieldAccess)tree).name.table.names._super;
192+
default:
193+
return false;
194+
}
195+
}
196+
182197
/** Check if the given tree is an explicit reference to the 'this' instance of the
183198
* class currently being compiled. This is true if tree is:
184199
* - An unqualified 'this' identifier

test/langtools/tools/javac/SuperInit/SuperInitFails.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,20 @@ static class Inner8 {
269269
x = 4;
270270
}
271271
}
272+
273+
static class Inner9 {
274+
interface Parent {
275+
boolean check = true;
276+
}
277+
278+
class Medium implements Parent {}
279+
280+
class Inner9Test extends Medium {
281+
Inner9Test() {
282+
boolean check1 = Inner9Test.super.check;
283+
boolean check2 = super.check;
284+
super();
285+
}
286+
}
287+
}
272288
}

test/langtools/tools/javac/SuperInit/SuperInitFails.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ SuperInitFails.java:232:16: compiler.err.cant.ref.before.ctor.called: x
2626
SuperInitFails.java:241:35: compiler.err.cant.ref.before.ctor.called: this
2727
SuperInitFails.java:253:13: compiler.err.cant.ref.before.ctor.called: x
2828
SuperInitFails.java:266:18: compiler.err.cant.ref.before.ctor.called: x
29+
SuperInitFails.java:282:50: compiler.err.cant.ref.before.ctor.called: check
30+
SuperInitFails.java:283:39: compiler.err.cant.ref.before.ctor.called: check
2931
SuperInitFails.java:35:13: compiler.err.call.must.only.appear.in.ctor
3032
SuperInitFails.java:39:14: compiler.err.call.must.only.appear.in.ctor
3133
SuperInitFails.java:43:14: compiler.err.call.must.only.appear.in.ctor
@@ -35,4 +37,4 @@ SuperInitFails.java:55:32: compiler.err.call.must.only.appear.in.ctor
3537
SuperInitFails.java:85:18: compiler.err.ctor.calls.not.allowed.here
3638
SuperInitFails.java:91:13: compiler.err.return.before.superclass.initialized
3739
SuperInitFails.java:152:18: compiler.err.call.must.only.appear.in.ctor
38-
37 errors
40+
39 errors

test/langtools/tools/javac/SuperInit/SuperInitFailsWarnings.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ SuperInitFails.java:188:32: compiler.warn.would.not.be.allowed.in.prologue: x
3232
SuperInitFails.java:231:41: compiler.warn.would.not.be.allowed.in.prologue: x
3333
SuperInitFails.java:232:16: compiler.warn.would.not.be.allowed.in.prologue: x
3434
SuperInitFails.java:241:35: compiler.warn.would.not.be.allowed.in.prologue: this
35+
SuperInitFails.java:282:50: compiler.warn.would.not.be.allowed.in.prologue: check
36+
SuperInitFails.java:283:39: compiler.warn.would.not.be.allowed.in.prologue: check

0 commit comments

Comments
 (0)