Bug 16764 – `hashOf` is misleading, error-prone, and useless

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-24T18:12:00Z
Last change time
2017-01-16T23:24:41Z
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2016-11-24T18:12:12Z
In our root `object` module we have a function `hashOf` [1] with correct definition: "to calculate hash of raw byte representation of given object" (at posting time, see [2]). This function should not be there because: 1. It is misleading (and as a result error-prone) because user doesn't expect that logical object structure is not considered and this function operates with raw representation bytes. 2. Has signature allowing incorrect usage (and as a result the function is extremely error-prone). 3. It is not generally useful because primary purpose of hash functions is to calculate hash of byte array with dynamic length (`void[]`) and this function only accept data with size known at compilation time. The code illustrating the issue (#1 and #2 from list above): --- void main() { // #1: hashOf(5); // #1: hash of raw int bytes, different on different systems real r; hashOf(r); // #1: hash of raw bytes of real including padding hashOf(MyStruct()); // #1: hash of raw bytes of struct including padding and ignoring `toHash` function int[] arr = [1]; hashOf(arr); // #1: hash of raw bytes of ptr+length assert(hashOf(arr) == hashOf([1])); // #1: as a result this fails // #2: hashOf(arr.ptr, arr.length); // #2: hash of raw bytes of ptr with seed set to length } --- [1] http://dlang.org/phobos/object.html#.hashOf [2] https://github.com/dlang/druntime/blob/7962fb8acaeb0008c531d1ae170cd15ff59558ea/src/object.d#L3203
Comment #1 by joeyemmons — 2016-12-04T07:11:34Z
Worst part is hashOf used to not work this way, got changed to cast to void[] when before is just called core.internal.hash.hashOf without messing with the input. The change is breaking and seems horribly wrong. Seems walter made the change. https://github.com/dlang/druntime/commit/267fa2a06289c04200f720019050e7387f5e5a00
Comment #2 by github-bugzilla — 2016-12-04T16:51:46Z
Commit pushed to stable at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/5c187e05ff87404ebf4884cfc548324c2c3ed0fc Revert object.hashOf changes from "use array interface to hashOf()" fixes issues 16654 & 16764.
Comment #3 by github-bugzilla — 2016-12-09T00:27:14Z
Commit pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/5c187e05ff87404ebf4884cfc548324c2c3ed0fc Revert object.hashOf changes from "use array interface to hashOf()"
Comment #4 by verylonglogin.reg — 2016-12-15T15:21:38Z
So #1 and #3 are solved by the pull, but #2 still stays. Opened Issue 16973 for it.
Comment #5 by github-bugzilla — 2016-12-27T13:11:13Z
Commit pushed to scope at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/5c187e05ff87404ebf4884cfc548324c2c3ed0fc Revert object.hashOf changes from "use array interface to hashOf()"
Comment #6 by github-bugzilla — 2017-01-07T03:02:15Z
Commit pushed to stable at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/5c187e05ff87404ebf4884cfc548324c2c3ed0fc Revert object.hashOf changes from "use array interface to hashOf()"
Comment #7 by github-bugzilla — 2017-01-16T23:24:41Z
Commit pushed to newCTFE at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/5c187e05ff87404ebf4884cfc548324c2c3ed0fc Revert object.hashOf changes from "use array interface to hashOf()"