Skip to content

Commit f038281

Browse files
committed
AST: Assert if mapTypeIntoContext()/mapTypeOutOfContext() used on the wrong sort of type
Also, bail out earlier if we know we have no work to do.
1 parent c044889 commit f038281

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/AST/ArchetypeBuilder.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,13 +2020,7 @@ void ArchetypeBuilder::dump(llvm::raw_ostream &out) {
20202020

20212021
Type ArchetypeBuilder::mapTypeIntoContext(const DeclContext *dc, Type type,
20222022
LazyResolver *resolver) {
2023-
// If the type is not dependent, there's nothing to map.
2024-
if (!type->hasTypeParameter())
2025-
return type;
2026-
20272023
auto genericParams = dc->getGenericParamsOfContext();
2028-
assert(genericParams && "Missing generic parameters for dependent context");
2029-
20302024
return mapTypeIntoContext(dc->getParentModule(), genericParams, type,
20312025
resolver);
20322026
}
@@ -2035,13 +2029,15 @@ Type ArchetypeBuilder::mapTypeIntoContext(Module *M,
20352029
GenericParamList *genericParams,
20362030
Type type,
20372031
LazyResolver *resolver) {
2038-
// If the type is not dependent, or we have no generic params, there's nothing
2039-
// to map.
2040-
if (!genericParams || !type->hasTypeParameter())
2032+
auto canType = type->getCanonicalType();
2033+
assert(!canType->hasArchetype() && "already have a contextual type");
2034+
if (!canType->hasTypeParameter())
20412035
return type;
20422036

2037+
assert(genericParams && "dependent type in non-generic context");
2038+
20432039
unsigned genericParamsDepth = genericParams->getDepth();
2044-
return type.transform([&](Type type) -> Type {
2040+
type = type.transform([&](Type type) -> Type {
20452041
// Map a generic parameter type to its archetype.
20462042
if (auto gpType = type->getAs<GenericTypeParamType>()) {
20472043
auto index = gpType->getIndex();
@@ -2064,7 +2060,7 @@ Type ArchetypeBuilder::mapTypeIntoContext(Module *M,
20642060
if (!myGenericParams->getAllArchetypes().empty())
20652061
return myGenericParams->getPrimaryArchetypes()[index];
20662062

2067-
// During type-checking, we may try to mapTypeInContext before
2063+
// During type-checking, we may try to mapTypeIntoContext before
20682064
// AllArchetypes has been built, so fall back to the generic params.
20692065
return myGenericParams->getParams()[index]->getArchetype();
20702066
}
@@ -2078,6 +2074,9 @@ Type ArchetypeBuilder::mapTypeIntoContext(Module *M,
20782074

20792075
return type;
20802076
});
2077+
2078+
assert(!type->hasTypeParameter() && "not fully substituted");
2079+
return type;
20812080
}
20822081

20832082
Type
@@ -2089,10 +2088,13 @@ ArchetypeBuilder::mapTypeOutOfContext(const DeclContext *dc, Type type) {
20892088
Type ArchetypeBuilder::mapTypeOutOfContext(ModuleDecl *M,
20902089
GenericParamList *genericParams,
20912090
Type type) {
2092-
// If the context is non-generic, we're done.
2093-
if (!genericParams)
2091+
auto canType = type->getCanonicalType();
2092+
assert(!canType->hasTypeParameter() && "already have an interface type");
2093+
if (!canType->hasArchetype())
20942094
return type;
20952095

2096+
assert(genericParams && "dependent type in non-generic context");
2097+
20962098
// Capture the archetype -> interface type mapping.
20972099
TypeSubstitutionMap subs;
20982100
for (auto params = genericParams; params != nullptr;
@@ -2102,7 +2104,10 @@ Type ArchetypeBuilder::mapTypeOutOfContext(ModuleDecl *M,
21022104
}
21032105
}
21042106

2105-
return type.subst(M, subs, SubstFlags::AllowLoweredTypes);
2107+
type = type.subst(M, subs, SubstFlags::AllowLoweredTypes);
2108+
2109+
assert(!type->hasArchetype() && "not fully substituted");
2110+
return type;
21062111
}
21072112

21082113
bool ArchetypeBuilder::addGenericSignature(GenericSignature *sig,

0 commit comments

Comments
 (0)