patterns: run Guards, NullCase and MatchException demos on 21 too; add switch-on-long check

Adds broken/SwitchOnLong.java and regenerates transcripts so every 'runs on 21, 25 and 27' claim has a transcript. Additive commit.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
This commit is contained in:
Claude
2026-09-24 11:54:48 +00:00
parent bc91b9c8d0
commit 65a4bbbbe3
6 changed files with 52 additions and 3 deletions
+10
View File
@@ -0,0 +1,10 @@
public class SwitchOnLong {
public static void main(String[] args) {
long n = 1L;
String s = switch (n) { // switch on long: not allowed without the preview
case 0L -> "zero";
default -> "other";
};
System.out.println(s);
}
}
@@ -1,3 +1,11 @@
$ javac src/Guards.java && java Guards (21.0.10+7-Ubuntu-124.04)
empty string
short string: abc
long string: abcdefgh
negative -4
int 12
other
$ javac src/Guards.java && java Guards (25.0.4.1+1-LTS) $ javac src/Guards.java && java Guards (25.0.4.1+1-LTS)
empty string empty string
short string: abc short string: abc
+4
View File
@@ -22,6 +22,10 @@ box of string hi
box of int 7 box of int 7
box of 2.5 box of 2.5
$ javac src/MatchExceptionDemo.java && java MatchExceptionDemo (21.0.10+7-Ubuntu-124.04)
MatchException: java.lang.IllegalStateException: accessor blew up
cause: java.lang.IllegalStateException: accessor blew up
$ javac src/MatchExceptionDemo.java && java MatchExceptionDemo (25.0.4.1+1-LTS) $ javac src/MatchExceptionDemo.java && java MatchExceptionDemo (25.0.4.1+1-LTS)
MatchException: java.lang.IllegalStateException: accessor blew up MatchException: java.lang.IllegalStateException: accessor blew up
cause: java.lang.IllegalStateException: accessor blew up cause: java.lang.IllegalStateException: accessor blew up
+6
View File
@@ -1,3 +1,9 @@
$ javac src/NullCase.java && java NullCase (21.0.10+7-Ubuntu-124.04)
without case null: NullPointerException
with case null: null!
case null, default: null or something else
instanceof with null: false
$ javac src/NullCase.java && java NullCase (25.0.4.1+1-LTS) $ javac src/NullCase.java && java NullCase (25.0.4.1+1-LTS)
without case null: NullPointerException without case null: NullPointerException
with case null: null! with case null: null!
+20
View File
@@ -12,6 +12,26 @@ preview/PrimitiveInstanceof.java:3: error: primitive patterns are a preview feat
(use --enable-preview to enable primitive patterns) (use --enable-preview to enable primitive patterns)
1 error 1 error
$ javac broken/SwitchOnLong.java (21.0.10+7-Ubuntu-124.04)
broken/SwitchOnLong.java:4: error: selector type long is not allowed
String s = switch (n) { // switch on long: not allowed without the preview
^
1 error
$ javac broken/SwitchOnLong.java (25.0.4.1+1-LTS)
broken/SwitchOnLong.java:4: error: primitive patterns are a preview feature and are disabled by default.
String s = switch (n) { // switch on long: not allowed without the preview
^
(use --enable-preview to enable primitive patterns)
1 error
$ javac broken/SwitchOnLong.java (27+35)
broken/SwitchOnLong.java:4: error: primitive patterns are a preview feature and are disabled by default.
String s = switch (n) { // switch on long: not allowed without the preview
^
(use --enable-preview to enable primitive patterns)
1 error
$ javac --enable-preview --release 25 preview/PrimitiveInstanceof.java && java --enable-preview PrimitiveInstanceof (25.0.4.1+1-LTS) $ javac --enable-preview --release 25 preview/PrimitiveInstanceof.java && java --enable-preview PrimitiveInstanceof (25.0.4.1+1-LTS)
100 fits in a byte: 100 100 fits in a byte: 100
1000 fits in a short: 1000 1000 fits in a short: 1000
+4 -3
View File
@@ -31,10 +31,10 @@ all3() { local fn="$1"; shift; [ -n "$JDK21" ] && { "$fn" "$JDK21" "$@"; echo; }
{ all3 cr src/TypePatterns.java TypePatterns; } > "$OUT/01-type-patterns.txt" { all3 cr src/TypePatterns.java TypePatterns; } > "$OUT/01-type-patterns.txt"
# 02 - guards and dominance # 02 - guards and dominance
{ both cr src/Guards.java Guards; echo; both co broken/Dominated.java; echo; both co broken/GuardBeforeGeneral.java; } > "$OUT/02-guards-and-dominance.txt" { all3 cr src/Guards.java Guards; echo; both co broken/Dominated.java; echo; both co broken/GuardBeforeGeneral.java; } > "$OUT/02-guards-and-dominance.txt"
# 03 - record patterns, and what happens when an accessor throws # 03 - record patterns, and what happens when an accessor throws
{ all3 cr src/RecordPatterns.java RecordPatterns; echo; both cr src/MatchExceptionDemo.java MatchExceptionDemo; } > "$OUT/03-record-patterns.txt" { all3 cr src/RecordPatterns.java RecordPatterns; echo; all3 cr src/MatchExceptionDemo.java MatchExceptionDemo; } > "$OUT/03-record-patterns.txt"
# 04 - exhaustiveness: the compiler's check, and what separate compilation can still break # 04 - exhaustiveness: the compiler's check, and what separate compilation can still break
{ all3 cr src/SealedExhaustive.java SealedExhaustive; echo { all3 cr src/SealedExhaustive.java SealedExhaustive; echo
@@ -51,10 +51,11 @@ all3() { local fn="$1"; shift; [ -n "$JDK21" ] && { "$fn" "$JDK21" "$@"; echo; }
done; } > "$OUT/05-separate-compilation.txt" done; } > "$OUT/05-separate-compilation.txt"
# 06 - null and unnamed patterns # 06 - null and unnamed patterns
{ both cr src/NullCase.java NullCase; echo; all3 cr src/Unnamed.java Unnamed; } > "$OUT/06-null-and-unnamed.txt" { all3 cr src/NullCase.java NullCase; echo; all3 cr src/Unnamed.java Unnamed; } > "$OUT/06-null-and-unnamed.txt"
# 07 - primitive patterns (preview) # 07 - primitive patterns (preview)
{ for f in PrimitiveInstanceof; do both co preview/$f.java; done; echo { for f in PrimitiveInstanceof; do both co preview/$f.java; done; echo
all3 co broken/SwitchOnLong.java; echo
for f in PrimitiveInstanceof PrimitiveSwitch PrimitiveInRecord PrimitiveEdges; do both crp preview/$f.java $f; echo; done for f in PrimitiveInstanceof PrimitiveSwitch PrimitiveInRecord PrimitiveEdges; do both crp preview/$f.java $f; echo; done
both crp preview/BoxedToNarrower.java BoxedToNarrower; } > "$OUT/07-primitive-patterns.txt" both crp preview/BoxedToNarrower.java BoxedToNarrower; } > "$OUT/07-primitive-patterns.txt"
{ o="$(mk)"; "$JDK25/bin/javac" --enable-preview --release 25 -d "$o" preview/PrimitiveSwitch.java 2>&1 | grep -v '^Note:' { o="$(mk)"; "$JDK25/bin/javac" --enable-preview --release 25 -d "$o" preview/PrimitiveSwitch.java 2>&1 | grep -v '^Note:'