<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 style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
Hello hello :-)
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0 elementToProof">I agree with you that both cases should not be valid. Regardless of the presence of a guard, if intra-case dominance is violated, the switch needs to be rejected. This is a compiler bug. Good catch
<span id="🚀">🚀</span></div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0">I think, the intra-case dominance is covered by the following rule at the spec (pasting):</div>
<blockquote itemscope="" itemtype="https://schemas.microsoft.com/QuotedText" style="border-left: 3px solid rgb(200, 200, 200); border-top-color: rgb(200, 200, 200); border-right-color: rgb(200, 200, 200); border-bottom-color: rgb(200, 200, 200); padding-left: 1ex; margin-left: 0.8ex; color: rgb(102, 102, 102);">
It is a compile-time error if in a switch block there is a case label with case patterns p1,...,pn (n > 1) where one of the patterns pi (1 ≤ i < n) dominates another of the patterns pj (i < j ≤ n).<br>
</blockquote>
<div class="elementToProof">Is this what you had in mind?</div>
<div class="elementToProof"><br>
</div>
<div class="elementToProof">Many thanks and best regards,</div>
<div class="elementToProof">Angelos</div>
<div class="elementToProof"><br>
</div>
</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> amber-spec-experts <amber-spec-experts-retn@openjdk.org> on behalf of Tagir Valeev <amaembo@gmail.com><br>
<b>Sent:</b> 18 August 2023 11:48<br>
<b>To:</b> amber-spec-experts <amber-spec-experts@openjdk.org><br>
<b>Subject:</b> [patterns] Domination in guarded multiple pattern labels</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hello!<br>
<br>
Consider the following code:<br>
<br>
public class Test {<br>
void test(Object obj) {<br>
switch (obj) {<br>
case Integer _, CharSequence _, String _ when obj.hashCode() > 0 -> {<br>
}<br>
default -> throw new IllegalStateException("Unexpected<br>
value: " + obj);<br>
}<br>
}<br>
}<br>
<br>
Javac compiles it, despite the `String _` pattern being unreachable.<br>
It looks like the spec [1] allows it. It speaks about domination of<br>
case labels, but not about the domination of individual patterns<br>
within the case label. I think this part should be improved. Namely,<br>
for several patterns under the single case label, domination rules<br>
must apply, and the guard must be ignored.<br>
<br>
It's interesting that if we remove the guard, the compilation fails:<br>
<br>
Test.java:4:45<br>
java: this case label is dominated by a preceding case label<br>
<br>
While formally according to the spec, this should be accepted, as spec<br>
in its current shape says us only about 'preceding case label', and<br>
there's no preceding case label at all, so the first label should not<br>
be dominated.<br>
<br>
With best regards,<br>
Tagir Valeev<br>
<br>
[1] <a href="https://cr.openjdk.org/~abimpoudis/unnamed/jep443-20230322/specs/unnamed-jls.html#jls-14.11.1">
https://cr.openjdk.org/~abimpoudis/unnamed/jep443-20230322/specs/unnamed-jls.html#jls-14.11.1</a><br>
</div>
</span></font></div>
</body>
</html>