@@ -4436,42 +4436,13 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
44364436 Type interfaceType,
44374437 GenericSignature parentSig,
44384438 Optional<UUID> knownID) {
4439- assert (existential->isExistentialType ());
44404439 assert (!interfaceType->hasArchetype () && " must be interface type" );
4441- // FIXME: Opened archetypes can't be transformed because the
4442- // the identity of the archetype has to be preserved. This
4443- // means that simplifying an opened archetype in the constraint
4444- // system to replace type variables with fixed types is not
4445- // yet supported. For now, assert that an opened archetype never
4446- // contains type variables to catch cases where type variables
4447- // would be applied to the type-checked AST.
4448- assert (!existential->hasTypeVariable () &&
4449- " opened existentials containing type variables cannot be simplified" );
44504440
4451- auto &ctx = existential->getASTContext ();
4452- auto &openedExistentialEnvironments =
4453- ctx.getImpl ().OpenedExistentialEnvironments ;
4454- // If we know the ID already...
4455- if (knownID) {
4456- // ... and we already have an archetype for that ID, return it.
4457- auto found = openedExistentialEnvironments.find (*knownID);
4458-
4459- if (found != openedExistentialEnvironments.end ()) {
4460- assert (found->second ->getOpenedExistentialType ()->isEqual (existential) &&
4461- " Retrieved the wrong generic environment?" );
4462- auto result = found->second ->mapTypeIntoContext (interfaceType)
4463- ->castTo <OpenedArchetypeType>();
4464- return CanOpenedArchetypeType (result);
4465- }
4466- } else {
4467- // Create a new ID.
4441+ if (!knownID)
44684442 knownID = UUID::fromTime ();
4469- }
44704443
4471- // / Create a generic environment for this opened archetype.
4472- auto genericEnv =
4444+ auto *genericEnv =
44734445 GenericEnvironment::forOpenedExistential (existential, parentSig, *knownID);
4474- openedExistentialEnvironments[*knownID] = genericEnv;
44754446
44764447 // Map the interface type into that environment.
44774448 auto result = genericEnv->mapTypeIntoContext (interfaceType)
@@ -4653,7 +4624,32 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
46534624GenericEnvironment *
46544625GenericEnvironment::forOpenedExistential (
46554626 Type existential, GenericSignature parentSig, UUID uuid) {
4627+ assert (existential->isExistentialType ());
4628+ // FIXME: Opened archetypes can't be transformed because the
4629+ // the identity of the archetype has to be preserved. This
4630+ // means that simplifying an opened archetype in the constraint
4631+ // system to replace type variables with fixed types is not
4632+ // yet supported. For now, assert that an opened archetype never
4633+ // contains type variables to catch cases where type variables
4634+ // would be applied to the type-checked AST.
4635+ assert (!existential->hasTypeVariable () &&
4636+ " opened existentials containing type variables cannot be simplified" );
4637+
46564638 auto &ctx = existential->getASTContext ();
4639+
4640+ auto &openedExistentialEnvironments =
4641+ ctx.getImpl ().OpenedExistentialEnvironments ;
4642+ auto found = openedExistentialEnvironments.find (uuid);
4643+
4644+ if (found != openedExistentialEnvironments.end ()) {
4645+ auto *existingEnv = found->second ;
4646+ assert (existingEnv->getOpenedExistentialType ()->isEqual (existential));
4647+ assert (existingEnv->getOpenedExistentialParentSignature ().getPointer () == parentSig.getPointer ());
4648+ assert (existingEnv->getOpenedExistentialUUID () == uuid);
4649+
4650+ return existingEnv;
4651+ }
4652+
46574653 auto signature = ctx.getOpenedExistentialSignature (existential, parentSig);
46584654
46594655 // Allocate and construct the new environment.
@@ -4662,7 +4658,12 @@ GenericEnvironment::forOpenedExistential(
46624658 OpenedGenericEnvironmentData, Type>(
46634659 0 , 0 , 1 , numGenericParams);
46644660 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4665- return new (mem) GenericEnvironment (signature, existential, parentSig, uuid);
4661+ auto *genericEnv =
4662+ new (mem) GenericEnvironment (signature, existential, parentSig, uuid);
4663+
4664+ openedExistentialEnvironments[uuid] = genericEnv;
4665+
4666+ return genericEnv;
46664667}
46674668
46684669// / Create a new generic environment for an opaque type with the given set of
0 commit comments