Inferred variable/field types should not be used as context when doing full resolution #51137
Labels
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
P1
A high priority bug; for example, a single project is unusable or has many test failures
type-bug
Incorrect behavior (everything from a crash to more subtle misbehavior)
Consider the following code:
The
// @dart=2.17
annotation disables horizontal inference. So, when analyzing (1), the analyzer should push down a context ofList<int> Function(?)
when analyzing the closure(m) => m.codeUnits
. Therefore, the inferred type of the parameterm
should be the greatest closure of?
, which isObject?
. As a result, there should be a compile-time error, because the typeObject?
doesn't support the getter.codeUnits
. The CFE produces the proper error message.Here's what happens when the analyzer analyzes this code: since (1) is a top level variable declaration with no explicit type, the analyzer first performs top level type inference, which involves resolving the right hand side of the declaration. The type
Object?
is correctly assigned tom
, however no error is reported because the analyzer doesn't report errors during top level type inference. The inferred type of the variablex
isString?
.Then, after top level type inference is complete, the analyzer visits the entire file and resolves it, reporting errors. Which means that it visits the declaration of
x
a second time. On the second visit, the inferred type ofString?
is pushed down as a context. As a result, even though horizontal inference is disabled, the typeString
is inferred form
, and so no error is reported.I believe the easiest way to fix this would be to change the analyzer so that when resolving a top level variable or field declaration, it only passes the context type down to the initializer if the type is explicit.
(Note that since the CFE already reports the correct error here, it should not be necessary to go through the breaking change process for this change).
The text was updated successfully, but these errors were encountered: