changeset 49038:605a5a1da9fe lworld

[lworld] Where statically discernible, javac should reject null assignments to, null casts to and null comparisons with value instances.
author sadayapalam
date Tue, 27 Feb 2018 13:34:17 +0530
parents ed5e12610039
children ece7d51734ba
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/langtools/tools/javac/diags/examples/IllegalNullAssignmentToFlattenableValue.java test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableFlagFromClass.out test/langtools/tools/javac/valhalla/lworld-values/CheckMultiDimensionalArrayStore.out test/langtools/tools/javac/valhalla/lworld-values/CheckNullAssign.java test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.java test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.out test/langtools/tools/javac/valhalla/lworld-values/FlattenableNegativeTest.out
diffstat 10 files changed, 26 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Feb 27 12:00:18 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Feb 27 13:34:17 2018 +0530
@@ -1107,7 +1107,7 @@
                      return isSubtypeNoCapture(t.getUpperBound(), s);
                  case BOT:
                      return
-                         s.hasTag(BOT) || s.hasTag(CLASS) ||
+                         s.hasTag(BOT) || (s.hasTag(CLASS) && !isValue(s)) ||
                          s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
                  case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
                  case NONE:
@@ -1677,7 +1677,7 @@
 
             @Override
             public Boolean visitClassType(ClassType t, Type s) {
-                if (s.hasTag(ERROR) || s.hasTag(BOT))
+                if (s.hasTag(ERROR) || s.hasTag(BOT) && !isValue(t))
                     return true;
 
                 if (s.hasTag(TYPEVAR)) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Feb 27 12:00:18 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Feb 27 13:34:17 2018 +0530
@@ -1215,10 +1215,6 @@
                         v.type = chk.checkLocalVarType(tree, tree.init.type.baseType(), tree.name);
                     }
                 }
-                if ((v.flags() & FLATTENABLE) != 0 || (v.type.hasTag(ARRAY) && types.isValue(((ArrayType) v.type).elemtype))) {
-                    if (haveNullInitializer(tree.init))
-                        log.error(tree.init.pos(), Errors.IllegalNullAssignmentToFlattenableValue);
-                }
                 if (tree.isImplicitlyTyped()) {
                     setSyntheticVariableType(tree, v.type);
                 }
@@ -1229,20 +1225,6 @@
             chk.setLint(prevLint);
         }
     }
-        // where
-        private boolean haveNullInitializer(JCExpression init) {
-            if (init.type != null) {
-                if (init.type.hasTag(BOT))
-                    return true;
-                if (init.type.hasTag(ARRAY)) {
-                    for (JCTree elem: ((JCNewArray) init).elems) {
-                        if (elem != null && elem.type != null && elem.type.hasTag(BOT))
-                            return true;
-                    }
-                }
-            }
-            return false;
-        }
 
     Fragment canInferLocalVarType(JCVariableDecl tree) {
         LocalInitScanner lis = new LocalInitScanner();
@@ -2585,12 +2567,6 @@
         }
         if (tree.elems != null) {
             attribExprs(tree.elems, localEnv, elemtype);
-            if (types.isValue(elemtype)) {
-                for (JCExpression elem : tree.elems) {
-                    if (elem.type != null && elem.type.hasTag(BOT))
-                        log.error(tree.pos(), Errors.IllegalNullAssignmentToFlattenableValue);
-                }
-            }
             owntype = new ArrayType(elemtype, syms.arrayClass);
         }
         if (!types.isReifiable(elemtype))
@@ -3443,13 +3419,6 @@
         Type capturedType = capture(owntype);
         attribExpr(tree.rhs, env, owntype);
         result = check(tree, capturedType, KindSelector.VAL, resultInfo);
-        if (tree.rhs.type != null && tree.rhs.type.hasTag(BOT)) {
-            Symbol v = TreeInfo.symbol(tree.lhs);
-            if ((v != null && (v.flags() & FLATTENABLE) != 0) ||
-                    tree.lhs.hasTag(INDEXED) && types.isValue(tree.lhs.type)) {
-                log.error(tree.pos(), Errors.IllegalNullAssignmentToFlattenableValue);
-            }
-        }
     }
 
     public void visitAssignop(JCAssignOp tree) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 27 12:00:18 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 27 13:34:17 2018 +0530
@@ -3216,9 +3216,6 @@
 compiler.err.value.may.not.extend=\
     value type may not extend another value or class
 
-compiler.err.illegal.null.assignment.to.flattenable.value=\
-    illegal null assignment to flattenable value instance
-
 # 0: name (of method)
 compiler.warn.value.does.not.support=\
     value types do not support {0}
