[Issue 15832] Crashing program when a helper template function is used to create a template struct
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Mon Mar 28 06:47:29 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=15832
--- Comment #7 from Atila Neves <atila.neves at gmail.com> ---
In the following code, the length is garbage, and uncommenting an assert makes
the problem move!
import std.traits;
import std.conv;
int delegate(int) dg;
static this() {
dg = i => i * 2;
}
struct MockScope(T) {
this(T)(ref T oldFunc, T newFunc) {
_oldFuncPtr = &oldFunc;
_oldFunc = oldFunc;
oldFunc = newFunc;
}
~this() {
*_oldFuncPtr = _oldFunc;
}
private:
T* _oldFuncPtr;
T _oldFunc;
}
struct Mock(T) {
this(ref T func) {
_returns = [ReturnType!T.init];
ReturnType!T inner(ParameterTypeTuple!T values) {
auto ret = _returns[0];
assert(_returns.length < 10, "Weird length1: " ~
_returns.length.to!string);
if(_returns.length > 1) _returns = _returns[1..$];
assert(_returns.length < 10, "Weird length2: " ~
_returns.length.to!string);
return ret;
}
func = &inner;
_scope = MockScope!(T)(func, &inner);
}
void returnValue(V...)(V value) {
_returns.length = 0;
foreach(v; value) _returns ~= v;
// uncomment below and things work
assert(_returns.length < 10, "Weird length3: " ~
_returns.length.to!string);
}
MockScope!T _scope;
ReturnType!T[] _returns;
}
auto mock(T)(ref T f) {
return Mock!T(f);
}
void main() {
//auto m = Mock!(typeof(dg))(dg);
auto m = mock(dg); // this should be equivalent but crashes
assert(dg(3) == 0);
m.returnValue(42);
assert(dg(3) == 42);
}
--
More information about the Digitalmars-d-bugs
mailing list