@@ -606,6 +606,8 @@ struct ASTContext::Implementation {
606606 void dump (llvm::raw_ostream &out) const ;
607607 };
608608
609+ using OpenedExistentialKey = std::pair<SubstitutionMap, UUID>;
610+
609611 llvm::DenseMap<ModuleDecl*, ModuleType*> ModuleTypes;
610612 llvm::DenseMap<std::pair<unsigned , unsigned >, GenericTypeParamType *>
611613 GenericParamTypes;
@@ -618,7 +620,8 @@ struct ASTContext::Implementation {
618620 llvm::DenseMap<BuiltinIntegerWidth, BuiltinIntegerType*> IntegerTypes;
619621 llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
620622 llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
621- llvm::DenseMap<UUID, GenericEnvironment *> OpenedExistentialEnvironments;
623+ llvm::DenseMap<OpenedExistentialKey, GenericEnvironment *>
624+ OpenedExistentialEnvironments;
622625 llvm::DenseMap<UUID, GenericEnvironment *> OpenedElementEnvironments;
623626 llvm::FoldingSet<IndexSubset> IndexSubsets;
624627 llvm::FoldingSet<AutoDiffDerivativeFunctionIdentifier>
@@ -5295,7 +5298,7 @@ OpaqueTypeArchetypeType *OpaqueTypeArchetypeType::getNew(
52955298 ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
52965299 LayoutConstraint layout) {
52975300 auto properties = getOpaqueTypeArchetypeProperties (
5298- environment->getOpaqueSubstitutions ());
5301+ environment->getOuterSubstitutions ());
52995302 auto arena = getArena (properties);
53005303 auto size = OpaqueTypeArchetypeType::totalSizeToAlloc<
53015304 ProtocolDecl *, Type, LayoutConstraint>(
@@ -5353,7 +5356,7 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
53535356
53545357 auto *genericEnv =
53555358 GenericEnvironment::forOpenedExistential (
5356- existential, GenericSignature (), *knownID);
5359+ existential, SubstitutionMap (), *knownID);
53575360
53585361 // Map the interface type into that environment.
53595362 auto result = genericEnv->mapTypeIntoContext (interfaceType)
@@ -5508,10 +5511,11 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
55085511
55095512 // Allocate and construct the new environment.
55105513 unsigned numGenericParams = signature.getGenericParams ().size ();
5511- size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
5514+ size_t bytes = totalSizeToAlloc<SubstitutionMap,
5515+ OpaqueEnvironmentData,
55125516 OpenedExistentialEnvironmentData,
55135517 OpenedElementEnvironmentData, Type>(
5514- 0 , 0 , 0 , numGenericParams);
5518+ 0 , 0 , 0 , 0 , numGenericParams);
55155519 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
55165520 return new (mem) GenericEnvironment (signature);
55175521}
@@ -5537,10 +5541,11 @@ GenericEnvironment *GenericEnvironment::forOpaqueType(
55375541 // Allocate and construct the new environment.
55385542 auto signature = opaque->getOpaqueInterfaceGenericSignature ();
55395543 unsigned numGenericParams = signature.getGenericParams ().size ();
5540- size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
5544+ size_t bytes = totalSizeToAlloc<SubstitutionMap,
5545+ OpaqueEnvironmentData,
55415546 OpenedExistentialEnvironmentData,
55425547 OpenedElementEnvironmentData, Type>(
5543- 1 , 0 , 0 , numGenericParams);
5548+ 1 , 1 , 0 , 0 , numGenericParams);
55445549 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment), arena);
55455550 env = new (mem) GenericEnvironment (signature, opaque, subs);
55465551
@@ -5553,7 +5558,7 @@ GenericEnvironment *GenericEnvironment::forOpaqueType(
55535558// / Create a new generic environment for an opened archetype.
55545559GenericEnvironment *
55555560GenericEnvironment::forOpenedExistential (
5556- Type existential, GenericSignature parentSig , UUID uuid) {
5561+ Type existential, SubstitutionMap subs , UUID uuid) {
55575562 assert (existential->isExistentialType ());
55585563 // FIXME: Opened archetypes can't be transformed because the
55595564 // the identity of the archetype has to be preserved. This
@@ -5567,32 +5572,36 @@ GenericEnvironment::forOpenedExistential(
55675572
55685573 auto &ctx = existential->getASTContext ();
55695574
5575+ auto key = std::make_pair (subs, uuid);
5576+
55705577 auto &openedExistentialEnvironments =
55715578 ctx.getImpl ().OpenedExistentialEnvironments ;
5572- auto found = openedExistentialEnvironments.find (uuid );
5579+ auto found = openedExistentialEnvironments.find (key );
55735580
55745581 if (found != openedExistentialEnvironments.end ()) {
55755582 auto *existingEnv = found->second ;
55765583 assert (existingEnv->getOpenedExistentialType ()->isEqual (existential));
5577- assert (existingEnv->getOpenedExistentialParentSignature (). getPointer () == parentSig. getPointer () );
5584+ assert (existingEnv->getOuterSubstitutions () == subs );
55785585 assert (existingEnv->getOpenedExistentialUUID () == uuid);
55795586
55805587 return existingEnv;
55815588 }
55825589
5590+ auto parentSig = subs.getGenericSignature ().getCanonicalSignature ();
55835591 auto signature = ctx.getOpenedExistentialSignature (existential, parentSig);
55845592
55855593 // Allocate and construct the new environment.
55865594 unsigned numGenericParams = signature.getGenericParams ().size ();
5587- size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
5595+ size_t bytes = totalSizeToAlloc<SubstitutionMap,
5596+ OpaqueEnvironmentData,
55885597 OpenedExistentialEnvironmentData,
55895598 OpenedElementEnvironmentData, Type>(
5590- 0 , 1 , 0 , numGenericParams);
5599+ 1 , 0 , 1 , 0 , numGenericParams);
55915600 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
55925601 auto *genericEnv =
5593- new (mem) GenericEnvironment (signature, existential, parentSig , uuid);
5602+ new (mem) GenericEnvironment (signature, existential, subs , uuid);
55945603
5595- openedExistentialEnvironments[uuid ] = genericEnv;
5604+ openedExistentialEnvironments[key ] = genericEnv;
55965605
55975606 return genericEnv;
55985607}
@@ -5621,11 +5630,12 @@ GenericEnvironment::forOpenedElement(GenericSignature signature,
56215630 // Allocate and construct the new environment.
56225631 unsigned numGenericParams = signature.getGenericParams ().size ();
56235632 unsigned numOpenedParams = signature.getInnermostGenericParams ().size ();
5624- size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
5633+ size_t bytes = totalSizeToAlloc<SubstitutionMap,
5634+ OpaqueEnvironmentData,
56255635 OpenedExistentialEnvironmentData,
56265636 OpenedElementEnvironmentData,
56275637 Type>(
5628- 0 , 0 , 1 , numGenericParams + numOpenedParams);
5638+ 1 , 0 , 0 , 1 , numGenericParams + numOpenedParams);
56295639 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
56305640 auto *genericEnv = new (mem) GenericEnvironment (signature,
56315641 uuid, shapeClass,
0 commit comments