Skip to content

RedBlackTree and hashOf do not play well together #10467

Open
@dlangBugzillaToGithub

Description

@dlangBugzillaToGithub

deadalnix (@deadalnix) reported this on 2021-05-21T12:01:06Z

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

CC List

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions