@@ -4244,19 +4244,25 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
42444244 CanType componentTy;
42454245 if (!var->getDeclContext ()->isTypeContext ()) {
42464246 componentTy = var->getInterfaceType ()->getCanonicalType ();
4247+ } else if (var->getDeclContext ()->getSelfProtocolDecl () &&
4248+ baseTy->isExistentialType ()) {
4249+ componentTy = var->getValueInterfaceType ()->getCanonicalType ();
4250+ ASSERT (!componentTy->hasTypeParameter ());
42474251 } else {
4248- componentTy =
4252+ // The mapTypeIntoContext() / mapTypeOutOfContext() dance is there
4253+ // to handle the case where baseTy being a type parameter subject
4254+ // to a superclass requirement.
4255+ componentTy = var->getValueInterfaceType ().subst (
42494256 GenericEnvironment::mapTypeIntoContext (genericEnv, baseTy)
4250- ->getTypeOfMember (var)
4251- ->getReferenceStorageReferent ()
4257+ ->getContextSubstitutionMap (var->getDeclContext ()))
42524258 ->mapTypeOutOfContext ()
42534259 ->getCanonicalType ();
4260+ }
42544261
4255- // The component type for an @objc optional requirement needs to be
4256- // wrapped in an optional.
4257- if (var->getAttrs ().hasAttribute <OptionalAttr>()) {
4258- componentTy = OptionalType::get (componentTy)->getCanonicalType ();
4259- }
4262+ // The component type for an @objc optional requirement needs to be
4263+ // wrapped in an optional.
4264+ if (var->getAttrs ().hasAttribute <OptionalAttr>()) {
4265+ componentTy = OptionalType::get (componentTy)->getCanonicalType ();
42604266 }
42614267
42624268 if (canStorageUseStoredKeyPathComponent (var, expansion)) {
0 commit comments