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

Missing error on switch/case type mismatch #34207

Open
askeksa-google opened this Issue Aug 21, 2018 · 5 comments

Comments

4 participants
@askeksa-google
Copy link
Contributor

askeksa-google commented Aug 21, 2018

From co19_2/Language/Statements/Switch/execution_t01:

main() {
   switch (1) {
     case '1': break;
     case '2': break;
     default:
   }

  switch ('abc') {
    case 1: break;
  }
}

The spec says (in 17.9):
"It is a compile-time error if the type of e may not be assigned to the type of ek."

From co19_2/Language/Statements/Switch/expressions_t01:

main() {
  var x = 1;
  switch (x) {
    case 0:
    case 2:
    case "false":
  }
}

The spec says (in 17.9):
"it is a compile-time error if the expressions ek are not compile-time constants
for all k ∈ 1..n. It is a compile-time error if the values of the expressions ek are
not either:
• instances of the same class C, for all k ∈ 1..n, or
• instances of a class that implements int, for all k ∈ 1..n, or
• instances of a class that implements String, for all k ∈ 1..n.
In other words, all the expressions in the cases evaluate to constants of the
exact same user defined class or are of certain known types."

@askeksa-google askeksa-google added this to Triaged in Dart Front End Aug 28, 2018

@dhil dhil self-assigned this Aug 28, 2018

@askeksa-google askeksa-google moved this from Triaged to In Progress in Dart Front End Aug 29, 2018

dart-bot pushed a commit that referenced this issue Sep 3, 2018

Checks whether the type of a switch expression is assignable to the
types of its case expressions.

Resolves the first part of
#34207. According to the
language team the second part may soon become irrelevant.

Change-Id: Ifce38f2a62c293156dafa94f16799d5a126c6b9c
Reviewed-on: https://dart-review.googlesource.com/71981
Commit-Queue: Daniel Hillerström <hillerstrom@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
@dhil

This comment has been minimized.

Copy link
Contributor

dhil commented Sep 3, 2018

CL 71981 implements the assignability check.

@kmillikin

This comment has been minimized.

Copy link
Member

kmillikin commented Sep 4, 2018

Fixed in a562030.

@kmillikin kmillikin closed this Sep 4, 2018

@kmillikin

This comment has been minimized.

Copy link
Member

kmillikin commented Sep 4, 2018

I should say, is this fixed in a562030?

@kmillikin kmillikin reopened this Sep 4, 2018

@dhil

This comment has been minimized.

Copy link
Contributor

dhil commented Sep 4, 2018

The check for whether the switch expression is assignable to each case expression has been implemented. But the second part of the issue has not been implemented yet:

The spec says (in 17.9):
"it is a compile-time error if the expressions ek are not compile-time constants
for all k ∈ 1..n. It is a compile-time error if the values of the expressions ek are
not either:
• instances of the same class C, for all k ∈ 1..n, or
• instances of a class that implements int, for all k ∈ 1..n, or
• instances of a class that implements String, for all k ∈ 1..n.
In other words, all the expressions in the cases evaluate to constants of the
exact same user defined class or are of certain known types."

Partly because the language team is considering changing this part of the specification. But also because as specified now, we may need constant evaluation to implement it properly.

@dhil dhil removed their assignment Sep 5, 2018

@jensjoha

This comment has been minimized.

Copy link
Contributor

jensjoha commented Sep 6, 2018

Would it be fair to add the label "requires-constant-evaluation" then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.