changeset 1240:d0c2cfbab151

Bug fixes for target-typing in conditional expressions under complexinference mode. *) Problems when calling varargs method with conditional argument *) Problems when calling generic method with conditional argument *) Fixed boxing routine to work with poly types *) Updated some tests to be Windows-friendly
author mcimadamore
date Wed, 07 Dec 2011 18:39:33 +0000
parents 2cf14bcc7aa6
children db92b1be943f
files src/share/classes/com/sun/tools/javac/code/Types.java src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples.not-yet.txt test/tools/javac/lambda/Conditional01.java test/tools/javac/lambda/Conditional02.java test/tools/javac/lambda/Conditional03.java test/tools/javac/lambda/LambdaConv21.java test/tools/javac/lambda/MethodReference34.java
diffstat 9 files changed, 157 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Dec 06 17:03:20 2011 -0800
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Wed Dec 07 18:39:33 2011 +0000
@@ -3598,6 +3598,9 @@
      * Return the class that boxes the given primitive.
      */
     public ClassSymbol boxedClass(Type t) {
+        while (t.tag == FORALL) {
+            t = ((ForAll)t).qtype;
+        }
         return reader.enterClass(syms.boxedName[t.tag]);
     }
 
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Dec 06 17:03:20 2011 -0800
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Dec 07 18:39:33 2011 +0000
@@ -1229,7 +1229,8 @@
         final ForAll fa = new ForAll(POLY_CONDITIONAL, Type.noType) {
             @Override
             public Type complete(final InferenceContext inferenceContext, final Type to, final boolean allowBoxing, final boolean check) {
-                attribExpr(tree.cond, env, syms.booleanType);
+                Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
+                attribExpr(tree.cond, localEnv, syms.booleanType);
                 
                 ForAll.Completer prevPolyCompleter = chk.setPolyCompleter(new ForAll.Completer() {
                     public Type complete(DiagnosticPosition pos, Env<AttrContext> env, ForAll t, Type pt, Warner warn) throws Infer.NoInstanceException {
@@ -1239,8 +1240,8 @@
                     }
                 });
 
-                attribExpr(tree.truepart, env, Infer.nestedPoly);
-                attribExpr(tree.falsepart, env, Infer.nestedPoly);
+                attribExpr(tree.truepart, localEnv, Infer.nestedPoly);
+                attribExpr(tree.falsepart, localEnv, Infer.nestedPoly);
 
                 chk.setPolyCompleter(prevPolyCompleter);
 
@@ -1248,6 +1249,13 @@
                                                 tree.truepart.type, tree.falsepart.type));
                 if (check) {
                     this.qtype = check(tree, typeToCheck, VAL, pkind, to);
+                } else {
+                    final Type target = inferenceContext.asUndetType(to, types);
+                    if (allowBoxing ?
+                            !types.isConvertible(typeToCheck, target) :
+                            !types.isSubtypeUnchecked(typeToCheck, target)) {
+                        throw new Infer.InvalidInstanceException(diags).setMessage("infer.incompatible.inferred.type.in.conditional", typeToCheck);
+                    }
                 }
                 return typeToCheck;
             }
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Dec 06 17:03:20 2011 -0800
+++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Dec 07 18:39:33 2011 +0000
@@ -610,6 +610,9 @@
     second operand: {0}\n\
     third operand : {1}
 
+compiler.misc.infer.incompatible.inferred.type.in.conditional=\
+    incompatible inferred type {0} for conditional expression
+
 compiler.misc.infer.incompatible.ret.types.in.lambda=\
     incompatible return type {0} in lambda expression
 
--- a/test/tools/javac/diags/examples.not-yet.txt	Tue Dec 06 17:03:20 2011 -0800
+++ b/test/tools/javac/diags/examples.not-yet.txt	Wed Dec 07 18:39:33 2011 +0000
@@ -159,6 +159,7 @@
 compiler.misc.type.diamond                                                       #LAMBDA
 compiler.misc.type.conditional                                                   #LAMBDA
 compiler.misc.bad.enclosing.method                                               #LAMBDA
