[Issue 21947] New: RedBlackTree and hashOf do not play well together

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 21 12:01:06 UTC 2021


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

          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 at puremagic.com
          Reporter: deadalnix at 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`
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/hash.d(521):    
   `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.

--


More information about the Digitalmars-d-bugs mailing list