@@ -3521,6 +3521,14 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35213521 return storage->isSettable (storage->getDeclContext ());
35223522 };
35233523
3524+ // We cannot use the same opened archetype in the getter and setter. Therefore
3525+ // we create a new one for both the getter and the setter.
3526+ auto renewOpenedArchetypes = [](SubstitutableType *type) -> Type {
3527+ if (auto *openedTy = dyn_cast<OpenedArchetypeType>(type))
3528+ return OpenedArchetypeType::get (openedTy->getOpenedExistentialType ());
3529+ return type;
3530+ };
3531+
35243532 if (auto var = dyn_cast<VarDecl>(storage)) {
35253533 CanType componentTy;
35263534 if (!var->getDeclContext ()->isTypeContext ()) {
@@ -3544,13 +3552,15 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35443552 auto id = getIdForKeyPathComponentComputedProperty (*this , var,
35453553 strategy);
35463554 auto getter = getOrCreateKeyPathGetter (*this , loc,
3547- var, subs,
3555+ var, subs.subst (renewOpenedArchetypes,
3556+ MakeAbstractConformanceForGenericType ()),
35483557 needsGenericContext ? genericEnv : nullptr ,
35493558 expansion, {}, baseTy, componentTy);
35503559
35513560 if (isSettableInComponent ()) {
35523561 auto setter = getOrCreateKeyPathSetter (*this , loc,
3553- var, subs,
3562+ var, subs.subst (renewOpenedArchetypes,
3563+ MakeAbstractConformanceForGenericType ()),
35543564 needsGenericContext ? genericEnv : nullptr ,
35553565 expansion, {}, baseTy, componentTy);
35563566 return KeyPathPatternComponent::forComputedSettableProperty (id,
@@ -3595,7 +3605,8 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35953605
35963606 auto id = getIdForKeyPathComponentComputedProperty (*this , decl, strategy);
35973607 auto getter = getOrCreateKeyPathGetter (*this , loc,
3598- decl, subs,
3608+ decl, subs.subst (renewOpenedArchetypes,
3609+ MakeAbstractConformanceForGenericType ()),
35993610 needsGenericContext ? genericEnv : nullptr ,
36003611 expansion,
36013612 indexTypes,
@@ -3604,7 +3615,8 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
36043615 auto indexPatternsCopy = getASTContext ().AllocateCopy (indexPatterns);
36053616 if (isSettableInComponent ()) {
36063617 auto setter = getOrCreateKeyPathSetter (*this , loc,
3607- decl, subs,
3618+ decl, subs.subst (renewOpenedArchetypes,
3619+ MakeAbstractConformanceForGenericType ()),
36083620 needsGenericContext ? genericEnv : nullptr ,
36093621 expansion,
36103622 indexTypes,
0 commit comments