Skip to content

Commit b19e907

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Non-nullify types after inference.
Change-Id: Ie50ea84d18bc44799cadc797c76d6506539de719 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150842 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent b0484ec commit b19e907

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:analyzer/src/dart/element/nullability_eliminator.dart';
1414
import 'package:analyzer/src/dart/element/type.dart';
1515
import 'package:analyzer/src/dart/element/type_algebra.dart';
1616
import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
17+
import 'package:analyzer/src/dart/element/type_demotion.dart';
1718
import 'package:analyzer/src/dart/element/type_schema.dart';
1819
import 'package:analyzer/src/error/codes.dart' show HintCode, StrongModeCode;
1920
import 'package:analyzer/src/generated/type_system.dart';
@@ -257,6 +258,7 @@ class GenericInferrer {
257258
}
258259
}
259260

261+
_nonNullifyTypes(result);
260262
return result;
261263
}
262264

@@ -461,6 +463,14 @@ class GenericInferrer {
461463
return t;
462464
}
463465

466+
void _nonNullifyTypes(List<DartType> types) {
467+
if (_typeSystem.isNonNullableByDefault) {
468+
for (var i = 0; i < types.length; i++) {
469+
types[i] = nonNullifyType(_typeSystem, types[i]);
470+
}
471+
}
472+
}
473+
464474
/// If in a legacy library, return the legacy version of the [type].
465475
/// Otherwise, return the original type.
466476
DartType _toLegacyType(DartType type) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/type.dart';
88
import 'package:analyzer/src/dart/element/replacement_visitor.dart';
99
import 'package:analyzer/src/dart/element/type.dart';
1010
import 'package:analyzer/src/dart/element/type_visitor.dart';
11+
import 'package:analyzer/src/generated/type_system.dart';
1112

1213
/// Returns [type] in which all promoted type variables have been replace with
1314
/// their unpromoted equivalents, and, if [library] is non-nullable by default,
@@ -29,8 +30,8 @@ bool hasPromotedTypeVariable(DartType type) {
2930

3031
/// Returns [type] in which all legacy types have been replaced with
3132
/// non-nullable types.
32-
DartType nonNullifyType(LibraryElement library, DartType type) {
33-
if (library.isNonNullableByDefault && type != null) {
33+
DartType nonNullifyType(TypeSystemImpl typeSystem, DartType type) {
34+
if (typeSystem.isNonNullableByDefault && type != null) {
3435
var visitor = const _DemotionNonNullification(demoteTypeVariables: false);
3536
return visitor.visit(type) ?? type;
3637
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FunctionExpressionResolver {
5151
}
5252

5353
var contextType = InferenceContext.getContext(node);
54-
contextType = nonNullifyType(_resolver.definingLibrary, contextType);
54+
contextType = nonNullifyType(_typeSystem, contextType);
5555

5656
if (contextType is FunctionType) {
5757
contextType = _matchFunctionTypeParameters(

pkg/analyzer/test/src/dart/element/generic_inferrer_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ class GenericFunctionInferenceTest extends AbstractTypeSystemNullSafetyTest {
479479
_assertTypes(_inferCall(f, [], returnType: stringNone), [stringNone]);
480480
}
481481

482+
void test_returnTypeFromContext_nonNullify() {
483+
// <T>() -> T
484+
var T = typeParameter('T');
485+
var f = functionTypeNone(
486+
typeFormals: [T],
487+
returnType: typeParameterTypeNone(T),
488+
);
489+
_assertTypes(_inferCall(f, [], returnType: intStar), [intNone]);
490+
}
491+
482492
void test_returnTypeWithBoundFromContext() {
483493
// <T extends num>() -> T
484494
var T = typeParameter('T', bound: numNone);

0 commit comments

Comments
 (0)