Untyped variable and wildcard patterns are not guaranteed to match #2619
Labels
flow-analysis
Discussions about possible future improvements to flow analysis
patterns
Issues related to pattern matching.
Consider the following code:
Can this code ever print
unreachable
? It seems like it should be impossible. But based on my reading of the spec, I think it could._
is considered by type inference to be equivalent toint _
, which means that the match will fail if the runtime type ofi
is not a subtype ofint
. In a program that's not fully migrated to null safety, it's possible thati
will have the valuenull
.This is really unfortunate, because even though this is unlikely to arise very often in practice, flow analysis has to reflect the semantics precisely (to avoid unsoundness escalalation), and therefore the
else
branch can't be flagged as unreachable code.We could fix this by changing this line from the spec (from Runtime semantics > Matching > Variable):
To something like:
In other words, if the variable pattern looked like it was guaranteed to match based on static analysis, then it would be guaranteed to match. And therefore all untyped variable and wildcard patterns would be guaranteed to match.
Note: there would be a negative consequence to that. A program like this could print
i = null
in a program that's not fully migrated:I personally think that's ok. IMHO it's no worse than the other examples of mixed mode unsoundness we have in Dart today, and it's way less bad than allowing
_
to sometimes fail to match.The text was updated successfully, but these errors were encountered: