Bug 17463 – format!(): variable __result cannot be read at compile time

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-06-02T11:50:46Z
Last change time
2022-11-18T16:57:28Z
Assigned to
No Owner
Creator
Eugene Wissner

Comments

Comment #0 by belka — 2017-06-02T11:50:46Z
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"); }
Comment #1 by razvan.nitu1305 — 2022-11-18T16:57:28Z
I cannot reproduce this. Compiling this code (making the std.format import non-static) compiles successfully.