Skip to content

Commit a2c7e7c

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: implement post-dominator analysis of ?? operations.
Change-Id: I66ed10fba630663e025cec8d805ed6677b2eb6ff Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/114555 Reviewed-by: Mike Fairhurst <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent c1dbced commit a2c7e7c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
330330
_flowAnalysis.ifNullExpression_rightBegin();
331331
try {
332332
_guards.add(leftType.node);
333-
var rightType = node.rightOperand.accept(this);
333+
DecoratedType rightType;
334+
_postDominatedLocals.doScoped(action: () {
335+
rightType = node.rightOperand.accept(this);
336+
});
334337
var ifNullNode = NullabilityNode.forIfNotNull();
335338
expressionType = DecoratedType(node.staticType, ifNullNode);
336339
_connect(rightType.node, expressionType.node,

pkg/nnbd_migration/test/edge_builder_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,6 +2982,22 @@ void test() {
29822982
assertEdge(always, decoratedTypeAnnotation('int i').node, hard: false);
29832983
}
29842984

2985+
test_postDominators_questionQuestionOperator() async {
2986+
await analyze('''
2987+
class C {
2988+
Object m() => null;
2989+
}
2990+
Object test(C x, C y) => x.m() ?? y.m();
2991+
''');
2992+
// There is a hard edge from x to `never` because `x.m()` is unconditionally
2993+
// reachable from the top of `test`.
2994+
assertEdge(decoratedTypeAnnotation('C x').node, never, hard: true);
2995+
// However, the edge from y to `never` is soft because `y.m()` is only
2996+
// executed if `x.m()` returned `null`.
2997+
assertEdge(decoratedTypeAnnotation('C y').node, never,
2998+
hard: false, guards: [decoratedTypeAnnotation('Object m').node]);
2999+
}
3000+
29853001
test_postDominators_reassign() async {
29863002
await analyze('''
29873003
void test(bool b, int i1, int i2) {

0 commit comments

Comments
 (0)