Skip to content

Commit 50fd5b5

Browse files
committed
Add ArchetypeType::isRoot() and use it instead of implicit "root" checks
1 parent 6ae6ab9 commit 50fd5b5

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5464,7 +5464,10 @@ class ArchetypeType : public SubstitutableType,
54645464

54655465
/// Return the root archetype parent of this archetype.
54665466
ArchetypeType *getRoot() const;
5467-
5467+
5468+
/// Determine whether this is a root archetype within the environment.
5469+
bool isRoot() const;
5470+
54685471
/// Get the generic environment this archetype lives in.
54695472
GenericEnvironment *getGenericEnvironment() const { return Environment; }
54705473

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,7 @@ class Verifier : public ASTWalker {
618618
}
619619

620620
// Get the archetype's generic signature.
621-
GenericEnvironment *archetypeEnv;
622-
if (auto seq = dyn_cast<SequenceArchetypeType>(root)) {
623-
archetypeEnv = seq->getGenericEnvironment();
624-
} else {
625-
auto rootPrimary = cast<PrimaryArchetypeType>(root);
626-
archetypeEnv = rootPrimary->getGenericEnvironment();
627-
}
621+
GenericEnvironment *archetypeEnv = root->getGenericEnvironment();
628622
auto archetypeSig = archetypeEnv->getGenericSignature();
629623

630624
auto genericCtx = Generics.back();

lib/AST/SubstitutionMap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
249249
// If we have an archetype, map out of the context so we can compute a
250250
// conformance access path.
251251
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
252+
// Only consider root archetypes.
253+
if (!archetype->isRoot())
254+
return Type();
255+
252256
if (!isa<PrimaryArchetypeType>(archetype) &&
253257
!isa<SequenceArchetypeType>(archetype))
254258
return Type();

lib/AST/Type.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,10 @@ ArchetypeType *ArchetypeType::getRoot() const {
32253225
Type(gp))->castTo<ArchetypeType>();
32263226
}
32273227

3228+
bool ArchetypeType::isRoot() const {
3229+
return getInterfaceType()->is<GenericTypeParamType>();
3230+
}
3231+
32283232
Type ArchetypeType::getExistentialType() const {
32293233
// Opened types hold this directly.
32303234
if (auto opened = dyn_cast<OpenedArchetypeType>(this))
@@ -4247,21 +4251,19 @@ static Type substType(Type derivedType,
42474251
if (isa<GenericTypeParamType>(substOrig))
42484252
return ErrorType::get(type);
42494253

4250-
if (isa<PrimaryArchetypeType>(substOrig))
4251-
return ErrorType::get(type);
4252-
4253-
if (isa<SequenceArchetypeType>(substOrig))
4254-
return ErrorType::get(type);
4255-
42564254
// Opened existentials cannot be substituted in this manner,
42574255
// but if they appear in the original type this is not an
42584256
// error.
4259-
if (isa<OpenedArchetypeType>(substOrig))
4257+
auto origArchetype = cast<ArchetypeType>(substOrig);
4258+
if (isa<OpenedArchetypeType>(origArchetype->getRoot()))
42604259
return Type(type);
42614260

4261+
// Root archetypes must already have been substituted above.
4262+
if (origArchetype->isRoot())
4263+
return ErrorType::get(type);
4264+
42624265
// For nested archetypes, we can substitute the parent.
4263-
auto nestedArchetype = cast<ArchetypeType>(substOrig);
4264-
auto parent = nestedArchetype->getParent();
4266+
auto parent = origArchetype->getParent();
42654267
assert(parent && "Not a nested archetype");
42664268

42674269
// Substitute into the parent type.
@@ -4273,7 +4275,7 @@ static Type substType(Type derivedType,
42734275
return Type(type);
42744276

42754277
// Get the associated type reference from a child archetype.
4276-
AssociatedTypeDecl *assocType = nestedArchetype->getInterfaceType()
4278+
AssociatedTypeDecl *assocType = origArchetype->getInterfaceType()
42774279
->castTo<DependentMemberType>()->getAssocType();
42784280

42794281
return getMemberForBaseType(lookupConformances, parent, substParent,

lib/IRGen/LocalTypeData.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ static void maybeEmitDebugInfoForLocalTypeData(IRGenFunction &IGF,
336336
auto type = dyn_cast<ArchetypeType>(key.Type);
337337
if (!type)
338338
return;
339+
if (!type->isRoot())
340+
return;
339341
if (!isa<PrimaryArchetypeType>(type) && !isa<SequenceArchetypeType>(type))
340342
return;
341343

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ static bool isArchetypeValidInFunction(ArchetypeType *A, const SILFunction *F) {
8989
auto root = A->getRoot();
9090
if (!isa<PrimaryArchetypeType>(root) && !isa<SequenceArchetypeType>(root))
9191
return true;
92-
if (isa<OpenedArchetypeType>(A->getRoot()))
92+
if (isa<OpenedArchetypeType>(root))
9393
return true;
94-
if (isa<OpaqueTypeArchetypeType>(A->getRoot()))
94+
if (isa<OpaqueTypeArchetypeType>(root))
9595
return true;
9696

9797
// Ok, we have a primary archetype, make sure it is in the nested generic

0 commit comments

Comments
 (0)