changeset 59056:9ab7ea5f4ba3 records

allow return in canonical, non-compact, constructors
author vromero
date Fri, 29 Nov 2019 11:20:39 -0500
parents 6633405e5db6
children 0272d0b567ea
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java test/langtools/tools/javac/records/RecordCompilationTests.java
diffstat 3 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Nov 28 10:57:25 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Nov 29 11:20:39 2019 -0500
@@ -2201,7 +2201,7 @@
         } else if (!env.info.isLambda &&
                 !env.info.isNewClass &&
                 env.enclMethod != null &&
-                TreeInfo.isCanonicalConstructor(env.enclMethod)) {
+                TreeInfo.isCompactConstructor(env.enclMethod)) {
             log.error(env.enclMethod,
                     Errors.InvalidCanonicalConstructorInRecord(env.enclMethod.sym, Fragments.CanonicalCantHaveReturnStatement));
         } else {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Thu Nov 28 10:57:25 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Fri Nov 29 11:20:39 2019 -0500
@@ -87,6 +87,11 @@
         return isConstructor(tree) && (((JCMethodDecl)tree).sym.flags_field & RECORD) != 0;
     }
 
+    public static boolean isCompactConstructor(JCTree tree) {
+        // the record flag is only set to the canonical constructor
+        return isCanonicalConstructor(tree) && (((JCMethodDecl)tree).sym.flags_field & COMPACT_RECORD_CONSTRUCTOR) != 0;
+    }
+
     public static boolean isReceiverParam(JCTree tree) {
         if (tree.hasTag(VARDEF)) {
             return ((JCVariableDecl)tree).nameexpr != null;
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Nov 28 10:57:25 2019 -0500
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Fri Nov 29 11:20:39 2019 -0500
@@ -402,12 +402,13 @@
                 "public R { if (x < 0) { this.x = -x; } }");
     }
 
-    public void testReturnInCanonical() {
+    public void testReturnInCanonical_Compact() {
         assertFail("compiler.err.invalid.canonical.constructor.in.record", "record R(int x) { # }",
                 "public R { return; }");
         assertFail("compiler.err.invalid.canonical.constructor.in.record", "record R(int x) { # }",
                 "public R { if (i < 0) { return; }}");
-        assertOK("record R(int x) { public R { Runnable r = () -> {return;};} }");
+        assertOK("record R(int x) { public R(int x) { this.x = x; return; } }");
+        assertOK("record R(int x) { public R { Runnable r = () -> { return; };} }");
     }
 
     public void testNoNativeMethods() {