Skip to content

Commit bf1cec2

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 732913f commit bf1cec2

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
@@ -3676,6 +3676,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
36763676
return true;
36773677
}
36783678

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

38803889
diagnoseIncompatibleProtocolsForMoveOnlyType(ED);
3890+
diagnoseExtensionOfInvertible(ED);
38813891

38823892
checkTupleExtension(ED);
38833893
}

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)