changeset 30403:c904bbdc5ec1

8078592: Compiler fails to reject erroneous use of diamond with anonymous classes involving "fresh" type variables. Reviewed-by: mcimadamore
author sadayapalam
date Sat, 25 Apr 2015 15:59:54 +0530
parents 989253a902c3
children 952a476681b2
files langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java langtools/test/tools/javac/generics/diamond/neg/Neg20.java langtools/test/tools/javac/generics/diamond/neg/Neg20.out
diffstat 3 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Wed Jul 05 20:31:05 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Sat Apr 25 15:59:54 2015 +0530
@@ -841,6 +841,15 @@
                 }
                 return true;
             }
+
+            @Override
+            public Boolean visitTypeVar(TypeVar t, Void s) {
+                /* Any type variable mentioned in the inferred type must have been declared as a type parameter
+                  (i.e cannot have been produced by capture conversion (5.1.10) or by inference (18.4)
+                */
+                return t.tsym.owner.type.getTypeArguments().contains(t);
+            }
+
             @Override
             public Boolean visitCapturedType(CapturedType t, Void s) {
                 return false;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/neg/Neg20.java	Sat Apr 25 15:59:54 2015 +0530
@@ -0,0 +1,36 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8078592
+ * @summary Compiler fails to reject erroneous use of diamond with anonymous classes involving "fresh" type variables.
+ * @compile/fail/ref=Neg20.out Neg20.java -XDrawDiagnostics
+ */
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+public class Neg20 {
+    static class Foo<E extends B<E>> {
+        public Foo<E> complexMethod(E a) {
+            return this;
+        }
+    }
+
+    static class Goo<@T E> {
+        public Goo<E> complexMethod(E a) {
+            return this;
+        }
+    }
+
+    static class B<V> {
+    }
+
+    @Target(ElementType.TYPE_USE)
+    static @interface T {
+    }
+
+    public static void check() {
+        Foo<?> t4 = new Foo<>() {
+        };
+        Goo<?> g4 = new Goo<>() {
+        };
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/neg/Neg20.out	Sat Apr 25 15:59:54 2015 +0530
@@ -0,0 +1,2 @@
+Neg20.java:31:28: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg20.Foo), (compiler.misc.diamond.invalid.arg: E, (compiler.misc.diamond: Neg20.Foo))
+1 error