Be more strict on inferred types with no constraints? #4197
Labels
inference
question
Further information is requested
type-inference
Type inference, issues or improvements
The specification of type inference in inference.md says about the greatest closure of a type in the section Type variable elimination that
which in the context means that the greatest closure of a type parameter which is in the target set
L
isObject?
.The section Type schema elimination says that
_
is treated as a type parameter in the target set.Next, the section Grounded constraint solution for a type variable says that
which in the context means that when a type variable
X
has the combined constraint_ <: X <: _
then the solution is the greatest closure of_
, that is,Object?
.Nevertheless, the implemented behavior chooses
dynamic
, at least in some cases:The fact that the analyzer does not report a compile-time error at the invocation of
arglebargle
shows that the type inferred forgetContextType(1)
isdynamic
rather thanObject?
. The common front end confirms this by also not reporting a compile-time error, and further by printingdynamic
at run time.It is a breaking change, but I'd recommend that type inference is adjusted to infer
Object?
rather thandynamic
in such cases. We could at least experiment with this outcome and see how severe the breakage is. The change would be language versioned such that developers can opt in to the more strict analysis when they are ready.@dart-lang/language-team, WDYT?
The text was updated successfully, but these errors were encountered: