Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 756cab8

Browse files
committed
8323657: Compilation of snippet results in VerifyError at runtime with --release 9 (and above)
Backport-of: c9cacfb25d1f15c879c961d2965a63c9fe4d9fa7
1 parent ce8d1c9 commit 756cab8

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/StringConcat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static com.sun.tools.javac.code.Kinds.Kind.MTH;
3838
import static com.sun.tools.javac.code.TypeTag.*;
3939
import static com.sun.tools.javac.jvm.ByteCodes.*;
40+
import static com.sun.tools.javac.tree.JCTree.Tag.LITERAL;
4041
import static com.sun.tools.javac.tree.JCTree.Tag.PLUS;
4142
import com.sun.tools.javac.jvm.Items.*;
4243

@@ -416,7 +417,7 @@ protected void emit(JCDiagnostic.DiagnosticPosition pos, List<JCTree> args, bool
416417
for (JCTree arg : t) {
417418
Object constVal = arg.type.constValue();
418419
if ("".equals(constVal)) continue;
419-
if (arg.type == syms.botType) {
420+
if (arg.type == syms.botType && arg.hasTag(LITERAL)) {
420421
// Concat the null into the recipe right away
421422
recipe.append((String) null);
422423
} else if (constVal != null) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @compile StringConcatWithAssignments.java
27+
* @run main StringConcatWithAssignments
28+
* @compile -XDstringConcat=inline StringConcatWithAssignments.java
29+
* @run main StringConcatWithAssignments
30+
* @compile -XDstringConcat=indy StringConcatWithAssignments.java
31+
* @run main StringConcatWithAssignments
32+
* @compile -XDstringConcat=indyWithConstants StringConcatWithAssignments.java
33+
* @run main StringConcatWithAssignments
34+
*/
35+
36+
import java.util.Objects;
37+
import java.util.function.Supplier;
38+
39+
public class StringConcatWithAssignments {
40+
public static void main(String[] args) {
41+
StringConcatWithAssignments instance = new StringConcatWithAssignments();
42+
assertEquals("nulltrue", instance.assignment());
43+
assertEquals("nulltrue", instance.invocation());
44+
}
45+
private String assignment() {
46+
boolean b;
47+
return ((((b = true) ? null : null) + "") + b);
48+
}
49+
private String invocation() {
50+
StringBuilder sideEffect = new StringBuilder();
51+
Supplier<Boolean> provider = () -> {
52+
sideEffect.append(true);
53+
return true;
54+
};
55+
return (((provider.get() ? null : null) + "") + sideEffect.toString());
56+
}
57+
private static void assertEquals(Object o1, Object o2) {
58+
if (!Objects.equals(o1, o2)) {
59+
throw new AssertionError("Expected that '" + o1 + "' and " +
60+
"'" + o2 + "' are equal.");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)