changeset 1708:da87f2bc8fd9

8033294: javac, spurious warning for instanceof operator Reviewed-by: jjg, jlahoda
author vromero
date Tue, 11 Feb 2014 21:01:12 +0000
parents ff6a1c2be8b8
children 02f050bc5569
files src/share/classes/com/sun/tools/javac/code/Type.java src/share/classes/com/sun/tools/javac/code/Types.java test/tools/javac/T8033294/RedundantWarningInIntersectionTest.java
diffstat 3 files changed, 92 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Type.java	Mon Feb 10 09:50:12 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/code/Type.java	Tue Feb 11 21:01:12 2014 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, 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
@@ -597,6 +597,10 @@
                 };
         }
 
+        public List<Type> getComponents() {
+            return interfaces_field.prepend(supertype_field);
+        }
+
         /** The Java source which this type represents.
          */
         public String toString() {
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Feb 10 09:50:12 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Feb 11 21:01:12 2014 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2014, 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
@@ -1012,23 +1012,10 @@
                     }
                 }
 
-                if (t.isCompound()) {
-                    Warner oldWarner = warnStack.head;
-                    warnStack.head = Warner.noWarnings;
-                    if (!visit(supertype(t), s))
-                        return false;
-                    for (Type intf : interfaces(t)) {
-                        if (!visit(intf, s))
-                            return false;
-                    }
-                    if (warnStack.head.hasLint(LintCategory.UNCHECKED))
-                        oldWarner.warn(LintCategory.UNCHECKED);
-                    return true;
-                }
-
-                if (s.isCompound()) {
-                    // call recursively to reuse the above code
-                    return visitClassType((ClassType)s, t);
+                if (t.isCompound() || s.isCompound()) {
+                    return !t.isCompound() ?
+                            visitIntersectionType((ClassType)s, t, true) :
+                            visitIntersectionType(t, s, false);
                 }
 
                 if (s.tag == CLASS || s.tag == ARRAY) {
@@ -1106,6 +1093,18 @@
                 return false;
             }
 
+            boolean visitIntersectionType(ClassType ict, Type s, boolean reverse) {
+                Warner warn = Warner.noWarnings;
+                for (Type c : ict.getComponents()) {
+                    warn.clear();
+                    if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn))
+                        return false;
+                }
+                if (warn.hasLint(LintCategory.UNCHECKED))
+                    warnStack.head.warn(LintCategory.UNCHECKED);
+                return true;
+            }
+
             @Override
             public Boolean visitArrayType(ArrayType t, Type s) {
                 switch (s.tag) {
@@ -3369,11 +3368,18 @@
     }
 
     private boolean giveWarning(Type from, Type to) {
-        Type subFrom = asSub(from, to.tsym);
-        return to.isParameterized() &&
-                (!(isUnbounded(to) ||
-                isSubtype(from, to) ||
-                ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
+        List<Type> bounds = to.isCompound() ?
+                ((ClassType)to).getComponents() : List.of(to);
+        for (Type b : bounds) {
+            Type subFrom = asSub(from, b.tsym);
+            if (b.isParameterized() &&
+                    (!(isUnbounded(b) ||
+                    isSubtype(from, b) ||
+                    ((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) {
+                return true;
+            }
+        }
+        return false;
     }
 
     private List<Type> superClosure(Type t, Type s) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/T8033294/RedundantWarningInIntersectionTest.java	Tue Feb 11 21:01:12 2014 +0000
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2014, 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
+ * @bug 8033294
+ * @summary javac, spurious warning for instanceof operator
+ * @compile -Werror -Xlint:unchecked RedundantWarningInIntersectionTest.java
+ */
+
+import java.math.BigDecimal;
+
+public class RedundantWarningInIntersectionTest {
+
+    class A<S extends A<S, T>, T> {
+
+        protected T p;
+
+        A(T p) {}
+
+        public S m(T parameter) {
+            @SuppressWarnings("unchecked")
+            S self = (S) new A<>(parameter);
+            return self;
+        }
+    }
+
+    class B<K extends Number & Comparable<? super K>> extends A<B<K>, K> {
+
+        B(K parameter) {
+            super(parameter);
+        }
+
+        public boolean m2() {
+            return (p instanceof BigDecimal);
+        }
+    }
+}