Skip to content

Commit 194cdf4

Browse files
committed
8277864: Compilation error thrown while doing a boxing conversion on selector expression
Reviewed-by: jlaskey, vromero
1 parent f39fe5b commit 194cdf4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ private void handleSwitch(JCTree tree,
347347
}
348348
}
349349
selector = translate(selector);
350-
statements.append(make.at(tree.pos).VarDef(temp, !hasNullCase ? attr.makeNullCheck(selector)
351-
: selector));
350+
boolean needsNullCheck = !hasNullCase && !seltype.isPrimitive();
351+
statements.append(make.at(tree.pos).VarDef(temp, needsNullCheck ? attr.makeNullCheck(selector)
352+
: selector));
352353
VarSymbol index = new VarSymbol(Flags.SYNTHETIC,
353354
names.fromString(tree.pos + target.syntheticNameChar() + "index"),
354355
syms.intType,

test/langtools/tools/javac/patterns/Switches.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/*
3030
* @test
31-
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113
31+
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113 8277864
3232
* @summary Check behavior of pattern switches.
3333
* @compile --enable-preview -source ${jdk.version} Switches.java
3434
* @run main/othervm --enable-preview Switches
@@ -85,6 +85,9 @@ void run() {
8585
assertEquals(2, switchOverNull1());
8686
assertEquals(2, switchOverNull2());
8787
assertEquals(2, switchOverNull3());
88+
assertEquals(5, switchOverPrimitiveInt(0));
89+
assertEquals(7, switchOverPrimitiveInt(1));
90+
assertEquals(9, switchOverPrimitiveInt(2));
8891
}
8992

9093
void run(Function<Object, Integer> mapper) {
@@ -591,6 +594,14 @@ private int switchOverNull3() {
591594
};
592595
}
593596

597+
private int switchOverPrimitiveInt(Integer i) {
598+
return switch (i) {
599+
case 0 -> 5 + 0;
600+
case Integer j && j == 1 -> 6 + j;
601+
case Integer j -> 7 + j;
602+
};
603+
}
604+
594605
//verify that for cases like:
595606
//case ConstantClassClash ->
596607
//ConstantClassClash is interpreted as a field, not as a class

0 commit comments

Comments
 (0)