Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ private void handleSwitch(JCTree tree,
}
}
selector = translate(selector);
statements.append(make.at(tree.pos).VarDef(temp, !hasNullCase ? attr.makeNullCheck(selector)
: selector));
boolean needsNullCheck = !hasNullCase && !seltype.isPrimitive();
statements.append(make.at(tree.pos).VarDef(temp, needsNullCheck ? attr.makeNullCheck(selector)
: selector));
VarSymbol index = new VarSymbol(Flags.SYNTHETIC,
names.fromString(tree.pos + target.syntheticNameChar() + "index"),
syms.intType,
Expand Down
13 changes: 12 additions & 1 deletion test/langtools/tools/javac/patterns/Switches.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/*
* @test
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113 8277864
* @summary Check behavior of pattern switches.
* @compile --enable-preview -source ${jdk.version} Switches.java
* @run main/othervm --enable-preview Switches
Expand Down Expand Up @@ -85,6 +85,9 @@ void run() {
assertEquals(2, switchOverNull1());
assertEquals(2, switchOverNull2());
assertEquals(2, switchOverNull3());
assertEquals(5, switchOverPrimitiveInt(0));
assertEquals(7, switchOverPrimitiveInt(1));
assertEquals(9, switchOverPrimitiveInt(2));
}

void run(Function<Object, Integer> mapper) {
Expand Down Expand Up @@ -591,6 +594,14 @@ private int switchOverNull3() {
};
}

private int switchOverPrimitiveInt(Integer i) {
return switch (i) {
case 0 -> 5 + 0;
case Integer j && j == 1 -> 6 + j;
case Integer j -> 7 + j;
};
}

//verify that for cases like:
//case ConstantClassClash ->
//ConstantClassClash is interpreted as a field, not as a class
Expand Down