Bug 7826 – [D2 Beta] Cannot use getHash in toHash without a warning
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-05T02:25:00Z
Last change time
2012-04-06T17:12:43Z
Assigned to
nobody
Creator
bus_dbugzilla
Comments
Comment #0 by bus_dbugzilla — 2012-04-05T02:25:46Z
This code works in 2.058, even with warnings enabled:
------------------------
struct Foo
{
string str;
const hash_t toHash()
{
return typeid(string).getHash(&str);
}
}
------------------------
But in the 2.059 beta:
>dmd -c -w test.d
test.d(5): Warning: toHash() must be declared as extern (D) uint toHash() const pure nothrow @safe, not const uint()
However, if the warning is is fixed, it *still* doesn't work:
------------------------
struct Foo
{
string str;
const pure nothrow @safe hash_t toHash()
{
return typeid(string).getHash(&str);
}
}
------------------------
testToHash.d(7): Error: pure function 'toHash' cannot call impure function 'getHash'
testToHash.d(7): Error: & D12TypeInfo_Aya6__initZ.getHash is not nothrow
testToHash.d(5): Error: function testToHash.Foo.toHash 'toHash' is nothrow yet may throw
Comment #1 by bus_dbugzilla — 2012-04-05T17:11:31Z
I don't know what the correct solution is supposed to be, but in any case it should be possible to use getHash in toHash with warnings enabled.
Comment #2 by clugdbug — 2012-04-06T00:35:06Z
Clearly if toHash() is required to be pure, getHash() must also be.
But I think it is unreasonable for the compiler to demand that toHash() be pure, especially at the present time.
Comment #3 by issues.dlang — 2012-04-06T00:49:29Z
I believe that tohash is among the functions which were decided must be @safe const pure nothrow on classes. So, in that case, I would expect it to be required (though not until Object is appropriately const-correct and whatnot). But I don't see why @safe, const, pure, or nothrow should be required for structs, since no inheritance is involved. With classes, with have to pick what we want and use that for everything (possibly with some overloads which don't have quite the same list of attributes), but structs don't have the same requirements/restrictions.
Comment #4 by github-bugzilla — 2012-04-06T17:10:03Z