changeset 47433:7a3fb234695d patterns

Fix: JDK-8187429: Missing unchecked conversion warning Fix: JDK-8187420: Error message mentions relevant types transposed
author sadayapalam
date Tue, 17 Oct 2017 12:35:54 +0530
parents ed6b1ad2cb86
children a21b299b0729
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java test/langtools/tools/javac/patterns/DuplicateBindingTest.out test/langtools/tools/javac/patterns/EnsureTypesOrderTest.java test/langtools/tools/javac/patterns/EnsureTypesOrderTest.out test/langtools/tools/javac/patterns/ImpossibleTypeTest.out test/langtools/tools/javac/patterns/UncheckedWarningOnMatchesTest.java test/langtools/tools/javac/patterns/UncheckedWarningOnMatchesTest.out
diffstat 7 files changed, 61 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 12 22:06:04 2017 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Oct 17 12:35:54 2017 +0530
@@ -1439,7 +1439,14 @@
                 if (c.pat != null) {
                     switch (tree.kind) {
                         case MATCHING:
-                            Type patType = attribTree(c.pat, switchEnv, castInfo(seltype));
+                            ResultInfo castInfo = unknownExprInfo;
+                            if (c.pat.getTag() == VARIABLEPATTERN) {
+                                JCVariablePattern variablePattern = (JCVariablePattern) c.pat;
+                                if (variablePattern.vartype == null)
+                                    castInfo = castInfo(seltype);
+                            }
+                            Type patType = attribTree(c.pat, switchEnv, castInfo);
+                            chk.checkCastable(tree.selector.pos(), seltype, patType);
                             if (c.pat.getTag() == CONSTANTPATTERN && !labels.add(patType.constValue())) {
                                 log.error(c.pos(), Errors.DuplicateCaseLabel);
                             }
@@ -3567,7 +3574,14 @@
 
     public void visitPatternTest(JCMatches tree) {
         Type exprtype = attribExpr(tree.expr, env); //no target type
-        attribTree(tree.pattern, env, castInfo(exprtype));
+        ResultInfo castInfo = unknownExprInfo;
+        if (tree.pattern.getTag() == VARIABLEPATTERN) {
+            JCVariablePattern variablePattern = (JCVariablePattern) tree.pattern;
+            if (variablePattern.vartype == null)
+                castInfo = castInfo(exprtype);
+        }
+        attribTree(tree.pattern, env, castInfo);
+        chk.checkCastable(tree.expr.pos(), exprtype, tree.pattern.type);
         result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo);
     }
 
--- a/test/langtools/tools/javac/patterns/DuplicateBindingTest.out	Thu Oct 12 22:06:04 2017 +0200
+++ b/test/langtools/tools/javac/patterns/DuplicateBindingTest.out	Tue Oct 17 12:35:54 2017 +0530
@@ -1,5 +1,5 @@
 DuplicateBindingTest.java:15:35: compiler.err.already.defined: kindname.variable, s, kindname.method, main(java.lang.String[])
 DuplicateBindingTest.java:19:22: compiler.err.already.defined: kindname.variable, k, kindname.method, main(java.lang.String[])
 DuplicateBindingTest.java:21:22: compiler.err.already.defined: kindname.variable, args, kindname.method, main(java.lang.String[])
-DuplicateBindingTest.java:23:22: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.Integer, java.lang.String)
+DuplicateBindingTest.java:18:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, java.lang.Integer)
 4 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/patterns/EnsureTypesOrderTest.java	Tue Oct 17 12:35:54 2017 +0530
@@ -0,0 +1,13 @@
+/*
+ * @test /nodynamiccopyright/
+   @bug 8187420
+ * @summary Error message mentions relevant types transposed
+ * @compile/fail/ref=EnsureTypesOrderTest.out -XDrawDiagnostics EnsureTypesOrderTest.java
+ */
+public class EnsureTypesOrderTest {
+    public static void main(String [] args) { 
+        if (args __matches String s) { 
+            System.out.println("Broken"); 
+        } 
+    } 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/patterns/EnsureTypesOrderTest.out	Tue Oct 17 12:35:54 2017 +0530
@@ -0,0 +1,2 @@
+EnsureTypesOrderTest.java:9:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String[], java.lang.String)
+1 error
\ No newline at end of file
--- a/test/langtools/tools/javac/patterns/ImpossibleTypeTest.out	Thu Oct 12 22:06:04 2017 +0200
+++ b/test/langtools/tools/javac/patterns/ImpossibleTypeTest.out	Tue Oct 17 12:35:54 2017 +0530
@@ -1,3 +1,3 @@
-ImpossibleTypeTest.java:13:25: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, java.lang.Integer)
+ImpossibleTypeTest.java:13:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.Integer, java.lang.String)
 ImpossibleTypeTest.java:16:25: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, ImpossibleTypeTest, null)
 2 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/patterns/UncheckedWarningOnMatchesTest.java	Tue Oct 17 12:35:54 2017 +0530
@@ -0,0 +1,23 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8187429
+ * @summary Missing unchecked conversion warning
+ * @compile/fail/ref=UncheckedWarningOnMatchesTest.out -Xlint:unchecked -Werror -XDrawDiagnostics UncheckedWarningOnMatchesTest.java
+ */
+import java.util.ArrayList;
+
+public class UncheckedWarningOnMatchesTest {
+
+    public static void main(String [] args) {
+
+        Object o = new ArrayList<UncheckedWarningOnMatchesTest>();
+        if (o __matches ArrayList<Integer> ai) {  // unchecked conversion
+            System.out.println("Blah");
+        }
+        switch (o) {
+            case ArrayList<Integer> ai:  // unchecked conversion
+                System.out.println("ArrayList<Integer>");
+                break;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/patterns/UncheckedWarningOnMatchesTest.out	Tue Oct 17 12:35:54 2017 +0530
@@ -0,0 +1,5 @@
+UncheckedWarningOnMatchesTest.java:14:13: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object, java.util.ArrayList<java.lang.Integer>
+UncheckedWarningOnMatchesTest.java:17:16: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object, java.util.ArrayList<java.lang.Integer>
+- compiler.err.warnings.and.werror
+1 error
+2 warnings
\ No newline at end of file