<div dir="ltr"><div><div><div><div>Thank you all!<br><br></div><div>I agree with Remi, the problem no longer exists when using a switch expression.<br></div><div>I thought I could not do it with our (obviously more contrived) code, but I have managed to do it in the end.<br></div><div><br></div>That said, after sending the email, I actually wondered if this problem was actually linked to the use of the "old switch" statement, and a possible lack of understanding of the control flow?<br></div>I am ready to be told "we cannot fix it because of JLS rule 4.5.6.7" or something like that.<br><br></div>Thank you again for taking a look at it,<br></div>Jean-Noël<br><br><br><div><div><div><div><br></div></div></div></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Wed, Dec 11, 2024 at 3:05 PM Angelos Bimpoudis <<a href="mailto:angelos.bimpoudis@oracle.com">angelos.bimpoudis@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-688562241304480375">




<div dir="ltr">
<div style="font-size:11pt;color:rgb(0,0,0)">
Filed it here: <a href="https://bugs.openjdk.org/browse/JDK-8345997" target="_blank">https://bugs.openjdk.org/browse/JDK-8345997</a></div>
<div style="font-size:11pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-size:11pt;color:rgb(0,0,0)">
Thank you for reaching out! </div>
<div style="font-size:11pt;color:rgb(0,0,0)">
Thx Remi for the minimized example as well.</div>
<div id="m_-3490843587185937718appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_-3490843587185937718divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> amber-dev <<a href="mailto:amber-dev-retn@openjdk.org" target="_blank">amber-dev-retn@openjdk.org</a>> on behalf of Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>><br>
<b>Sent:</b> 11 December 2024 13:44<br>
<b>To:</b> Gavin Bierman <<a href="mailto:gavin.bierman@oracle.com" target="_blank">gavin.bierman@oracle.com</a>><br>
<b>Cc:</b> Jean-Noël Rouvignac (ForgeRock) <<a href="mailto:jean-noel.rouvignac@pingidentity.com" target="_blank">jean-noel.rouvignac@pingidentity.com</a>>; amber-dev <<a href="mailto:amber-dev@openjdk.org" target="_blank">amber-dev@openjdk.org</a>><br>
<b>Subject:</b> Re: Enum: difference of behaviour between exhaustive switch vs. using default:</font>
<div> </div>
</div>
<div>
<div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<div>Hello,<br>
</div>
<div>I think it can be reduced to</div>
<div>
<div style="background-color:rgb(255,255,255);color:rgb(8,8,8)">
<pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">public enum </span><span style="color:rgb(0,0,0)">Action </span>{ <span style="color:rgb(135,16,148);font-style:italic">IGNORE</span>, <span style="color:rgb(135,16,148);font-style:italic">REJECT </span>}</pre>
</div>
</div>
<div>
<div style="background-color:rgb(255,255,255);color:rgb(8,8,8)">
<pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">private static void </span><span style="color:rgb(0,98,122)">bad</span>() {<br>    <span style="color:rgb(0,0,0)">String s</span>;<br>    <span style="color:rgb(0,51,179)">switch </span>(<span style="font-style:italic">getAction</span>()) {<br>        <span style="color:rgb(0,51,179)">case </span><span style="color:rgb(135,16,148);font-style:italic">IGNORE</span>:<br>            <span style="color:rgb(0,0,0)">s </span>= <span style="color:rgb(6,125,23)">"foo"</span>;<br>            <span style="color:rgb(0,51,179)">break</span>;<br>        <span style="color:rgb(0,51,179)">case </span><span style="color:rgb(135,16,148);font-style:italic">REJECT</span>:<span style="color:rgb(140,140,140);font-style:italic"><br></span><span style="color:rgb(140,140,140);font-style:italic">            </span><span style="color:rgb(0,51,179)">throw new </span>RuntimeException(<span style="color:rgb(6,125,23)">"REJECTED"</span>);<br>    };<br>    <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(0,0,0)">s</span>); <span style="color:rgb(140,140,140);font-style:italic">// <------- variable s might not have been initialized<br></span>}<br><br><span style="color:rgb(0,51,179)">private static void </span><span style="color:rgb(0,98,122)">ok</span>() {<br>    <span style="color:rgb(0,0,0)">String s </span>= <span style="color:rgb(0,51,179)">switch </span>(<span style="font-style:italic">getAction</span>()) {<br>        <span style="color:rgb(0,51,179)">case </span><span style="color:rgb(135,16,148);font-style:italic">IGNORE</span>:<br>            <span style="color:rgb(0,51,179)">yield </span><span style="color:rgb(6,125,23)">"foo"</span>;<br>        <span style="color:rgb(0,51,179)">case </span><span style="color:rgb(135,16,148);font-style:italic">REJECT</span>:<span style="color:rgb(140,140,140);font-style:italic"><br></span><span style="color:rgb(140,140,140);font-style:italic">            </span><span style="color:rgb(0,51,179)">throw new </span>RuntimeException(<span style="color:rgb(6,125,23)">"REJECTED"</span>);<br>    };<br>    <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(0,0,0)">s</span>); <span style="color:rgb(140,140,140);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>
</div>
<div><br>
</div>
<div>Rémi<br>
</div>
<div><br>
</div>
<hr id="m_-3490843587185937718x_zwchr">
<div>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt">
<b>From: </b>"Gavin Bierman" <<a href="mailto:gavin.bierman@oracle.com" target="_blank">gavin.bierman@oracle.com</a>><br>
<b>To: </b>"Jean-Noël Rouvignac (ForgeRock)" <<a href="mailto:jean-noel.rouvignac@pingidentity.com" target="_blank">jean-noel.rouvignac@pingidentity.com</a>><br>
<b>Cc: </b>"amber-dev" <<a href="mailto:amber-dev@openjdk.org" target="_blank">amber-dev@openjdk.org</a>><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>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);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="m_-3490843587185937718x_lineBreakAtBeginningOfMessage">
<div><br>
<blockquote>
<div>On 10 Dec 2024, at 16:10, Jean-Noël Rouvignac (ForgeRock) <<a href="mailto:jean-noel.rouvignac@pingidentity.com" target="_blank">jean-noel.rouvignac@pingidentity.com</a>> wrote:</div>
<br>
<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">
<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>
</div>
</div>

