@@ -604,50 +604,30 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
604
604
Witness witness) {
605
605
auto requirementInfo = Types.getConstantInfo (requirement);
606
606
607
+ // Work out the lowered function type of the SIL witness thunk.
608
+ auto reqtOrigTy = cast<GenericFunctionType>(requirementInfo.LoweredType );
609
+
607
610
// Mapping from the requirement's generic signature to the witness
608
611
// thunk's generic signature.
609
- SubstitutionMap reqtSubMap;
612
+ auto reqtSubs = witness.getRequirementToSyntheticSubs ();
613
+ auto reqtSubMap = reqtOrigTy->getGenericSignature ()->getSubstitutionMap (reqtSubs);
610
614
611
615
// The generic environment for the witness thunk.
612
- GenericEnvironment *genericEnv = nullptr ;
616
+ auto *genericEnv = witness.getSyntheticEnvironment ();
617
+
618
+ // The type of the witness thunk.
619
+ auto input = reqtOrigTy->getInput ().subst (reqtSubMap)->getCanonicalType ();
620
+ auto result = reqtOrigTy->getResult ().subst (reqtSubMap)->getCanonicalType ();
613
621
614
- // Work out the lowered function type of the SIL witness thunk.
615
- auto reqtOrigTy = cast<GenericFunctionType>(requirementInfo.LoweredType );
616
622
CanAnyFunctionType reqtSubstTy;
617
- SubstitutionList witnessSubs;
618
- if (witness.requiresSubstitution ()) {
619
- genericEnv = witness.getSyntheticEnvironment ();
620
- witnessSubs = witness.getSubstitutions ();
621
-
622
- auto reqtSubs = witness.getRequirementToSyntheticSubs ();
623
- reqtSubMap = reqtOrigTy->getGenericSignature ()
624
- ->getSubstitutionMap (reqtSubs);
625
-
626
- auto input = reqtOrigTy->getInput ().subst (reqtSubMap);
627
- auto result = reqtOrigTy->getResult ().subst (reqtSubMap);
628
-
629
- if (genericEnv) {
630
- auto *genericSig = genericEnv->getGenericSignature ();
631
- reqtSubstTy = cast<GenericFunctionType>(
632
- GenericFunctionType::get (genericSig, input, result,
633
- reqtOrigTy->getExtInfo ())
634
- ->getCanonicalType ());
635
- } else {
636
- reqtSubstTy = cast<FunctionType>(
637
- FunctionType::get (input, result,
638
- reqtOrigTy->getExtInfo ())
639
- ->getCanonicalType ());
640
- }
623
+ if (genericEnv) {
624
+ auto *genericSig = genericEnv->getGenericSignature ();
625
+ reqtSubstTy = CanGenericFunctionType::get (
626
+ genericSig->getCanonicalSignature (),
627
+ input, result, reqtOrigTy->getExtInfo ());
641
628
} else {
642
- reqtSubMap = SubstitutionMap::getProtocolSubstitutions (
643
- conformance->getProtocol (),
644
- conformance->getType (),
645
- ProtocolConformanceRef (conformance));
646
-
647
- auto input = reqtOrigTy->getInput ().subst (reqtSubMap)->getCanonicalType ();
648
- auto result = reqtOrigTy->getResult ().subst (reqtSubMap)->getCanonicalType ();
649
-
650
- reqtSubstTy = CanFunctionType::get (input, result, reqtOrigTy->getExtInfo ());
629
+ reqtSubstTy = CanFunctionType::get (
630
+ input, result, reqtOrigTy->getExtInfo ());
651
631
}
652
632
653
633
// Lower the witness thunk type with the requirement's abstraction level.
@@ -659,7 +639,7 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
659
639
Mangle::ASTMangler NewMangler;
660
640
std::string nameBuffer = NewMangler.mangleWitnessThunk (conformance,
661
641
requirement.getDecl ());
662
-
642
+
663
643
// If the thunked-to function is set to be always inlined, do the
664
644
// same with the witness, on the theory that the user wants all
665
645
// calls removed if possible, e.g. when we're able to devirtualize
@@ -694,6 +674,10 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
694
674
695
675
SILGenFunction SGF (*this , *f);
696
676
677
+ // Substitutions mapping the generic parameters of the witness to
678
+ // archetypes of the witness thunk generic environment.
679
+ auto witnessSubs = witness.getSubstitutions ();
680
+
697
681
// Open-code certain protocol witness "thunks".
698
682
if (maybeOpenCodeProtocolWitness (SGF, conformance, linkage,
699
683
selfInterfaceType, selfType, genericEnv,
0 commit comments