Skip to content

Commit c254f4d

Browse files
committed
Setting the underlying type should set the structural type too
This avoids making the structural type dependent on whether the interface type has been computed and neatly avoids special-casing non-parsed declarations which set their underlying type up front.
1 parent d5c014b commit c254f4d

6 files changed

+12
-18
lines changed

Diff for: lib/AST/ASTPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
22942294
// preserving sugar.
22952295
llvm::SaveAndRestore<GenericEnvironment*> setGenericEnv(Options.GenericEnv,
22962296
decl->getGenericEnvironment());
2297-
printTypeLoc(TypeLoc::withoutLoc(Ty));
2297+
printTypeLoc(TypeLoc(decl->getUnderlyingTypeRepr(), Ty));
22982298
printGenericDeclGenericRequirements(decl);
22992299
}
23002300
}

Diff for: lib/AST/Decl.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,13 @@ Type TypeAliasDecl::getUnderlyingType() const {
35533553
}
35543554

35553555
void TypeAliasDecl::setUnderlyingType(Type underlying) {
3556+
// lldb creates global typealiases containing archetypes
3557+
// sometimes...
3558+
if (underlying->hasArchetype() && isGenericContext())
3559+
underlying = underlying->mapTypeOutOfContext();
3560+
getASTContext().evaluator.cacheOutput(
3561+
StructuralTypeRequest{const_cast<TypeAliasDecl *>(this)},
3562+
std::move(underlying));
35563563
getASTContext().evaluator.cacheOutput(
35573564
UnderlyingTypeRequest{const_cast<TypeAliasDecl *>(this)},
35583565
std::move(underlying));

Diff for: lib/AST/GenericSignatureBuilder.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -3785,8 +3785,6 @@ PotentialArchetype *GenericSignatureBuilder::realizePotentialArchetype(
37853785

37863786
static Type getStructuralType(TypeDecl *typeDecl) {
37873787
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
3788-
if (typealias->hasInterfaceType())
3789-
return typealias->getDeclaredInterfaceType();
37903788
return typealias->getStructuralType();
37913789
}
37923790

Diff for: lib/AST/TypeCheckRequests.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,5 @@ UnderlyingTypeRequest::getCachedResult() const {
837837

838838
void UnderlyingTypeRequest::cacheResult(Type value) const {
839839
auto *typeAlias = std::get<0>(getStorage());
840-
// lldb creates global typealiases containing archetypes
841-
// sometimes...
842-
if (value->hasArchetype() && typeAlias->isGenericContext())
843-
value = value->mapTypeOutOfContext();
844840
typeAlias->UnderlyingTy.setType(value);
845841
}

Diff for: lib/Sema/TypeCheckDecl.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3492,14 +3492,14 @@ UnderlyingTypeRequest::evaluate(Evaluator &evaluator,
34923492
return ErrorType::get(typeAlias->getASTContext());
34933493
}
34943494

3495-
if (TypeChecker::validateType(typeAlias->getASTContext(),
3496-
typeAlias->getUnderlyingTypeLoc(),
3495+
auto underlyingLoc = TypeLoc(typeAlias->getUnderlyingTypeRepr());
3496+
if (TypeChecker::validateType(typeAlias->getASTContext(), underlyingLoc,
34973497
TypeResolution::forInterface(typeAlias),
34983498
options)) {
34993499
typeAlias->setInvalid();
35003500
return ErrorType::get(typeAlias->getASTContext());
35013501
}
3502-
return ty;
3502+
return underlyingLoc.getType();
35033503
}
35043504

35053505
/// Bind the given function declaration, which declares an operator, to the corresponding operator declaration.

Diff for: lib/Sema/TypeCheckGeneric.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,7 @@ RequirementRequest::evaluate(Evaluator &evaluator,
966966

967967
llvm::Expected<Type>
968968
StructuralTypeRequest::evaluate(Evaluator &evaluator,
969-
TypeAliasDecl *typeAlias) const {
970-
// Fast path: If the interface type is already resolved, there's no need
971-
// to compute the structural type. This also prevents us from writing
972-
// ErrorTypes into otherwise valid ASTs with generated typealiases.
973-
if (typeAlias->hasInterfaceType()) {
974-
return typeAlias->getInterfaceType()->getMetatypeInstanceType();
975-
}
976-
969+
TypeAliasDecl *typeAlias) const {
977970
TypeResolutionOptions options((typeAlias->getGenericParams()
978971
? TypeResolverContext::GenericTypeAliasDecl
979972
: TypeResolverContext::TypeAliasDecl));

0 commit comments

Comments
 (0)