@@ -318,7 +318,7 @@ class Dart2TypeSystem extends TypeSystem {
318
318
* and generic functions; compute lazily and cache.
319
319
*/
320
320
DartType instantiateToBounds (DartType type,
321
- {List <bool > hasError, Map <TypeParameterType , DartType > knownTypes}) {
321
+ {List <bool > hasError, Map <TypeParameterElement , DartType > knownTypes}) {
322
322
List <TypeParameterElement > typeFormals = typeFormalsAsElements (type);
323
323
List <DartType > arguments = instantiateTypeFormalsToBounds (typeFormals,
324
324
hasError: hasError, knownTypes: knownTypes);
@@ -338,20 +338,19 @@ class Dart2TypeSystem extends TypeSystem {
338
338
List <DartType > instantiateTypeFormalsToBounds (
339
339
List <TypeParameterElement > typeFormals,
340
340
{List <bool > hasError,
341
- Map <TypeParameterType , DartType > knownTypes}) {
341
+ Map <TypeParameterElement , DartType > knownTypes}) {
342
342
int count = typeFormals.length;
343
343
if (count == 0 ) {
344
344
return const < DartType > [];
345
345
}
346
346
347
- Set <TypeParameterType > all = new Set <TypeParameterType >();
347
+ Set <TypeParameterElement > all = new Set <TypeParameterElement >();
348
348
// all ground
349
- Map <TypeParameterType , DartType > defaults = knownTypes ?? {};
349
+ Map <TypeParameterElement , DartType > defaults = knownTypes ?? {};
350
350
// not ground
351
- Map <TypeParameterType , DartType > partials = {};
351
+ Map <TypeParameterElement , DartType > partials = {};
352
352
353
- for (TypeParameterElement typeParameterElement in typeFormals) {
354
- TypeParameterType typeParameter = typeParameterElement.type;
353
+ for (TypeParameterElement typeParameter in typeFormals) {
355
354
all.add (typeParameter);
356
355
if (! defaults.containsKey (typeParameter)) {
357
356
if (typeParameter.bound == null ) {
@@ -362,8 +361,8 @@ class Dart2TypeSystem extends TypeSystem {
362
361
}
363
362
}
364
363
365
- List <TypeParameterType > getFreeParameters (DartType rootType) {
366
- List <TypeParameterType > parameters = null ;
364
+ List <TypeParameterElement > getFreeParameters (DartType rootType) {
365
+ List <TypeParameterElement > parameters = null ;
367
366
Set <DartType > visitedTypes = new HashSet <DartType >();
368
367
369
368
void appendParameters (DartType type) {
@@ -374,9 +373,12 @@ class Dart2TypeSystem extends TypeSystem {
374
373
return ;
375
374
}
376
375
visitedTypes.add (type);
377
- if (type is TypeParameterType && all.contains (type)) {
378
- parameters ?? = < TypeParameterType > [];
379
- parameters.add (type);
376
+ if (type is TypeParameterType ) {
377
+ var element = type.element;
378
+ if (all.contains (element)) {
379
+ parameters ?? = < TypeParameterElement > [];
380
+ parameters.add (element);
381
+ }
380
382
} else if (AnalysisDriver .useSummary2) {
381
383
if (type is FunctionType ) {
382
384
appendParameters (type.returnType);
@@ -396,17 +398,17 @@ class Dart2TypeSystem extends TypeSystem {
396
398
bool hasProgress = true ;
397
399
while (hasProgress) {
398
400
hasProgress = false ;
399
- for (TypeParameterType parameter in partials.keys) {
401
+ for (TypeParameterElement parameter in partials.keys) {
400
402
DartType value = partials[parameter];
401
- List <TypeParameterType > freeParameters = getFreeParameters (value);
403
+ List <TypeParameterElement > freeParameters = getFreeParameters (value);
402
404
if (freeParameters == null ) {
403
405
defaults[parameter] = value;
404
406
partials.remove (parameter);
405
407
hasProgress = true ;
406
408
break ;
407
409
} else if (freeParameters.every (defaults.containsKey)) {
408
- defaults[parameter] = value. substitute2 (
409
- defaults.values. toList (), defaults.keys. toList () );
410
+ defaults[parameter] =
411
+ Substitution . fromMap ( defaults). substituteType (value );
410
412
partials.remove (parameter);
411
413
hasProgress = true ;
412
414
break ;
@@ -426,19 +428,20 @@ class Dart2TypeSystem extends TypeSystem {
426
428
var range = defaults.values.toList ();
427
429
// Build a substitution Phi mapping each uncompleted type variable to
428
430
// dynamic, and each completed type variable to its default.
429
- for (TypeParameterType parameter in partials.keys) {
431
+ for (TypeParameterElement parameter in partials.keys) {
430
432
domain.add (parameter);
431
433
range.add (DynamicTypeImpl .instance);
432
434
}
433
435
// Set the default for an uncompleted type variable (T extends B)
434
436
// to be Phi(B)
435
- for (TypeParameterType parameter in partials.keys) {
436
- defaults[parameter] = partials[parameter].substitute2 (range, domain);
437
+ for (TypeParameterElement parameter in partials.keys) {
438
+ defaults[parameter] = Substitution .fromPairs (domain, range)
439
+ .substituteType (partials[parameter]);
437
440
}
438
441
}
439
442
440
443
List <DartType > orderedArguments =
441
- typeFormals.map ((p) => defaults[p.type ]).toList ();
444
+ typeFormals.map ((p) => defaults[p]).toList ();
442
445
return orderedArguments;
443
446
}
444
447
@@ -1217,18 +1220,15 @@ class Dart2TypeSystem extends TypeSystem {
1217
1220
class GenericInferrer {
1218
1221
final Dart2TypeSystem _typeSystem;
1219
1222
final TypeProvider typeProvider;
1220
- final Map <TypeParameterElement , List <_TypeConstraint >> constraints;
1223
+ final Map <TypeParameterElement , List <_TypeConstraint >> constraints = {} ;
1221
1224
1222
1225
/// Buffer recording constraints recorded while performing a recursive call to
1223
1226
/// [_matchSubtypeOf] that might fail, so that any constraints recorded during
1224
1227
/// the failed match can be rewound.
1225
1228
final _undoBuffer = < _TypeConstraint > [];
1226
1229
1227
1230
GenericInferrer (this .typeProvider, this ._typeSystem,
1228
- Iterable <TypeParameterElement > typeFormals)
1229
- : constraints = new HashMap (
1230
- equals: (x, y) => x.location == y.location,
1231
- hashCode: (x) => x.location.hashCode) {
1231
+ Iterable <TypeParameterElement > typeFormals) {
1232
1232
for (var formal in typeFormals) {
1233
1233
constraints[formal] = [];
1234
1234
}
@@ -1280,30 +1280,29 @@ class GenericInferrer {
1280
1280
AstNode errorNode,
1281
1281
bool failAtError: false ,
1282
1282
bool downwardsInferPhase: false }) {
1283
- var fnTypeParams = TypeParameterTypeImpl .getTypes (typeFormals);
1284
-
1285
1283
// Initialize the inferred type array.
1286
1284
//
1287
1285
// In the downwards phase, they all start as `?` to offer reasonable
1288
1286
// degradation for f-bounded type parameters.
1289
1287
var inferredTypes = new List <DartType >.filled (
1290
- fnTypeParams .length, UnknownInferredType .instance);
1288
+ typeFormals .length, UnknownInferredType .instance);
1291
1289
var _inferTypeParameter = downwardsInferPhase
1292
1290
? _inferTypeParameterFromContext
1293
1291
: _inferTypeParameterFromAll;
1294
1292
1295
- for (int i = 0 ; i < fnTypeParams .length; i++ ) {
1296
- TypeParameterType typeParam = fnTypeParams [i];
1293
+ for (int i = 0 ; i < typeFormals .length; i++ ) {
1294
+ TypeParameterElement typeParam = typeFormals [i];
1297
1295
1298
- var typeParamBound = typeParam.bound;
1299
1296
_TypeConstraint extendsClause;
1300
- if (considerExtendsClause && ! typeParamBound.isDynamic) {
1301
- extendsClause = new _TypeConstraint .fromExtends (typeParam,
1302
- typeParam.bound.substitute2 (inferredTypes, fnTypeParams));
1297
+ if (considerExtendsClause && typeParam.bound != null ) {
1298
+ extendsClause = new _TypeConstraint .fromExtends (
1299
+ typeParam,
1300
+ Substitution .fromPairs (typeFormals, inferredTypes)
1301
+ .substituteType (typeParam.bound));
1303
1302
}
1304
1303
1305
1304
inferredTypes[i] =
1306
- _inferTypeParameter (constraints[typeParam.element ], extendsClause);
1305
+ _inferTypeParameter (constraints[typeParam], extendsClause);
1307
1306
}
1308
1307
1309
1308
// If the downwards infer phase has failed, we'll catch this in the upwards
@@ -1313,14 +1312,14 @@ class GenericInferrer {
1313
1312
}
1314
1313
1315
1314
// Check the inferred types against all of the constraints.
1316
- var knownTypes = new HashMap < TypeParameterType , DartType >(
1317
- equals : (x, y) => x.element == y.element,
1318
- hashCode : (x) => x.element.hashCode) ;
1319
- for ( int i = 0 ; i < fnTypeParams.length; i ++ ) {
1320
- TypeParameterType typeParam = fnTypeParams[i];
1321
- var constraints = this .constraints[typeParam.element];
1322
- var typeParamBound =
1323
- typeParam.bound. substitute2 (inferredTypes, fnTypeParams) ;
1315
+ var knownTypes = < TypeParameterElement , DartType > {};
1316
+ for ( int i = 0 ; i < typeFormals.length; i ++ ) {
1317
+ TypeParameterElement typeParam = typeFormals[i] ;
1318
+ var constraints = this .constraints[typeParam];
1319
+ var typeParamBound = typeParam.bound != null
1320
+ ? Substitution . fromPairs (typeFormals, inferredTypes)
1321
+ . substituteType (typeParam.bound)
1322
+ : typeProvider.dynamicType ;
1324
1323
1325
1324
var inferred = inferredTypes[i];
1326
1325
bool success =
@@ -1338,7 +1337,7 @@ class GenericInferrer {
1338
1337
errorReporter? .reportErrorForNode (
1339
1338
StrongModeCode .COULD_NOT_INFER ,
1340
1339
errorNode,
1341
- [typeParam, _formatError (typeParam, inferred, constraints)]);
1340
+ [typeParam.name , _formatError (typeParam, inferred, constraints)]);
1342
1341
1343
1342
// Heuristic: even if we failed, keep the erroneous type.
1344
1343
// It should satisfy at least some of the constraints (e.g. the return
@@ -1350,7 +1349,7 @@ class GenericInferrer {
1350
1349
if (failAtError) return null ;
1351
1350
errorReporter
1352
1351
? .reportErrorForNode (StrongModeCode .COULD_NOT_INFER , errorNode, [
1353
- typeParam,
1352
+ typeParam.name ,
1354
1353
' Inferred candidate type $inferred has type parameters'
1355
1354
' ${(inferred as FunctionType ).typeFormals }, but a function with'
1356
1355
' type parameters cannot be used as a type argument.'
@@ -1383,21 +1382,21 @@ class GenericInferrer {
1383
1382
}
1384
1383
1385
1384
// Use instantiate to bounds to finish things off.
1386
- var hasError = new List <bool >.filled (fnTypeParams .length, false );
1385
+ var hasError = new List <bool >.filled (typeFormals .length, false );
1387
1386
var result = _typeSystem.instantiateTypeFormalsToBounds (typeFormals,
1388
1387
hasError: hasError, knownTypes: knownTypes);
1389
1388
1390
1389
// Report any errors from instantiateToBounds.
1391
1390
for (int i = 0 ; i < hasError.length; i++ ) {
1392
1391
if (hasError[i]) {
1393
1392
if (failAtError) return null ;
1394
- TypeParameterType typeParam = fnTypeParams [i];
1395
- var typeParamBound =
1396
- typeParam.bound. substitute2 (inferredTypes, fnTypeParams );
1393
+ TypeParameterElement typeParam = typeFormals [i];
1394
+ var typeParamBound = Substitution . fromPairs (typeFormals, inferredTypes)
1395
+ . substituteType ( typeParam.bound ?? typeProvider.objectType );
1397
1396
// TODO(jmesserly): improve this error message.
1398
1397
errorReporter
1399
1398
? .reportErrorForNode (StrongModeCode .COULD_NOT_INFER , errorNode, [
1400
- typeParam,
1399
+ typeParam.name ,
1401
1400
"\n Recursive bound cannot be instantiated: '$typeParamBound '."
1402
1401
"\n Consider passing explicit type argument(s) "
1403
1402
"to the generic.\n\n '"
@@ -1485,9 +1484,9 @@ class GenericInferrer {
1485
1484
return lower;
1486
1485
}
1487
1486
1488
- String _formatError (TypeParameterType typeParam, DartType inferred,
1487
+ String _formatError (TypeParameterElement typeParam, DartType inferred,
1489
1488
Iterable <_TypeConstraint > constraints) {
1490
- var intro = "Tried to infer '$inferred ' for '$typeParam '"
1489
+ var intro = "Tried to infer '$inferred ' for '${ typeParam . name } '"
1491
1490
" which doesn't work:" ;
1492
1491
1493
1492
var constraintsByOrigin = < _TypeConstraintOrigin , List <_TypeConstraint >> {};
@@ -1680,7 +1679,7 @@ class GenericInferrer {
1680
1679
var constraints = this .constraints[t1.element];
1681
1680
if (constraints != null ) {
1682
1681
if (! identical (t2, UnknownInferredType .instance)) {
1683
- var constraint = new _TypeConstraint (origin, t1, upper: t2);
1682
+ var constraint = new _TypeConstraint (origin, t1.element , upper: t2);
1684
1683
constraints.add (constraint);
1685
1684
_undoBuffer.add (constraint);
1686
1685
}
@@ -1691,7 +1690,7 @@ class GenericInferrer {
1691
1690
var constraints = this .constraints[t2.element];
1692
1691
if (constraints != null ) {
1693
1692
if (! identical (t1, UnknownInferredType .instance)) {
1694
- var constraint = new _TypeConstraint (origin, t2, lower: t1);
1693
+ var constraint = new _TypeConstraint (origin, t2.element , lower: t1);
1695
1694
constraints.add (constraint);
1696
1695
_undoBuffer.add (constraint);
1697
1696
}
@@ -1831,7 +1830,7 @@ class GenericInferrer {
1831
1830
void _rewindConstraints (int previousRewindBufferLength) {
1832
1831
while (_undoBuffer.length > previousRewindBufferLength) {
1833
1832
var constraint = _undoBuffer.removeLast ();
1834
- var element = constraint.typeParameter.element ;
1833
+ var element = constraint.typeParameter;
1835
1834
assert (identical (constraints[element].last, constraint));
1836
1835
constraints[element].removeLast ();
1837
1836
}
@@ -2985,7 +2984,7 @@ class UnknownInferredTypeElement extends ElementImpl
2985
2984
/// A constraint on a type parameter that we're inferring.
2986
2985
class _TypeConstraint extends _TypeRange {
2987
2986
/// The type parameter that is constrained by [lowerBound] or [upperBound] .
2988
- final TypeParameterType typeParameter;
2987
+ final TypeParameterElement typeParameter;
2989
2988
2990
2989
/// Where this constraint comes from, used for error messages.
2991
2990
///
@@ -2996,8 +2995,10 @@ class _TypeConstraint extends _TypeRange {
2996
2995
{DartType upper, DartType lower})
2997
2996
: super (upper: upper, lower: lower);
2998
2997
2999
- _TypeConstraint .fromExtends (TypeParameterType type, DartType extendsType)
3000
- : this (new _TypeConstraintFromExtendsClause (type, extendsType), type,
2998
+ _TypeConstraint .fromExtends (
2999
+ TypeParameterElement element, DartType extendsType)
3000
+ : this (
3001
+ new _TypeConstraintFromExtendsClause (element, extendsType), element,
3001
3002
upper: extendsType);
3002
3003
3003
3004
bool get isDownwards => origin is ! _TypeConstraintFromArgument ;
@@ -3049,15 +3050,15 @@ class _TypeConstraintFromArgument extends _TypeConstraintOrigin {
3049
3050
}
3050
3051
3051
3052
class _TypeConstraintFromExtendsClause extends _TypeConstraintOrigin {
3052
- final TypeParameterType typeParam;
3053
+ final TypeParameterElement typeParam;
3053
3054
final DartType extendsType;
3054
3055
3055
3056
_TypeConstraintFromExtendsClause (this .typeParam, this .extendsType);
3056
3057
3057
3058
@override
3058
3059
formatError () {
3059
3060
return [
3060
- "Type parameter '$typeParam '" ,
3061
+ "Type parameter '${ typeParam . name } '" ,
3061
3062
"declared to extend '$extendsType '."
3062
3063
];
3063
3064
}
0 commit comments