</div></blockquote></div><div><br clear="all"></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><table style="border-collapse:collapse;padding:0px;margin:0px"><tbody><tr><td style="width:113px"><a href="https://www.pingidentity.com/" target="_blank"></a><a href="https://www.pingidentity.com/" target="_blank"></a><a href="https://www.pingidentity.com/" target="_blank"><img alt="Ping Identity" src="https://www.pingidentity.com//content/dam/picr/img/em/ping-logo.png"></a></td><td><table><tbody><tr><td style="vertical-align:top"><span style="color:rgb(230,29,60);display:inline-block;margin-bottom:3px;font-family:arial,helvetica,sans-serif;font-weight:bold;font-size:14px">Jean-Noel Rouvignac</span><br><span style="display:inline-block;margin-bottom:2px;font-family:arial,helvetica,sans-serif;font-size:14px">Senior Principal Software Engineer</span><br><span style="font-family:arial,helvetica,sans-serif;font-size:14px;display:inline-block;margin-bottom:3px"><font color="#1530f9"><a href="mailto:jean-noel.rouvignac@pingidentity.com" target="_blank">jean-noel.rouvignac@pingidentity.com</a></font><br><span style="display:inline-block;margin-bottom:2px"></span><br style="font-family:Times;font-size:medium"><span style="display:inline-block;margin-bottom:2px"></span></span></td></tr></tbody></table></td></tr><tr><td colspan="2"><table style="border-collapse:collapse;border:medium;margin:8px 0px 0px;width:408.938px"><tbody><tr style="height:40px;border-top:1px solid rgb(211,211,211);border-bottom:1px solid rgb(211,211,211)"><td style="font-family:arial,helvetica,sans-serif;font-size:14px;font-weight:bold;color:rgb(64,71,75)"><a href="https://www.pingidentity.com/en/events/youniverse.html" target="_blank"></a><a href="https://www.pingidentity.com/en/events/youniverse.html" target="_blank"></a><a href="https://www.pingidentity.com/en/events/youniverse.html" target="_blank"><img style="height: 100px; width: 400px;" src="https://www.pingidentity.com/content/dam/picr/img/em/YOUniverse-2024-EmailSig-560x140-v03.png"></a><br><div style="padding:0px;margin:0px"><table style="border-collapse:collapse;padding:0px;margin:0px"><tbody><tr><td colspan="2"><table style="border-collapse:collapse;border:medium;margin:8px 0px 0px;width:408.938px"><tbody><tr style="height:40px;border-top:1px solid rgb(211,211,211);border-bottom:1px solid rgb(211,211,211)"><td style="font-size:14px;font-weight:bold;color:rgb(64,71,75)">Connect with us:</td><td style="padding:4px 0px 0px 20px"><a href="https://www.glassdoor.com/Overview/Working-at-Ping-Identity-EI_IE380907.11,24.htm" title="Ping on Glassdoor" style="margin-right:16px" target="_blank"></a><a href="https://www.glassdoor.com/Overview/Working-at-Ping-Identity-EI_IE380907.11,24.htm" target="_blank"><img src="https://www.pingidentity.com/content/dam/picr/img/em/social-glassdoor.png" alt="Glassdoor logo" style="border: medium; margin: 0px;"></a> <a href="https://www.linkedin.com/company/21870" title="Ping on LinkedIn" style="margin-right:16px" target="_blank"></a><a href="https://www.linkedin.com/company/21870" target="_blank"><img src="https://www.pingidentity.com//content/dam/picr/img/em/social-linkedin.png" alt="LinkedIn logo" style="border: medium; margin: 0px;"></a> <a href="https://twitter.com/pingidentity" title="Ping on Twitter" style="margin-right:16px" target="_blank"></a><a href="https://twitter.com/pingidentity" target="_blank"><img src="https://www.pingidentity.com//content/dam/picr/img/em/social-twitter.png" alt="twitter logo" style="border: medium; margin: 0px;"></a> <a href="https://www.facebook.com/pingidentitypage" title="Ping on Facebook" style="margin-right:16px" target="_blank"></a><a href="https://www.facebook.com/pingidentitypage" target="_blank"><img src="https://www.pingidentity.com//content/dam/picr/img/em/social-facebook.png" alt="facebook logo" style="border: medium; margin: 0px;"></a> <a href="https://www.youtube.com/user/PingIdentityTV" title="Ping on Youtube" style="margin-right:16px" target="_blank"></a><a href="https://www.youtube.com/user/PingIdentityTV" target="_blank"><img src="https://www.pingidentity.com//content/dam/picr/img/em/social-youtube.png" alt="youtube logo" style="border: medium; margin: 0px 0px 3px;"></a> <a href="https://www.pingidentity.com/en/blog.html" title="Ping Blog" style="margin-right:16px" target="_blank"></a><a href="https://www.pingidentity.com/en/blog.html" target="_blank"><img src="https://www.pingidentity.com//content/dam/picr/img/em/social-blog.png" alt="Blog logo" style="border: medium; margin: 0px;"></a><br></td></tr></tbody></table></td></tr></tbody></table><div><div style="padding:0px;margin:0px"><font face="Arial" size="2"><font color="#787878">To view our privacy policy, click </font><a href="https://www.pingidentity.com/en/legal/privacy.html" target="_blank">here</a><br></font></div><div style="padding:0px;margin:0px"><font face="Arial" size="2"><font color="#787878">To stop receiving these emails, click </font><a href="https://4.pingidentity.com/PreferenceCenter.html" target="_blank">here</a></font></div></div></div></td></tr></tbody></table></td></tr></tbody></table></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>