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

Exhaustiveness check should be able to prefer API instead of non-API witnesses #52682

Open
scheglov opened this issue Jun 12, 2023 · 1 comment
Labels
analyzer-language-patterns Issues with analyzer's support for the patterns language feature area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-messages Poor/undesirable messaging in errors/warnings emitted by the CFE. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@scheglov
Copy link
Contributor

See https://dart-review.googlesource.com/c/sdk/+/308520 for the initial question.

With this CL, if we write switch like this:

void f(TypedLiteral node) {
  switch (node) {
  }
}

...and use "Add missing case" twice, it does end up with using implementation classes.

void f(TypedLiteral node) {
  switch (node) {
    case ListLiteralImpl():
      // TODO: Handle this case.
    case SetOrMapLiteralImpl():
      // TODO: Handle this case.
  }
}

It does not have to, and this API version works as well.

void f(TypedLiteral node) {
  switch (node) {
    case ListLiteral():
      // TODO: Handle this case.
    case SetOrMapLiteral():
      // TODO: Handle this case.
  }
}

So, here we might have a tooling issue. Either CFE should be enhanced to prefer reporting witnesses that are available in the target code, maybe some filtering, etc. Or the quick fix should climb up. Ideologically, I think it should be CFE. Or the analyzer in a way how it feeds data into CFE, e.g. never return non-API classes that the target package should not use. Not different than how code completion will not suggest non-API classes from other packages.

@scheglov scheglov added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. area-front-end Use area-front-end for front end / CFE / kernel format related issues. analyzer-language-patterns Issues with analyzer's support for the patterns language feature labels Jun 12, 2023
@bwilkerson
Copy link
Member

It appears that we do a better job than I expected. I don't have your CL patched in, but when I mark TypedLiteral as sealed and change the example slightly to force the switch to be exhaustive:

void f(TypedLiteral node) {
  var x = switch (node) {
    ListLiteral() => 2,
    SetOrMapLiteral() => 1,
  };
  print(x);
}

It realizes that those two types are supertypes of all of the subtypes of TypedLiteral and doesn't complain (which I thought it would).

I agree, though, that the tools need to do a better job with package-private types.

@pq pq added the P3 A lower priority bug or feature request label Oct 30, 2023
@johnniwinther johnniwinther added the cfe-messages Poor/undesirable messaging in errors/warnings emitted by the CFE. label Jan 9, 2024
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-language-patterns Issues with analyzer's support for the patterns language feature area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-messages Poor/undesirable messaging in errors/warnings emitted by the CFE. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants