Bug 7833 – [2.059 Beta] Changelog should clearly mention struct literal/opCmp/opEquals/toHash changes

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-05T17:35:00Z
Last change time
2017-07-21T04:37:08Z
Keywords
spec
Assigned to
nobody
Creator
bus_dbugzilla

Comments

Comment #0 by bus_dbugzilla — 2012-04-05T17:35:31Z
struct Foo { const bool opEquals(ref const Foo b) { return true; } } void main() { assert( Foo() == Foo() ); } >dmd testOpCmp.d testOpCmp.d(11): Error: function testOpCmp.Foo.opEquals (ref const(Foo) b) is not callable using argument types (Foo) testOpCmp.d(11): Error: Foo() is not an lvalue It works if Foo is changed to a class.
Comment #1 by issues.dlang — 2012-04-05T18:22:49Z
Actually, I don't believe that this is a bug. With this release, Foo() has been fixed so that it's not an lvalue. Previously Foo() would have been considered an lvalue but a Foo returned from a function would not making it so that assert(foo == Foo()); worked by assert(foo == getFoo()); did not. Many of us considered it incredibly inconsistent to treat struct literals as lvalues. They're temporaries and don't represent variables or places in memory at all. So, they shouldn't be lvalues. And not they're not, so your opEquals doesn't work. Given that auto ref currently only works with templates, I believe that the correct solution for this is to then declare 2 overloads of opEquals. struct Foo { bool opEquals(ref const Foo b) const { return true; } bool opEquals(Foo b) const { return true; } } One will work with lvalues and avoid copying them, whereas the other will work with rvalues. I believe that when Kenji fixed it so that struct literals aren't lvalues, he also went into Phobos and fixed it so that all structs which define opEquals define both overloads of opEquals. You'll need to do the same. So yes, this change breaks code, but as I understand it, it's a bug fix, not a regression.
Comment #2 by bus_dbugzilla — 2012-04-05T19:18:05Z
I see. I had tried removing the "const" and it still failed, but removing both "const" AND "ref" does indeed work. I'm going to remove the "rejects-valid" and leave this open (with a new title) because I think it's very important the changelog clearly mentions this in the "Changed Features", Ie that struct literals are no longer lvalues *and* that this means opCmp/opEquals/etc now need a non-const non-ref overload.
Comment #3 by bus_dbugzilla — 2012-04-06T00:10:57Z
It also needs to mention this stuff in the "Operator Overloading" page of the documentation.
Comment #4 by bus_dbugzilla — 2012-04-08T23:17:14Z
The changes also need to be reflected on the "Associative Arrays" page with toHash.
Comment #5 by lovelydear — 2012-04-21T10:16:19Z
On 2.059, the test compiles, but now, swapping "struct" with "class", we get: PS E:\DigitalMars\dmd2\samples> rdmd bug.d bug.d(16): Error: no property 'opCall' for type 'bug.Foo' bug.d(16): Error: no property 'opCall' for type 'bug.Foo'
Comment #6 by dmitry.olsh — 2012-04-21T10:30:02Z
Class instances are created with new or emplace function. Foo() won't work for classes by design.
Comment #7 by dlang-bugzilla — 2017-07-21T04:37:08Z
I believe the train has left the station insofar updating the changelog goes. I believe the spec pages have received lots of updates throughout the 5 years since this issue was filed. If you believe they are still lacking in some regard, please open a new issue (or, better yet, send a pull request, as you're already accustomed with the subject.)