Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specification of continue behavior inside a switch statement makes no sense #30011

Closed
stereotype441 opened this issue Jun 26, 2017 · 5 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). area-specification closed-duplicate Closed in favor of an existing report

Comments

@stereotype441
Copy link
Member

The Dart language spec says:

Let s_c be a continue statement. If s_c is of the form continue L;, then let s_E be the innermost labeled do, for or while statement or case clause with label L enclosing s_c.

But for the typical usage of continue within a switch statement, the target of the continue statement typically doesn't enclose s_c. For example:

switch (x) {
  case 0:
    ...
    continue foo; // s_c
  foo:
  case 1: // s_E (does not enclose s_c)
    ...
    break;
}

If we took the spec literally, the target of a continue statement inside a switch would only be able to point to the originating case clause, which effectively allows turning a case clause into a loop. I'm pretty sure that wasn't the intent.

I think the language should be adjusted to say something like this:

Let s_c be a continue statement. If s_c is of the form continue L;, then let s_E be the innermost labeled do, for or while statement enclosing s_c, or the innermost or case clause with label L from a switch statement enclosing s_c.

...which allows continue expressions within switch statements to jump from one case clause to another.

I've verified that analyzer, dart2js, and the VM all behave consistently with the proposed spec change.

@stereotype441 stereotype441 added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). area-specification labels Jun 26, 2017
@kmillikin kmillikin added the closed-duplicate Closed in favor of an existing report label Jun 26, 2017
@kmillikin
Copy link

This seems like a duplicate of #23628. It has been fixed in the spec in the repository.

@stereotype441
Copy link
Member Author

I don't believe this bug has been fixed (nor has #23628). The spec text in the repository still says:

Let s_c be a continue statement. If s_c is of the form continue L;, then let s_E be the innermost labeled do, for or while statement or case clause with label L enclosing s_c.

(see

Let $s_c$ be a \CONTINUE{} statement. If $s_c$ is of the form \code{\CONTINUE{} $L$;}, then let $s_E$ be the innermost labeled \DO{} (\ref{do}), \FOR{} (\ref{for}) or \WHILE{} (\ref{while}) statement or case clause with label $L$ enclosing $s_c$. If $s_c$ is of the form \code{\CONTINUE{};} then let $s_E$ be the innermost \DO{} (\ref{do}), \FOR{} (\ref{for}) or \WHILE{} (\ref{while}) statement enclosing $s_c$. It is a compile-time error if no such statement or case clause $s_E$ exists within the innermost function in which $s_c$ occurs.
)

@stereotype441 stereotype441 reopened this Jun 26, 2017
@eernstg
Copy link
Member

eernstg commented Jun 26, 2017

I looked into the spec too, and I agree that it still does not mention how continue works in a switch.

@kmillikin
Copy link

You are right. I was misled because the duplicate had been closed.

@lrhn
Copy link
Member

lrhn commented Jun 26, 2017

The runtime semantics are correct in the current specification, and doesn't rely on the "enclosing case clause", but the static warning still uses it. I'll reword that as well.

CL: https://codereview.chromium.org/2961603002/

@lrhn lrhn closed this as completed in 82a97d8 Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). area-specification closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

4 participants