Skip to content

Commit 8ab3964

Browse files
committed
Sema: ban extensions of invertible protocols
There's no good reason to permit them. Unlike Sendable, Copyable conformances are pervasive, so this is as if we're permitting extensions of `Any`. Until there's a good argument in favor of such extensions, I'm removing the capability now.
1 parent d337fef commit 8ab3964

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,8 +2537,8 @@ ERROR(invalid_nominal_extension,none,
25372537
(Type, Type))
25382538
NOTE(invalid_extension_rewrite,none,
25392539
"did you mean to extend %0 instead?", (Type))
2540-
ERROR(synthesized_nominal_extension,none,
2541-
"cannot extend synthesized type %0", (Type))
2540+
ERROR(cannot_extend_nominal,none,
2541+
"cannot extend %kind0", (const NominalTypeDecl *))
25422542

25432543
ERROR(retroactive_not_in_extension_inheritance_clause,none,
25442544
"'retroactive' attribute only applies in inheritance clauses in "

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,6 +3677,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
36773677
return true;
36783678
}
36793679

3680+
static void diagnoseExtensionOfInvertible(ExtensionDecl *ED) {
3681+
auto *nominal = ED->getExtendedNominal();
3682+
if (auto *proto = dyn_cast_or_null<ProtocolDecl>(nominal)) {
3683+
if (proto->getInvertibleProtocolKind()) {
3684+
ED->diagnose(diag::cannot_extend_nominal, nominal);
3685+
}
3686+
}
3687+
}
3688+
36803689
static void checkTupleExtension(ExtensionDecl *ED) {
36813690
auto *nominal = ED->getExtendedNominal();
36823691
if (!nominal || !isa<BuiltinTupleDecl>(nominal))
@@ -3879,6 +3888,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
38793888
TypeChecker::checkDistributedActor(SF, nominal);
38803889

38813890
diagnoseIncompatibleProtocolsForMoveOnlyType(ED);
3891+
diagnoseExtensionOfInvertible(ED);
38823892

38833893
checkTupleExtension(ED);
38843894
}

test/Sema/copyable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ enum namespace {
2626

2727
func Copyable() -> Copyable { return 0 }
2828
}
29+
30+
extension Copyable { // expected-error {{cannot extend protocol 'Copyable'}}
31+
func hello() {}
32+
}

0 commit comments

Comments
 (0)