struct Wrapper {
// the bug only reproduces if there's an explicit ctor (in addition to the dtor):
this(int* _dtors) { this.dtors = _dtors; }
~this() { ++*dtors; }
int* dtors;
alias dtors this;
}
int* explicit(int* dtors) { return Wrapper(dtors).dtors; }
int* implicit(int* dtors) { return Wrapper(dtors); }
unittest {
bool check() {
{
int dtors;
int* foo = explicit(&dtors);
assert(foo == &dtors);
assert(dtors == 1); // <-- assertion fails in CTFE only
}
{
int dtors;
int* foo = implicit(&dtors);
assert(foo == &dtors);
assert(dtors == 1); // <-- assertion fails in both CTFE and runtime
}
return true;
}
assert(check());
// static assert(check()); // <-- uncomment to get CTFE behavior validation too
}
Comment #1 by kinke — 2022-05-13T10:56:09Z
These are most likely 2 separate issues. The CTFE one is probably a duplicate of https://issues.dlang.org/show_bug.cgi?id=22536 (not restricted to array literals). And the runtime one related to `alias this`.
Comment #2 by robert.schadek — 2024-12-13T19:22:44Z