Skip to content

Commit 25355b2

Browse files
slavapestovkavon
authored andcommitted
Sema: Don't re-sugar the type in StructuralTypeRequest::evaluate()
(cherry picked from commit 1db69ab)
1 parent 7bf570d commit 25355b2

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,10 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
10371037

10381038
auto getStructuralType = [](TypeDecl *typeDecl) -> Type {
10391039
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
1040-
if (typealias->getUnderlyingTypeRepr() != nullptr) {
1041-
auto type = typealias->getStructuralType();
1042-
if (auto *aliasTy = cast<TypeAliasType>(type.getPointer()))
1043-
return aliasTy->getSinglyDesugaredType();
1044-
return type;
1045-
}
1040+
// If the type alias was parsed from a user-written type representation,
1041+
// request a structural type to avoid unnecessary type checking work.
1042+
if (typealias->getUnderlyingTypeRepr() != nullptr)
1043+
return typealias->getStructuralType();
10461044
return typealias->getUnderlyingType();
10471045
}
10481046

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,30 +1090,36 @@ Type StructuralTypeRequest::evaluate(Evaluator &evaluator,
10901090
? TypeResolverContext::GenericTypeAliasDecl
10911091
: TypeResolverContext::TypeAliasDecl));
10921092

1093+
auto parentDC = typeAlias->getDeclContext();
1094+
auto &ctx = parentDC->getASTContext();
1095+
1096+
auto underlyingTypeRepr = typeAlias->getUnderlyingTypeRepr();
1097+
10931098
// This can happen when code completion is attempted inside
10941099
// of typealias underlying type e.g. `typealias F = () -> Int#^TOK^#`
1095-
auto &ctx = typeAlias->getASTContext();
1096-
auto underlyingTypeRepr = typeAlias->getUnderlyingTypeRepr();
10971100
if (!underlyingTypeRepr) {
10981101
typeAlias->setInvalid();
10991102
return ErrorType::get(ctx);
11001103
}
11011104

1102-
const auto type =
1103-
TypeResolution::forStructural(typeAlias, options,
1104-
/*unboundTyOpener*/ nullptr,
1105-
/*placeholderHandler*/ nullptr,
1106-
/*packElementOpener*/ nullptr)
1105+
auto result = TypeResolution::forStructural(typeAlias, options,
1106+
/*unboundTyOpener*/ nullptr,
1107+
/*placeholderHandler*/ nullptr,
1108+
/*packElementOpener*/ nullptr)
11071109
.resolveType(underlyingTypeRepr);
11081110

1111+
// Don't build a generic siganture for a protocol extension, because this
1112+
// request might be evaluated while building a protocol requirement signature.
1113+
if (parentDC->getSelfProtocolDecl())
1114+
return result;
1115+
11091116
auto genericSig = typeAlias->getGenericSignature();
11101117
SubstitutionMap subs;
11111118
if (genericSig)
11121119
subs = genericSig->getIdentitySubstitutionMap();
11131120

11141121
Type parent;
1115-
auto parentDC = typeAlias->getDeclContext();
11161122
if (parentDC->isTypeContext())
11171123
parent = parentDC->getSelfInterfaceType();
1118-
return TypeAliasType::get(typeAlias, parent, subs, type);
1124+
return TypeAliasType::get(typeAlias, parent, subs, result);
11191125
}

0 commit comments

Comments
 (0)