Skip to content

Commit 278ead3

Browse files
committed
Resugar the structural type
Structural type computation ought to actually return a typealias type here using the same mechanism we compute the interface type with (minus the metatype).
1 parent 2ab5ea8 commit 278ead3

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -965,15 +965,44 @@ RequirementRequest::evaluate(Evaluator &evaluator,
965965
}
966966

967967
llvm::Expected<Type>
968-
swift::StructuralTypeRequest::evaluate(Evaluator &evaluator,
969-
TypeAliasDecl *D) const {
970-
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
971-
if (!D->getDeclContext()->isCascadingContextForLookup(
972-
/*functionsAreNonCascading*/true)) {
968+
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+
977+
TypeResolutionOptions options((typeAlias->getGenericParams()
978+
? TypeResolverContext::GenericTypeAliasDecl
979+
: TypeResolverContext::TypeAliasDecl));
980+
981+
if (!typeAlias->getDeclContext()->isCascadingContextForLookup(
982+
/*functionsAreNonCascading*/ true)) {
973983
options |= TypeResolutionFlags::KnownNonCascadingDependency;
974984
}
985+
986+
// This can happen when code completion is attempted inside
987+
// of typealias underlying type e.g. `typealias F = () -> Int#^TOK^#`
988+
auto &ctx = typeAlias->getASTContext();
989+
auto underlyingTypeRepr = typeAlias->getUnderlyingTypeRepr();
990+
if (!underlyingTypeRepr) {
991+
typeAlias->setInvalid();
992+
return ErrorType::get(ctx);
993+
}
975994

976-
auto typeRepr = D->getUnderlyingTypeLoc().getTypeRepr();
977-
auto resolution = TypeResolution::forStructural(D);
978-
return resolution.resolveType(typeRepr, options);
995+
auto resolution = TypeResolution::forStructural(typeAlias);
996+
auto type = resolution.resolveType(underlyingTypeRepr, options);
997+
998+
auto *genericSig = typeAlias->getGenericSignature();
999+
SubstitutionMap subs;
1000+
if (genericSig)
1001+
subs = genericSig->getIdentitySubstitutionMap();
1002+
1003+
Type parent;
1004+
auto parentDC = typeAlias->getDeclContext();
1005+
if (parentDC->isTypeContext())
1006+
parent = parentDC->getDeclaredInterfaceType();
1007+
return TypeAliasType::get(typeAlias, parent, subs, type);
9791008
}

0 commit comments

Comments
 (0)