Skip to content

Commit 7c46df6

Browse files
authored
Merge pull request #28633 from kitaisreal/classes-equatable-hashable-synthesis-diagnostics
[Type checker]: Classes `Hashable` and `Equatable` synthesis diagnostic
2 parents 163528f + 98d5e96 commit 7c46df6

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,9 @@ NOTE(missing_member_type_conformance_prevents_synthesis, none,
27172717
"protocol %2, preventing synthesized conformance "
27182718
"of %3 to %2",
27192719
(unsigned, Type, Type, Type))
2720+
NOTE(classes_automatic_protocol_synthesis,none,
2721+
"automatic synthesis of '%0' is not supported for classes",
2722+
(StringRef))
27202723

27212724
// Dynamic Self
27222725
ERROR(dynamic_self_non_method,none,

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
165165
nominal->getDeclaredInterfaceType());
166166
}
167167
}
168+
169+
if (auto *classDecl = dyn_cast<ClassDecl>(nominal)) {
170+
ctx.Diags.diagnose(classDecl->getLoc(),
171+
diag::classes_automatic_protocol_synthesis,
172+
protocol->getName().str());
173+
}
168174
}
169175

170176
/// Creates a named variable based on a prefix character and a numeric index.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -primary-file %s
2+
3+
class Foo: Equatable {}
4+
// expected-error@-1 {{type 'Foo' does not conform to protocol 'Equatable'}} expected-note@-1 {{automatic synthesis of 'Equatable' is not supported for classes}}
5+
6+
class Bar: Hashable {}
7+
// expected-error@-1 {{type 'Bar' does not conform to protocol 'Hashable'}} expected-note@-1 {{automatic synthesis of 'Hashable' is not supported for classes}}
8+
// expected-error@-2 {{type 'Bar' does not conform to protocol 'Equatable'}} expected-note@-2 {{automatic synthesis of 'Equatable' is not supported for classes}}

0 commit comments

Comments
 (0)