<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>Hello,<br data-mce-bogus="1"></div><div>I think it can be reduced to</div><div><div style="background-color: #ffffff; color: #080808;" data-mce-style="background-color: #ffffff; color: #080808;"><pre style="font-family: 'JetBrains Mono',monospace; font-size: 9.8pt;" data-mce-style="font-family: 'JetBrains Mono',monospace; font-size: 9.8pt;"><span style="color: #0033b3;" data-mce-style="color: #0033b3;">public enum </span><span style="color: #000000;" data-mce-style="color: #000000;">Action </span>{ <span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">IGNORE</span>, <span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">REJECT </span>}</pre></div></div><div><div style="background-color: #ffffff; color: #080808;" data-mce-style="background-color: #ffffff; color: #080808;"><pre style="font-family: 'JetBrains Mono',monospace; font-size: 9.8pt;" data-mce-style="font-family: 'JetBrains Mono',monospace; font-size: 9.8pt;"><span style="color: #0033b3;" data-mce-style="color: #0033b3;">private static void </span><span style="color: #00627a;" data-mce-style="color: #00627a;">bad</span>() {<br> <span style="color: #000000;" data-mce-style="color: #000000;">String s</span>;<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">switch </span>(<span style="font-style: italic;" data-mce-style="font-style: italic;">getAction</span>()) {<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">case </span><span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">IGNORE</span>:<br> <span style="color: #000000;" data-mce-style="color: #000000;">s </span>= <span style="color: #067d17;" data-mce-style="color: #067d17;">"foo"</span>;<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">break</span>;<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">case </span><span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">REJECT</span>:<span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;"><br></span><span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;" data-mce-style="color: #0033b3;">throw new </span>RuntimeException(<span style="color: #067d17;" data-mce-style="color: #067d17;">"REJECTED"</span>);<br> };<br> <span style="color: #000000;" data-mce-style="color: #000000;">System</span>.<span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">out</span>.println(<span style="color: #000000;" data-mce-style="color: #000000;">s</span>); <span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;">// <------- variable s might not have been initialized<br></span>}<br><br><span style="color: #0033b3;" data-mce-style="color: #0033b3;">private static void </span><span style="color: #00627a;" data-mce-style="color: #00627a;">ok</span>() {<br> <span style="color: #000000;" data-mce-style="color: #000000;">String s </span>= <span style="color: #0033b3;" data-mce-style="color: #0033b3;">switch </span>(<span style="font-style: italic;" data-mce-style="font-style: italic;">getAction</span>()) {<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">case </span><span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">IGNORE</span>:<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">yield </span><span style="color: #067d17;" data-mce-style="color: #067d17;">"foo"</span>;<br> <span style="color: #0033b3;" data-mce-style="color: #0033b3;">case </span><span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">REJECT</span>:<span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;"><br></span><span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;"> </span><span style="color: #0033b3;" data-mce-style="color: #0033b3;">throw new </span>RuntimeException(<span style="color: #067d17;" data-mce-style="color: #067d17;">"REJECTED"</span>);<br> };<br> <span style="color: #000000;" data-mce-style="color: #000000;">System</span>.<span style="color: #871094; font-style: italic;" data-mce-style="color: #871094; font-style: italic;">out</span>.println(<span style="color: #000000;" data-mce-style="color: #000000;">s</span>); <span style="color: #8c8c8c; font-style: italic;" data-mce-style="color: #8c8c8c; font-style: italic;">// ok !<br></span>}</pre></div></div><div><br></div><div>so there is a bug in DA/DU rules and the workaround is to use a switch expression.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Gavin Bierman" <gavin.bierman@oracle.com><br><b>To: </b>"Jean-Noël Rouvignac (ForgeRock)" <jean-noel.rouvignac@pingidentity.com><br><b>Cc: </b>"amber-dev" <amber-dev@openjdk.org><br><b>Sent: </b>Wednesday, December 11, 2024 1:20:15 PM<br><b>Subject: </b>Re: Enum: difference of behaviour between exhaustive switch vs. using default:<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
Could you file this as a bug, and I will take a look?
<div><br>
</div>
<div>Thanks,</div>
<div>Gavin<br id="lineBreakAtBeginningOfMessage">
<div><br>
<blockquote>
<div>On 10 Dec 2024, at 16:10, Jean-Noël Rouvignac (ForgeRock) <jean-noel.rouvignac@pingidentity.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div dir="ltr">
<div>Hello amber-dev experts!<br>
<br>
</div>
<div>I am modernizing our codebase by making it use enhanced / exhaustive switches.<br>
<br>
</div>
<div>In several places, I replaced `default: ` by `case THE_ONLY_UNUSED_ENUM_VALUE:`, except that I am hitting an unexpected difference in behaviour, at least from my point of view.
<br>
<br>
</div>
<div>I have reduced the code to the following reproducer (tested on the <a href="https://dev.java/playground/" target="_blank">
https://dev.java/playground/</a>), where `main1()` compiles, but `main2()` does not. And yet, I am under the impression both should be equivalent?<br clear="all">
</div>
<div dir="ltr" class="gmail_signature">
<table style="border-collapse:collapse;padding:0px;margin:0px">
<tbody>
<tr>
<td style="width:113px"><br>
What do you think?<br>
Thanks a lot.<br>
<br>
<br>
<br>
import java.io.IOException;<br>
<br>
class Main {<br>
public enum Action { IGNORE, REJECT }<br>
<br>
public static void main(String[] args) {<br>
main1();<br>
main2();<br>
}<br>
<br>
private static void main1() {<br>
String s;<br>
try {<br>
s = getValue();<br>
} catch (IOException e) {<br>
switch (getAction()) {<br>
case IGNORE:<br>
return;<br>
default:<br>
throw new RuntimeException("REJECTED");<br>
}<br>
}<br>
<br>
System.out.println(s);<br>
}<br>
<br>
private static void main2() {<br>
String s;<br>
try {<br>
s = getValue();<br>
} catch (IOException e) {<br>
switch (getAction()) {<br>
case IGNORE:<br>
return;<br>
case REJECT: // <------------------- Fails compilation<br>
throw new RuntimeException("REJECTED");<br>
}<br>
}<br>
<br>
System.out.println(s); // <------- Main.java:40: error: variable s might not have been initialized<br>
}<br>
<br>
static Action getAction() {<br>
return Action.IGNORE;<br>
}<br>
<br>
static String getValue() throws IOException {<br>
return "SUCCESS";<br>
}<br>
}<br>
</td>
<td><br>
</td>
</tr>
<tr>
<td colspan="2"><br>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<br>
<i style="margin:0px;padding:0px;border:0px;outline:0px;vertical-align:baseline;background:rgb(255,255,255);font-family:proxima-nova-zendesk,system-ui,-apple-system,system-ui,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",Arial,sans-serif;color:rgb(85,85,85)"><span style="margin:0px;padding:0px;border:0px;outline:0px;vertical-align:baseline;background:transparent;font-family:proxima-nova-zendesk,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",Arial,sans-serif;font-weight:600"><font size="2">CONFIDENTIALITY
NOTICE: This email may contain confidential and privileged material for the sole use of the intended recipient(s). Any review, use, distribution or disclosure by others is strictly prohibited. If you have received this communication in error, please notify
the sender immediately by e-mail and delete the message and any file attachments from your computer. Thank you.</font></span></i></div>
</blockquote>
</div>
<br>
</div><br></blockquote></div></div></body></html>