www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21947] New: RedBlackTree and hashOf do not play well together


          Issue ID: 21947
           Summary: RedBlackTree and hashOf do not play well together
           Product: D
           Version: D2
          Hardware: All
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: deadalnix gmail.com

See https://run.dlang.io/is/0oIsaQ

struct S {
    S[] childs;

    int opCmp(const ref S rhs) const {
        if (childs.length != rhs.childs.length) {
            return cast(int) (childs.length - rhs.childs.length);

        foreach (const ref c; childs) {
            // ...

        return 0;

import std.container.rbtree;
alias SSet = RedBlackTree!S;

Which fails with the following error:

/dlang/dmd/linux/bin64/../../src/phobos/std/container/rbtree.d(1113): Error:
` safe` function `std.container.rbtree.RedBlackTree!(S, "a < b",
false).RedBlackTree.toHash` cannot call ` system` function
   `core.internal.hash.hashOf!(S).hashOf` is declared here
/dlang/dmd/linux/bin64/../../src/phobos/std/container/rbtree.d(1113): Error:
function `core.internal.hash.hashOf!(S).hashOf` is not `nothrow`
/dlang/dmd/linux/bin64/../../src/phobos/std/container/rbtree.d(1107): Error:
`nothrow` function `std.container.rbtree.RedBlackTree!(S, "a < b",
false).RedBlackTree.toHash` may throw
onlineapp.d(18): Error: template instance
`std.container.rbtree.RedBlackTree!(S, "a < b", false)` error instantiating

Apparently, the auto-generated hashOf isn't nothrow, while RedBlackTree is.

It is a bit puzzling why these constraints are in place. Changing childs to be
an int[] instead, for instance, works.

I'm not sure is RedBlackTree has unrealistic expectation when it comes to the
provided hash method, or if the autogenerated one is wrong, but it seems to be
a problem to me that elements all provided by phobos to not fit together.

May 21