Skip to content

Commit 32e27f6

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: Add the ability to use null for nullability nodes in substitutions.
A nullability node of `null` will be discarded during a substitution. For instance, attempting to substitute `T=int?null` into `T?1` will produce `int?1`, rather than `int?2` where `2` is a substitution node. We will need this functionality in order to compare function types for equality, which is in turn needed to integrate with flow analysis. Change-Id: Ife5e3761b083d4584b2aff760dcb104529e7f9d4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112745 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 22eaf08 commit 32e27f6

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

pkg/nnbd_migration/lib/src/nullability_node.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,14 @@ abstract class NullabilityNode {
410410
/// variable being eliminated by the substitution, and [innerNode] is the
411411
/// nullability node for the type being substituted in its place.
412412
///
413-
/// [innerNode] may be `null`. TODO(paulberry): when?
414-
///
415-
/// Additional constraints are recorded in [constraints] as necessary to make
416-
/// the new nullability node behave consistently with the old nodes.
417-
/// TODO(paulberry): this should become unnecessary once constraint solving is
418-
/// performed directly using [NullabilityNode] objects.
413+
/// If either [innerNode] or [outerNode] is `null`, then the other node is
414+
/// returned.
419415
factory NullabilityNode.forSubstitution(
420-
NullabilityNode innerNode, NullabilityNode outerNode) =
421-
NullabilityNodeForSubstitution._;
416+
NullabilityNode innerNode, NullabilityNode outerNode) {
417+
if (innerNode == null) return outerNode;
418+
if (outerNode == null) return innerNode;
419+
return NullabilityNodeForSubstitution._(innerNode, outerNode);
420+
}
422421

423422
/// Creates a [NullabilityNode] representing the nullability of a type
424423
/// annotation appearing explicitly in the user's program.

pkg/nnbd_migration/test/nullability_node_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,12 @@ class NullabilityNodeTest {
594594
expect(edge.isSatisfied, true);
595595
}
596596

597+
test_substitution_simplify_null() {
598+
var n1 = newNode(1);
599+
expect(subst(null, n1), same(n1));
600+
expect(subst(n1, null), same(n1));
601+
}
602+
597603
test_unconstrainted_node_non_nullable() {
598604
var n1 = newNode(1);
599605
propagate();

0 commit comments

Comments
 (0)