www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17463] New: format!(): variable __result cannot be read at

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

          Issue ID: 17463
           Summary: format!(): variable __result cannot be read at compile
                    time
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: belka caraus.de

Originally the problem appeared by using 2.074's format with a compile time
format string.

Here is the code:

static import std.format;

struct SomeId
{
    invariant
    {
    }

    ref SomeId write(string fmt, string value)
    {
        return this;
    }

    ref SomeId write(string value)
    {
        return write("%s", value);
    }

    void toString(scope void delegate(const(char)[]) sink)
    {
        write("asdf");
    }
}

void main()
{
    cast(void) format!"ID: %s"(SomeId());
}


Compile time error:

Error: variable __result cannot be read at compile time
main.d(30):        called from here: this.write("asdf")
/usr/include/dmd/phobos/std/format.d(3111):        called from here:
val.toString(delegate (const(char)[] s)
{
put(w, s);
}
)
/usr/include/dmd/phobos/std/format.d(3434):        called from here:
formatObject(w, val, f)
/usr/include/dmd/phobos/std/format.d-mixin-3800(3800):        called from here:
formatValue(w, _param_3, f)
/usr/include/dmd/phobos/std/format.d(549):        called from here:
formatNth(w, spec, cast(ulong)currentArg, _param_2)
/usr/include/dmd/phobos/std/format.d(5524):        called from here:
formattedWrite(w, fmt, _param_1)
main.d(7):        called from here: format("%s", SomeId())
main.d(9):        called from here: (*function () => null)()
main.d(37): Error: template instance main.checkFormatException!("%s", SomeId())
error instantiating


The code works if I do something of the following:
- remove invariant
- make SomeId.write(string value) return this instead of returning another
write(...)
- remove the delegate argument from SomeId.toString()


The behavior isn't specific for dmd 2.074.1 and doesn't seem to be a bug in
Phobos. I could reproduce it with 2.073.2, 2.072.2, 2.071.2 and 2.070.2.

Just add the following code:

private enum ctfpMessage = "Cannot format floating point types at
compile-time";

package static const checkFormatException(alias fmt) =
{
    std.format.format(fmt, SomeId());
    return null;
}();

and change the main function to:

void main()
{
    cast(void) checkFormatException!("%s");
}

--
Jun 02 2017