Skip to content

[extension types] Switch statement is not exhaustive on sealed class #54444

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

Closed
sgrekhov opened this issue Dec 22, 2023 · 4 comments
Closed

[extension types] Switch statement is not exhaustive on sealed class #54444

sgrekhov opened this issue Dec 22, 2023 · 4 comments
Labels
cfe-feature-cleanup Feature work post release. feature-extension-types Implementation of the extension type feature type-question A question about expected behavior or functionality

Comments

@sgrekhov
Copy link
Contributor

Switch statement with the matched value an extension type with a dyrect subrtype of a sealed class as a representation type is not exhaustive. But similar switch expression is. Example

// SharedOptions=--enable-experiment=inline-class

sealed class A {}

class B extends A {}

extension type BET(B _) {}

String test1(BET b) { // Error. The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type.
  switch (b) {
    case B _: return 'B';
  }
}

String test2(BET b) => switch (b) {
    B _ => 'B'
  };

main() {
  test1(BET(B()));
  test2(BET(B()));
}

Is this expected or issue?

Tested on on Dart SDK version: 3.3.0-edge.3e09072221b5fe4e8ea3d4fb69f120af8aa499cd (main) (Fri Dec 22 00:37:15 2023 +0000) on "linux_x64"

@sgrekhov sgrekhov added type-question A question about expected behavior or functionality fe-analyzer-shared-exhaustiveness feature-extension-types Implementation of the extension type feature labels Dec 22, 2023
@eernstg
Copy link
Member

eernstg commented Dec 22, 2023

The error message comes from the flow analysis. This analysis has not yet been specified in all details. However, I would recommend that the flow analysis relies on the extension type erasure when it is computing whether or not it the switch statement can complete normally: The erasure yields the most precise approximation to the actual run-time behavior that we have.

@stereotype441, do you agree?

@lrhn
Copy link
Member

lrhn commented Dec 24, 2023

The extension type erasures are the only types that precisely describe runtime behavior, so they are the ones we need to use at compile-time to statically predict runtime behavior.

Since the matched value can, hypothetically, contain any possible value of the erased representation type, that is the type that must be exhausted. The type checks performed by the case patterns are done using erased types too, because that's all that exist at runtime.

@sgrekhov sgrekhov changed the title [extensiot types] Switch statement is not exhaustive on sealed class [extension types] Switch statement is not exhaustive on sealed class Dec 27, 2023
@eernstg
Copy link
Member

eernstg commented Dec 29, 2023

The language design question has been posed here: dart-lang/language#3534.

If that proposal is accepted then the error in test1 should no longer be reported, but for now we're blocked on dart-lang/language#3534.

@sgrekhov
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cfe-feature-cleanup Feature work post release. feature-extension-types Implementation of the extension type feature type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

4 participants