8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jun 27 09:54:50 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jun 27 16:04:05 2013 +0100
@@ -756,25 +756,15 @@
JCTree.JCExpression initializer,
Type type) {
- // in case no lint value has been set up for this env, scan up
- // env stack looking for smallest enclosing env for which it is set.
- Env<AttrContext> lintEnv = env;
- while (lintEnv.info.lint == null)
- lintEnv = lintEnv.next;
-
- // Having found the enclosing lint value, we can initialize the lint value for this class
- // ... but ...
- // There's a problem with evaluating annotations in the right order, such that
- // env.info.enclVar.attributes_field might not yet have been evaluated, and so might be
- // null. In that case, calling augment will throw an NPE. To avoid this, for now we
- // revert to the jdk 6 behavior and ignore the (unevaluated) attributes.
- if (env.info.enclVar.annotationsPendingCompletion()) {
- env.info.lint = lintEnv.info.lint;
- } else {
- env.info.lint = lintEnv.info.lint.augment(env.info.enclVar);
- }
-
- Lint prevLint = chk.setLint(env.info.lint);
+ /* When this env was created, it didn't have the correct lint nor had
+ * annotations has been processed.
+ * But now at this phase we have already processed annotations and the
+ * correct lint must have been set in chk, so we should use that one to
+ * attribute the initializer.
+ */
+ Lint prevLint = env.info.lint;
+ env.info.lint = chk.getLint();
+
JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
try {
@@ -786,10 +776,11 @@
memberEnter.typeAnnotate(initializer, env, null);
annotate.flush();
Type itype = attribExpr(initializer, env, type);
- if (itype.constValue() != null)
+ if (itype.constValue() != null) {
return coerce(itype, type).constValue();
- else
+ } else {
return null;
+ }
} finally {
env.info.lint = prevLint;
log.useSource(prevSource);
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Jun 27 09:54:50 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Jun 27 16:04:05 2013 +0100
@@ -218,6 +218,14 @@
return prev;
}
+ /* This idiom should be used only in cases when it is needed to set the lint
+ * of an environment that has been created in a phase previous to annotations
+ * processing.
+ */
+ Lint getLint() {
+ return lint;
+ }
+
DeferredLintHandler setDeferredLintHandler(DeferredLintHandler newDeferredLintHandler) {
DeferredLintHandler prev = deferredLintHandler;
deferredLintHandler = newDeferredLintHandler;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8016099/UncheckedWarningRegressionTest.java Thu Jun 27 16:04:05 2013 +0100
@@ -0,0 +1,32 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8016099
+ * @summary Some SuppressWarnings annotations ignored ( unchecked, rawtypes )
+ * @compile UncheckedWarningRegressionTest.java
+ * @compile/fail/ref=UncheckedWarningRegressionTest.out -XDrawDiagnostics -Werror -Xlint:unchecked UncheckedWarningRegressionTest.java
+ */
+
+public class UncheckedWarningRegressionTest {
+ <T> void suppressedWarningsFinalInitializer() {
+ @SuppressWarnings("unchecked")
+ T[] tt = (T[]) FINAL_EMPTY_ARRAY;
+ }
+
+ final Object[] FINAL_EMPTY_ARRAY = {};
+
+ <T> void finalInitializer() {
+ T[] tt = (T[]) FINAL_EMPTY_ARRAY;
+ }
+
+ <T> void suppressedWarningsNonFinalInitializer() {
+ @SuppressWarnings("unchecked")
+ T[] tt = (T[]) NON_FINAL_EMPTY_ARRAY;
+ }
+
+ Object[] NON_FINAL_EMPTY_ARRAY = {};
+
+ <T> void nonFinalInitializer() {
+ T[] tt = (T[]) NON_FINAL_EMPTY_ARRAY;
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8016099/UncheckedWarningRegressionTest.out Thu Jun 27 16:04:05 2013 +0100
@@ -0,0 +1,5 @@
+UncheckedWarningRegressionTest.java:18:24: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object[], T[]
+UncheckedWarningRegressionTest.java:29:24: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object[], T[]
+- compiler.err.warnings.and.werror
+1 error
+2 warnings