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.
Comment #1 by schveiguy — 2022-04-03T02:21:22Z
This recently came up again: https://forum.dlang.org/post/[email protected]
It's because the machinery to infer @safe and nothrow is not done for class methods, even on templated classes.
What needs to happen is a 2-layer method for `toHash`, where one is inferred, and the other one must copy the attributes.
I'm not sure why the hard-coded attributes were added, but they seem to have been added when `toHash` was added.
Comment #2 by robert.schadek — 2024-12-01T16:38:55Z