www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19003] New: format!"" breaks with structs containing


          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 puremagic.com
          Reporter: default_357-line 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; }

        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.

Jun 18 2018