Bug 19003 – format!"" breaks with structs containing invariants violated in .init

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-06-18T12:46:17Z
Last change time
2018-07-19T16:04:58Z
Assigned to
No Owner
Creator
FeepingCreature

Comments

Comment #0 by default_357-line — 2018-06-18T12:46:17Z
Consider this code: module test; import std.conv : to; import std.format : format; struct S { int i; @disable this(); this(int i) in { assert(i > 4); } body { this.i = i; } invariant { assert(i > 4); } string toString() { return "S("~(i.to!string)~")"; } } void main() { S s = S(5); format!"s = %s"(s); } It's important to remember that Type.init is not necessarily a usable instance of S. Most importantly, you cannot safely call methods on a T.init! That is not what T.init is for. Nevertheless, format!"" attempts to validate its format string by calling format("s = %s", S.init) in CTFE. format then calls S.init.toString(), which calls its invariant, which predictably errors. Probably, just calling format with Args.init is not the right way to validate the format string.
Comment #1 by default_357-line — 2018-06-19T05:32:30Z
Comment #2 by github-bugzilla — 2018-07-19T16:04:58Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/65ca2aef50e588a7c09ef4e88d0bbc4391d80293 Add unittest for issue #19003 https://github.com/dlang/phobos/commit/43becea1e70ba4fc2a823159b48daa709f1423df Merge pull request #6594 from FeepingCreature/fix/Issue-19003-dont-call-tostring-on-type-init Fix issue 19003: don't call toString on T.init merged-on-behalf-of: Sebastian Wilzbach <[email protected]>