@@ -569,17 +569,12 @@ static bool isPointerToVoid(ASTContext &Ctx, Type Ty, bool &IsMutable) {
569
569
return BGT->getGenericArgs ().front ()->isVoid ();
570
570
}
571
571
572
- // / Even if the type is not generic, it might be inside of a generic
573
- // / context or have a free-standing 'where' clause, so we need to
574
- // / those check requirements too.
575
- // /
576
- // / Return true on success.
577
572
bool TypeChecker::checkContextualRequirements (GenericTypeDecl *decl,
578
- Type parentTy,
579
- SourceLoc loc ,
580
- DeclContext *dc ) {
581
- if (! parentTy || parentTy-> hasUnboundGenericType () ||
582
- parentTy->hasTypeVariable ()) {
573
+ Type parentTy, SourceLoc loc,
574
+ ModuleDecl *module ,
575
+ GenericSignature contextSig ) {
576
+ assert ( parentTy && " expected a parent type " );
577
+ if (parentTy-> hasUnboundGenericType () || parentTy->hasTypeVariable ()) {
583
578
return true ;
584
579
}
585
580
@@ -599,12 +594,16 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,
599
594
noteLoc = loc;
600
595
}
601
596
597
+ if (contextSig) {
598
+ parentTy = contextSig.getGenericEnvironment ()->mapTypeIntoContext (parentTy);
599
+ }
600
+
602
601
const auto subMap = parentTy->getContextSubstitutions (decl->getDeclContext ());
603
602
const auto genericSig = decl->getGenericSignature ();
604
603
605
604
const auto result =
606
605
TypeChecker::checkGenericArguments (
607
- dc , loc, noteLoc,
606
+ module , loc, noteLoc,
608
607
decl->getDeclaredInterfaceType (),
609
608
genericSig.getGenericParams (),
610
609
genericSig.getRequirements (),
@@ -679,7 +678,13 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
679
678
return type;
680
679
}
681
680
682
- if (TypeChecker::checkContextualRequirements (decl, parentTy, loc, dc))
681
+ if (!parentTy) {
682
+ return type;
683
+ }
684
+
685
+ if (TypeChecker::checkContextualRequirements (
686
+ decl, parentTy, loc, dc->getParentModule (),
687
+ resolution.getGenericSignature ()))
683
688
return type;
684
689
685
690
return ErrorType::get (resolution.getASTContext ());
@@ -872,10 +877,6 @@ Type TypeResolution::applyUnboundGenericArguments(
872
877
}
873
878
}
874
879
875
- SourceLoc noteLoc = decl->getLoc ();
876
- if (noteLoc.isInvalid ())
877
- noteLoc = loc;
878
-
879
880
// Realize the types of the generic arguments and add them to the
880
881
// substitution map.
881
882
for (unsigned i = 0 , e = genericArgs.size (); i < e; ++i) {
@@ -895,11 +896,25 @@ Type TypeResolution::applyUnboundGenericArguments(
895
896
auto *module = getDeclContext ()->getParentModule ();
896
897
897
898
if (!skipRequirementsCheck && getStage () > TypeResolutionStage::Structural) {
899
+ TypeSubstitutionMap contextualSubs = subs;
900
+ if (getStage () == TypeResolutionStage::Interface) {
901
+ if (const auto contextSig = getGenericSignature ()) {
902
+ auto *genericEnv = contextSig.getGenericEnvironment ();
903
+ for (auto &pair : contextualSubs) {
904
+ pair.second = genericEnv->mapTypeIntoContext (pair.second );
905
+ }
906
+ }
907
+ }
908
+
909
+ SourceLoc noteLoc = decl->getLoc ();
910
+ if (noteLoc.isInvalid ())
911
+ noteLoc = loc;
912
+
898
913
auto result = TypeChecker::checkGenericArguments (
899
- getDeclContext () , loc, noteLoc,
914
+ module , loc, noteLoc,
900
915
UnboundGenericType::get (decl, parentTy, getASTContext ()),
901
916
genericSig.getGenericParams (), genericSig.getRequirements (),
902
- QueryTypeSubstitutionMap{subs });
917
+ QueryTypeSubstitutionMap{contextualSubs });
903
918
904
919
switch (result) {
905
920
case RequirementCheckResult::Failure:
0 commit comments