Switch Expression - complete normally - spec omission?

Manoj Palat manoj.palat at in.ibm.com
Sat May 16 09:10:56 UTC 2020


Thanks Alex for the reply and clarifying that we are discussing Switch Statements with Switch rules.
My bad for putting the title as Switch Expressions - don't want to correct the title now which
will break the chain of the mail.

Regards,
Manoj

-----"amber-spec-experts" <amber-spec-experts-bounces at openjdk.java.net> wrote: -----
To: amber-spec-experts at openjdk.java.net
From: Alex Buckley
Sent by: "amber-spec-experts"
Date: 05/15/2020 11:34PM
Subject: [EXTERNAL] Re: Switch Expression - complete normally - spec omission?

On 5/14/2020 7:22 PM, Manoj Palat wrote:
> I think there is a spec omission regarding "complete normally for switch statements whose switch block consists of switch rules
>
> Ref JLS 14 Sec 14.22
> ...
> A switch statement whose switch block consists of switch rules can complete
> normally iff at least one of the following is true:
> – One of the switch rules introduces a switch rule expression (which is
> necessarily a statement expression).
> – One of the switch rules introduces a switch rule block that can complete
> normally.
> – One of the switch rules introduces a switch rule block that contains a reachable
> break statement which exits the switch statement.
> ...
> Now consider:
>
> switch (b) {
> case 1 -> {
> throw new Exception();
> }
> }
>
> As per the above definition, this switch statement cannot complete normally;
> but consider "b" having a value other than 1 and then it completes normally.

/* -----
To demonstrate the point, the switch statement above uses a switch rule
block that completes abruptly, but could alternatively have used a
switch rule `throw` statement:

switch (b) {
   case 1 -> throw new Exception();
}

Also, to clarify for the many readers of this list, we are discussing
switch statements, which never require a `default` label. We are not
discussing switch expressions, which almost always require a `default`
label.
----- */

>
> Also consider, 14.11.3. which says:
> "If no switch label matches, the entire switch statement completes normally."
>
> which looks inconsistent. One hand says: "completes normally" the other:
> "iff at least one of the following ..." not mentioning default.
>
> Hence, shouldn't the item,
> -> – The switch block does not contain a default label.
>
> also be added in the list in 14.22?

I agree that without a `default` switch label, 14.11.3 may evaluate "If
no switch label matches, the entire switch statement completes
normally."  Since we can see a way for the statement to complete
normally, 14.22 ought to say that the statement _can_ complete normally.
So, it looks right to add "– The switch block does not contain a default
label."

Alex




More information about the amber-spec-observers mailing list