Skip to content

Commit 2aec910

Browse files
committedApr 5, 2023
8304883: Record Deconstruction causes bytecode error
Reviewed-by: vromero
1 parent 0e0c022 commit 2aec910

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -536,19 +536,6 @@ private void handleSwitch(JCTree tree,
536536
JCExpression test = (JCExpression) this.<JCTree>translate(label.pat);
537537
if (label.guard != null) {
538538
JCExpression guard = translate(label.guard);
539-
if (hasJoinedNull) {
540-
JCPattern pattern = label.pat;
541-
while (pattern instanceof JCParenthesizedPattern parenthesized) {
542-
pattern = parenthesized.pattern;
543-
}
544-
Assert.check(pattern.hasTag(Tag.BINDINGPATTERN));
545-
BindingSymbol binding = (BindingSymbol) ((JCBindingPattern) pattern).var.sym;
546-
guard = makeBinary(Tag.OR,
547-
makeBinary(Tag.EQ,
548-
make.Ident(bindingContext.getBindingFor(binding)),
549-
makeNull()),
550-
guard);
551-
}
552539
test = makeBinary(Tag.AND, test, guard);
553540
}
554541
c.stats = translate(c.stats);

‎test/langtools/tools/javac/patterns/DeconstructionDesugaring.java

+53-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,12 +23,13 @@
2323

2424
/**
2525
* @test
26-
* @bug 8291769 8301858 8304694
26+
* @bug 8291769 8301858 8304694 8304883
2727
* @summary Verify more complex switches work properly
2828
* @compile --enable-preview -source ${jdk.version} DeconstructionDesugaring.java
2929
* @run main/othervm --enable-preview DeconstructionDesugaring
3030
*/
3131

32+
import java.util.Objects;
3233
import java.util.function.ToIntFunction;
3334
public class DeconstructionDesugaring {
3435

@@ -45,8 +46,21 @@ private void test() {
4546
assertEquals(runCheckExpressionWithUnconditional1(new R5(null)), 3);
4647
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(42)), 1);
4748
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(new Object())), 2);
48-
assertEquals(runFallThrough(new R6(1, 1)), 0);
49-
assertEquals(runFallThrough(new R6(0, 0)), 1);
49+
assertEquals(switchNullable1(new R6(0, 0)), "int: 0, int: 0");
50+
assertEquals(switchNullable1(new R6(0L, 0)), "obj: 0, obj: 0");
51+
assertEquals(switchNullable2(new R6(0, 0)), "int: 0, int: 0");
52+
assertEquals(switchNullable2(new R6(0L, 0)), "obj: 0, int: 0");
53+
assertEquals(switchNullable2(new R6(0, 0L)), "int: 0, obj: 0");
54+
assertEquals(switchNullable2(new R6(0L, 0L)), "obj: 0, obj: 0");
55+
assertEquals(switchNullableNPE(new R6(1, 1)), "obj: 1, obj: 1");
56+
try {
57+
switchNullableNPE(new R6(null, 1));
58+
throw new AssertionError("Expected NPE, but got none.");
59+
} catch (NullPointerException ex) {
60+
//expected.
61+
}
62+
assertEquals(runFallThrough(new R7(1, 1)), 0);
63+
assertEquals(runFallThrough(new R7(0, 0)), 1);
5064
}
5165

5266
private void test(ToIntFunction<Object> task) {
@@ -116,10 +130,33 @@ case R1(Object o):
116130
}
117131
}
118132

119-
public static int runFallThrough(R6 r) {
133+
private String switchNullable1(R6 r) {
134+
return switch (r) {
135+
case R6(Integer i1, Integer i2) -> "int: " + i1 + ", int: " + i2;
136+
case R6(Object o1, Object o2) -> "obj: " + o1 + ", obj: " + o2;
137+
};
138+
}
139+
140+
private String switchNullable2(R6 r) {
141+
return switch (r) {
142+
case R6(Integer i1, Integer i2) -> "int: " + i1 + ", int: " + i2;
143+
case R6(Integer i1, Object o2) -> "int: " + i1 + ", obj: " + o2;
144+
case R6(Object o1, Integer i2) -> "obj: " + o1 + ", int: " + i2;
145+
case R6(Object o1, Object o2) -> "obj: " + o1 + ", obj: " + o2;
146+
};
147+
}
148+
149+
private String switchNullableNPE(R6 r) {
150+
return switch (r) {
151+
case R6(Object o1, Object o2) when ((int) o1) == 0 && ((int) o2) == 0 -> "int: " + o1 + ", int: " + o2;
152+
case R6(Object o1, Object o2) -> "obj: " + o1 + ", obj: " + o2;
153+
};
154+
}
155+
156+
public static int runFallThrough(R7 r) {
120157
switch (r) {
121-
case R6(var v1, var v2) when v1 != 0: return 0;
122-
case R6(var v1, var v2):
158+
case R7(var v1, var v2) when v1 != 0: return 0;
159+
case R7(var v1, var v2):
123160
}
124161
return 1;
125162
}
@@ -134,6 +171,13 @@ private void assertEquals(int expected, int actual) {
134171
}
135172
}
136173

174+
private void assertEquals(String expected, String actual) {
175+
if (!Objects.equals(expected, actual)) {
176+
throw new AssertionError("expected: " + expected + ", " +
177+
"actual: " + actual);
178+
}
179+
}
180+
137181
record R1(Object o) {}
138182
record R2(Object o) {}
139183
record R3(Object o) {}
@@ -145,5 +189,6 @@ final class Sub3 extends Super {}
145189

146190
record R4(Super o) {}
147191
record R5(R4 o) {}
148-
record R6(int i1, int i2) {}
192+
record R6(Object o1, Object o2) {}
193+
record R7(int i1, int i2) {}
149194
}

0 commit comments

Comments
 (0)
Please sign in to comment.