java-topology/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchConstants.java
russell@unturf.com 0a580b313d undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
2026-03-26 17:11:57 -04:00

71 lines
1.8 KiB
Java

/*
* @test /nodynamiccopyright/
* @summary Retain exhaustiveness properties of switches with a constant selector
* @enablePreview
* @compile/fail/ref=PrimitivePatternsSwitchConstants.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDexhaustivityMaxBaseChecks=0 PrimitivePatternsSwitchConstants.java
*/
public class PrimitivePatternsSwitchConstants {
void testConstExpressions() {
switch (42) { // error: not exhaustive
case byte _ :
}
switch (42l) { // error: not exhaustive
case byte _ :
}
switch (123456) { // error: not exhaustive
case byte _ :
}
switch (16_777_216) { // error: not exhaustive
case float _ :
}
switch (16_777_217) { // error: not exhaustive
case float _ :
}
switch (42d) { // error: not exhaustive
case float _ :
}
switch (1) { // OK
case long _ :
}
final int i = 42;
switch (i) { // OK
case long _ :
}
switch (1) { // error: non-exhaustive
case Long _ : // error: widening primitive conversion and boxing is not supported
}
switch (42) {
case byte bb -> {}
case int ii -> {} // OK
};
switch (42) {
case 42 -> {}
case int ii -> {} // OK
};
switch (42) {
case (byte) 42 -> {}
case int ii -> {} // OK
};
switch (42) {
case 42 -> {}
default -> {} // OK
};
switch (42) {
default -> {} // OK
case 42 -> {}
};
}
}