struct S { this(ref int val) { ++val; } }
pure unittest
{
int val = 3;
auto s = S(val);
assert(val == 4);
}
The above fails to compile with dmd v2.074.0:
purestaticstruct.d(6): Error: pure function 'purestaticstruct.__unittestL3_1' cannot call impure constructor 'purestaticstruct.S.this'
If the struct is moved inside the unittest, it compiles as expected.
Comment #1 by ag0aep6g — 2017-05-09T15:23:08Z
You have to mark the constructor as `pure`:
struct S { this(ref int val) pure { ++val; } }
Attributes are only inferred under special circumstances. You generally have to be explicit with them. Closing as invalid. Please reopen if I'm missing the point.
Comment #2 by nick — 2017-05-12T08:35:59Z
Yes, thanks. What I was confused by was actually when the struct is defined inside the unittest with the static keyword - pure is not inferred. Non-static nested struct inference works, it seemed weird that adding static prevents inference. But obviously the compiler treats it the same as the code in comment 0.