Bug 17387 – static struct this(ref) not pure

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2017-05-09T15:07:00Z
Last change time
2017-05-12T08:35:59Z
Assigned to
nobody
Creator
nick

Comments

Comment #0 by nick — 2017-05-09T15:07:32Z
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.