According to the spec, "If applied to an expression, it is the source representation of that expression. Semantic analysis is not done for that expression." But it seems that semantic analysis always occurs. This does not compile: --- const char [] q = (1.2143*nonexistent).stringof; --- By contrast, is() expressions also don't do semantic analysis, and they work correctly: static assert(!is(typeof(1.2143*nonexistent)));
The spec's .stringof example doesn't even compile: import std.stdio; struct Foo { } enum Enum { RED } typedef int myint; void main() { writefln((1+2).stringof); // "1 + 2" writefln(Foo.stringof); // "Foo" writefln(test.Foo.stringof); // "test.Foo" writefln(int.stringof); // "int" writefln((int*[5][]).stringof); // "int*[5][]" writefln(Enum.RED.stringof); // "Enum.RED" writefln(test.myint.stringof); // "test.myint" writefln((5).stringof); // "5" } test.Foo.stringof and test.myint.stringof refuse to compile. Adding "module test;" makes them compile, but then the results are not "test.Foo" and "test.myint", but only "Foo" and "myint".
Added to DStress as http://dstress.kuehne.cn/compile/s/stringof_01_A.d
Still an issue in D2.
@RazvanN7 created dlang/dlang.org pull request #2679 "Fix Issue 1142 - .stringof performs semantic analysis" fixing this issue: - Fix Issue 1142 - .stringof performs semantic analysis https://github.com/dlang/dlang.org/pull/2679
dlang/dlang.org pull request #2679 "Fix Issue 1142 - .stringof performs semantic analysis" was merged into master: - aeeeac7d2aa62b68d803b3bdb996a1ee06979ce3 by RazvanN7: Fix Issue 1142 - .stringof performs semantic analysis https://github.com/dlang/dlang.org/pull/2679