Skip to content

Commit 37ae078

Browse files
stereotype441Commit Queue
authored andcommitted
Analyzer: Fix incorrect type in PropertyElementResolver._resolve.
The `unpromotedType` computed by `PropertyElementResolver._resolve` (which eventually propagates to both `FlowAnalysis.propertyGet` and to `PropertyElementResolverResult.getType`) is intended to represent the static type of the "property get" expression in the absence of promotion. If the property get targets a `PropertyAccessorElement` (a getter), this is the getter's return type. But if it targets a `MethodElement`, this is the function type of the method (because the "property get" is doing a tear-off of the method). Previously, `PropertyElementResolver._resolve` was always using the return type, meaning that if the property get was targeting a `MethodElement`, it was computing the wrong type. Fortunately, this didn't lead to any bugs, because (a) the type passed to `FlowAnalysis.propertyGet` is only used for determining whether a field access should be promoted, and method tear-offs aren't promotable, and (b) the analyzer only uses the type stored in `PropertyElementResolverResult.getType` when the element being targeted is a `PropertyAccessorElement`. I'm in the middle of a larger arc of work trying to introduce a new, simpler mechanism for flow analysis to be told about the static types of property gets, and part of that arc of work will involve introducing a temporary check to verify that the old and new mechanisms see the same static types. Fixing this incorrect type will allow the temporary check to pass. Change-Id: Ib13b12d1176a781dc56c3e79c150df481b48a0d5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390585 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 7e7da42 commit 37ae078

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,11 @@ class PropertyElementResolver with ScopeHelpers {
488488

489489
DartType? getType;
490490
if (hasRead) {
491-
var unpromotedType =
492-
result.getter?.returnType ?? _typeSystem.typeProvider.dynamicType;
491+
var unpromotedType = switch (result.getter) {
492+
MethodElement(:var type) => type,
493+
PropertyAccessorElement(:var returnType) => returnType,
494+
_ => result.recordField?.type ?? _typeSystem.typeProvider.dynamicType
495+
};
493496
getType = _resolver.flowAnalysis.flow
494497
?.propertyGet(
495498
node,

0 commit comments

Comments
 (0)