@@ -4436,42 +4436,13 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
4436
4436
Type interfaceType,
4437
4437
GenericSignature parentSig,
4438
4438
Optional<UUID> knownID) {
4439
- assert (existential->isExistentialType ());
4440
4439
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" );
4450
4440
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)
4468
4442
knownID = UUID::fromTime ();
4469
- }
4470
4443
4471
- // / Create a generic environment for this opened archetype.
4472
- auto genericEnv =
4444
+ auto *genericEnv =
4473
4445
GenericEnvironment::forOpenedExistential (existential, parentSig, *knownID);
4474
- openedExistentialEnvironments[*knownID] = genericEnv;
4475
4446
4476
4447
// Map the interface type into that environment.
4477
4448
auto result = genericEnv->mapTypeIntoContext (interfaceType)
@@ -4653,7 +4624,32 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
4653
4624
GenericEnvironment *
4654
4625
GenericEnvironment::forOpenedExistential (
4655
4626
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
+
4656
4638
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 ()->isEqual (parentSig));
4648
+ assert (existingEnv->getOpenedExistentialUUID () == uuid);
4649
+
4650
+ return existingEnv;
4651
+ }
4652
+
4657
4653
auto signature = ctx.getOpenedExistentialSignature (existential, parentSig);
4658
4654
4659
4655
// Allocate and construct the new environment.
@@ -4662,7 +4658,12 @@ GenericEnvironment::forOpenedExistential(
4662
4658
OpenedGenericEnvironmentData, Type>(
4663
4659
0 , 0 , 1 , numGenericParams);
4664
4660
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;
4666
4667
}
4667
4668
4668
4669
// / Create a new generic environment for an opaque type with the given set of
0 commit comments