@@ -328,7 +328,7 @@ struct ASTContext::Implementation {
328328 CanGenericSignature SingleGenericParameterSignature;
329329
330330 // / The existential signature <T : P> for each P.
331- llvm::DenseMap<std::pair<CanType, const DeclContext *>, CanGenericSignature>
331+ llvm::DenseMap<std::pair<CanType, const GenericSignatureImpl *>, CanGenericSignature>
332332 ExistentialSignatures;
333333
334334 // / Overridden declarations.
@@ -4441,17 +4441,18 @@ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew(
44414441}
44424442
44434443CanTypeWrapper<OpenedArchetypeType>
4444- OpenedArchetypeType::get (CanType existential, const DeclContext *useDC ,
4444+ OpenedArchetypeType::get (CanType existential, GenericSignature parentSig ,
44454445 Optional<UUID> knownID) {
4446- return get (existential,
4447- OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC),
4448- useDC , knownID);
4446+ assert (existential-> isExistentialType ());
4447+ auto interfaceType = OpenedArchetypeType::getSelfInterfaceTypeFromContext (parentSig, existential-> getASTContext ());
4448+ return get (existential, interfaceType, parentSig , knownID);
44494449}
44504450
44514451CanOpenedArchetypeType OpenedArchetypeType::get (CanType existential,
44524452 Type interfaceType,
4453- const DeclContext *useDC ,
4453+ GenericSignature parentSig ,
44544454 Optional<UUID> knownID) {
4455+ assert (existential->isExistentialType ());
44554456 assert (!interfaceType->hasArchetype () && " must be interface type" );
44564457 // FIXME: Opened archetypes can't be transformed because the
44574458 // the identity of the archetype has to be preserved. This
@@ -4485,7 +4486,7 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
44854486
44864487 // / Create a generic environment for this opened archetype.
44874488 auto genericEnv =
4488- GenericEnvironment::forOpenedExistential (existential, useDC , *knownID);
4489+ GenericEnvironment::forOpenedExistential (existential, parentSig , *knownID);
44894490 openedExistentialEnvironments[*knownID] = genericEnv;
44904491
44914492 // Map the interface type into that environment.
@@ -4495,22 +4496,22 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
44954496}
44964497
44974498CanType OpenedArchetypeType::getAny (CanType existential, Type interfaceType,
4498- const DeclContext *useDC) {
4499+ GenericSignature parentSig) {
4500+ assert (existential->isAnyExistentialType ());
44994501 if (auto metatypeTy = existential->getAs <ExistentialMetatypeType>()) {
45004502 auto instanceTy =
45014503 metatypeTy->getExistentialInstanceType ()->getCanonicalType ();
45024504 return CanMetatypeType::get (
4503- OpenedArchetypeType::getAny (instanceTy, interfaceType, useDC ));
4505+ OpenedArchetypeType::getAny (instanceTy, interfaceType, parentSig ));
45044506 }
45054507 assert (existential->isExistentialType ());
4506- return OpenedArchetypeType::get (existential, interfaceType, useDC );
4508+ return OpenedArchetypeType::get (existential, interfaceType, parentSig );
45074509}
45084510
45094511CanType OpenedArchetypeType::getAny (CanType existential,
4510- const DeclContext *useDC) {
4511- return getAny (existential,
4512- OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC),
4513- useDC);
4512+ GenericSignature parentSig) {
4513+ auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext (parentSig, existential->getASTContext ());
4514+ return getAny (existential, interfaceTy, parentSig);
45144515}
45154516
45164517void SubstitutionMap::Storage::Profile (
@@ -4666,19 +4667,14 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
46664667
46674668// / Create a new generic environment for an opened archetype.
46684669GenericEnvironment *
4669- GenericEnvironment::forOpenedExistential (Type existential,
4670- const DeclContext *useDC , UUID uuid) {
4670+ GenericEnvironment::forOpenedExistential (
4671+ Type existential, GenericSignature parentSig , UUID uuid) {
46714672 auto &ctx = existential->getASTContext ();
4672- auto signature = ctx.getOpenedArchetypeSignature (existential, useDC);
4673-
4674- SubstitutionMap subs;
4675- if (auto *useEnvironment = useDC->getGenericEnvironmentOfContext ()) {
4676- subs = useEnvironment->getForwardingSubstitutionMap ();
4677- }
4678- return GenericEnvironment::forOpenedExistential (existential, signature, uuid);
4673+ auto signature = ctx.getOpenedArchetypeSignature (existential, parentSig);
4674+ return GenericEnvironment::forOpenedArchetypeSignature (existential, signature, uuid);
46794675}
46804676
4681- GenericEnvironment *GenericEnvironment::forOpenedExistential (
4677+ GenericEnvironment *GenericEnvironment::forOpenedArchetypeSignature (
46824678 Type existential, GenericSignature signature, UUID uuid) {
46834679 // Allocate and construct the new environment.
46844680 auto &ctx = existential->getASTContext ();
@@ -5187,22 +5183,19 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
51875183 return canonicalSig;
51885184}
51895185
5190- Type OpenedArchetypeType::getSelfInterfaceTypeFromContext (const DeclContext *useDC) {
5191- auto typeContext = useDC->getInnermostTypeContext ();
5192- if (typeContext && typeContext->getSelfProtocolDecl ()) {
5193- return typeContext->getProtocolSelfType ();
5194- } else {
5195- return GenericTypeParamType::get (
5196- /* isTypeSequence=*/ false ,
5197- /* depth*/ useDC->getGenericContextDepth () + 1 , /* index*/ 0 ,
5198- useDC->getASTContext ());
5199- }
5186+ Type OpenedArchetypeType::getSelfInterfaceTypeFromContext (GenericSignature parentSig,
5187+ ASTContext &ctx) {
5188+ unsigned depth = 0 ;
5189+ if (!parentSig.getGenericParams ().empty ())
5190+ depth = parentSig.getGenericParams ().back ()->getDepth () + 1 ;
5191+ return GenericTypeParamType::get (/* isTypeSequence=*/ false ,
5192+ /* depth=*/ depth, /* index=*/ 0 ,
5193+ ctx);
52005194}
52015195
52025196CanGenericSignature
5203- ASTContext::getOpenedArchetypeSignature (Type type, const DeclContext *useDC ) {
5197+ ASTContext::getOpenedArchetypeSignature (Type type, GenericSignature parentSig ) {
52045198 assert (type->isExistentialType ());
5205- assert (useDC && " Must have a working declaration context!" );
52065199
52075200 if (auto existential = type->getAs <ExistentialType>())
52085201 type = existential->getConstraintType ();
@@ -5213,7 +5206,7 @@ ASTContext::getOpenedArchetypeSignature(Type type, const DeclContext *useDC) {
52135206 // The opened archetype signature for a protocol type is identical
52145207 // to the protocol's own canonical generic signature if there aren't any
52155208 // outer generic parameters to worry about.
5216- if (!useDC-> isGenericContext ()) {
5209+ if (parentSig. isNull ()) {
52175210 if (const auto protoTy = dyn_cast<ProtocolType>(constraint)) {
52185211 return protoTy->getDecl ()->getGenericSignature ().getCanonicalSignature ();
52195212 }
@@ -5222,31 +5215,23 @@ ASTContext::getOpenedArchetypeSignature(Type type, const DeclContext *useDC) {
52225215 // Otherwise we need to build a generic signature that captures any outer
52235216 // generic parameters. This ensures that we keep e.g. generic superclass
52245217 // existentials contained in a well-formed generic context.
5225- auto found = getImpl ().ExistentialSignatures .find ({constraint, useDC});
5218+ auto canParentSig = parentSig.getCanonicalSignature ();
5219+ auto found = getImpl ().ExistentialSignatures .find ({constraint, canParentSig.getPointer ()});
52265220 if (found != getImpl ().ExistentialSignatures .end ())
52275221 return found->second ;
52285222
5229- GenericSignature outerSignature;
5230- auto *typeContext = useDC->getInnermostTypeContext ();
5231- if (typeContext && typeContext->getSelfProtocolDecl ()) {
5232- outerSignature = GenericSignature ();
5233- } else {
5234- outerSignature = useDC->getGenericSignatureOfContext ();
5235- }
5236-
5237- auto genericParam = OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC)
5238- ->getCanonicalType ()->getAs <GenericTypeParamType>();
5239-
5223+ auto genericParam = OpenedArchetypeType::getSelfInterfaceTypeFromContext (canParentSig, type->getASTContext ())
5224+ ->castTo <GenericTypeParamType>();
52405225 Requirement requirement (RequirementKind::Conformance, genericParam,
52415226 constraint);
52425227 auto genericSig = buildGenericSignature (
5243- *this , outerSignature. getCanonicalSignature () ,
5228+ *this , canParentSig ,
52445229 {genericParam}, {requirement});
52455230
52465231 CanGenericSignature canGenericSig (genericSig);
52475232
52485233 auto result = getImpl ().ExistentialSignatures .insert (
5249- std::make_pair (std::make_pair (constraint, useDC ), canGenericSig));
5234+ std::make_pair (std::make_pair (constraint, canParentSig. getPointer () ), canGenericSig));
52505235 assert (result.second );
52515236 (void ) result;
52525237
0 commit comments