+compiler.misc.infer.incompatible.inferred.type.in.conditional                    #LAMBDA
 compiler.misc.infer.incompatible.ret.types.in.lambda                             #LAMBDA
 compiler.misc.infer.incompatible.arg.types.in.lambda                             #LAMBDA
 compiler.misc.infer.incompatible.thrown.types.in.lambda                          #LAMBDA
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/Conditional01.java	Wed Dec 07 18:39:33 2011 +0000
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary conditional and varargs
+ * @compile -XDcomplexinference Conditional01.java
+ */
+
+import java.util.*;
+
+class Conditional01 {
+    void varargs(Object ... args) {  }
+
+    void test(boolean flag, List<String> ls) {
+       varargs(flag ? "" : ls);
+       varargs(null, flag ? "" : ls);
+       varargs(flag ? "" : ls());
+       varargs(null, flag ? "" : ls());
+    }
+
+    List<String> ls() { return null; }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/Conditional02.java	Wed Dec 07 18:39:33 2011 +0000
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary inference and conditionals
+ * @compile -XDcomplexinference Conditional02.java
+ */
+
+class Conditional02 {
+    
+    <Z> void m1(Z z) { }
+    <Z> void m2(Z... z) { }
+
+    void test(boolean flag) {
+        m1(flag ? "" : "");
+        m2(flag ? "" : "");
+        m2("", flag ? "" : "");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/Conditional03.java	Wed Dec 07 18:39:33 2011 +0000
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary conditionals and boxing
+ * @compile -XDcomplexinference Conditional03.java
+ */
+
+class Conditional03 {
+    
+   void m1(Object o) { }
+   void m2(int i) { }
+
+   void test(boolean cond) {
+       m1((cond ? 1 : 1));
+       m1((cond ? box(1) : box(1)));
+   }
+   
+   Integer box(int i) { return i; }
+}
--- a/test/tools/javac/lambda/LambdaConv21.java	Tue Dec 06 17:03:20 2011 -0800
+++ b/test/tools/javac/lambda/LambdaConv21.java	Wed Dec 07 18:39:33 2011 +0000
@@ -41,13 +41,13 @@
         void m();
     }
     
-    interface SAM_Void<X> {
+    interface SAM_java_lang_Void<X> {
         void m();
     }
     
     static void m_void() { assertTrue(true); }
     
-    static Void m_Void() { assertTrue(true); return null; }
+    static Void m_java_lang_Void() { assertTrue(true); return null; }
     
     public static void main(String[] args) {
         testExpressionLambda();
@@ -58,26 +58,26 @@
     static void testExpressionLambda() {
         SAM_void s1 = ()->m_void();
         s1.m();
-        SAM_Void s2 = ()->m_void();
+        SAM_java_lang_Void s2 = ()->m_void();
         s2.m();
-        SAM_void s3 = ()->m_Void();
+        SAM_void s3 = ()->m_java_lang_Void();
         s3.m();
-        SAM_Void s4 = ()->m_Void();
+        SAM_java_lang_Void s4 = ()->m_java_lang_Void();
         s4.m();
     }
     
     static void testStatementLambda() {
         SAM_void s1 = ()-> { m_void(); };
         s1.m();
-        SAM_Void s2 = ()-> { m_void(); };
+        SAM_java_lang_Void s2 = ()-> { m_void(); };
         s2.m();
-        SAM_void s3 = ()-> { return m_Void(); };
+        SAM_void s3 = ()-> { return m_java_lang_Void(); };
         s3.m();
-        SAM_Void s4 = ()-> { return m_Void(); };
+        SAM_java_lang_Void s4 = ()-> { return m_java_lang_Void(); };
         s4.m();
-        SAM_void s5 = ()-> { m_Void(); };
+        SAM_void s5 = ()-> { m_java_lang_Void(); };
         s5.m();
-        SAM_Void s6 = ()-> { m_Void(); };
+        SAM_java_lang_Void s6 = ()-> { m_java_lang_Void(); };
         s6.m();
     }
 }
--- a/test/tools/javac/lambda/MethodReference34.java	Tue Dec 06 17:03:20 2011 -0800
+++ b/test/tools/javac/lambda/MethodReference34.java	Wed Dec 07 18:39:33 2011 +0000
@@ -41,22 +41,22 @@
         void m();
     }
     
-    interface SAM_Void<X> {
+    interface SAM_java_lang_Void<X> {
         void m();
     }
     
     static void m_void() { assertTrue(true); }
     
-    static Void m_Void() { assertTrue(true); return null; }
+    static Void m_java_lang_Void() { assertTrue(true); return null; }
     
     public static void main(String[] args) {
         SAM_void s1 = MethodReference34#m_void;
         s1.m();
-        SAM_Void s2 = MethodReference34#m_void;
+        SAM_java_lang_Void s2 = MethodReference34#m_void;
         s2.m();
-        SAM_void s3 = MethodReference34#m_Void;
+        SAM_void s3 = MethodReference34#m_java_lang_Void;
         s3.m();
-        SAM_Void s4 = MethodReference34#m_Void;
+        SAM_java_lang_Void s4 = MethodReference34#m_java_lang_Void;
         s4.m();
         assertTrue(assertionCount == 4);
     }