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

Commit 3c34c8f

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Fix type substitution of "star" types.
When performing type substitution, there are three types involved, the substitution site (`this` in TypeImpl.substitute2), the type we are replacing (`parameterType` in TypeImpl.substitute2), and the replacement type (`argumentType` in TypeImpl.substitute2). Previously, we only handled the nullabilities of `this` and `argumentType`. This CL adds support for the possibility that `parameterType` might have nullability "*". This happens because TypeParameterElement.type returns a star type, so for instance when subsituting `int` for `T` in `List<T>` to form `List<int>`, we are actually substituting `int` for `T*` in `List<T*>`. Change-Id: I0a61fdc47ec8aa205dc0c539c49ea7799ed4ac05 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103542 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 77f2603 commit 3c34c8f

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,7 +3244,8 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
32443244
[List<FunctionTypeAliasElement> prune]) {
32453245
int length = parameterTypes.length;
32463246
for (int i = 0; i < length; i++) {
3247-
if (parameterTypes[i] == this) {
3247+
var parameterType = parameterTypes[i];
3248+
if (parameterType is TypeParameterTypeImpl && parameterType == this) {
32483249
TypeImpl argumentType = argumentTypes[i];
32493250

32503251
// TODO(scheglov) It should not happen, but sometimes arguments are null.
@@ -3254,15 +3255,29 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
32543255

32553256
// TODO(scheglov) Proposed substitution rules for nullability.
32563257
NullabilitySuffix resultNullability;
3258+
NullabilitySuffix parameterNullability =
3259+
parameterType.nullabilitySuffix;
32573260
NullabilitySuffix argumentNullability = argumentType.nullabilitySuffix;
3258-
if (argumentNullability == NullabilitySuffix.question ||
3259-
nullabilitySuffix == NullabilitySuffix.question) {
3260-
resultNullability = NullabilitySuffix.question;
3261-
} else if (argumentNullability == NullabilitySuffix.star ||
3262-
nullabilitySuffix == NullabilitySuffix.star) {
3263-
resultNullability = NullabilitySuffix.star;
3261+
if (parameterNullability == NullabilitySuffix.none) {
3262+
if (argumentNullability == NullabilitySuffix.question ||
3263+
nullabilitySuffix == NullabilitySuffix.question) {
3264+
resultNullability = NullabilitySuffix.question;
3265+
} else if (argumentNullability == NullabilitySuffix.star ||
3266+
nullabilitySuffix == NullabilitySuffix.star) {
3267+
resultNullability = NullabilitySuffix.star;
3268+
} else {
3269+
resultNullability = NullabilitySuffix.none;
3270+
}
3271+
} else if (parameterNullability == NullabilitySuffix.star) {
3272+
if (argumentNullability == NullabilitySuffix.question ||
3273+
nullabilitySuffix == NullabilitySuffix.question) {
3274+
resultNullability = NullabilitySuffix.question;
3275+
} else {
3276+
resultNullability = argumentNullability;
3277+
}
32643278
} else {
3265-
resultNullability = NullabilitySuffix.none;
3279+
// We should never be substituting for `T?`.
3280+
throw new StateError('Tried to substitute for T?');
32663281
}
32673282

32683283
return argumentType.withNullability(resultNullability);

pkg/analyzer/test/src/diagnostics/default_list_constructor_mismatch_test.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ var l = new List<String?>(3);
3636
''');
3737
}
3838

39-
@failingTest
4039
test_inferredType() async {
41-
// This test is failing because summary support is incomplete, which results
42-
// in the constructor having a type of 'List<C*>*'.
4340
await assertErrorsInCode('''
4441
class C {}
4542
List<C> v = List(5);
@@ -48,16 +45,6 @@ List<C> v = List(5);
4845
]);
4946
}
5047

51-
test_starType() async {
52-
// TODO(brianwilkerson) This test is currently taking advantage of the fact
53-
// that the SDK is not opted in, which makes the use of `int` below a
54-
// reference to 'int*'. When it's possible to opt-out in a test this needs
55-
// to be updated to use an explicitly opted out type.
56-
await assertNoErrorsInCode('''
57-
List<int> v = List(5);
58-
''');
59-
}
60-
6148
test_typeParameter() async {
6249
await assertErrorsInCode('''
6350
class C<T> {

0 commit comments

Comments
 (0)