diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java index 95e17f45bcc72..ad65876b9d10d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java @@ -312,11 +312,6 @@ public static EnumSet asFlagSet(long flags) { */ public static final long NAME_FILLED = 1L<<52; //ParamSymbols only - /** - * Flag to mark a lazy static field - */ - public static final long LAZY= 1L<<49; // VarSymbols only (shared with LAMBDA_METHOD) - /** * Flag to indicate the given ModuleSymbol is a system module. */ @@ -433,14 +428,14 @@ public static String toSource(long flags) { RecordMethodFlags = AccessFlags | ABSTRACT | STATIC | SYNCHRONIZED | FINAL | STRICTFP; public static final long - ExtendedStandardFlags = (long)StandardFlags | DEFAULT | SEALED | NON_SEALED | LAZY, + ExtendedStandardFlags = (long)StandardFlags | DEFAULT | SEALED | NON_SEALED, ExtendedMemberClassFlags = (long)MemberClassFlags | SEALED | NON_SEALED, ExtendedMemberStaticClassFlags = (long) MemberStaticClassFlags | SEALED | NON_SEALED, ExtendedClassFlags = (long)ClassFlags | SEALED | NON_SEALED, ModifierFlags = ((long)StandardFlags & ~INTERFACE) | DEFAULT | SEALED | NON_SEALED, InterfaceMethodMask = ABSTRACT | PRIVATE | STATIC | PUBLIC | STRICTFP | DEFAULT, AnnotationTypeElementMask = ABSTRACT | PUBLIC, - LocalVarFlags = FINAL | PARAMETER, + LocalVarFlags = FINAL | PARAMETER | STATIC, ReceiverParamFlags = PARAMETER; public static Set asModifierSet(long flags) { @@ -532,12 +527,7 @@ public enum Flag { BAD_OVERRIDE(Flags.BAD_OVERRIDE), SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC), THROWS(Flags.THROWS), - LAZY(Flags.LAZY) { - @Override - public String toString() { - return "lazy-static"; - } - }, + LAMBDA_METHOD(Flags.LAMBDA_METHOD), TYPE_TRANSLATED(Flags.TYPE_TRANSLATED), MODULE(Flags.MODULE), AUTOMATIC_MODULE(Flags.AUTOMATIC_MODULE), diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java index 4bb0dc33dcec2..39f3171d8b1f6 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java @@ -1215,10 +1215,10 @@ else if (sym.owner.kind != TYP) mask = LocalVarFlags; else if ((sym.owner.flags_field & INTERFACE) != 0) { implicit = InterfaceVarFlags; - mask = InterfaceVarFlags | LAZY; + mask = InterfaceVarFlags; } else - mask = VarFlags | LAZY; + mask = VarFlags; break; case MTH: if (sym.name == names.init) { @@ -1308,9 +1308,6 @@ else if ((sym.owner.flags_field & INTERFACE) != 0) { mask |= INTERFACE; } else { - if ((illegal & LAZY) != 0) { - illegal &= ~STATIC; - } log.error(pos, Errors.ModNotAllowedHere(asFlagSet(illegal))); } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java index 0498f944fa3e3..c5f66fc5ea396 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java @@ -102,7 +102,6 @@ public static Lower instance(Context context) { private final PkgInfo pkginfoOpt; private final boolean optimizeOuterThis; private final boolean useMatchException; - private final boolean desugarLazyStatics; @SuppressWarnings("this-escape") protected Lower(Context context) { @@ -134,7 +133,6 @@ protected Lower(Context context) { Preview preview = Preview.instance(context); useMatchException = Feature.PATTERN_SWITCH.allowedInSource(source) && (preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH)); - desugarLazyStatics = options.isSet("desugarLazyStatics"); } /** The currently enclosing class. @@ -1126,38 +1124,10 @@ MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp, return accessor; } - MethodSymbol lazyStaticAccessSymbol(Symbol sym) { - // lookup symbol and create one if it doesn't exist - Symbol getter = sym.owner.members().findFirst(lazyStaticGetName(sym)); - if (getter == null) { - getter = new MethodSymbol( - STATIC | SYNTHETIC | (sym.flags() & AccessFlags), - lazyStaticGetName(sym), - new MethodType(List.nil(), sym.type, List.nil(), syms.methodClass), - sym.owner); - sym.owner.members().enter(getter); - } - return (MethodSymbol)getter; - } - - JCMethodDecl lazyStaticAccessDef(Symbol sym) { - MethodSymbol getter = lazyStaticAccessSymbol(sym); - Assert.checkNonNull(getter); - // create synthetic getter tree - JCMethodDecl initDef = make.MethodDef(getter, make.Block(0, List.of(make.Return(make.Ident(sym))))); - JCClassDecl currentDecl = classDef(currentClass); - currentDecl.defs = currentDecl.defs.prepend(initDef); - return initDef; - } - Name lazyStaticInitName(Symbol sym) { return sym.name.append('$', names.fromString("init")); } - Name lazyStaticGetName(Symbol sym) { - return sym.name.append('$', names.fromString("get")); - } - /** The qualifier to be used for accessing a symbol in an outer class. * This is either C.sym or C.this.sym, depending on whether or not * sym is static. @@ -1289,16 +1259,6 @@ JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean break; case MTH: case VAR: if (sym.owner.kind == TYP) { - boolean isLazyStatic = (sym.flags() & LAZY) != 0; - if (desugarLazyStatics && isLazyStatic && - sym.owner != currentClass) { - Symbol access = lazyStaticAccessSymbol(sym); - JCExpression receiver = make.Select( - base != null ? base : make.QualIdent(access.owner), - access); - return make.App(receiver, List.nil()); - } - // Access methods are required for // - private members, // - protected members in a superclass of an @@ -3714,13 +3674,13 @@ public void visitVarDef(JCVariableDecl tree) { new MethodSymbol((tree.mods.flags&STATIC) | BLOCK, names.empty, null, currentClass); - // handle lazy - if ((tree.sym.flags() & LAZY) != 0) { - splitInit(tree); - lazyStaticAccessDef(tree.sym); - } } - if (tree.init != null) tree.init = translate(tree.init, tree.type); + // handle static locals + if (tree.sym.isStatic() && tree.sym.owner.kind == MTH) { + splitInit(tree); + } else { + if (tree.init != null) tree.init = translate(tree.init, tree.type); + } result = tree; currentMethodSym = oldMethodSym; } @@ -3735,7 +3695,7 @@ private MethodSymbol splitInit(JCVariableDecl tree) { currentClass); enterSynthetic(tree.pos(), initSym, currentClass.members()); // create synthetic init tree - JCExpression initExpr = tree.init; + JCExpression initExpr = translate(tree.init, tree.type); JCMethodDecl initDef = make.MethodDef(initSym, make.Block(0, List.of(make.Return(initExpr)))); JCClassDecl currentDecl = classDef(currentClass); currentDecl.defs = currentDecl.defs.prepend(initDef); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java index f1cc97baa7d2f..b129b3902a709 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java @@ -81,7 +81,6 @@ public class Gen extends JCTree.Visitor { /** Format of stackmap tables to be generated. */ private final Code.StackMapFormat stackMap; - private final boolean desugarLazyStatics; /** A type that serves as the expected type for all method expressions. */ @@ -133,7 +132,6 @@ protected Gen(Context context) { this.stackMap = StackMapFormat.JSR202; annotate = Annotate.instance(context); qualifiedSymbolCache = new HashMap<>(); - desugarLazyStatics = options.isSet("desugarLazyStatics"); } /** Switches @@ -2321,7 +2319,7 @@ public void visitIndexed(JCArrayAccess tree) { public void visitIdent(JCIdent tree) { Symbol sym = tree.sym; - if (desugarLazyStatics && isLazyStatic(sym)) { + if (isStaticLocal(sym)) { sym = ((VarSymbol)sym).lazyConstValue(); } if (tree.name == names._this || tree.name == names._super) { @@ -2361,9 +2359,6 @@ private boolean nonVirtualForPrivateAccess(Symbol sym) { public void visitSelect(JCFieldAccess tree) { Symbol sym = tree.sym; - if (desugarLazyStatics && isLazyStatic(sym)) { - sym = ((VarSymbol)sym).lazyConstValue(); - } if (tree.name == names._class) { code.emitLdc((LoadableConstant)checkDimension(tree.pos(), tree.selected.type)); @@ -2432,8 +2427,10 @@ public boolean isInvokeDynamic(Symbol sym) { return sym.kind == MTH && ((MethodSymbol)sym).isDynamic(); } - public boolean isLazyStatic(Symbol sym) { - return (sym.flags() & LAZY) != 0; + public boolean isStaticLocal(Symbol sym) { + return sym.kind == VAR && + sym.isStatic() && + sym.owner.kind == MTH; } public void visitLiteral(JCLiteral tree) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index 2f22e494c9801..5cf9203ae61cd 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -2818,7 +2818,8 @@ List blockStatement() { case ASSERT: return List.of(parseSimpleStatement()); case MONKEYS_AT: - case FINAL: { + case FINAL: + case STATIC: { dc = token.docComment(); JCModifiers mods = modifiersOpt(); if (isDeclaration()) { @@ -3497,27 +3498,11 @@ protected JCModifiers modifiersOpt(JCModifiers partial) { flag = Flags.SEALED; break; } - if (isLazyStaticIdentifier()) { - // @@@: make sure this is a field - checkSourceLevel(Feature.LAZY_STATICS); - flag = Flags.LAZY | Flags.STATIC | Flags.FINAL; - nextToken(); - nextToken(); - break; - } break loop; } default: break loop; } - if ((flags & flag) != 0) { - if ((flag == Flags.STATIC && (flags & Flags.LAZY) != 0) || - ((flag & Flags.LAZY) != 0 && (flags & (Flags.STATIC | Flags.LAZY)) == Flags.STATIC)) { - log.error(DiagnosticFlag.SYNTAX, token.pos, - Errors.IllegalCombinationOfModifiers(Set.of(Flags.Flag.LAZY), Set.of(Flags.Flag.STATIC))); - } else { - log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier); - } - } + if ((flags & flag) != 0) log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier); lastPos = token.pos; nextToken(); if (flag == Flags.ANNOTATION) { @@ -4719,9 +4704,8 @@ private List constructorOrMethodOrFieldDeclaration(JCModifiers mods, Nam // Field if (!isVoid && typarams.isEmpty()) { if (!isRecord || (isRecord && (mods.flags & Flags.STATIC) != 0)) { - boolean isLazy = (mods.flags & Flags.LAZY) != 0; List defs = - variableDeclaratorsRest(pos, mods, type, name, isInterface || isLazy, dc, + variableDeclaratorsRest(pos, mods, type, name, isInterface, dc, new ListBuffer(), false).toList(); accept(SEMI); storeEnd(defs.last(), S.prevToken().endPos); @@ -4859,7 +4843,7 @@ protected boolean isSealedClassStart(boolean local) { } protected boolean isLazyStaticIdentifier() { - return (token.name() == names.lazy && peekToken(0, TokenKind.SUB, STATIC)); + return false; } private boolean allowedAfterSealedOrNonSealed(Token next, boolean local, boolean currentIsNonSealed) { diff --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index 5f20459832428..028b3af84bfef 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -574,7 +574,7 @@ static U m(String param) { // can't be explicitly static for (String s : List.of("static record RR() { }", "static interface I {}", "static enum E { A }")) { - assertFail("compiler.err.illegal.start.of.expr", "class R { void m() { #S } }".replaceFirst("#S", s)); + assertOK("class R { void m() { #S } }".replaceFirst("#S", s)); } // but static fields can be accessed