Type propagation should infer both upper and lower bounds for type parameters of literal lists and maps #18269
Labels
analyzer-ux
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
In some circumstances it makes sense for type propagation to infer the most specific possible type for the elements of a literal list or map. For example, when offering completions for:
main() {
var numbers = [1, 1, 2, 3, 5];
numbers[0].
it makes sense to infer that numbers has type List<int>, so that we can offer completions for int.
However, in others, it makes sense for type propagation to infer the least specific possible type. For example, when generating hints for:
class A {}
class B extends A {}
void main() {
List<A> as = [];
[new B()].addAll(as);
}
we should infer that the type of [new B()] is List<A>, so that it is compatible with the call to addAll() (see issue #17934).
I suspect that we can address this issue by inferring both a lower bound and an upper bound for each type parameter, using a Hindley–Milner style type inference algorithm. In the example above, upon encountering "List <A> as = []" we would infer a type of List<T1> where T1 <= A. Upon encountering "[new B()]", we would [new B()]", we would infer a type of List<T2> where B <= T2 <= dynamic. Then, when processing ".addAll(as)" we would unify types T1 and T2 and deduce that B <= T2 = T1 <= A.
The text was updated successfully, but these errors were encountered: