提交 70731009 编写于 作者: M mcimadamore

6885255: Improve usability of raw warnings

Summary: raw warnings should be disabled in (i) instanceof expressions and (ii) when java.lang.Class is not parameterized
Reviewed-by: jjg
上级 926e89e5
......@@ -2008,7 +2008,7 @@ public class Attr extends JCTree.Visitor {
public void visitTypeCast(JCTypeCast tree) {
Type clazztype = attribType(tree.clazz, env);
chk.validate(tree.clazz, env);
chk.validate(tree.clazz, env, false);
Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly);
Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
if (exprtype.constValue() != null)
......@@ -2021,7 +2021,7 @@ public class Attr extends JCTree.Visitor {
tree.expr.pos(), attribExpr(tree.expr, env));
Type clazztype = chk.checkReifiableReferenceType(
tree.clazz.pos(), attribType(tree.clazz, env));
chk.validate(tree.clazz, env);
chk.validate(tree.clazz, env, false);
chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
result = check(tree, syms.booleanType, VAL, pkind, pt);
}
......
......@@ -906,33 +906,15 @@ public class Check {
*
* and we can't make sure that the bound is already attributed because
* of possible cycles.
*/
private Validator validator = new Validator();
/** Visitor method: Validate a type expression, if it is not null, catching
*
* Visitor method: Validate a type expression, if it is not null, catching
* and reporting any completion failures.
*/
void validate(JCTree tree, Env<AttrContext> env) {
try {
if (tree != null) {
validator.env = env;
tree.accept(validator);
checkRaw(tree, env);
}
} catch (CompletionFailure ex) {
completionError(tree.pos(), ex);
}
validate(tree, env, true);
}
//where
void checkRaw(JCTree tree, Env<AttrContext> env) {
if (lint.isEnabled(Lint.LintCategory.RAW) &&
tree.type.tag == CLASS &&
!TreeInfo.isDiamond(tree) &&
!env.enclClass.name.isEmpty() && //anonymous or intersection
tree.type.isRaw()) {
log.warning(Lint.LintCategory.RAW,
tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
}
void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) {
new Validator(env).validateTree(tree, checkRaw, true);
}
/** Visitor method: Validate a list of type expressions.
......@@ -946,9 +928,16 @@ public class Check {
*/
class Validator extends JCTree.Visitor {
boolean isOuter;
Env<AttrContext> env;
Validator(Env<AttrContext> env) {
this.env = env;
}
@Override
public void visitTypeArray(JCArrayTypeTree tree) {
validate(tree.elemtype, env);
tree.elemtype.accept(this);
}
@Override
......@@ -960,10 +949,14 @@ public class Check {
List<Type> forms = tree.type.tsym.type.getTypeArguments();
ListBuffer<Type> tvars_buf = new ListBuffer<Type>();
boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class;
// For matching pairs of actual argument types `a' and
// formal type parameters with declared bound `b' ...
while (args.nonEmpty() && forms.nonEmpty()) {
validate(args.head, env);
validateTree(args.head,
!(isOuter && is_java_lang_Class),
false);
// exact type arguments needs to know their
// bounds (for upper and lower bound
......@@ -1015,14 +1008,14 @@ public class Check {
@Override
public void visitTypeParameter(JCTypeParameter tree) {
validate(tree.bounds, env);
validateTrees(tree.bounds, true, isOuter);
checkClassBounds(tree.pos(), tree.type);
}
@Override
public void visitWildcard(JCWildcard tree) {
if (tree.inner != null)
validate(tree.inner, env);
validateTree(tree.inner, true, isOuter);
}
@Override
......@@ -1060,7 +1053,34 @@ public class Check {
public void visitTree(JCTree tree) {
}
Env<AttrContext> env;
public void validateTree(JCTree tree, boolean checkRaw, boolean isOuter) {
try {
if (tree != null) {
this.isOuter = isOuter;
tree.accept(this);
if (checkRaw)
checkRaw(tree, env);
}
} catch (CompletionFailure ex) {
completionError(tree.pos(), ex);
}
}
public void validateTrees(List<? extends JCTree> trees, boolean checkRaw, boolean isOuter) {
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
validateTree(l.head, checkRaw, isOuter);
}
void checkRaw(JCTree tree, Env<AttrContext> env) {
if (lint.isEnabled(Lint.LintCategory.RAW) &&
tree.type.tag == CLASS &&
!TreeInfo.isDiamond(tree) &&
!env.enclClass.name.isEmpty() && //anonymous or intersection
tree.type.isRaw()) {
log.warning(Lint.LintCategory.RAW,
tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
}
}
}
/* *************************************************************************
......
......@@ -27,8 +27,8 @@ class T6747671<E> {
A<B>.Z<A<B>> z3;//raw warning (2)
void test(Object arg1, B arg2) {//raw warning
boolean b = arg1 instanceof A;//raw warning
Object a = (A)arg1;//raw warning
boolean b = arg1 instanceof A;//ok
Object a = (A)arg1;//ok
A a2 = new A() {};//raw warning (2)
a2.new Z() {};//raw warning
}
......
......@@ -4,9 +4,7 @@ T6747671.java:23:13: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:27:14: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:27:7: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:30:37: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:31:21: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671<E>.A<X>.Z<Y>
11 warnings
9 warnings
/**
* @test /nodynamiccopyright/
* @bug 6885255
* @summary -Xlint:rawtypes
* @compile/ref=T6885255.out -XDrawDiagnostics -Xlint:rawtypes T6885255.java
*/
class T6885255 {
static class Test<X, Y> {}
Class<Test> ct; //no warn - outer Class w/ raw param
Class<Test<Test, Test>> ctt; //warn - outer Class w/o raw param (2)
Class<Class<Test>> cct; //warn - outer Class w/o raw param
Class<Class<Test<Test, Test>>> cctt; //warn - outer Class w/o raw param (2)
Object o1 = (Test)null; //no warn - outer raw and cast
Object o2 = (Test<Test, Test>)null; //warn - inner raw (2)
Object o3 = (Class)null; //no warn - outer raw and cast
Object o4 = (Class<Test>)null; //no warn - outer Class w/ raw param
Object o5 = (Class<Test<Test, Test>>)null; //warn - outer Class w/ non raw param (2)
Object o6 = (Class<Class<Test<Test, Test>>>)null; //warn - outer Class w/ non raw param (2)
Object o7 = (Test<Class, Class>)null; //warn - inner raw (2)
Object o8 = (Test<Class<Test>, Class<Test>>)null; //warn - inner Class (2)
boolean b = null instanceof Test; //no warn - raw and instanceof
}
T6885255.java:13:16: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:13:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:15:17: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:16:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:16:28: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:19:23: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:19:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:24:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:24:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:25:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:25:41: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:27:23: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class<T>
T6885255.java:27:30: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class<T>
T6885255.java:28:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
T6885255.java:28:42: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
15 warnings
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册