8222169: java.lang.AssertionError switch expression in ternary operator - ?
authorjlahoda
Thu, 16 May 2019 10:52:36 +0200
changeset 54894 382101e97784
parent 54893 3e1ecfd3ea18
child 54895 4f1f939d8f5d
8222169: java.lang.AssertionError switch expression in ternary operator - ? Summary: Ensure the stack size recoded at the begining of the let expression is the correct one. Reviewed-by: vromero Contributed-by: vicente.romero@oracle.com, jan.lahoda@oracle.com
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
test/langtools/tools/javac/T8222795/ConditionalAndPostfixOperator.java
test/langtools/tools/javac/switchexpr/ExpressionSwitch-old.out
test/langtools/tools/javac/switchexpr/ExpressionSwitch.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon May 13 07:41:32 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu May 16 10:52:36 2019 +0200
@@ -2309,6 +2309,8 @@
     }
 
     public void visitLetExpr(LetExpr tree) {
+        code.resolvePending();
+
         int limit = code.nextreg;
         int prevLetExprStart = code.setLetExprStackPos(code.state.stacksize);
         try {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8222795/ConditionalAndPostfixOperator.java	Thu May 16 10:52:36 2019 +0200
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019, 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
+ * @bug 8222169
+ * @summary post inc operator inside compute function of HashMap results in Exception
+ * @compile ConditionalAndPostfixOperator.java
+ * @run main ConditionalAndPostfixOperator
+ */
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+public class ConditionalAndPostfixOperator {
+    public static void main(String... args) {
+        Map<String, Integer> m = new HashMap<>();
+        String key = "a";
+        m.put(key, val());
+        assertEquals(2, m.compute(key, (k, v) -> (v > 5) ? v-- : v++));
+
+        Integer v = val();
+
+        assertEquals(2, (v > 5) ? v-- : v++);
+        assertEquals(3, v);
+    }
+
+    static void assertEquals(Integer expected, Integer actual) {
+        if (!Objects.equals(expected, actual)) {
+            throw new AssertionError("Expected: " + expected + ", " +
+                                     "actual: " + actual);
+        }
+    }
+
+    static int val() { return 2; }
+
+}
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitch-old.out	Mon May 13 07:41:32 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitch-old.out	Thu May 16 10:52:36 2019 +0200
@@ -1,4 +1,4 @@
-ExpressionSwitch.java:38:16: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.expressions)
-ExpressionSwitch.java:39:20: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.rules)
-ExpressionSwitch.java:89:21: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.multiple.case.labels)
+ExpressionSwitch.java:39:16: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.expressions)
+ExpressionSwitch.java:40:20: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.rules)
+ExpressionSwitch.java:92:31: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.multiple.case.labels)
 3 errors
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitch.java	Mon May 13 07:41:32 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitch.java	Thu May 16 10:52:36 2019 +0200
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 8206986
+ * @bug 8206986 8222169
  * @summary Check expression switch works.
  * @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
  * @compile --enable-preview -source ${jdk.version} ExpressionSwitch.java
@@ -27,6 +27,7 @@
         assertEquals(convert1("B"), 0);
         assertEquals(convert1("C"), 1);
         assertEquals(convert1(""), -1);
+        assertEquals(convert1(null), -2);
         assertEquals(convert2("A"), 0);
         assertEquals(convert2("B"), 0);
         assertEquals(convert2("C"), 1);
@@ -85,11 +86,13 @@
     }
 
     private int convert1(String s) {
-        return switch (s) {
-            case "A", "B" -> 0;
-            case "C" -> { break 1; }
-            default -> -1;
-        };
+        return s == null
+                ? -2
+                : switch (s) {
+                      case "A", "B" -> 0;
+                      case "C" -> { break 1; }
+                      default -> -1;
+                  };
     }
 
     private int convert2(String s) {