--- a/test/langtools/tools/javac/diags/examples/IllegalNullAssignmentToFlattenableValue.java	Tue Feb 27 12:00:18 2018 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-/*
- * Copyright (c) 2018, 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.
- */
-
-// key: compiler.err.illegal.null.assignment.to.flattenable.value
-
-
-class IllegalNullAssignmentToFlattenableValue {
-    __Flattenable V v = null;
-    __ByValue final class V {
-        final int x = 10;
-    }
-}
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableFlagFromClass.out	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableFlagFromClass.out	Tue Feb 27 13:34:17 2018 +0530
@@ -1,3 +1,3 @@
-CheckFlattenableFlagFromClass.java:11:13: compiler.err.illegal.null.assignment.to.flattenable.value
-CheckFlattenableFlagFromClass.java:12:17: compiler.err.illegal.null.assignment.to.flattenable.value
-2 errors
\ No newline at end of file
+CheckFlattenableFlagFromClass.java:11:15: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableFlagFromClass.V)
+CheckFlattenableFlagFromClass.java:12:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableFlagFromClass.V)
+2 errors
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckMultiDimensionalArrayStore.out	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckMultiDimensionalArrayStore.out	Tue Feb 27 13:34:17 2018 +0530
@@ -1,5 +1,5 @@
-CheckMultiDimensionalArrayStore.java:11:41: compiler.err.illegal.null.assignment.to.flattenable.value
-CheckMultiDimensionalArrayStore.java:12:28: compiler.err.illegal.null.assignment.to.flattenable.value
-CheckMultiDimensionalArrayStore.java:14:36: compiler.err.illegal.null.assignment.to.flattenable.value
-CheckMultiDimensionalArrayStore.java:15:29: compiler.err.illegal.null.assignment.to.flattenable.value
+CheckMultiDimensionalArrayStore.java:11:43: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, CheckMultiDimensionalArrayStore.V)
+CheckMultiDimensionalArrayStore.java:12:30: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, CheckMultiDimensionalArrayStore.V)
+CheckMultiDimensionalArrayStore.java:14:38: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, CheckMultiDimensionalArrayStore.V)
+CheckMultiDimensionalArrayStore.java:15:31: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, CheckMultiDimensionalArrayStore.V)
 4 errors
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckNullAssign.java	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckNullAssign.java	Tue Feb 27 13:34:17 2018 +0530
@@ -1,12 +1,13 @@
 /*
  * @test /nodynamiccopyright/
- * @summary Check assignment of null to value types - legal scenarios.
+ * @summary Assignment of null to value types should be disallowed.
  *
- * @compile -XDrawDiagnostics CheckNullAssign.java
+ * @compile/fail/ref=CheckNullAssign.out -XDrawDiagnostics CheckNullAssign.java
  */
 
 final __ByValue class CheckNullAssign {
     CheckNullAssign foo(CheckNullAssign cna) {
+        // All of the below involve subtype/assignability checks and should be rejected.
         cna = null;
         foo(null);
         if (null instanceof CheckNullAssign) {}
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.java	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.java	Tue Feb 27 13:34:17 2018 +0530
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @summary Check that null can be casted to a value type; but cannot be compared with value types.
+ * @summary null cannot be casted to and compared with value types.
  *
  * @compile/fail/ref=CheckNullCastable.out -XDrawDiagnostics CheckNullCastable.java
  */
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.out	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckNullCastable.out	Tue Feb 27 13:34:17 2018 +0530
@@ -1,3 +1,4 @@
-CheckNullCastable.java:11:17: compiler.err.value.does.not.support: !=
-CheckNullCastable.java:12:18: compiler.err.value.does.not.support: !=
-2 errors
+CheckNullCastable.java:10:54: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, CheckNullCastable)
+CheckNullCastable.java:11:17: compiler.err.incomparable.types: CheckNullCastable, compiler.misc.type.null
+CheckNullCastable.java:12:18: compiler.err.incomparable.types: compiler.misc.type.null, CheckNullCastable
+3 errors
--- a/test/langtools/tools/javac/valhalla/lworld-values/FlattenableNegativeTest.out	Tue Feb 27 12:00:18 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/FlattenableNegativeTest.out	Tue Feb 27 13:34:17 2018 +0530
@@ -1,10 +1,10 @@
-FlattenableNegativeTest.java:13:39: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:17:21: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:22:17: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:28:23: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:29:24: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:31:21: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:32:19: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:33:23: compiler.err.illegal.null.assignment.to.flattenable.value
-FlattenableNegativeTest.java:34:22: compiler.err.illegal.null.assignment.to.flattenable.value
+FlattenableNegativeTest.java:13:39: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:17:23: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:22:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:28:25: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:29:34: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:31:23: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:32:21: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:33:25: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
+FlattenableNegativeTest.java:34:32: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, FlattenableNegativeTest.V)
 9 errors