Giter Club home page Giter Club logo

Comments (4)

eernstg avatar eernstg commented on August 16, 2024 1

This is a known issue. A similar situation was discussed in dart-lang/language#2977.

Every switch expression is subject to exhaustiveness checking, and the exhaustiveness analysis does recognize this case, so the following version of the code is accepted:

int add(int? a, int? b) => switch ((a, b)) {
      (_, null) => a!,
      (null, _) => b!,
      (final int a, final int b) => a + b
    };

However, exhaustiveness analysis is different from the flow analysis which is also performed, and the exhaustiveness analysis is stronger in some instances. So the declaration is accepted because it is subject to both kinds of analysis.

A switch statement is different: It is only subject to exhaustiveness analysis in the case where the static type of the scrutinee is an 'always-exhaustive type', and that doesn't include the record type (int?, int?), because it doesn't include int?.

This means that the switch statement is subject to flow analysis (that one is always enabled), but not to exhaustiveness analysis. And the flow analysis isn't smart enough to detect that this particular switch statement is exhaustive.

The issue dart-lang/language#2977 explicitly mentions this catch-22: If the default case is included then it is flagged as unreachable, but if it is omitted then the switch statement isn't recognized as being exhaustive.

There is no beautiful solution, but you might use something like the following:

int add(int? a, int? b) {
  switch ((a, b)) {
    case (_, null): return a!;
    case (null, _): return b!;
    case (final int a, final int b): return a + b;
  }
  // Flow analysis cannot see that the switch is exhaustive.
  throw "Unreachable";
}

from sdk.

srawlins avatar srawlins commented on August 16, 2024

Mm yeah. The way to comply with both of these is to add a final return outside the switch.

It's possible the body_might_complete_normally logic could be updated to treat switch statements as though they considered exhaustiveness...

from sdk.

nate-thegrate avatar nate-thegrate commented on August 16, 2024

It could have something to do with how records are analyzed, since having just 1 parameter works fine:

// no linter issues!
int increment(int? a) {
  switch (a) {
    case null: return 0;
    case final int a: return a + 1;
  }
}

from sdk.

nate-thegrate avatar nate-thegrate commented on August 16, 2024

Thank you @eernstg!

I think it makes sense to close this one as a duplicate of dart-lang/language#2977.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.