4881267: improve diagnostic for "instanceof T" for type parameter T

Mon, 23 Sep 2013 18:29:27 +0400

author
kizune
date
Mon, 23 Sep 2013 18:29:27 +0400
changeset 2049
64e79d38bd07
parent 2048
809a50f24d6f
child 2050
09301757bb32

4881267: improve diagnostic for "instanceof T" for type parameter T
Reviewed-by: vromero, jjg

src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Check.java file | annotate | diff | comparison | revisions
test/tools/javac/T4881267.java file | annotate | diff | comparison | revisions
test/tools/javac/T4881267.out file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Sep 23 17:27:38 2013 +0400
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Sep 23 18:29:27 2013 +0400
     1.3 @@ -3102,8 +3102,14 @@
     1.4      public void visitTypeTest(JCInstanceOf tree) {
     1.5          Type exprtype = chk.checkNullOrRefType(
     1.6              tree.expr.pos(), attribExpr(tree.expr, env));
     1.7 -        Type clazztype = chk.checkReifiableReferenceType(
     1.8 -            tree.clazz.pos(), attribType(tree.clazz, env));
     1.9 +        Type clazztype = attribType(tree.clazz, env);
    1.10 +        if (!clazztype.hasTag(TYPEVAR)) {
    1.11 +            clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype);
    1.12 +        }
    1.13 +        if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
    1.14 +            log.error(tree.clazz.pos(), "illegal.generic.type.for.instof");
    1.15 +            clazztype = types.createErrorType(clazztype);
    1.16 +        }
    1.17          chk.validate(tree.clazz, env, false);
    1.18          chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
    1.19          result = check(tree, syms.booleanType, VAL, resultInfo);
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Sep 23 17:27:38 2013 +0400
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Sep 23 18:29:27 2013 +0400
     2.3 @@ -706,20 +706,6 @@
     2.4          return t;
     2.5      }
     2.6  
     2.7 -    /** Check that type is a reifiable class, interface or array type.
     2.8 -     *  @param pos           Position to be used for error reporting.
     2.9 -     *  @param t             The type to be checked.
    2.10 -     */
    2.11 -    Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) {
    2.12 -        t = checkClassOrArrayType(pos, t);
    2.13 -        if (!t.isErroneous() && !types.isReifiable(t)) {
    2.14 -            log.error(pos, "illegal.generic.type.for.instof");
    2.15 -            return types.createErrorType(t);
    2.16 -        } else {
    2.17 -            return t;
    2.18 -        }
    2.19 -    }
    2.20 -
    2.21      /** Check that type is a reference type, i.e. a class, interface or array type
    2.22       *  or a type variable.
    2.23       *  @param pos           Position to be used for error reporting.
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/T4881267.java	Mon Sep 23 18:29:27 2013 +0400
     3.3 @@ -0,0 +1,12 @@
     3.4 +/*
     3.5 + * @test /nodynamiccopyright/
     3.6 + * @bug 4881267
     3.7 + * @summary improve diagnostic for "instanceof T" for type parameter T
     3.8 + * @compile/fail/ref=T4881267.out -XDrawDiagnostics T4881267.java
     3.9 + */
    3.10 +
    3.11 +class T4881267 {
    3.12 +    <T> void m(Object o) {
    3.13 +        boolean b = o instanceof T;
    3.14 +    }
    3.15 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/T4881267.out	Mon Sep 23 18:29:27 2013 +0400
     4.3 @@ -0,0 +1,2 @@
     4.4 +T4881267.java:10:34: compiler.err.illegal.generic.type.for.instof
     4.5 +1 error

mercurial