Switch Expression - complete normally - spec omission?

Alex Buckley alex.buckley at oracle.com
Fri May 15 18:03:57 UTC 2020


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