<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
The reason is that according to Casting Contexts/Section 5.5 (or Testing Contexts in JDK 23/Section 5.7 which is very similar to casting contexts) not only it is not unconditionally exact but there is no conversion at all that can support byte to Integer. If
there were one, we would see a bullet that says: a widening primitive conversion followed by boxing. </div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
The permitted conversions are the ones listed in Chapter 5.5 or 5.7 in the accompanying spec diff –
<a href="https://cr.openjdk.org/~abimpoudis/instanceof/jep455-20240424/specs/instanceof-jls.html#jls-5.7">
https://cr.openjdk.org/~abimpoudis/instanceof/jep455-20240424/specs/instanceof-jls.html#jls-5.7</a>.</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
This is also evident by trying to do the cast in JShell:</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
jshell> byte b = (byte) 42</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
b ==> 42</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
jshell> (Integer) b</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
| Error:</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
| incompatible types: byte cannot be converted to java.lang.Integer</div>
<div style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
| (Integer) b</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
| ^</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
Note, that while dominance checks the relation between two case labels, those two case labels can be independently applicable to the selector type. </div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> David Alayachew <davidalayachew@gmail.com><br>
<b>Sent:</b> 21 May 2024 00:12<br>
<b>To:</b> Aggelos Biboudis <abimpoudis@openjdk.org><br>
<b>Cc:</b> compiler-dev <compiler-dev@openjdk.org><br>
<b>Subject:</b> Re: RFR: 8332463: Byte conditional pattern case element dominates short constant case element</font>
<div> </div>
</div>
<div>
<div dir="auto">
<div>I understand the first 2 cases, but not the third. How is byte -> Integer not unconditionally exact? What possible scenario could occur where one would lose data?<br>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Mon, May 20, 2024, 4:31 AM Aggelos Biboudis <<a href="mailto:abimpoudis@openjdk.org">abimpoudis@openjdk.org</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
It seems that the compiler introduced a rule that does not exist in the spec. The fix is simple, and it will fix the behaviour of JDK 23 according to the spec. For example the following is accepted by JDK 22 and needs to continue to be accepted by JDK 23:<br>
<br>
<br>
public static int test() {<br>
Byte i = (byte) 42;<br>
return switch (i) {<br>
case Byte ib -> 1;<br>
case (short) 0 -> 2; // OK - not dominated<br>
};<br>
}<br>
<br>
<br>
Similarly for primitive type patterns:<br>
<br>
<br>
public static int test() {<br>
Byte i = (byte) 42;<br>
return switch (i) {<br>
case Byte ib -> 1;<br>
case short s -> 2; // Also not dominated since there is no unconditionally exact conversion from short to Byte<br>
};<br>
}<br>
<br>
public static int test() {<br>
int i = 42;<br>
return switch (i) {<br>
case Integer ib -> 1;<br>
case byte ip -> 2; // Also not dominated since there is no unconditionally exact conversion from byte to Integer<br>
};<br>
}<br>
<br>
-------------<br>
<br>
Commit messages:<br>
- 8332463: Byte conditional pattern case element dominates short constant case element<br>
<br>
Changes: <a href="https://git.openjdk.org/jdk/pull/19301/files" rel="noreferrer noreferrer" target="_blank">
https://git.openjdk.org/jdk/pull/19301/files</a><br>
Webrev: <a href="https://webrevs.openjdk.org/?repo=jdk&pr=19301&range=00" rel="noreferrer noreferrer" target="_blank">
https://webrevs.openjdk.org/?repo=jdk&pr=19301&range=00</a><br>
Issue: <a href="https://bugs.openjdk.org/browse/JDK-8332463" rel="noreferrer noreferrer" target="_blank">
https://bugs.openjdk.org/browse/JDK-8332463</a><br>
Stats: 104 lines in 5 files changed: 94 ins; 7 del; 3 mod<br>
Patch: <a href="https://git.openjdk.org/jdk/pull/19301.diff" rel="noreferrer noreferrer" target="_blank">
https://git.openjdk.org/jdk/pull/19301.diff</a><br>
Fetch: git fetch <a href="https://git.openjdk.org/jdk.git" rel="noreferrer noreferrer" target="_blank">
https://git.openjdk.org/jdk.git</a> pull/19301/head:pull/19301<br>
<br>
PR: <a href="https://git.openjdk.org/jdk/pull/19301" rel="noreferrer noreferrer" target="_blank">
https://git.openjdk.org/jdk/pull/19301</a><br>
</blockquote>
</div>
</div>
</div>
</div>
</body>
</html>