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