[Issue 19877] [dip1000] std.container.rbtree is unsafely accessing private data

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 7 10:40:12 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=19877

Ajieskola at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Ajieskola at gmail.com
          Component|phobos                      |druntime

--- Comment #3 from Ajieskola at gmail.com ---
I'd argue it behaves correctly now. Private state should be private. If
`.hashOf`, or any other external funtion, needs to access it directly, it ought
to fail. The correct thing to do is to add `toHash` member function to the
element `struct`.

Current `-dip1000` prevents errors like this:

```
struct Ternary
{ private ubyte state;
  @safe auto opEquals(const Ternary rhs) const
  { if (state >= 2) return rhs.state >= 2;
    else return state == rhs.state;
  }
}

@safe void main()
{ Ternary a = void, b = void;
  //fails with high likelihood
  if(a == b) assert(.hashOf(a) == .hashOf(b), "WAT?");
}
```

Perhaps the error message should suggest adding the `toHash` function, though.

--


More information about the Digitalmars-d-bugs mailing list