changeset 20251:90a73be53aa0

4881267: improve diagnostic for "instanceof T" for type parameter T Reviewed-by: vromero, jjg
author kizune
date Mon, 23 Sep 2013 18:29:27 +0400
parents 26472e76d2f5
children fae5c47ba7a4
files langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java langtools/src/share/classes/com/sun/tools/javac/comp/Check.java langtools/test/tools/javac/T4881267.java langtools/test/tools/javac/T4881267.out
diffstat 4 files changed, 22 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Sep 23 17:27:38 2013 +0400
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Sep 23 18:29:27 2013 +0400
@@ -3102,8 +3102,14 @@
     public void visitTypeTest(JCInstanceOf tree) {
         Type exprtype = chk.checkNullOrRefType(
             tree.expr.pos(), attribExpr(tree.expr, env));
-        Type clazztype = chk.checkReifiableReferenceType(
-            tree.clazz.pos(), attribType(tree.clazz, env));
+        Type clazztype = attribType(tree.clazz, env);
+        if (!clazztype.hasTag(TYPEVAR)) {
+            clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype);
+        }
+        if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
+            log.error(tree.clazz.pos(), "illegal.generic.type.for.instof");
+            clazztype = types.createErrorType(clazztype);
+        }
         chk.validate(tree.clazz, env, false);
         chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
         result = check(tree, syms.booleanType, VAL, resultInfo);
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Sep 23 17:27:38 2013 +0400
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Sep 23 18:29:27 2013 +0400
@@ -706,20 +706,6 @@
         return t;
     }
 
-    /** Check that type is a reifiable class, interface or array type.
-     *  @param pos           Position to be used for error reporting.
-     *  @param t             The type to be checked.
-     */
-    Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) {
-        t = checkClassOrArrayType(pos, t);
-        if (!t.isErroneous() && !types.isReifiable(t)) {
-            log.error(pos, "illegal.generic.type.for.instof");
-            return types.createErrorType(t);
-        } else {
-            return t;
-        }
-    }
-
     /** Check that type is a reference type, i.e. a class, interface or array type
      *  or a type variable.
      *  @param pos           Position to be used for error reporting.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T4881267.java	Mon Sep 23 18:29:27 2013 +0400
@@ -0,0 +1,12 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 4881267
+ * @summary improve diagnostic for "instanceof T" for type parameter T
+ * @compile/fail/ref=T4881267.out -XDrawDiagnostics T4881267.java
+ */
+
+class T4881267 {
+    <T> void m(Object o) {
+        boolean b = o instanceof T;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T4881267.out	Mon Sep 23 18:29:27 2013 +0400
@@ -0,0 +1,2 @@
+T4881267.java:10:34: compiler.err.illegal.generic.type.for.instof
+1 error