6751514: Unary post-increment with type variables crash javac during lowering
authormcimadamore
Thu, 09 Oct 2008 15:56:20 +0100
changeset 1468 19d266637353
parent 1467 290837884931
child 1469 9c381121f301
6751514: Unary post-increment with type variables crash javac during lowering Summary: Lower.abstractRval should take into account parenthesized expressions Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java
langtools/src/share/classes/com/sun/tools/javac/comp/TransTypes.java
langtools/test/tools/javac/generics/T6751514.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Tue Oct 07 15:39:19 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Thu Oct 09 15:56:20 2008 +0100
@@ -1884,6 +1884,9 @@
                     }
                 });
         }
+        case JCTree.TYPECAST: {
+            return abstractLval(((JCTypeCast)lval).expr, builder);
+        }
         }
         throw new AssertionError(lval);
     }
@@ -2713,10 +2716,7 @@
             // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
             // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
             // (but without recomputing x)
-            JCTree arg = (tree.lhs.getTag() == JCTree.TYPECAST)
-                ? ((JCTypeCast)tree.lhs).expr
-                : tree.lhs;
-            JCTree newTree = abstractLval(arg, new TreeBuilder() {
+            JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() {
                     public JCTree build(final JCTree lhs) {
                         int newTag = tree.getTag() - JCTree.ASGOffset;
                         // Erasure (TransTypes) can change the type of
@@ -2768,9 +2768,8 @@
         // or
         // translate to tmp1=lval(e); tmp2=tmp1; (typeof tree)tmp1 OP 1; tmp2
         // where OP is += or -=
-        final boolean cast = tree.arg.getTag() == JCTree.TYPECAST;
-        final JCExpression arg = cast ? ((JCTypeCast)tree.arg).expr : tree.arg;
-        return abstractLval(arg, new TreeBuilder() {
+        final boolean cast = TreeInfo.skipParens(tree.arg).getTag() == JCTree.TYPECAST;
+        return abstractLval(tree.arg, new TreeBuilder() {
                 public JCTree build(final JCTree tmp1) {
                     return abstractRval(tmp1, tree.arg.type, new TreeBuilder() {
                             public JCTree build(final JCTree tmp2) {
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/TransTypes.java	Tue Oct 07 15:39:19 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/TransTypes.java	Thu Oct 09 15:56:20 2008 +0100
@@ -623,8 +623,8 @@
     }
 
     public void visitAssignop(JCAssignOp tree) {
-        tree.lhs = translate(tree.lhs, null);
-        tree.rhs = translate(tree.rhs, erasure(tree.rhs.type));
+        tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
+        tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
         tree.type = erasure(tree.type);
         result = tree;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/T6751514.java	Thu Oct 09 15:56:20 2008 +0100
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug     6751514
+ * @summary Unary post-increment with type variables crash javac during lowering
+ * @author Maurizio Cimadamore
+ */
+
+public class T6751514 {
+
+    static class Foo<X> {
+        X x;
+        Foo (X x) {
+            this.x = x;
+        }
+    }
+
+    static void test1(Foo<Integer> foo) {
+        int start = foo.x;
+        equals(foo.x += 1, start + 1);
+        equals(foo.x++, start + 1);
+        equals(++foo.x, start + 3);
+        equals(foo.x--, start + 3);
+        equals(foo.x -= 1, start + 1);
+        equals(--foo.x, start);
+    }
+
+    static void test2(Foo<Integer> foo) {
+        int start = foo.x;
+        equals((foo.x) += 1, start + 1);
+        equals((foo.x)++, start + 1);
+        equals(++(foo.x), start + 3);
+        equals((foo.x)--, start + 3);
+        equals((foo.x) -= 1, start + 1);
+        equals(--(foo.x), start);
+    }
+
+    static void test3(Foo<Integer> foo) {
+        int start = foo.x;
+        equals(((foo.x)) += 1, start + 1);
+        equals(((foo.x))++, start + 1);
+        equals(++((foo.x)), start + 3);
+        equals(((foo.x))--, start + 3);
+        equals(((foo.x)) -= 1, start + 1);
+        equals(--((foo.x)), start);
+    }
+
+    public static void main(String[] args) {
+        test1(new Foo<Integer>(1));
+        test2(new Foo<Integer>(1));
+        test3(new Foo<Integer>(1));
+    }
+
+    static void equals(int found, int req) {
+        if (found != req) {
+            throw new AssertionError("Error (expected: "+ req +
+                                     " - found: " + found + ")");
+        }
+    }
+}
\ No newline at end of file