@@ -791,6 +791,7 @@ static void getWitnessMethodSubstitutions(
791
791
GenericSignature *requirementSig,
792
792
GenericSignature *witnessThunkSig,
793
793
ArrayRef<Substitution> origSubs,
794
+ bool isDefaultWitness,
794
795
SmallVectorImpl<Substitution> &newSubs) {
795
796
796
797
if (witnessThunkSig == nullptr )
@@ -806,19 +807,28 @@ static void getWitnessMethodSubstitutions(
806
807
SubstitutionMap subMap;
807
808
808
809
// Take apart substitutions from the conforming type.
809
- //
810
- // If `Self` maps to a bound generic type, this gives us the
811
- // substitutions for the concrete type's generic parameters.
812
- auto witnessSubs = getSubstitutionsForProtocolConformance (conformanceRef);
813
-
810
+ ArrayRef<Substitution> witnessSubs;
811
+ auto *rootConformance = conformance->getRootNormalConformance ();
812
+ auto *witnessSig = rootConformance->getGenericSignature ();
814
813
unsigned depth = 0 ;
815
- if (!witnessSubs.empty ()) {
816
- auto *rootConformance = conformance->getRootNormalConformance ();
817
- depth = rootConformance->getGenericSignature ()->getGenericParams ().back ()
818
- ->getDepth () + 1 ;
819
- auto *witnessSig = rootConformance->getGenericSignature ();
814
+ if (isDefaultWitness) {
815
+ // For default witnesses, we substitute all of Self.
816
+ auto gp = witnessThunkSig->getGenericParams ().front ()->getCanonicalType ();
817
+ subMap.addSubstitution (gp, origSubs.front ().getReplacement ());
818
+ subMap.addConformances (gp, origSubs.front ().getConformances ());
819
+
820
+ // For default witnesses, innermost generic parameters are always at
821
+ // depth 1.
822
+ depth = 1 ;
823
+ } else {
824
+ // If `Self` maps to a bound generic type, this gives us the
825
+ // substitutions for the concrete type's generic parameters.
826
+ witnessSubs = getSubstitutionsForProtocolConformance (conformanceRef);
820
827
821
- witnessSig->getSubstitutionMap (witnessSubs, subMap);
828
+ if (!witnessSubs.empty ()) {
829
+ witnessSig->getSubstitutionMap (witnessSubs, subMap);
830
+ depth = witnessSig->getGenericParams ().back ()->getDepth () + 1 ;
831
+ }
822
832
}
823
833
824
834
// Next, take apart caller-side substitutions.
@@ -890,17 +900,15 @@ static void getWitnessMethodSubstitutions(ApplySite AI, SILFunction *F,
890
900
891
901
ArrayRef<Substitution> origSubs = AI.getSubstitutions ();
892
902
893
- if (F->getLoweredFunctionType ()->getRepresentation ()
894
- == SILFunctionTypeRepresentation::WitnessMethod &&
895
- F->getLoweredFunctionType ()->getDefaultWitnessMethodProtocol (
896
- *Module.getSwiftModule ())) {
897
- // Default witness thunks use the generic signature of the requirement.
898
- NewSubs.append (origSubs.begin (), origSubs.end ());
899
- return ;
900
- }
903
+ bool isDefaultWitness =
904
+ F->getLoweredFunctionType ()->getRepresentation ()
905
+ == SILFunctionTypeRepresentation::WitnessMethod &&
906
+ F->getLoweredFunctionType ()->getDefaultWitnessMethodProtocol (
907
+ *Module.getSwiftModule ())
908
+ == CRef.getRequirement ();
901
909
902
910
getWitnessMethodSubstitutions (Module, CRef, requirementSig, witnessThunkSig,
903
- origSubs, NewSubs);
911
+ origSubs, isDefaultWitness, NewSubs);
904
912
}
905
913
906
914
// / Check if an upcast is legal.
0 commit comments