Skip to content

Commit c8b7181

Browse files
committed
Always store a generic environment in a nested archetype.
1 parent 1a563d4 commit c8b7181

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5396,7 +5396,7 @@ class ArchetypeType : public SubstitutableType,
53965396
}
53975397
Type InterfaceType;
53985398
MutableArrayRef<std::pair<Identifier, Type>> NestedTypes;
5399-
mutable GenericEnvironment *Environment = nullptr;
5399+
GenericEnvironment *Environment = nullptr;
54005400

54015401
void populateNestedTypes() const;
54025402
void resolveNestedType(std::pair<Identifier, Type> &nested) const;
@@ -5474,7 +5474,7 @@ class ArchetypeType : public SubstitutableType,
54745474
ArchetypeType *getRoot() const;
54755475

54765476
/// Get the generic environment this archetype lives in.
5477-
GenericEnvironment *getGenericEnvironment() const;
5477+
GenericEnvironment *getGenericEnvironment() const { return Environment; }
54785478

54795479
/// Get the protocol/class existential type that most closely represents the
54805480
/// set of constraints on this archetype.
@@ -5706,7 +5706,8 @@ class NestedArchetypeType final : public ArchetypeType,
57065706
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
57075707
DependentMemberType *InterfaceType,
57085708
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
5709-
Type Superclass, LayoutConstraint Layout);
5709+
Type Superclass, LayoutConstraint Layout,
5710+
GenericEnvironment *Environment);
57105711

57115712
/// Retrieve the parent of this archetype, or null if this is a
57125713
/// primary archetype.
@@ -5729,7 +5730,8 @@ class NestedArchetypeType final : public ArchetypeType,
57295730
ArchetypeType *Parent,
57305731
Type InterfaceType,
57315732
ArrayRef<ProtocolDecl *> ConformsTo,
5732-
Type Superclass, LayoutConstraint Layout);
5733+
Type Superclass, LayoutConstraint Layout,
5734+
GenericEnvironment *Environment);
57335735
};
57345736
BEGIN_CAN_TYPE_WRAPPER(NestedArchetypeType, ArchetypeType)
57355737
CanArchetypeType getParent() const {

lib/AST/GenericEnvironment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
281281
auto *depMemTy = requirements.anchor->castTo<DependentMemberType>();
282282
result = NestedArchetypeType::getNew(ctx, parentArchetype, depMemTy,
283283
requirements.protos, superclass,
284-
requirements.layout);
284+
requirements.layout, this);
285285
parentArchetype->registerNestedType(depMemTy->getName(), result);
286286
} else if (genericParam->isTypeSequence()) {
287287
result = SequenceArchetypeType::get(ctx, this, genericParam,

lib/AST/Type.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,19 +3210,6 @@ ArchetypeType::ArchetypeType(TypeKind Kind,
32103210
getSubclassTrailingObjects<ProtocolDecl *>());
32113211
}
32123212

3213-
GenericEnvironment *ArchetypeType::getGenericEnvironment() const {
3214-
if (Environment) {
3215-
return Environment;
3216-
}
3217-
3218-
// Nested archetypes get their environment from their root.
3219-
if (auto nested = dyn_cast<NestedArchetypeType>(this)) {
3220-
return Environment = nested->getRoot()->getGenericEnvironment();
3221-
}
3222-
3223-
llvm_unreachable("Archetype without a generic environment!");
3224-
}
3225-
32263213
ArchetypeType *ArchetypeType::getRoot() const {
32273214
auto parent = this;
32283215
while (auto nested = dyn_cast<NestedArchetypeType>(parent)) {
@@ -3283,11 +3270,11 @@ NestedArchetypeType::NestedArchetypeType(const ASTContext &Ctx,
32833270
Type InterfaceType,
32843271
ArrayRef<ProtocolDecl *> ConformsTo,
32853272
Type Superclass,
3286-
LayoutConstraint Layout)
3273+
LayoutConstraint Layout,
3274+
GenericEnvironment *Environment)
32873275
: ArchetypeType(TypeKind::NestedArchetype, Ctx,
32883276
Parent->getRecursiveProperties(),
3289-
InterfaceType, ConformsTo, Superclass, Layout,
3290-
/*Environment=*/nullptr),
3277+
InterfaceType, ConformsTo, Superclass, Layout, Environment),
32913278
Parent(Parent)
32923279
{
32933280
}
@@ -3608,7 +3595,8 @@ CanNestedArchetypeType NestedArchetypeType::getNew(
36083595
DependentMemberType *InterfaceType,
36093596
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
36103597
Type Superclass,
3611-
LayoutConstraint Layout) {
3598+
LayoutConstraint Layout,
3599+
GenericEnvironment *Environment) {
36123600
assert(!Superclass || Superclass->getClassOrBoundGenericClass());
36133601

36143602
// Gather the set of protocol declarations to which this archetype conforms.
@@ -3621,7 +3609,7 @@ CanNestedArchetypeType NestedArchetypeType::getNew(
36213609
alignof(NestedArchetypeType), arena);
36223610

36233611
return CanNestedArchetypeType(::new (mem) NestedArchetypeType(
3624-
Ctx, Parent, InterfaceType, ConformsTo, Superclass, Layout));
3612+
Ctx, Parent, InterfaceType, ConformsTo, Superclass, Layout, Environment));
36253613
}
36263614

36273615
CanPrimaryArchetypeType

lib/IRGen/GenArchetype.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ llvm::Value *irgen::emitArchetypeWitnessTableRef(IRGenFunction &IGF,
186186
auto wtable = IGF.tryGetLocalTypeData(archetype, localDataKind);
187187
if (wtable) return wtable;
188188

189-
auto origRoot = archetype->getRoot();
190-
auto environment = origRoot->getGenericEnvironment();
189+
auto environment = archetype->getGenericEnvironment();
191190

192191
// Otherwise, ask the generic signature for the environment for the best
193192
// path to the conformance.

0 commit comments

Comments
 (0)