diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index cc0e5c38e58..82966a53b2f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -124,6 +124,7 @@ public class Attr extends JCTree.Visitor { final ArgumentAttr argumentAttr; final MatchBindingsComputer matchBindingsComputer; final AttrRecover attrRecover; + final LocalProxyVarsGen localProxyVarsGen; public static Attr instance(Context context) { Attr instance = context.get(attrKey); @@ -163,6 +164,7 @@ protected Attr(Context context) { argumentAttr = ArgumentAttr.instance(context); matchBindingsComputer = MatchBindingsComputer.instance(context); attrRecover = AttrRecover.instance(context); + localProxyVarsGen = LocalProxyVarsGen.instance(context); Options options = Options.instance(context); @@ -1250,6 +1252,24 @@ public void visitMethodDef(JCMethodDecl tree) { // Attribute method body. attribStat(tree.body, localEnv); + if (isConstructor) { + ListBuffer prologueCode = new ListBuffer<>(); + for (JCTree stat : tree.body.stats) { + prologueCode.add(stat); + /* gather all the stats in the body until a `super` or `this` constructor invocation is found, + * the constructor invocation will also be included + */ + if (stat instanceof JCExpressionStatement expStmt && + expStmt.expr instanceof JCMethodInvocation mi && + TreeInfo.isConstructorCall(mi)) { + break; + } + } + if (!prologueCode.isEmpty()) { + CtorPrologueVisitor ctorPrologueVisitor = new CtorPrologueVisitor(localEnv); + ctorPrologueVisitor.scan(prologueCode.toList()); + } + } } localEnv.info.scope.leave(); @@ -1261,6 +1281,178 @@ public void visitMethodDef(JCMethodDecl tree) { } } + class CtorPrologueVisitor extends TreeScanner { + Env localEnv; + CtorPrologueVisitor(Env localEnv) { + this.localEnv = localEnv; + } + + @Override + public void visitLambda(JCLambda lambda) { + super.visitLambda(lambda); + // lambda in prologue + classDeclAndLambdaHelper(TreeInfo.symbolsFor(lambda.body)); + } + + @Override + public void visitClassDef(JCClassDecl classDecl) { + super.visitClassDef(classDecl); + // local class in prologue + classDeclAndLambdaHelper(TreeInfo.symbolsFor(classDecl.defs)); + } + + private void classDeclAndLambdaHelper(java.util.List symbols) { + for (TreeInfo.SymAndTree symAndTree : symbols) { + Symbol sym = symAndTree.symbol(); + JCTree tree = filter(symAndTree.tree()); + if (sym.kind == VAR && + rs.isEarlyReference( + localEnv, + tree instanceof JCFieldAccess fa ? + fa.selected : + null, + (VarSymbol) sym)) { + log.error(tree, Errors.CantRefBeforeCtorCalled(sym)); + } + } + } + + @Override + public void visitApply(JCMethodInvocation tree) { + super.visitApply(tree); + Name name = TreeInfo.name(tree.meth); + boolean isConstructorCall = name == names._this || name == names._super; + Symbol msym = TreeInfo.symbolFor(tree.meth); + if (!isConstructorCall && !msym.isStatic()) { + if (msym.owner == env.enclClass.sym || + msym.isMemberOf(env.enclClass.sym, types)) { + if (tree.meth instanceof JCFieldAccess fa) { + if (TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.sym.type, fa.selected)) { + log.error(tree.meth, Errors.CantRefBeforeCtorCalled(msym)); + } + } else { + log.error(tree.meth, Errors.CantRefBeforeCtorCalled(msym)); + } + } + } + if (isConstructorCall) { + if (tree.meth instanceof JCFieldAccess fa) { + if (TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.sym.type, fa.selected)) { + log.error(tree.meth, Errors.CantRefBeforeCtorCalled(msym)); + } + } + } + // we still need to check the args + for (JCExpression arg : tree.args) { + analyzeTree(arg); + } + } + + @Override + public void visitNewClass(JCNewClass tree) { + super.visitNewClass(tree); + checkNewClassAndMethRefs(tree, tree.type); + } + + @Override + public void visitReference(JCMemberReference tree) { + super.visitReference(tree); + if (tree.getMode() == JCMemberReference.ReferenceMode.NEW) { + checkNewClassAndMethRefs(tree, tree.expr.type); + } + } + + void checkNewClassAndMethRefs(JCTree tree, Type t) { + if (t.tsym.isEnclosedBy(env.enclClass.sym) && + !t.tsym.isStatic() && + !t.tsym.isDirectlyOrIndirectlyLocal()) { + log.error(tree, Errors.CantRefBeforeCtorCalled(t.getEnclosingType().tsym)); + } + } + + @Override + public void visitAssign(JCAssign tree) { + super.visitAssign(tree); + if (tree.rhs.type.constValue() == null) { + analyzeTree(tree.rhs); + } + } + + @Override + public void visitVarDef(JCVariableDecl tree) { + super.visitVarDef(tree); + if (tree.init != null && tree.init.type.constValue() == null) { + analyzeTree(tree.init); + } + } + + JCTree filter(JCTree tree) { + tree = TreeInfo.skipParens(tree); + if (tree.hasTag(TYPECAST)) { + tree = filter(((JCTypeCast)tree).expr); + } + return tree; + } + + void analyzeTree(JCTree jcTree) { + jcTree = filter(jcTree); + java.util.List symbols = TreeInfo.symbolsFor(jcTree); + for (TreeInfo.SymAndTree symAndTree : symbols) { + Symbol sym = symAndTree.symbol(); + JCTree tree = filter(symAndTree.tree()); + if (!sym.isStatic() && !isMethodParam(tree)) { + if (sym.name == names._this || sym.name == names._super) { + if (TreeInfo.isExplicitThisReference(types, (ClassType)localEnv.enclClass.sym.type, tree)) { + log.error(tree, Errors.CantRefBeforeCtorCalled(sym)); + } + } else { + if (sym != null && + !sym.owner.isAnonymous() && + sym.owner != localEnv.enclClass.sym && + sym.kind == VAR && + sym.owner.kind == TYP) { + if (!tree.hasTag(IDENT) && !tree.hasTag(SELECT)) { + throw new AssertionError("unexpected tree " + tree + " with tag " + tree.getTag()); + } + Symbol owner = tree.hasTag(IDENT) ? + sym.owner : + ((JCFieldAccess)tree).selected.type.tsym; + if (localEnv.enclClass.sym.isSubClass(owner, types) && + sym.isInheritedIn(localEnv.enclClass.sym, types)) { + log.error(tree, Errors.CantRefBeforeCtorCalled(sym)); + } + } else { + if ((sym.isFinal() || sym.isStrict()) && + sym.kind == VAR && + sym.owner.kind == TYP) + localProxyVarsGen.addFinalOrStrictFieldReadInPrologue(localEnv.enclMethod, sym); + } + } + } + } + } + + boolean isMethodParam(JCTree tree) { + JCTree treeToCheck = null; + if (tree.hasTag(IDENT)) { + treeToCheck = tree; + } else if (tree instanceof JCFieldAccess) { + JCFieldAccess fa = (JCFieldAccess) tree; + while (fa.selected.hasTag(SELECT)) { + fa = (JCFieldAccess)fa.selected; + } + treeToCheck = fa; + } + if (treeToCheck != null) { + Symbol sym = TreeInfo.symbolFor(treeToCheck instanceof JCFieldAccess fa ? fa.selected : treeToCheck); + if (sym != null){ + return sym.owner.kind == MTH; + } + } + return false; + } + } + public void visitVarDef(JCVariableDecl tree) { // Local variables have not been entered yet, so we need to do it now: if (env.info.scope.owner.kind == MTH || env.info.scope.owner.kind == VAR) { @@ -1333,6 +1525,10 @@ public void visitVarDef(JCVariableDecl tree) { //fixup local variable type v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name); } + if (v.owner.kind == TYP && !v.isStatic() && v.isStrict()) { + CtorPrologueVisitor ctorPrologueVisitor = new CtorPrologueVisitor(initEnv); + ctorPrologueVisitor.scan(tree.init); + } } finally { initEnv.info.ctorPrologue = previousCtorPrologue; } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LocalProxyVarsGen.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LocalProxyVarsGen.java index dea2dbfaf66..682c1a42c0e 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LocalProxyVarsGen.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LocalProxyVarsGen.java @@ -88,8 +88,8 @@ public static LocalProxyVarsGen instance(Context context) { private TreeMaker make; private final UnsetFieldsInfo unsetFieldsInfo; private ClassSymbol currentClass = null; - private java.util.List strictInstanceFields; - private Map> strictFieldsReadInPrologue = new HashMap<>(); + private java.util.List finalOrStrictInstanceFields; + private Map> finalOrStrictFieldsReadInPrologue = new HashMap<>(); private final boolean noLocalProxyVars; @@ -105,10 +105,10 @@ protected LocalProxyVarsGen(Context context) { noLocalProxyVars = options.isSet("noLocalProxyVars"); } - public void addStrictFieldReadInPrologue(JCMethodDecl constructor, Symbol sym) { - Set fieldSet = strictFieldsReadInPrologue.getOrDefault(constructor, new HashSet<>()); + public void addFinalOrStrictFieldReadInPrologue(JCMethodDecl constructor, Symbol sym) { + Set fieldSet = finalOrStrictFieldsReadInPrologue.getOrDefault(constructor, new HashSet<>()); fieldSet.add(sym); - strictFieldsReadInPrologue.put(constructor, fieldSet); + finalOrStrictFieldsReadInPrologue.put(constructor, fieldSet); } public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) { @@ -128,41 +128,41 @@ public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) { @Override public void visitClassDef(JCClassDecl tree) { ClassSymbol prevCurrentClass = currentClass; - java.util.List prevStrictInstanceFields = strictInstanceFields; + java.util.List prevFinalOrStrictInstanceFields = finalOrStrictInstanceFields; try { currentClass = tree.sym; - strictInstanceFields = tree.defs.stream() + finalOrStrictInstanceFields = tree.defs.stream() .filter(t -> t.hasTag(VARDEF)) .map(t -> (JCVariableDecl)t) - .filter(vd -> vd.sym.isStrict() && !vd.sym.isStatic()) + .filter(vd -> (vd.sym.isStrict() || vd.sym.isFinal()) && !vd.sym.isStatic()) .collect(List.collector()); super.visitClassDef(tree); } finally { currentClass = prevCurrentClass; - strictInstanceFields = prevStrictInstanceFields; + finalOrStrictInstanceFields = prevFinalOrStrictInstanceFields; } } public void visitMethodDef(JCMethodDecl tree) { - if (strictFieldsReadInPrologue.get(tree) != null) { - Set fieldSet = strictFieldsReadInPrologue.get(tree); - java.util.List strictFieldsRead = new ArrayList<>(); - for (JCVariableDecl sfield : strictInstanceFields) { - if (fieldSet.contains(sfield.sym)) { - strictFieldsRead.add(sfield); + if (finalOrStrictFieldsReadInPrologue.get(tree) != null) { + Set fieldSet = finalOrStrictFieldsReadInPrologue.get(tree); + java.util.List finalOrStrictFieldsRead = new ArrayList<>(); + for (JCVariableDecl field : finalOrStrictInstanceFields) { + if (fieldSet.contains(field.sym)) { + finalOrStrictFieldsRead.add(field); } } - addLocalProxiesFor(tree, strictFieldsRead); - strictFieldsReadInPrologue.remove(tree); + addLocalProxiesFor(tree, finalOrStrictFieldsRead); + finalOrStrictFieldsReadInPrologue.remove(tree); } super.visitMethodDef(tree); } - void addLocalProxiesFor(JCMethodDecl constructor, java.util.List multiAssignedStrictFields) { + void addLocalProxiesFor(JCMethodDecl constructor, java.util.List finalOrStrictFields) { ListBuffer localDeclarations = new ListBuffer<>(); Map fieldToLocalMap = new LinkedHashMap<>(); - for (JCVariableDecl fieldDecl : multiAssignedStrictFields) { + for (JCVariableDecl fieldDecl : finalOrStrictFields) { long flags = SYNTHETIC; VarSymbol proxy = new VarSymbol(flags, newLocalName(fieldDecl.name.toString()), fieldDecl.sym.erasure(types), constructor.sym); fieldToLocalMap.put(fieldDecl.sym, proxy); @@ -211,7 +211,7 @@ void addLocalProxiesFor(JCMethodDecl constructor, java.util.List } } - Name newLocalName(String name) { + private Name newLocalName(String name) { return names.fromString("local" + target.syntheticNameChar() + name); } 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 ad7a66ae7ee..acb029114a4 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 @@ -1537,16 +1537,6 @@ Symbol findVar(DiagnosticPosition pos, Env env, Name name) { (sym.flags() & STATIC) == 0) { if (staticOnly) return new StaticError(sym); - if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) { - if (!env.tree.hasTag(ASSIGN) || !TreeInfo.isIdentOrThisDotIdent(((JCAssign)env.tree).lhs)) { - if (!sym.isStrictInstance()) { - return new RefBeforeCtorCalledError(sym); - } else { - localProxyVarsGen.addStrictFieldReadInPrologue(env.enclMethod, sym); - return sym; - } - } - } } return sym; } else { @@ -2054,8 +2044,6 @@ Symbol findFun(Env env, Name name, (sym.flags() & STATIC) == 0) { if (staticOnly) return new StaticError(sym); - if (env1.info.ctorPrologue && env1 == env) - return new RefBeforeCtorCalledError(sym); } return sym; } else { @@ -3826,9 +3814,6 @@ Symbol findSelfContaining(DiagnosticPosition pos, if (staticOnly) { // current class is not an inner class, stop search return new StaticError(sym); - } else if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) { - // early construction context, stop search - return new RefBeforeCtorCalledError(sym); } else { // found it return sym; @@ -3887,8 +3872,6 @@ Symbol resolveSelf(DiagnosticPosition pos, if (sym != null) { if (staticOnly) sym = new StaticError(sym); - else if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) - sym = new RefBeforeCtorCalledError(sym); return accessBase(sym, pos, env.enclClass.sym.type, name, true); } @@ -3902,8 +3885,6 @@ else if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbo //this might be a default super call if one of the superinterfaces is 'c' for (Type t : pruneInterfaces(env.enclClass.type)) { if (t.tsym == c) { - if (env.info.ctorPrologue) - log.error(pos, Errors.CantRefBeforeCtorCalled(name)); env.info.defaultSuperCallSite = t; return new VarSymbol(0, names._super, types.asSuper(env.enclClass.type, c), env.enclClass.sym); @@ -3940,64 +3921,6 @@ private List pruneInterfaces(Type t) { return result.toList(); } - /** - * Determine if an early instance field reference may appear in a constructor prologue. - * - *

- * This is only allowed when: - * - The field is being assigned a value (i.e., written but not read) - * - The field is not inherited from a superclass - * - The assignment is not within a lambda, because that would require - * capturing 'this' which is not allowed prior to super(). - * - *

- * Note, this method doesn't catch all such scenarios, because this method - * is invoked for symbol "x" only for "x = 42" but not for "this.x = 42". - * We also don't verify that the field has no initializer, which is required. - * To catch those cases, we rely on similar logic in Attr.checkAssignable(). - */ - private boolean isAllowedEarlyReference(DiagnosticPosition pos, Env env, VarSymbol v) { - - // Check assumptions - Assert.check(env.info.ctorPrologue); - Assert.check((v.flags_field & STATIC) == 0); - - // The symbol must appear in the LHS of an assignment statement - if (!(env.tree instanceof JCAssign assign)) - return false; - - // The assignment statement must not be within a lambda - if (env.info.isLambda) - return false; - - // Get the symbol's qualifier, if any - JCExpression lhs = TreeInfo.skipParens(assign.lhs); - JCExpression base; - switch (lhs.getTag()) { - case IDENT: - base = null; - break; - case SELECT: - JCFieldAccess select = (JCFieldAccess)lhs; - base = select.selected; - if (!TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base)) - return false; - break; - default: - return false; - } - - // If an early reference, the field must not be declared in a superclass - if (isEarlyReference(env, base, v) && v.owner != env.enclClass.sym) - return false; - - // The flexible constructors feature must be enabled - preview.checkSourceLevel(pos, Feature.FLEXIBLE_CONSTRUCTORS); - - // OK - return true; - } - /** * Determine if the variable appearance constitutes an early reference to the current class. * diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java index 5e3b043fb11..528c1a927ab 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java @@ -50,6 +50,7 @@ import javax.lang.model.element.ElementKind; import javax.tools.JavaFileObject; +import java.util.ArrayList; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.ToIntFunction; @@ -964,6 +965,41 @@ public static Name fullName(JCTree tree) { } } + public record SymAndTree(Symbol symbol, JCTree tree) {} + + public static java.util.List symbolsFor(List nodes) { + java.util.List result = new ArrayList<>(); + for (JCTree node : nodes) { + java.util.List partialResult = symbolsFor(node); + if (!partialResult.isEmpty()) { + result.addAll(partialResult); + } + } + return result; + } + + public static java.util.List symbolsFor(JCTree node) { + java.util.List result = new ArrayList<>(); + new TreeScanner() { + @Override + public void scan(JCTree tree) { + super.scan(tree); + if (tree != null) { + Symbol symbol = TreeInfo.symbolFor(tree); + if (symbol != null) { + result.add(new SymAndTree(symbol, tree)); + } + } + } + + @Override + public void visitSelect(JCFieldAccess tree) { + // do not go deeper + } + }.scan(node); + return result; + } + public static Symbol symbolFor(JCTree node) { Symbol sym = symbolForImpl(node); @@ -1007,6 +1043,8 @@ private static Symbol symbolForImpl(JCTree node) { if (node.type != null) return node.type.tsym; return null; + case TYPECAST: + return symbolFor(((JCTypeCast)node).expr); default: return null; } diff --git a/test/langtools/tools/javac/8278078/InvalidThisAndSuperInConstructorArgTest.out b/test/langtools/tools/javac/8278078/InvalidThisAndSuperInConstructorArgTest.out index 1109eb0fdc4..4fd6ebf1323 100644 --- a/test/langtools/tools/javac/8278078/InvalidThisAndSuperInConstructorArgTest.out +++ b/test/langtools/tools/javac/8278078/InvalidThisAndSuperInConstructorArgTest.out @@ -1,14 +1,14 @@ -InvalidThisAndSuperInConstructorArgTest.java:23:29: compiler.err.cant.ref.before.ctor.called: super -InvalidThisAndSuperInConstructorArgTest.java:26:28: compiler.err.cant.ref.before.ctor.called: super -InvalidThisAndSuperInConstructorArgTest.java:29:29: compiler.err.cant.ref.before.ctor.called: this -InvalidThisAndSuperInConstructorArgTest.java:32:28: compiler.err.cant.ref.before.ctor.called: this +InvalidThisAndSuperInConstructorArgTest.java:23:35: compiler.err.cant.ref.before.ctor.called: toString() +InvalidThisAndSuperInConstructorArgTest.java:26:34: compiler.err.cant.ref.before.ctor.called: toString() +InvalidThisAndSuperInConstructorArgTest.java:29:34: compiler.err.cant.ref.before.ctor.called: toString() +InvalidThisAndSuperInConstructorArgTest.java:32:33: compiler.err.cant.ref.before.ctor.called: toString() InvalidThisAndSuperInConstructorArgTest.java:35:33: compiler.err.not.encl.class: java.lang.AssertionError InvalidThisAndSuperInConstructorArgTest.java:38:32: compiler.err.not.encl.class: java.lang.AssertionError InvalidThisAndSuperInConstructorArgTest.java:41:33: compiler.err.not.encl.class: java.lang.AssertionError InvalidThisAndSuperInConstructorArgTest.java:44:32: compiler.err.not.encl.class: java.lang.AssertionError -InvalidThisAndSuperInConstructorArgTest.java:47:38: compiler.err.cant.ref.before.ctor.called: super +InvalidThisAndSuperInConstructorArgTest.java:47:44: compiler.err.cant.ref.before.ctor.called: get() InvalidThisAndSuperInConstructorArgTest.java:50:39: compiler.err.not.encl.class: InvalidThisAndSuperInConstructorArgTest.InterfaceWithDefault InvalidThisAndSuperInConstructorArgTest.java:53:38: compiler.err.not.encl.class: InvalidThisAndSuperInConstructorArgTest.InterfaceWithDefault -InvalidThisAndSuperInConstructorArgTest.java:56:39: compiler.err.cant.ref.before.ctor.called: super +InvalidThisAndSuperInConstructorArgTest.java:56:45: compiler.err.cant.ref.before.ctor.called: get() InvalidThisAndSuperInConstructorArgTest.java:59:28: compiler.err.cant.ref.before.ctor.called: this 13 errors diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview1.java b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview1.java index abb6bacbc8f..0d7938d7216 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview1.java +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview1.java @@ -2,7 +2,7 @@ * @test /nodynamiccopyright/ * @bug 8334258 * @summary Disallow early assignment if FLEXIBLE_CONSTRUCTORS preview feature is not enabled - * @compile/fail/ref=EarlyAssignmentNoPreview1.out -XDrawDiagnostics EarlyAssignmentNoPreview1.java + * @compile/fail/ref=EarlyAssignmentNoPreview1.out --release 24 -XDrawDiagnostics EarlyAssignmentNoPreview1.java */ public class EarlyAssignmentNoPreview1 { diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview2.java b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview2.java index 3c33734f42a..64332244e01 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview2.java +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview2.java @@ -2,7 +2,7 @@ * @test /nodynamiccopyright/ * @bug 8334258 * @summary Disallow early assignment if FLEXIBLE_CONSTRUCTORS preview feature is not enabled - * @compile/fail/ref=EarlyAssignmentNoPreview2.out -XDrawDiagnostics EarlyAssignmentNoPreview2.java + * @compile/fail/ref=EarlyAssignmentNoPreview2.out --release 24 -XDrawDiagnostics EarlyAssignmentNoPreview2.java */ public class EarlyAssignmentNoPreview2 { diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview3.java b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview3.java index f6269f2cb1f..e021d80e6c1 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview3.java +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview3.java @@ -2,7 +2,7 @@ * @test /nodynamiccopyright/ * @bug 8334258 * @summary Disallow early assignment if FLEXIBLE_CONSTRUCTORS preview feature is not enabled - * @compile/fail/ref=EarlyAssignmentNoPreview3.out -XDrawDiagnostics EarlyAssignmentNoPreview3.java + * @compile/fail/ref=EarlyAssignmentNoPreview3.out --release 24 -XDrawDiagnostics EarlyAssignmentNoPreview3.java */ public class EarlyAssignmentNoPreview3 { diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignments.java b/test/langtools/tools/javac/SuperInit/EarlyAssignments.java index c3cad5d7016..571f4f01432 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignments.java +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignments.java @@ -18,9 +18,9 @@ public Inner1() { } public Inner1(int y) { - y = x; // FAIL - early 'this' reference - y = this.x; // FAIL - early 'this' reference - y = Inner1.this.x; // FAIL - early 'this' reference + y = x; // OK - "x" belongs to this class + y = this.x; // OK - "x" belongs to this class + y = Inner1.this.x; // OK - "x" belongs to this class super(); } @@ -95,19 +95,19 @@ public static class Inner4 { public Inner4() { x = 0; // OK - x = x + 1; // FAIL - illegal early access + x = x + 1; // OK super(); } public Inner4(int a) { this.x = 0; // OK - this.x = this.x + 1; // FAIL - illegal early access + this.x = this.x + 1; // OK super(); } public Inner4(char a) { Inner4.this.x = 0; // OK - Inner4.this.x = Inner4.this.x + 1; // FAIL - illegal early access + Inner4.this.x = Inner4.this.x + 1; // OK super(); } } diff --git a/test/langtools/tools/javac/SuperInit/EarlyAssignments.out b/test/langtools/tools/javac/SuperInit/EarlyAssignments.out index 38182c2d312..827dddce6a8 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyAssignments.out +++ b/test/langtools/tools/javac/SuperInit/EarlyAssignments.out @@ -1,7 +1,4 @@ -EarlyAssignments.java:21:17: compiler.err.cant.ref.before.ctor.called: x -EarlyAssignments.java:22:17: compiler.err.cant.ref.before.ctor.called: this -EarlyAssignments.java:23:23: compiler.err.cant.ref.before.ctor.called: this -EarlyAssignments.java:31:21: compiler.err.cant.ref.before.ctor.called: super +EarlyAssignments.java:31:26: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:32:21: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:33:26: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:34:34: compiler.err.cant.ref.before.ctor.called: x @@ -13,17 +10,14 @@ EarlyAssignments.java:66:13: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:67:17: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:68:25: compiler.err.cant.ref.before.ctor.called: this EarlyAssignments.java:69:31: compiler.err.cant.ref.before.ctor.called: this -EarlyAssignments.java:98:17: compiler.err.cant.ref.before.ctor.called: x -EarlyAssignments.java:104:22: compiler.err.cant.ref.before.ctor.called: this -EarlyAssignments.java:110:35: compiler.err.cant.ref.before.ctor.called: this EarlyAssignments.java:119:17: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:124:22: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:129:29: compiler.err.cant.ref.before.ctor.called: x -EarlyAssignments.java:134:17: compiler.err.cant.ref.before.ctor.called: super -EarlyAssignments.java:139:23: compiler.err.cant.ref.before.ctor.called: this +EarlyAssignments.java:134:22: compiler.err.cant.ref.before.ctor.called: x +EarlyAssignments.java:139:28: compiler.err.cant.ref.before.ctor.called: x EarlyAssignments.java:148:13: compiler.err.cant.assign.initialized.before.ctor.called: x EarlyAssignments.java:157:13: compiler.err.cant.assign.val.to.var: final, x -EarlyAssignments.java:168:13: compiler.err.cant.ref.before.ctor.called: this +EarlyAssignments.java:168:18: compiler.err.cant.ref.before.ctor.called: EarlyAssignments.Inner8 - compiler.note.preview.filename: EarlyAssignments.java, DEFAULT - compiler.note.preview.recompile -26 errors +20 errors diff --git a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.java b/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.java index 9336c4dc183..475915dddca 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.java +++ b/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.java @@ -2,7 +2,7 @@ * @test /nodynamiccopyright/ * @bug 8334248 * @summary Invalid error for early construction local class constructor method reference - * @compile/fail/ref=EarlyIndirectOuterCapture.out -XDrawDiagnostics EarlyIndirectOuterCapture.java + * @compile EarlyIndirectOuterCapture.java */ public class EarlyIndirectOuterCapture { @@ -18,7 +18,7 @@ class InnerSuperclass { } static class InnerOuter extends EarlyIndirectOuterCapture { // accessible class InnerInnerOuter extends EarlyIndirectOuterCapture { // not accessible InnerInnerOuter() { - super(/* which enclosing instance here ? */new InnerSuperclass() { }); + super(new InnerSuperclass() { }); // should this be accepted?, InnerSuperclass is not an inner class of InnerInnerOuter } InnerInnerOuter(boolean b) { diff --git a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out b/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out deleted file mode 100644 index 7b96671a2bd..00000000000 --- a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out +++ /dev/null @@ -1,2 +0,0 @@ -EarlyIndirectOuterCapture.java:21:60: compiler.err.cant.ref.before.ctor.called: this -1 error diff --git a/test/langtools/tools/javac/SuperInit/EarlyLocalClass.out b/test/langtools/tools/javac/SuperInit/EarlyLocalClass.out index ee01f9c403d..6ca47a65241 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyLocalClass.out +++ b/test/langtools/tools/javac/SuperInit/EarlyLocalClass.out @@ -1,4 +1,4 @@ -EarlyLocalClass.java:12:32: compiler.err.cant.ref.before.ctor.called: this +EarlyLocalClass.java:12:37: compiler.err.cant.ref.before.ctor.called: hashCode() - compiler.note.preview.filename: EarlyLocalClass.java, DEFAULT - compiler.note.preview.recompile 1 error diff --git a/test/langtools/tools/javac/SuperInit/SuperInitFails.java b/test/langtools/tools/javac/SuperInit/SuperInitFails.java index feb5b81c1b0..864ca59c115 100644 --- a/test/langtools/tools/javac/SuperInit/SuperInitFails.java +++ b/test/langtools/tools/javac/SuperInit/SuperInitFails.java @@ -152,7 +152,7 @@ public SuperInitFails(float[][] x) { } public SuperInitFails(int[][] z) { - super((Runnable)() -> x); // this should FAIL + super((Runnable)() -> System.err.println(x)); // this should FAIL } public SuperInitFails(long[][] z) { diff --git a/test/langtools/tools/javac/SuperInit/SuperInitFails.out b/test/langtools/tools/javac/SuperInit/SuperInitFails.out index 4394a6741c3..5fe959ea908 100644 --- a/test/langtools/tools/javac/SuperInit/SuperInitFails.out +++ b/test/langtools/tools/javac/SuperInit/SuperInitFails.out @@ -1,23 +1,23 @@ SuperInitFails.java:57:9: compiler.err.cant.ref.before.ctor.called: hashCode() -SuperInitFails.java:62:9: compiler.err.cant.ref.before.ctor.called: this -SuperInitFails.java:67:9: compiler.err.cant.ref.before.ctor.called: super -SuperInitFails.java:72:23: compiler.err.cant.ref.before.ctor.called: this -SuperInitFails.java:77:23: compiler.err.cant.ref.before.ctor.called: super -SuperInitFails.java:94:9: compiler.err.cant.ref.before.ctor.called: this +SuperInitFails.java:62:13: compiler.err.cant.ref.before.ctor.called: hashCode() +SuperInitFails.java:67:14: compiler.err.cant.ref.before.ctor.called: hashCode() +SuperInitFails.java:72:28: compiler.err.cant.ref.before.ctor.called: hashCode() +SuperInitFails.java:77:29: compiler.err.cant.ref.before.ctor.called: hashCode() SuperInitFails.java:99:33: compiler.err.cant.ref.before.ctor.called: this SuperInitFails.java:104:14: compiler.err.cant.ref.before.ctor.called: this SuperInitFails.java:108:20: compiler.err.not.encl.class: java.lang.Object -SuperInitFails.java:112:17: compiler.err.cant.ref.before.ctor.called: super +SuperInitFails.java:112:23: compiler.err.cant.ref.before.ctor.called: spliterator() SuperInitFails.java:119:22: compiler.err.call.must.only.appear.in.ctor -SuperInitFails.java:125:9: compiler.err.cant.ref.before.ctor.called: this +SuperInitFails.java:125:9: compiler.err.cant.ref.before.ctor.called: SuperInitFails SuperInitFails.java:133:9: compiler.err.non.canonical.constructor.invoke.another.constructor: SuperInitFails.Record1 SuperInitFails.java:138:9: compiler.err.non.canonical.constructor.invoke.another.constructor: SuperInitFails.Record2 -SuperInitFails.java:155:31: compiler.err.cant.ref.before.ctor.called: x -SuperInitFails.java:159:15: compiler.err.cant.ref.before.ctor.called: this +SuperInitFails.java:155:50: compiler.err.cant.ref.before.ctor.called: x +SuperInitFails.java:159:15: compiler.err.cant.ref.before.ctor.called: SuperInitFails SuperInitFails.java:168:13: compiler.err.cant.ref.before.ctor.called: x SuperInitFails.java:172:17: compiler.err.cant.ref.before.ctor.called: x SuperInitFails.java:176:24: compiler.err.cant.ref.before.ctor.called: x SuperInitFails.java:180:18: compiler.err.cant.ref.before.ctor.called: x +SuperInitFails.java:186:32: compiler.err.cant.ref.before.ctor.called: x SuperInitFails.java:195:25: compiler.err.return.before.superclass.initialized SuperInitFails.java:200:33: compiler.err.ctor.calls.not.allowed.here SuperInitFails.java:205:29: compiler.err.redundant.superclass.init diff --git a/test/langtools/tools/javac/SuperInit/ValueClassSuperInitFails.out b/test/langtools/tools/javac/SuperInit/ValueClassSuperInitFails.out index 914a97b63aa..9ce5ced7c4d 100644 --- a/test/langtools/tools/javac/SuperInit/ValueClassSuperInitFails.out +++ b/test/langtools/tools/javac/SuperInit/ValueClassSuperInitFails.out @@ -1,18 +1,19 @@ ValueClassSuperInitFails.java:67:9: compiler.err.cant.ref.before.ctor.called: hashCode() -ValueClassSuperInitFails.java:72:9: compiler.err.cant.ref.before.ctor.called: this -ValueClassSuperInitFails.java:77:9: compiler.err.cant.ref.before.ctor.called: super -ValueClassSuperInitFails.java:82:33: compiler.err.cant.ref.before.ctor.called: this -ValueClassSuperInitFails.java:87:33: compiler.err.cant.ref.before.ctor.called: super +ValueClassSuperInitFails.java:72:13: compiler.err.cant.ref.before.ctor.called: hashCode() +ValueClassSuperInitFails.java:77:14: compiler.err.cant.ref.before.ctor.called: hashCode() +ValueClassSuperInitFails.java:82:38: compiler.err.cant.ref.before.ctor.called: hashCode() +ValueClassSuperInitFails.java:87:39: compiler.err.cant.ref.before.ctor.called: hashCode() ValueClassSuperInitFails.java:109:33: compiler.err.cant.ref.before.ctor.called: this ValueClassSuperInitFails.java:114:14: compiler.err.cant.ref.before.ctor.called: this ValueClassSuperInitFails.java:118:20: compiler.err.not.encl.class: java.lang.Object -ValueClassSuperInitFails.java:122:17: compiler.err.cant.ref.before.ctor.called: super +ValueClassSuperInitFails.java:122:23: compiler.err.cant.ref.before.ctor.called: spliterator() ValueClassSuperInitFails.java:129:22: compiler.err.call.must.only.appear.in.ctor -ValueClassSuperInitFails.java:135:9: compiler.err.cant.ref.before.ctor.called: this +ValueClassSuperInitFails.java:135:9: compiler.err.cant.ref.before.ctor.called: ValueClassSuperInitFails ValueClassSuperInitFails.java:143:9: compiler.err.non.canonical.constructor.invoke.another.constructor: ValueClassSuperInitFails.Record1 ValueClassSuperInitFails.java:148:9: compiler.err.non.canonical.constructor.invoke.another.constructor: ValueClassSuperInitFails.Record2 -ValueClassSuperInitFails.java:161:41: compiler.err.cant.ref.before.ctor.called: this -ValueClassSuperInitFails.java:179:15: compiler.err.cant.ref.before.ctor.called: this +ValueClassSuperInitFails.java:161:46: compiler.err.cant.ref.before.ctor.called: hashCode() +ValueClassSuperInitFails.java:175:49: compiler.err.cant.ref.before.ctor.called: x +ValueClassSuperInitFails.java:179:15: compiler.err.cant.ref.before.ctor.called: ValueClassSuperInitFails ValueClassSuperInitFails.java:43:13: compiler.err.call.must.only.appear.in.ctor ValueClassSuperInitFails.java:47:14: compiler.err.call.must.only.appear.in.ctor ValueClassSuperInitFails.java:51:14: compiler.err.call.must.only.appear.in.ctor @@ -24,4 +25,4 @@ ValueClassSuperInitFails.java:99:13: compiler.err.return.before.superclass.initi ValueClassSuperInitFails.java:170:18: compiler.err.call.must.only.appear.in.ctor - compiler.note.preview.filename: ValueClassSuperInitFails.java, DEFAULT - compiler.note.preview.recompile -24 errors +25 errors diff --git a/test/langtools/tools/javac/cantReferenceBeforeCtor/CantReferenceBeforeConstructorTest.out b/test/langtools/tools/javac/cantReferenceBeforeCtor/CantReferenceBeforeConstructorTest.out index 78b0b35e373..e7352bb162f 100644 --- a/test/langtools/tools/javac/cantReferenceBeforeCtor/CantReferenceBeforeConstructorTest.out +++ b/test/langtools/tools/javac/cantReferenceBeforeCtor/CantReferenceBeforeConstructorTest.out @@ -1,2 +1,2 @@ -CantReferenceBeforeConstructorTest.java:30:13: compiler.err.cant.ref.before.ctor.called: this +CantReferenceBeforeConstructorTest.java:30:17: compiler.err.cant.ref.before.ctor.called: CantReferenceBeforeConstructorTest.BB.CC() 1 error diff --git a/test/langtools/tools/javac/diags/examples/CantRefBeforeConstr.java b/test/langtools/tools/javac/diags/examples/CantRefBeforeConstr.java index cc83c86f80d..45728a3c86b 100644 --- a/test/langtools/tools/javac/diags/examples/CantRefBeforeConstr.java +++ b/test/langtools/tools/javac/diags/examples/CantRefBeforeConstr.java @@ -24,12 +24,11 @@ // key: compiler.err.cant.ref.before.ctor.called class Base { + int i; Base(int i) { } } class CantRefBeforeConstr extends Base { - int i; - CantRefBeforeConstr() { super(i); } diff --git a/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed.out b/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed.out index a938776a8cb..0c251910be0 100644 --- a/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed.out +++ b/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed.out @@ -1,2 +1,2 @@ -NewBeforeOuterConstructed.java:27:21: compiler.err.cant.ref.before.ctor.called: this +NewBeforeOuterConstructed.java:27:21: compiler.err.cant.ref.before.ctor.called: NewBeforeOuterConstructed 1 error diff --git a/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed2.out b/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed2.out index 68e957aacf5..4c82aa9d32e 100644 --- a/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed2.out +++ b/test/langtools/tools/javac/implicitThis/NewBeforeOuterConstructed2.out @@ -1,2 +1,2 @@ -NewBeforeOuterConstructed2.java:20:35: compiler.err.cant.ref.before.ctor.called: this +NewBeforeOuterConstructed2.java:20:35: compiler.err.cant.ref.before.ctor.called: NewBeforeOuterConstructed2 1 error diff --git a/test/langtools/tools/javac/lambda/MethodReferenceNoThisTest.out b/test/langtools/tools/javac/lambda/MethodReferenceNoThisTest.out index a411c340484..f5a535b73ff 100644 --- a/test/langtools/tools/javac/lambda/MethodReferenceNoThisTest.out +++ b/test/langtools/tools/javac/lambda/MethodReferenceNoThisTest.out @@ -1,2 +1,2 @@ -MethodReferenceNoThisTest.java:22:15: compiler.err.cant.ref.before.ctor.called: this +MethodReferenceNoThisTest.java:22:15: compiler.err.cant.ref.before.ctor.called: MethodReferenceNoThisTest 1 error diff --git a/test/langtools/tools/javac/lambda/methodReference/MethodReferenceInConstructorInvocation.out b/test/langtools/tools/javac/lambda/methodReference/MethodReferenceInConstructorInvocation.out index 1f850a022b2..1d41e3776b1 100644 --- a/test/langtools/tools/javac/lambda/methodReference/MethodReferenceInConstructorInvocation.out +++ b/test/langtools/tools/javac/lambda/methodReference/MethodReferenceInConstructorInvocation.out @@ -1,3 +1,3 @@ MethodReferenceInConstructorInvocation.java:20:21: compiler.err.cant.ref.before.ctor.called: super -MethodReferenceInConstructorInvocation.java:24:30: compiler.err.cant.ref.before.ctor.called: super +MethodReferenceInConstructorInvocation.java:24:36: compiler.err.cant.ref.before.ctor.called: getString() 2 errors diff --git a/test/langtools/tools/javac/valhalla/value-objects/DualNonDuplicateErrors.out b/test/langtools/tools/javac/valhalla/value-objects/DualNonDuplicateErrors.out index 3ba6f7d19ee..eb03edf7343 100644 --- a/test/langtools/tools/javac/valhalla/value-objects/DualNonDuplicateErrors.out +++ b/test/langtools/tools/javac/valhalla/value-objects/DualNonDuplicateErrors.out @@ -1,5 +1,5 @@ -DualNonDuplicateErrors.java:18:13: compiler.err.cant.ref.before.ctor.called: this DualNonDuplicateErrors.java:18:9: compiler.err.cant.ref.before.ctor.called: foo(DualNonDuplicateErrors) +DualNonDuplicateErrors.java:18:13: compiler.err.cant.ref.before.ctor.called: this - compiler.note.preview.filename: DualNonDuplicateErrors.java, DEFAULT - compiler.note.preview.recompile 2 errors diff --git a/test/langtools/tools/javac/valhalla/value-objects/ValueObjectCompilationTests.java b/test/langtools/tools/javac/valhalla/value-objects/ValueObjectCompilationTests.java index 1b8a03f5494..3e89639569e 100644 --- a/test/langtools/tools/javac/valhalla/value-objects/ValueObjectCompilationTests.java +++ b/test/langtools/tools/javac/valhalla/value-objects/ValueObjectCompilationTests.java @@ -919,11 +919,11 @@ value class V { } """ ); - assertOK( + assertFail("compiler.err.cant.ref.before.ctor.called", """ value class Test { Test t = null; - Runnable r = () -> { System.err.println(t); }; // compiler will generate a local proxy for `t` + Runnable r = () -> { System.err.println(t); }; // cant reference `t` from a lambda expression in the prologue } """ );