Skip to content

Commit 389ad7e

Browse files
committed
Issue 32114. Fix instantiating typedefs to bounds.
[email protected] Bug: #32114 Change-Id: I215eafb6314fe265d4809ff6ed80e4a567365a4f Reviewed-on: https://dart-review.googlesource.com/46381 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f5ec114 commit 389ad7e

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
9494
/**
9595
* The version of data format, should be incremented on every format change.
9696
*/
97-
static const int DATA_VERSION = 50;
97+
static const int DATA_VERSION = 51;
9898

9999
/**
100100
* The number of exception contexts allowed to write. Once this field is

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8397,7 +8397,9 @@ class TypeNameResolver {
83978397
}
83988398
} else {
83998399
if (element is GenericTypeAliasElementImpl) {
8400-
type = element.typeAfterSubstitution(null) ?? dynamicType;
8400+
List<DartType> typeArguments =
8401+
typeSystem.instantiateTypeFormalsToBounds(element.typeParameters);
8402+
type = element.typeAfterSubstitution(typeArguments) ?? dynamicType;
84018403
} else {
84028404
type = typeSystem.instantiateToBounds(type);
84038405
}

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,28 @@ class StrongTypeSystemImpl extends TypeSystem {
309309
DartType instantiateToBounds(DartType type,
310310
{List<bool> hasError, Map<TypeParameterType, DartType> knownTypes}) {
311311
List<TypeParameterElement> typeFormals = typeFormalsAsElements(type);
312+
List<DartType> arguments = instantiateTypeFormalsToBounds(typeFormals,
313+
hasError: hasError, knownTypes: knownTypes);
314+
if (arguments == null) {
315+
return type;
316+
}
317+
318+
return instantiateType(type, arguments);
319+
}
320+
321+
/**
322+
* Given uninstantiated [typeFormals], instantiate them to their bounds.
323+
* See the issue for the algorithm description.
324+
*
325+
* https://github.com/dart-lang/sdk/issues/27526#issuecomment-260021397
326+
*/
327+
List<DartType> instantiateTypeFormalsToBounds(
328+
List<TypeParameterElement> typeFormals,
329+
{List<bool> hasError,
330+
Map<TypeParameterType, DartType> knownTypes}) {
312331
int count = typeFormals.length;
313332
if (count == 0) {
314-
return type;
333+
return null;
315334
}
316335

317336
Set<TypeParameterType> all = new Set<TypeParameterType>();
@@ -394,7 +413,7 @@ class StrongTypeSystemImpl extends TypeSystem {
394413

395414
List<DartType> orderedArguments =
396415
typeFormals.map((p) => defaults[p.type]).toList();
397-
return instantiateType(type, orderedArguments);
416+
return orderedArguments;
398417
}
399418

400419
@override
@@ -1222,6 +1241,13 @@ abstract class TypeSystem {
12221241
}
12231242
}
12241243

1244+
/**
1245+
* Given uninstantiated [typeFormals], instantiate them to their bounds.
1246+
*/
1247+
List<DartType> instantiateTypeFormalsToBounds(
1248+
List<TypeParameterElement> typeFormals,
1249+
{List<bool> hasError});
1250+
12251251
/**
12261252
* Return `true` if the [leftType] is assignable to the [rightType] (that is,
12271253
* if leftType <==> rightType).
@@ -1530,6 +1556,13 @@ class TypeSystemImpl extends TypeSystem {
15301556
return type;
15311557
}
15321558

1559+
@override
1560+
List<DartType> instantiateTypeFormalsToBounds(
1561+
List<TypeParameterElement> typeFormals,
1562+
{List<bool> hasError}) {
1563+
return null;
1564+
}
1565+
15331566
@override
15341567
bool isAssignableTo(DartType leftType, DartType rightType,
15351568
{bool isDeclarationCast = false}) {

pkg/analyzer/lib/src/summary/resynthesize.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,9 +1299,32 @@ class _ReferenceInfo {
12991299
return type;
13001300
} else if (element is GenericTypeAliasElementHandle) {
13011301
GenericTypeAliasElementImpl actualElement = element.actualElement;
1302-
List<DartType> argumentTypes =
1303-
new List.generate(numTypeArguments, getTypeArgument);
1304-
return actualElement.typeAfterSubstitution(argumentTypes);
1302+
List<DartType> typeArguments;
1303+
if (numTypeArguments == numTypeParameters) {
1304+
typeArguments = _buildTypeArguments(numTypeArguments, getTypeArgument);
1305+
} else if (libraryResynthesizer.summaryResynthesizer.strongMode &&
1306+
instantiateToBoundsAllowed) {
1307+
if (!_isBeingInstantiatedToBounds) {
1308+
_isBeingInstantiatedToBounds = true;
1309+
_isRecursiveWhileInstantiateToBounds = false;
1310+
try {
1311+
typeArguments = libraryResynthesizer
1312+
.summaryResynthesizer.context.typeSystem
1313+
.instantiateTypeFormalsToBounds(element.typeParameters);
1314+
if (_isRecursiveWhileInstantiateToBounds) {
1315+
typeArguments = _dynamicTypeArguments;
1316+
}
1317+
} finally {
1318+
_isBeingInstantiatedToBounds = false;
1319+
}
1320+
} else {
1321+
_isRecursiveWhileInstantiateToBounds = true;
1322+
typeArguments = _dynamicTypeArguments;
1323+
}
1324+
} else {
1325+
typeArguments = _dynamicTypeArguments;
1326+
}
1327+
return actualElement.typeAfterSubstitution(typeArguments);
13051328
} else if (element is FunctionTypedElement) {
13061329
if (element is FunctionTypeAliasElementHandle) {
13071330
List<DartType> typeArguments;

pkg/analyzer/test/src/summary/resynthesize_ast_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ class ResynthesizeAstStrongTest extends _ResynthesizeAstTest {
6666
test_const_constructor_inferred_args() =>
6767
test_const_constructor_inferred_args();
6868

69-
@override
70-
@failingTest
71-
test_instantiateToBounds_functionTypeAlias_simple() async {
72-
await super.test_instantiateToBounds_functionTypeAlias_simple();
73-
}
74-
7569
@override
7670
@failingTest
7771
test_syntheticFunctionType_genericClosure() async {

pkg/analyzer/test/src/task/strong/checker_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,6 @@ void main() {
12931293
''');
12941294
}
12951295

1296-
@failingTest
12971296
test_fuzzyArrowLegacyAssignability_GlobalInference() async {
12981297
// Test for legacy fuzzy arrow support on assignability, pending
12991298
// cleanup. https://github.com/dart-lang/sdk/issues/29630

0 commit comments

Comments
 (0)