Skip to content

Commit d2f9e83

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: integrate flow analysis of ?? into FixBuilder
Change-Id: I3d576c704ffd0b3b44fff0ceb442e355790ecb84 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121342 Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
1 parent 04d800a commit d2f9e83

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pkg/nnbd_migration/lib/src/fix_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ abstract class FixBuilder extends GeneralizingAstVisitor<DartType> {
205205
case TokenType.QUESTION_QUESTION:
206206
// If `a ?? b` is used in a non-nullable context, we don't want to
207207
// migrate it to `(a ?? b)!`. We want to migrate it to `a ?? b!`.
208-
// TODO(paulberry): once flow analysis supports `??`, integrate it here.
209-
// See https://github.com/dart-lang/sdk/issues/38680
210208
var leftType = visitSubexpression(node.leftOperand,
211209
_typeSystem.makeNullable(_contextType as TypeImpl));
210+
_flowAnalysis.ifNullExpression_rightBegin();
212211
var rightType = visitSubexpression(node.rightOperand, _contextType);
212+
_flowAnalysis.ifNullExpression_end();
213213
return _typeSystem.leastUpperBound(
214214
_typeSystem.promoteToNonNull(leftType as TypeImpl), rightType);
215215
default:

pkg/nnbd_migration/test/fix_builder_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ _f(int/*?*/ x, double/*?*/ y) {
420420
visitSubexpression(findNode.binary('??'), 'num?');
421421
}
422422

423+
test_binaryExpression_question_question_flow() async {
424+
await analyze('''
425+
_f(int/*?*/ x, int/*?*/ y) =>
426+
<dynamic>[x ?? (y != null ? 1 : throw 'foo'), y + 1];
427+
''');
428+
// The null check on the RHS of the `??` doesn't promote, because it is not
429+
// guaranteed to execute.
430+
visitSubexpression(findNode.listLiteral('['), 'List<dynamic>',
431+
nullChecked: {findNode.simple('y +')});
432+
}
433+
423434
test_binaryExpression_question_question_nullChecked() async {
424435
await analyze('''
425436
Object/*!*/ _f(int/*?*/ x, double/*?*/ y) {

0 commit comments

Comments
 (0)