@@ -209,7 +209,7 @@ Type TypeChecker::resolveTypeInContext(
209
209
// If we have a callback to report dependencies, do so.
210
210
if (unsatisfiedDependency &&
211
211
(*unsatisfiedDependency)(requestResolveTypeDecl (typeDecl)))
212
- return nullptr ;
212
+ return Type () ;
213
213
214
214
// If we found a generic parameter, map to the archetype if there is one.
215
215
if (auto genericParam = dyn_cast<GenericTypeParamDecl>(typeDecl)) {
@@ -233,7 +233,7 @@ Type TypeChecker::resolveTypeInContext(
233
233
// We might have an invalid extension that didn't resolve.
234
234
if (ownerNominal == nullptr && ownerDC->isExtensionContext ()) {
235
235
assert (cast<ExtensionDecl>(ownerDC)->isInvalid ());
236
- return ErrorType::get (ownerDC-> getASTContext () );
236
+ return ErrorType::get (Context );
237
237
}
238
238
239
239
auto assocType = dyn_cast<AssociatedTypeDecl>(typeDecl);
@@ -372,7 +372,7 @@ Type TypeChecker::resolveTypeInContext(
372
372
373
373
auto protoSelf = parentDC->getProtocolSelf ();
374
374
if (protoSelf == nullptr )
375
- return ErrorType::get (parentDC-> getASTContext () );
375
+ return ErrorType::get (Context );
376
376
377
377
auto selfType = protoSelf
378
378
->getDeclaredType ()
@@ -402,61 +402,85 @@ Type TypeChecker::resolveTypeInContext(
402
402
}
403
403
404
404
assert (incomplete && " Should have found type by now" );
405
- return ErrorType::get (ownerDC-> getASTContext () );
405
+ return ErrorType::get (Context );
406
406
}
407
407
408
- Type TypeChecker::applyGenericArguments (Type type, SourceLoc loc ,
409
- DeclContext *dc,
408
+ Type TypeChecker::applyGenericArguments (Type type, TypeDecl *decl ,
409
+ SourceLoc loc, DeclContext *dc,
410
410
GenericIdentTypeRepr *generic,
411
411
bool isGenericSignature,
412
412
GenericTypeResolver *resolver) {
413
413
414
- auto unbound = type->getAs <UnboundGenericType>();
415
- if (!unbound) {
416
- if (!type->is <ErrorType>()) {
417
- auto diag = diagnose (loc, diag::not_a_generic_type, type);
414
+ if (type->is <ErrorType>()) {
415
+ generic->setInvalid ();
416
+ return type;
417
+ }
418
+
419
+ // We must either have an unbound generic type, or a generic type alias.
420
+ if (!type->is <UnboundGenericType>() &&
421
+ !(isa<TypeAliasDecl>(decl) &&
422
+ cast<TypeAliasDecl>(decl)->getGenericParams ())) {
423
+
424
+ auto diag = diagnose (loc, diag::not_a_generic_type, type);
425
+
426
+ // Don't add fixit on module type; that isn't the right type regardless
427
+ // of whether it had generic arguments.
428
+ if (!type->is <ModuleType>())
429
+ diag.fixItRemove (generic->getAngleBrackets ());
418
430
419
- // Don't add fixit on module type; that isn't the right type regardless
420
- // of whether it had generic arguments.
421
- if (!type->is <ModuleType>())
422
- diag.fixItRemove (generic->getAngleBrackets ());
423
- }
424
431
generic->setInvalid ();
425
432
return type;
426
433
}
427
434
435
+ // If we have a non-generic type alias, we have an unbound generic type.
436
+ // Grab the decl from the unbound generic type.
437
+ //
438
+ // The idea is if you write:
439
+ //
440
+ // typealias Foo = Bar.Baz
441
+ //
442
+ // Then 'Foo<Int>' applies arguments to Bar.Baz, whereas if you write:
443
+ //
444
+ // typealias Foo<T> = Bar.Baz<T>
445
+ //
446
+ // Then 'Foo<Int>' applies arguments to Foo itself.
447
+ //
448
+ if (isa<TypeAliasDecl>(decl) &&
449
+ !cast<TypeAliasDecl>(decl)->getGenericParams ()) {
450
+ decl = type->castTo <UnboundGenericType>()->getDecl ();
451
+ }
452
+
428
453
// Make sure we have the right number of generic arguments.
429
454
// FIXME: If we have fewer arguments than we need, that might be okay, if
430
455
// we're allowed to deduce the remaining arguments from context.
431
- auto unboundDecl = unbound-> getDecl ( );
456
+ auto genericDecl = cast<GenericTypeDecl>(decl );
432
457
auto genericArgs = generic->getGenericArgs ();
433
- auto genericParams = unboundDecl ->getGenericParams ();
458
+ auto genericParams = genericDecl ->getGenericParams ();
434
459
if (genericParams->size () != genericArgs.size ()) {
435
- diagnose (loc, diag::type_parameter_count_mismatch, unboundDecl ->getName (),
460
+ diagnose (loc, diag::type_parameter_count_mismatch, decl ->getName (),
436
461
genericParams->size (), genericArgs.size (),
437
462
genericArgs.size () < genericParams->size ())
438
463
.highlight (generic->getAngleBrackets ());
439
- diagnose (unboundDecl , diag::generic_type_declared_here,
440
- unboundDecl ->getName ());
464
+ diagnose (decl , diag::generic_type_declared_here,
465
+ decl ->getName ());
441
466
return nullptr ;
442
467
}
443
468
444
469
SmallVector<TypeLoc, 8 > args;
445
470
for (auto tyR : genericArgs)
446
471
args.push_back (tyR);
447
472
448
- return applyUnboundGenericArguments (unbound , loc, dc, args,
473
+ return applyUnboundGenericArguments (type, genericDecl , loc, dc, args,
449
474
isGenericSignature, resolver);
450
475
}
451
476
452
477
// / Apply generic arguments to the given type.
453
478
Type TypeChecker::applyUnboundGenericArguments (
454
- UnboundGenericType *unbound , SourceLoc loc, DeclContext *dc,
479
+ Type type, GenericTypeDecl *decl , SourceLoc loc, DeclContext *dc,
455
480
MutableArrayRef<TypeLoc> genericArgs, bool isGenericSignature,
456
481
GenericTypeResolver *resolver) {
457
482
458
- assert (unbound &&
459
- genericArgs.size () == unbound->getDecl ()->getGenericParams ()->size () &&
483
+ assert (genericArgs.size () == decl->getGenericParams ()->size () &&
460
484
" invalid arguments, use applyGenericArguments for diagnostic emitting" );
461
485
462
486
// Make sure we always have a resolver to use.
@@ -477,50 +501,48 @@ Type TypeChecker::applyUnboundGenericArguments(
477
501
478
502
genericArgTypes.push_back (genericArg.getType ());
479
503
}
480
-
504
+
481
505
// If we're completing a generic TypeAlias, then we map the types provided
482
506
// onto the underlying type.
483
- if (auto *TAD = dyn_cast<TypeAliasDecl>(unbound-> getDecl () )) {
507
+ if (auto *TAD = dyn_cast<TypeAliasDecl>(decl )) {
484
508
auto signature = TAD->getGenericSignature ();
485
- assert (TAD->getGenericParams ()->getAllArchetypes ().size ()
486
- == genericArgs.size () &&
487
- signature->getInnermostGenericParams ().size () == genericArgs.size ()&&
488
- " argument arity mismatch" );
489
509
490
- SmallVector<Substitution, 4 > subs;
491
- subs.reserve (genericArgs.size ());
492
-
493
- // If we have any nested archetypes from an outer type, include them
494
- // verbatim.
495
- auto outerParams = signature->getGenericParams ();
496
- outerParams = outerParams.drop_back (genericArgs.size ());
497
- for (auto param : outerParams) {
498
- Type type = resolver->resolveGenericTypeParamType (param);
510
+ TypeSubstitutionMap subs;
511
+ for (unsigned i = 0 , e = genericArgs.size (); i < e; i++) {
512
+ auto t = signature->getInnermostGenericParams ()[i];
513
+ subs[t->getCanonicalType ().getPointer ()] = genericArgs[i].getType ();
514
+ }
499
515
500
- subs.push_back (Substitution (type, {}));
516
+ if (auto outerSig = TAD->getDeclContext ()->getGenericSignatureOfContext ()) {
517
+ for (auto outerParam : outerSig->getGenericParams ()) {
518
+ subs[outerParam->getCanonicalType ().getPointer ()] =
519
+ ArchetypeBuilder::mapTypeIntoContext (TAD->getDeclContext (),
520
+ outerParam);
521
+ }
501
522
}
502
-
503
- for (auto t : genericArgs)
504
- subs.push_back (Substitution (t.getType (), {}));
505
-
506
- auto subst = TAD->getGenericParams ()->getSubstitutionMap (subs);
523
+
524
+ // FIXME: Change callers to pass the right type in for generic typealiases
525
+ if (type->is <UnboundGenericType>() || isa<NameAliasType>(type.getPointer ())) {
526
+ type = ArchetypeBuilder::mapTypeOutOfContext (TAD, TAD->getUnderlyingType ());
527
+ }
528
+
529
+ type = type.subst (dc->getParentModule (), subs, None);
507
530
508
531
// FIXME: return a SubstitutedType to preserve the fact that
509
532
// we resolved a generic TypeAlias, for availability diagnostics.
510
533
// A better fix might be to introduce a BoundGenericAliasType
511
534
// which desugars as appropriate.
512
- return SubstitutedType::get (
513
- TAD->getDeclaredType (),
514
- TAD->getUnderlyingType ().subst (TAD->getParentModule (), subst, None),
515
- Context);
535
+ return SubstitutedType::get (TAD->getDeclaredType (), type, Context);
516
536
}
517
537
518
538
// Form the bound generic type.
519
- auto *BGT = BoundGenericType::get (cast<NominalTypeDecl>(unbound->getDecl ()),
520
- unbound->getParent (), genericArgTypes);
539
+ auto *UGT = type->castTo <UnboundGenericType>();
540
+ auto *BGT = BoundGenericType::get (cast<NominalTypeDecl>(decl),
541
+ UGT->getParent (), genericArgTypes);
542
+
521
543
// Check protocol conformance.
522
544
if (!BGT->hasTypeParameter () && !BGT->hasTypeVariable ()) {
523
- SourceLoc noteLoc = unbound-> getDecl () ->getLoc ();
545
+ SourceLoc noteLoc = decl ->getLoc ();
524
546
if (noteLoc.isInvalid ())
525
547
noteLoc = loc;
526
548
@@ -532,14 +554,13 @@ Type TypeChecker::applyUnboundGenericArguments(
532
554
ArrayRef<Type> allGenericArgs = BGT->getAllGenericArgs (scratch);
533
555
534
556
// Check the generic arguments against the generic signature.
535
- auto genericSig = unbound->getDecl ()->getGenericSignature ();
536
- if (!unbound->getDecl ()->hasType () ||
537
- unbound->getDecl ()->isValidatingGenericSignature ()) {
557
+ auto genericSig = decl->getGenericSignature ();
558
+ if (!decl->hasType () || decl->isValidatingGenericSignature ()) {
538
559
diagnose (loc, diag::recursive_requirement_reference);
539
560
return nullptr ;
540
561
}
541
562
assert (genericSig != nullptr );
542
- if (checkGenericArguments (dc, loc, noteLoc, unbound , genericSig,
563
+ if (checkGenericArguments (dc, loc, noteLoc, UGT , genericSig,
543
564
allGenericArgs))
544
565
return nullptr ;
545
566
@@ -549,14 +570,16 @@ Type TypeChecker::applyUnboundGenericArguments(
549
570
return BGT;
550
571
}
551
572
552
- static Type applyGenericTypeReprArgs (TypeChecker &TC, Type type, SourceLoc loc,
573
+ static Type applyGenericTypeReprArgs (TypeChecker &TC, Type type,
574
+ TypeDecl *decl,
575
+ SourceLoc loc,
553
576
DeclContext *dc,
554
577
GenericIdentTypeRepr *generic,
555
578
bool isGenericSignature,
556
579
GenericTypeResolver *resolver) {
557
580
558
- Type ty = TC.applyGenericArguments (type, loc, dc, generic, isGenericSignature ,
559
- resolver);
581
+ Type ty = TC.applyGenericArguments (type, decl, loc, dc, generic,
582
+ isGenericSignature, resolver);
560
583
if (!ty)
561
584
return ErrorType::get (TC.Context );
562
585
return ty;
@@ -660,7 +683,7 @@ static Type resolveTypeDecl(TypeChecker &TC, TypeDecl *typeDecl, SourceLoc loc,
660
683
661
684
if (generic && !options.contains (TR_ResolveStructure)) {
662
685
// Apply the generic arguments to the type.
663
- type = applyGenericTypeReprArgs (TC, type, loc, dc, generic,
686
+ type = applyGenericTypeReprArgs (TC, type, typeDecl, loc, dc, generic,
664
687
options.contains (TR_GenericSignature),
665
688
resolver);
666
689
}
@@ -1035,7 +1058,7 @@ static Type resolveNestedIdentTypeComponent(
1035
1058
// If there are generic arguments, apply them now.
1036
1059
if (auto genComp = dyn_cast<GenericIdentTypeRepr>(comp)) {
1037
1060
memberType = applyGenericTypeReprArgs (
1038
- TC, memberType, comp->getIdLoc (), DC, genComp,
1061
+ TC, memberType, typeDecl, comp->getIdLoc (), DC, genComp,
1039
1062
options.contains (TR_GenericSignature), resolver);
1040
1063
1041
1064
// Propagate failure.
@@ -1154,7 +1177,7 @@ static Type resolveNestedIdentTypeComponent(
1154
1177
// If there are generic arguments, apply them now.
1155
1178
if (auto genComp = dyn_cast<GenericIdentTypeRepr>(comp))
1156
1179
memberType = applyGenericTypeReprArgs (
1157
- TC, memberType, comp->getIdLoc (), DC, genComp,
1180
+ TC, memberType, member, comp->getIdLoc (), DC, genComp,
1158
1181
options.contains (TR_GenericSignature), resolver);
1159
1182
1160
1183
// If we found a reference to an associated type or other member type that
@@ -2235,16 +2258,17 @@ Type TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
2235
2258
2236
2259
Type valueTy = resolveType (repr->getValue (), withoutContext (options));
2237
2260
if (!valueTy || valueTy->is <ErrorType>()) return valueTy;
2238
-
2261
+
2262
+ auto dictDecl = TC.Context .getDictionaryDecl ();
2263
+
2239
2264
if (auto dictTy = TC.getDictionaryType (repr->getBrackets ().Start , keyTy,
2240
2265
valueTy)) {
2241
2266
// Check the requirements on the generic arguments.
2242
- auto unboundTy = UnboundGenericType::get (TC.Context .getDictionaryDecl (),
2243
- nullptr , TC.Context );
2267
+ auto unboundTy = dictDecl->getDeclaredType ();
2244
2268
TypeLoc args[2 ] = { TypeLoc (repr->getKey ()), TypeLoc (repr->getValue ()) };
2245
2269
2246
2270
if (!TC.applyUnboundGenericArguments (
2247
- unboundTy, repr->getStartLoc (), DC, args,
2271
+ unboundTy, dictDecl, repr->getStartLoc (), DC, args,
2248
2272
options.contains (TR_GenericSignature), Resolver)) {
2249
2273
return ErrorType::get (TC.Context );
2250
2274
}
0 commit comments