Bug 10045 – Missed warning with wrong toHash() + toHash documentation

Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-08T04:31:46Z
Last change time
2023-04-10T15:02:02Z
Keywords
diagnostic, pull
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-05-08T04:31:46Z
struct Foo1 { hash_t toHash() { return 0; } } struct Foo2 { hash_t toHash() const { // line 7. return 0; } } struct Foo3 { hash_t toHash() const nothrow @safe { return 0; } } void main () { int[Foo1] aa1; aa1[Foo1()] = 1; int[Foo2] aa2; aa2[Foo2()] = 2; int[Foo3] aa3; aa3[Foo3()] = 3; } Compiling with -wi DMD 2.063beta gives: temp2.d(7): Warning: toHash() must be declared as extern (D) size_t toHash() const nothrow @safe, not const uint() Expected: temp2.d(2): Warning: toHash() must be declared as extern (D) size_t toHash() const nothrow @safe, not const uint() temp2.d(7): Warning: toHash() must be declared as extern (D) size_t toHash() const nothrow @safe, not const uint() Additionally I think the site documentation of the section "Using Structs or Unions as the KeyType" should be updated: http://dlang.org/hash-map.html Because it shows signatures that are different from the one required in the warning: const hash_t opHash(); const bool opEquals(ref const KeyType s); const int opCmp(ref const KeyType s);
Comment #1 by code — 2014-10-04T15:45:57Z
It's a pity that you didn't made a pull for the documentation.
Comment #2 by code — 2014-10-04T15:46:15Z
Comment #3 by code — 2014-10-04T16:57:15Z
Nowadays the compiler silently generates a toHash function when the signature of the declared one doesn't match. This makes it unnecessary hard to declare a toHash function.
Comment #4 by razvan.nitu1305 — 2023-04-10T15:02:02Z
I cannot reproduce this.