Skip to content

Commit 8be5353

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Flow analysis: improve handling of types in "why not promoted" logic.
This CL plumbs the types of `this` and property get expression from the CFE and analyzer into flow analysis, so that flow analysis will be able to create more accurate "why not promoted" information for those expression types. This made it possible to eliminate a clumsy aspect of the previous implementation, namely that we would consider a promotion attempt like `if (x.y == null) return;` as an attempt to promote the type of `x.y` to `Object`; now we compute the type the user is actually trying to promote to, so we will be able to generate more accurate "why not promoted" messages. Bug: #44898 Change-Id: I67f9fc59e72103194a1ea6b1c4dfeae8aeb194a2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187064 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent c43be34 commit 8be5353

22 files changed

+420
-339
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 181 additions & 150 deletions
Large diffs are not rendered by default.

pkg/_fe_analyzer_shared/test/flow_analysis/flow_analysis_mini_ast.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ Statement if_(Expression condition, List<Statement> ifTrue,
144144
[List<Statement>? ifFalse]) =>
145145
new _If(condition, ifTrue, ifFalse);
146146

147-
Statement implicitThis_whyNotPromoted(
147+
Statement implicitThis_whyNotPromoted(String staticType,
148148
void Function(Map<Type, NonPromotionReason>) callback) =>
149-
new _WhyNotPromoted_ImplicitThis(callback);
149+
new _WhyNotPromoted_ImplicitThis(Type(staticType), callback);
150150

151151
Statement labeled(Statement body) => new _LabeledStatement(body);
152152

@@ -161,7 +161,7 @@ Statement switch_(Expression expression, List<SwitchCase> cases,
161161
Expression this_(String type) => new _This(Type(type));
162162

163163
Expression thisOrSuperPropertyGet(String name, {String type = 'Object?'}) =>
164-
new _ThisOrSuperPropertyGet(name, type);
164+
new _ThisOrSuperPropertyGet(name, Type(type));
165165

166166
Expression throw_(Expression operand) => new _Throw(operand);
167167

@@ -284,7 +284,7 @@ abstract class Expression extends Node implements _Visitable<Type> {
284284

285285
/// If `this` is an expression `x`, creates the expression `x.name`.
286286
Expression propertyGet(String name, {String type = 'Object?'}) =>
287-
new _PropertyGet(this, name, type);
287+
new _PropertyGet(this, name, Type(type));
288288

289289
/// If `this` is an expression `x`, creates a pseudo-expression that models
290290
/// evaluation of `x` followed by execution of [stmt]. This can be used to
@@ -435,9 +435,6 @@ class Harness extends TypeOperations<Var, Type> {
435435

436436
Harness({this.legacy = false});
437437

438-
@override
439-
Type get topType => Type('Object?');
440-
441438
/// Updates the harness so that when a [factor] query is invoked on types
442439
/// [from] and [what], [result] will be returned.
443440
void addFactor(String from, String what, String result) {
@@ -1477,7 +1474,7 @@ class _PropertyGet extends Expression {
14771474

14781475
final String propertyName;
14791476

1480-
final String type;
1477+
final Type type;
14811478

14821479
_PropertyGet(this.target, this.propertyName, this.type);
14831480

@@ -1490,8 +1487,8 @@ class _PropertyGet extends Expression {
14901487
Type _visit(
14911488
Harness h, FlowAnalysis<Node, Statement, Expression, Var, Type> flow) {
14921489
target._visit(h, flow);
1493-
flow.propertyGet(this, target, propertyName);
1494-
return Type(type);
1490+
flow.propertyGet(this, target, propertyName, type);
1491+
return type;
14951492
}
14961493
}
14971494

@@ -1566,15 +1563,15 @@ class _This extends Expression {
15661563
@override
15671564
Type _visit(
15681565
Harness h, FlowAnalysis<Node, Statement, Expression, Var, Type> flow) {
1569-
flow.thisOrSuper(this);
1566+
flow.thisOrSuper(this, type);
15701567
return type;
15711568
}
15721569
}
15731570

15741571
class _ThisOrSuperPropertyGet extends Expression {
15751572
final String propertyName;
15761573

1577-
final String type;
1574+
final Type type;
15781575

15791576
_ThisOrSuperPropertyGet(this.propertyName, this.type);
15801577

@@ -1584,8 +1581,8 @@ class _ThisOrSuperPropertyGet extends Expression {
15841581
@override
15851582
Type _visit(
15861583
Harness h, FlowAnalysis<Node, Statement, Expression, Var, Type> flow) {
1587-
flow.thisOrSuperPropertyGet(this, propertyName);
1588-
return Type(type);
1584+
flow.thisOrSuperPropertyGet(this, propertyName, type);
1585+
return type;
15891586
}
15901587
}
15911588

@@ -1760,9 +1757,11 @@ class _WhyNotPromoted extends Expression {
17601757
}
17611758

17621759
class _WhyNotPromoted_ImplicitThis extends Statement {
1760+
final Type staticType;
1761+
17631762
final void Function(Map<Type, NonPromotionReason>) callback;
17641763

1765-
_WhyNotPromoted_ImplicitThis(this.callback) : super._();
1764+
_WhyNotPromoted_ImplicitThis(this.staticType, this.callback) : super._();
17661765

17671766
@override
17681767
String toString() => 'implicit this (whyNotPromoted)';
@@ -1776,7 +1775,7 @@ class _WhyNotPromoted_ImplicitThis extends Statement {
17761775
assert(!Type._allowComparisons);
17771776
Type._allowComparisons = true;
17781777
try {
1779-
callback(flow.whyNotPromoted(null));
1778+
callback(flow.whyNotPromotedImplicitThis(staticType));
17801779
} finally {
17811780
Type._allowComparisons = false;
17821781
}

0 commit comments

Comments
 (0)