The mechanism that turns []
from List<X> xs = const [];
into <Never>[]
doesn't handle invariance
#4223
Labels
[]
from List<X> xs = const [];
into <Never>[]
doesn't handle invariance
#4223
Thanks to @sgrekhov for bringing this up. Consider the following program:
The current implementations (in particular, DartPad based on Dart SDK 3.7.0-243.0.dev) report an error both during analysis and during compilation:
Note that the analyzer replaces
T
byNever
and DDC replaces it byObject?
. None of them will work, because function types are invariant in their type parameter bounds.However, I couldn't find any specification document where the specified behavior is given.
One useful approach could be to report an error when a constant inference makes an attempt to replace a type variable in the context type, and that type variable is in an invariant position. Another approach could be to replace the entire function type by
Never
when it occurs in a covariant position and byObject?
when it occurs in a contravariant position.@dart-lang/language-team, WDYT?
The text was updated successfully, but these errors were encountered: