Skip to content

[Type checker]: Classes Hashable and Equatable synthesis diagnostic #28633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,9 @@ NOTE(missing_member_type_conformance_prevents_synthesis, none,
"protocol %2, preventing synthesized conformance "
"of %3 to %2",
(unsigned, Type, Type, Type))
NOTE(classes_automatic_protocol_synthesis,none,
"automatic synthesis of '%0' is not supported for classes",
(StringRef))

// Dynamic Self
ERROR(dynamic_self_non_method,none,
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
nominal->getDeclaredInterfaceType());
}
}

if (auto *classDecl = dyn_cast<ClassDecl>(nominal)) {
ctx.Diags.diagnose(classDecl->getLoc(),
diag::classes_automatic_protocol_synthesis,
protocol->getName().str());
}
}

/// Creates a named variable based on a prefix character and a numeric index.
Expand Down
8 changes: 8 additions & 0 deletions test/Sema/classes_equatable_hashable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend -typecheck -verify -primary-file %s

class Foo: Equatable {}
// expected-error@-1 {{type 'Foo' does not conform to protocol 'Equatable'}} expected-note@-1 {{automatic synthesis of 'Equatable' is not supported for classes}}

class Bar: Hashable {}
// expected-error@-1 {{type 'Bar' does not conform to protocol 'Hashable'}} expected-note@-1 {{automatic synthesis of 'Hashable' is not supported for classes}}
// expected-error@-2 {{type 'Bar' does not conform to protocol 'Equatable'}} expected-note@-2 {{automatic synthesis of 'Equatable' is not supported for classes}}