[Issue 19003] New: format!"" breaks with structs containing invariants violated in .init

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 18 12:46:17 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19003

          Issue ID: 19003
           Summary: format!"" breaks with structs containing invariants
                    violated in .init
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: default_357-line at yahoo.de

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.

--


More information about the Digitalmars-d-bugs mailing list