Skip to content

Commit cdcaec2

Browse files
committed
Sema: Allow extensions of parametrized protocols
1 parent b40d807 commit cdcaec2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,8 @@ ExtendedTypeRequest::evaluate(Evaluator &eval, ExtensionDecl *ext) const {
27842784
}
27852785

27862786
// Cannot extend function types, tuple types, etc.
2787-
if (!extendedType->getAnyNominal()) {
2787+
if (!extendedType->getAnyNominal() &&
2788+
!extendedType->is<ParametrizedProtocolType>()) {
27882789
diags.diagnose(ext->getLoc(), diag::non_nominal_extension, extendedType)
27892790
.highlight(extendedRepr->getSourceRange());
27902791
return error();

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,21 @@ static Type formExtensionInterfaceType(
551551
if (type->is<ProtocolCompositionType>())
552552
type = type->getCanonicalType();
553553

554+
// A parametrized protocol type is not a nominal. Unwrap it to get
555+
// the underlying nominal, and record a same-type requirement for
556+
// the primary associated type.
557+
if (auto *paramProtoTy = type->getAs<ParametrizedProtocolType>()) {
558+
auto *protoTy = paramProtoTy->getBaseType();
559+
type = protoTy;
560+
561+
auto *depMemTy = DependentMemberType::get(
562+
protoTy->getDecl()->getSelfInterfaceType(),
563+
paramProtoTy->getAssocType());
564+
sameTypeReqs.emplace_back(
565+
RequirementKind::SameType, depMemTy,
566+
paramProtoTy->getArgumentType());
567+
}
568+
554569
Type parentType = type->getNominalParent();
555570
GenericTypeDecl *genericDecl = type->getAnyGeneric();
556571

0 commit comments

Comments
 (0)