OpenJDK / amber / amber
changeset 57017:1f7e5db93261 records-and-sealed
assign unassigned components only for the compact record constructor
author | vromero |
---|---|
date | Tue, 20 Aug 2019 15:31:12 -0400 |
parents | 821f4cb1f879 |
children | 3ff2c65453c9 b8a279cd5eb2 |
files | src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java test/langtools/tools/javac/records/RecordCompilationTests.java test/langtools/tools/javac/records/mandated_members/MandatedMembersNotBeingEntered.java test/langtools/tools/javac/records/mandated_members/canonical_constructor/MethodParametersForCanonicalConstructorTest.java |
diffstat | 6 files changed, 32 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java Tue Aug 20 14:33:57 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java Tue Aug 20 15:31:12 2019 -0400 @@ -340,6 +340,11 @@ */ public static final long RECORD = 1L<<61; + /** + * Flag to mark a record constructor as a compact one + */ + public static final long COMPACT_RECORD_CONSTRUCTOR = 1L<<51; + /** Modifier masks. */ public static final int
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java Tue Aug 20 14:33:57 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java Tue Aug 20 15:31:12 2019 -0400 @@ -970,23 +970,25 @@ constructorInvocationName == names._super) { RecordConstructorHelper helper = new RecordConstructorHelper(sym, TreeInfo.recordFields(tree)); JCMethodDecl methDecl = (JCMethodDecl)def; - if (constructorInvocationName == names.empty) { - JCStatement supCall = make.at(methDecl.body.pos).Exec(make.Apply(List.nil(), - make.Ident(names._super), List.nil())); - methDecl.body.stats = methDecl.body.stats.prepend(supCall); - } - ListBuffer<JCStatement> initializations = new ListBuffer<>(); - List<Name> inits = helper.inits(); - InitializationFinder initFinder = new InitializationFinder(inits); - initFinder.scan(methDecl.body.stats); - List<Name> found = initFinder.found.toList(); - inits = inits.diff(found); - if (!inits.isEmpty()) { - for (Name initName : inits) { - initializations.add(make.Exec(make.Assign(make.Select(make.Ident(names._this), - initName), make.Ident(initName)))); + if ((methDecl.mods.flags & Flags.COMPACT_RECORD_CONSTRUCTOR) != 0) { + if (constructorInvocationName == names.empty) { + JCStatement supCall = make.at(methDecl.body.pos).Exec(make.Apply(List.nil(), + make.Ident(names._super), List.nil())); + methDecl.body.stats = methDecl.body.stats.prepend(supCall); } - methDecl.body.stats = methDecl.body.stats.appendList(initializations.toList()); + ListBuffer<JCStatement> initializations = new ListBuffer<>(); + List<Name> inits = helper.inits(); + InitializationFinder initFinder = new InitializationFinder(inits); + initFinder.scan(methDecl.body.stats); + List<Name> found = initFinder.found.toList(); + inits = inits.diff(found); + if (!inits.isEmpty()) { + for (Name initName : inits) { + initializations.add(make.Exec(make.Assign(make.Select(make.Ident(names._this), + initName), make.Ident(initName)))); + } + methDecl.body.stats = methDecl.body.stats.appendList(initializations.toList()); + } } } }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Aug 20 14:33:57 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Aug 20 15:31:12 2019 -0400 @@ -4096,6 +4096,9 @@ log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidMethDeclRetTypeReq); else if (annosAfterParams.nonEmpty()) illegal(annosAfterParams.head.pos); + if (isRecord && token.kind == LBRACE) { + mods.flags |= Flags.COMPACT_RECORD_CONSTRUCTOR; + } return List.of(methodDeclaratorRest( pos, mods, null, names.init, typarams, isInterface, true, isRecord, dc));
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java Tue Aug 20 14:33:57 2019 -0400 +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java Tue Aug 20 15:31:12 2019 -0400 @@ -264,9 +264,8 @@ assertOK("record R(int x, int y) { # }", goodCtor); // Not OK to redeclare canonical without DA - // @@@ Should fail -// assertFail("", "record R(int x, int y) { # }", -// "public R(int x, int y) { this.x = x; }"); + assertFail("compiler.err.var.might.not.have.been.initialized", "record R(int x, int y) { # }", + "public R(int x, int y) { this.x = x; }"); // canonical ctor must be public assertFail("compiler.err.canonical.constructor.must.be.public", "record R(int x, int y) { # }",
--- a/test/langtools/tools/javac/records/mandated_members/MandatedMembersNotBeingEntered.java Tue Aug 20 14:33:57 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/** - * @test - * @summary test to check that mandated members are entered - * @compile MandatedMembersNotBeingEntered.java - */ - -record MandatedMembersNotBeingEntered(int i, int j) { - public MandatedMembersNotBeingEntered(int i, int j, Object obj) {} -}
--- a/test/langtools/tools/javac/records/mandated_members/canonical_constructor/MethodParametersForCanonicalConstructorTest.java Tue Aug 20 14:33:57 2019 -0400 +++ b/test/langtools/tools/javac/records/mandated_members/canonical_constructor/MethodParametersForCanonicalConstructorTest.java Tue Aug 20 15:31:12 2019 -0400 @@ -43,7 +43,10 @@ } record R3(int i, int j) { - public R3(int i, int j) {} + public R3(int i, int j) { + this.i = i; + this.j = j; + } } public static void main(String args[]) throws Throwable {