8222169: java.lang.AssertionError switch expression in ternary operator - ?
Ensure the stack size recoded at the begining of the let expression is the correct one. Co-authored-by: Vicente Romero <vicente.romero@oracle.com> Reviewed-by: vromero
This commit is contained in:
parent
6e7ff5e656
commit
b32a840e53
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm
test/langtools/tools/javac
@ -2309,6 +2309,8 @@ public class Gen extends JCTree.Visitor {
|
||||
}
|
||||
|
||||
public void visitLetExpr(LetExpr tree) {
|
||||
code.resolvePending();
|
||||
|
||||
int limit = code.nextreg;
|
||||
int prevLetExprStart = code.setLetExprStackPos(code.state.stacksize);
|
||||
try {
|
||||
|
@ -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; }
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -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 @@ public class ExpressionSwitch {
|
||||
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 @@ public class ExpressionSwitch {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user