'in' expression for AA calls KeyType.toHash/opEquals if exist, but not detected as GC usage even if these functions are not @nogc.
Example: (In this case, compiler should require that toHash/opEquals are @nogc.)
struct KeyType
{
int x;
size_t toHash() const @safe nothrow
{
return x;
}
bool opEquals(in KeyType r) const
{
return x == r.x;
}
}
ulong func(ulong[KeyType] aa) @nogc
{
if (auto p = KeyType(10) in aa) // can call KeyType.toHash/opEquals
return *p;
return aa.length;
}
Comment #1 by stanislav.blinov — 2021-12-08T16:51:16Z
If we go by the spec: https://dlang.org/spec/garbage.html#op_involving_gc
it explicitly lists AA lookups as GC operations. That being said, AAs outright swallow attributes (see related issue). Regardless, if the specification is correct with regards to GC usage, in this case at the very least `func` shouldn't compile.
Comment #2 by robert.schadek — 2024-12-13T18:32:47Z