Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 22c086f

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Remove unsafe uses of TypeImpl.withNullability (use TypeSystem instead).
Change-Id: I5df47ca582aa5583055d3700668de992676bc42a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103541 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent aa785d9 commit 22c086f

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

pkg/analyzer/lib/src/dart/element/type.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,12 @@ abstract class TypeImpl implements DartType {
29582958
*
29592959
* If the nullability of `this` already matches [nullabilitySuffix], `this`
29602960
* is returned.
2961+
*
2962+
* Note: this method just does low-level manipulations of the underlying type,
2963+
* so it is what you want if you are constructing a fresh type and want it to
2964+
* have the correct nullability suffix, but it is generally *not* what you
2965+
* want if you're manipulating existing types. For manipulating existing
2966+
* types, please use the methods in [TypeSystem].
29612967
*/
29622968
TypeImpl withNullability(NullabilitySuffix nullabilitySuffix);
29632969

pkg/analyzer/lib/src/generated/static_type_analyzer.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<void> {
778778
TypeImpl staticType = _getStaticType(operand, read: true);
779779

780780
if (node.operator.type == TokenType.BANG) {
781-
// TODO(paulberry): This does the wrong thing if staticType is a type
782-
// parameter type.
783-
staticType = staticType.withNullability(NullabilitySuffix.none);
781+
staticType = _typeSystem.promoteToNonNull(staticType);
784782
} else {
785783
// No need to check for `intVar++`, the result is `int`.
786784
if (!staticType.isDartCoreInt) {
@@ -916,8 +914,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<void> {
916914

917915
if (node.operator.type == TokenType.QUESTION_PERIOD &&
918916
_nonNullableEnabled) {
919-
staticType =
920-
(staticType as TypeImpl).withNullability(NullabilitySuffix.question);
917+
staticType = _typeSystem.makeNullable(staticType);
921918
}
922919
staticType = _inferGenericInstantiationFromContext(node, staticType);
923920

@@ -1235,22 +1232,24 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<void> {
12351232
* Compute the return type of the method or function represented by the given
12361233
* type that is being invoked.
12371234
*/
1238-
DartType _computeInvokeReturnType(DartType type,
1235+
DartType /*!*/ _computeInvokeReturnType(DartType type,
12391236
{@required bool isNullableInvoke}) {
1240-
TypeImpl returnType;
1237+
TypeImpl /*!*/ returnType;
12411238
if (type is InterfaceType) {
12421239
MethodElement callMethod = type.lookUpMethod(
12431240
FunctionElement.CALL_METHOD_NAME, _resolver.definingLibrary);
1244-
returnType = callMethod?.type?.returnType;
1241+
returnType = callMethod?.type?.returnType ?? _dynamicType;
12451242
} else if (type is FunctionType) {
1246-
returnType = type.returnType;
1243+
returnType = type.returnType ?? _dynamicType;
1244+
} else {
1245+
returnType = _dynamicType;
12471246
}
12481247

12491248
if (isNullableInvoke && _nonNullableEnabled) {
1250-
returnType = returnType?.withNullability(NullabilitySuffix.question);
1249+
returnType = _typeSystem.makeNullable(returnType);
12511250
}
12521251

1253-
return returnType ?? _dynamicType;
1252+
return returnType;
12541253
}
12551254

12561255
/**

pkg/analyzer/lib/src/generated/type_system.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,13 @@ abstract class TypeSystem implements public.TypeSystem {
20462046
return getLeastUpperBound(leftType, rightType);
20472047
}
20482048

2049+
/// Returns a nullable version of [type]. The result would be equivalent to
2050+
/// the union `type | Null` (if we supported union types).
2051+
DartType makeNullable(TypeImpl type) {
2052+
// TODO(paulberry): handle type parameter types
2053+
return type.withNullability(NullabilitySuffix.question);
2054+
}
2055+
20492056
/// Attempts to find the appropriate substitution for [typeParameters] that can
20502057
/// be applied to [src] to make it equal to [dest]. If no such substitution can
20512058
/// be found, `null` is returned.

0 commit comments

Comments
 (0)