Disallowing break label (and continue label) inside an expression switch
Guy Steele
guy.steele at oracle.com
Fri Mar 23 18:41:07 UTC 2018
See comments in two places below.
> On Mar 23, 2018, at 2:45 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>
> Just want to sync and make sure we're on the same page here...
>
> Certain constructs (switch expression, switch statement, for/while/do) give meaning to some flavor of break. Inside those, you can't use the other flavor, nor can you break "through" a construct of the opposite flavor.
>
> switch-expression {
> break / break-label not allowed;
> break-expr allowed;
> continue, return not allowed;
>
> if (foo) {
> break / break-label disallowed;
> break-expr allowed;
> }
>
> LOOP:
> for (...) {
> break, continue allowed;
> return not allowed;
> break-label allowed if within the switch expression
> break expression not allowed;
> }
>
> switch (e) {
> // same as for loop
Actually, same as for loop _except_ continue not allowed.
> switch-expression {
> break expr allowed
> break, break-label, continue, return not allowed
> }
> }
>
> More formally; we can construct a table whose rows are the control constructs and whose columns are the nonlocal branches, and whose entries are "handlers" for a nonlocal branch. Each block has a parent (the immediately enclosing block.)
>
> break-e break break-l continue return
>
> switch-e L X X X X
> switch-s X L P L P
> for X L P L P
> while X L P L P
> block P P P P P
> labeled X X L* X P
> lambda X X X X L
> method X X X X L
>
> The handlers mean:
>
> X -- not allowed
> P -- let the parent handle it
> L -- handle it and complete normally
> L* -- handle it and complete normally if the labels match, otherwise P
>
> (I might have mangled the labeled row, in which case surely Guy will correct me.)
Right. It should be:
labeled P P L* P P
“In all other cases of abrupt completion of the Statement, the labeled statement completes abruptly for the same reason.” JLS SE8 14.7, page 413
All the other rows look good to me.
> The idea here is that each nested block acts as an "exception handler", catching some exceptions, and propagating others, and some contexts act as exception barriers, like trying to throw a "continue" out of a method.
—Guy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180323/aa4faead/attachment-0001.html>
More information about the amber-spec-experts
mailing list