Skip to content

Commit b912aeb

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Flow analysis: change tryPromoteToNonNull to promoteToNonNull.
The flow analysis engine is now responsible for figuring out whether a promotion actually occurred; this is less error prone. Change-Id: Idce8af01fae982a9cc240ec4675812f8d7fd68a0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109883 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 7adca0e commit b912aeb

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,9 @@ class State<Variable, Type> {
684684
TypeOperations<Variable, Type> typeOperations, Variable variable) {
685685
var previousType = promoted[variable];
686686
previousType ??= typeOperations.variableType(variable);
687-
var type = typeOperations.tryPromoteToNonNull(previousType);
687+
var type = typeOperations.promoteToNonNull(previousType);
688688

689-
if (type != null) {
689+
if (!typeOperations.isSameType(type, previousType)) {
690690
var newPromoted = <Variable, Type>{}..addAll(promoted);
691691
newPromoted[variable] = type;
692692
return State<Variable, Type>._(
@@ -924,12 +924,11 @@ abstract class TypeOperations<Variable, Type> {
924924
/// Return `true` if the [leftType] is a subtype of the [rightType].
925925
bool isSubtypeOf(Type leftType, Type rightType);
926926

927-
/// Returns the non-null promoted version of [type], if it is different,
928-
/// otherwise `null`. For example, given `int?`, returns `int`.
927+
/// Returns the non-null promoted version of [type].
929928
///
930929
/// Note that some types don't have a non-nullable version (e.g.
931-
/// `FutureOr<int?>`), so `null` may be returned even if the type is nullable.
932-
Type tryPromoteToNonNull(Type type);
930+
/// `FutureOr<int?>`), so [type] may be returned even if it is nullable.
931+
Type /*!*/ promoteToNonNull(Type type);
933932

934933
/// Return the static type of the given [variable].
935934
Type variableType(Variable variable);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,8 @@ class _TypeSystemTypeOperations
479479
}
480480

481481
@override
482-
DartType tryPromoteToNonNull(covariant TypeImpl type) {
483-
TypeImpl promotedType = typeSystem.promoteToNonNull(type);
484-
return isSameType(type, promotedType) ? null : promotedType;
482+
DartType promoteToNonNull(DartType type) {
483+
return typeSystem.promoteToNonNull(type);
485484
}
486485

487486
@override

pkg/analyzer/test/src/dart/resolution/flow_analysis_unit_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,12 @@ class _Harness
500500
}
501501

502502
@override
503-
_Type tryPromoteToNonNull(_Type type) {
503+
_Type promoteToNonNull(_Type type) {
504504
if (type.type.endsWith('?')) {
505505
return _Type(type.type.substring(0, type.type.length - 1));
506+
} else {
507+
return type;
506508
}
507-
return null;
508509
}
509510

510511
@override

pkg/nnbd_migration/lib/src/decorated_type_operations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DecoratedTypeOperations
3232
}
3333

3434
@override
35-
DecoratedType tryPromoteToNonNull(DecoratedType type) {
35+
DecoratedType promoteToNonNull(DecoratedType type) {
3636
throw new UnimplementedError('TODO(paulberry)');
3737
}
3838

0 commit comments

Comments
 (0)