changeset 57900:d16c08f341e8 records-and-sealed

move the code that checks for returns in the canonical constructor to Attr
author vromero
date Thu, 10 Oct 2019 12:10:41 -0400
parents b1ab905e1621
children 027c1e2a196b 4e5c9c683088
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java
diffstat 4 files changed, 10 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Oct 09 19:44:45 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 10 12:10:41 2019 -0400
@@ -2004,6 +2004,11 @@
             log.error(tree.pos(), Errors.RetOutsideMeth);
         } else if (env.info.yieldResult != null) {
             log.error(tree.pos(), Errors.ReturnOutsideSwitchExpression);
+        } else if (!env.info.isLambda &&
+                !env.info.isNewClass &&
+                env.enclMethod != null &&
+                TreeInfo.isCanonicalConstructor(env.enclMethod)) {
+            log.error(tree, Errors.CanonicalCantHaveReturnStatement);
         } else {
             // Attribute return expression, if it exists, and check that
             // it conforms to result type of enclosing method.
@@ -2666,8 +2671,8 @@
         try {
             if (needsRecovery && isSerializable(pt())) {
                 localEnv.info.isSerializable = true;
-                localEnv.info.isLambda = true;
             }
+            localEnv.info.isLambda = true;
             List<Type> explicitParamTypes = null;
             if (that.paramKind == JCLambda.ParameterKind.EXPLICIT) {
                 //attribute lambda parameters
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Wed Oct 09 19:44:45 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Thu Oct 10 12:10:41 2019 -0400
@@ -119,8 +119,6 @@
      */
     JCTree preferredTreeForDiagnostics;
 
-    MethodSymbol recordImplicitConstructor;
-
     /** Duplicate this context, replacing scope field and copying all others.
      */
     AttrContext dup(WriteableScope scope) {
@@ -142,7 +140,6 @@
         info.isNewClass = isNewClass;
         info.preferredTreeForDiagnostics = preferredTreeForDiagnostics;
         info.visitingServiceImplementation = visitingServiceImplementation;
-        info.recordImplicitConstructor = recordImplicitConstructor;
         return info;
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Wed Oct 09 19:44:45 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Oct 10 12:10:41 2019 -0400
@@ -1358,11 +1358,6 @@
                     if (canonicalInit.type.asMethodType().thrown.stream().anyMatch(exc -> !isUnchecked(exc))) {
                         log.error(canonicalDecl, Errors.MethodCantThrowCheckedException);
                     }
-                    ReturnFinder initFinder = new ReturnFinder();
-                    initFinder.scan(canonicalDecl.body.stats);
-                    if (initFinder.hasReturn) {
-                        log.error(canonicalDecl, Errors.CanonicalCantHaveReturnStatement);
-                    }
                     // let's use the RECORD flag to mark it as the canonical constructor
                     canonicalInit.flags_field |= Flags.RECORD;
                 }
@@ -1438,15 +1433,6 @@
 
     }
 
-    class ReturnFinder extends TreeScanner {
-        boolean hasReturn = false;
-
-        @Override
-        public void visitReturn(JCReturn tree) {
-            hasReturn = true;
-        }
-    }
-
     private MethodSymbol lookupMethod(TypeSymbol tsym, Name name, List<Type> argtypes) {
         for (Symbol s : tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
             if (types.isSameTypes(s.type.getParameterTypes(), argtypes)) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Wed Oct 09 19:44:45 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Thu Oct 10 12:10:41 2019 -0400
@@ -82,6 +82,10 @@
         }
     }
 
+    public static boolean isCanonicalConstructor(JCTree tree) {
+        return isConstructor(tree) && ((JCMethodDecl)tree).sym.isRecord();
+    }
+
     public static boolean isReceiverParam(JCTree tree) {
         if (tree.hasTag(VARDEF)) {
             return ((JCVariableDecl)tree).nameexpr != null;