Bug 16216 – struct equality compares padding

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-06-28T13:47:10Z
Last change time
2024-12-13T18:48:43Z
Keywords
wrong-code
Assigned to
No Owner
Creator
ag0aep6g
Moved to GitHub: dmd#19150 →

Comments

Comment #0 by ag0aep6g — 2016-06-28T13:47:10Z
---- struct Result { int value = 0; ubyte len = 0; } void stompStack(ubyte val) { ubyte[1024] x = val; } Result resultWithGarbagePadding() { Result result = void; result.value = 0; result.len = 0; return result; } void main() { Result r; stompStack(1); Result s = resultWithGarbagePadding(); assert(s.tupleof == r.tupleof); /* passes, ok */ assert(*cast(ubyte[5]*)&s == *cast(ubyte[5]*)&r); /* passes, ok */ assert(s == r); /* fails, should pass */ assert(s is r); /* fails, should probably pass */ } ---- Spec says [1]: "Equality for struct objects means the logical product of all equality results of the corresponding object fields." [1] https://dlang.org/spec/expression.html#EqualExpression
Comment #1 by bugzilla — 2019-02-08T21:53:00Z
PR: https://github.com/dlang/dmd/pull/9331 The PR does not affect the `is` comparisons. I'm not sure yet if it should.
Comment #2 by bugzilla — 2019-02-09T02:25:08Z
Now fixed for the == and != case.
Comment #3 by ag0aep6g — 2019-02-10T10:39:18Z
(In reply to Walter Bright from comment #1) > The PR does not affect the `is` comparisons. I'm not sure yet if it should. I think we're good with regards to `is`. The spec says: "For struct objects [...], identity is defined as the bits in the operands being identical." The padding is part of "the bits", so including it in the comparison is correct. I don't know why I thought `is` should behave differently. Maybe I had missed that sentence in the spec. (In reply to Walter Bright from comment #2) > Now fixed for the == and != case. I'm not a fan of calling this "fixed" when it needs a special compiler switch.
Comment #4 by robert.schadek — 2024-12-13T18:48:43Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19150 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB