Bug 4018 – __FILE__ and __LINE__ as default template parameters not set to instantiation point per spec
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-27T09:30:00Z
Last change time
2015-01-05T15:31:11Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
Jesse.K.Phillips+D
Comments
Comment #0 by Jesse.K.Phillips+D — 2010-03-27T09:30:37Z
The specification states that:
The __FILE__ and __LINE__ expand to the source file name and line number at the point of instantiation.
This is not the case for the code below:
-------------------
import std.stdio;
void ODSfd(alias n)() {
writefln("%s:%s | %s = %d", __FILE__, __LINE__, n.stringof, n);
}
void ODSfs(alias n)() {
writefln("%s:%s | %s = %s", __FILE__, __LINE__, n.stringof, n);
}
void ODSfx(alias n)() {
writefln("%s:%s | %s = 0x%08x", __FILE__, __LINE__, n.stringof, n);
}
void main() {
int zbar = 5;
string foo = "hi";
ODSfd!(zbar);
ODSfs!(foo);
ODSfx!(zbar);
}
-------------------
Expected output:
file.d:17 | zbar = 5
file.d:18 | foo = hi
file.d:19 | zbar = 0x00000005
Actual output:
file.d:4 | zbar = 5
file.d:7 | foo = hi
file.d:10 | zbar = 0x00000005
Comment #1 by Justin.SpahrSummers — 2010-03-27T09:44:50Z
(In reply to comment #0)
Note that the docs say that referring to template value parameters. The code you posted is intended behavior; you would have to add a couple value parameters to each one that default to __FILE__ and __LINE__ and print those out.
Comment #2 by Jesse.K.Phillips+D — 2010-03-27T09:55:31Z
In that case, the below has the same result.
------------
import std.stdio;
void ODSfd(alias n, string file = __FILE__, int line = __LINE__)() {
writefln("%s:%s | %s = %d", file, line, n.stringof, n);
}
void ODSfs(alias n, string file = __FILE__, int line = __LINE__)() {
writefln("%s:%s | %s = %s", file, line, n.stringof, n);
}
void ODSfx(alias n, string file = __FILE__, int line = __LINE__)() {
writefln("%s:%s | %s = 0x%08x", file, line, n.stringof, n);
}
void main() {
int zbar = 5;
string foo = "hi";
ODSfd!(zbar);
ODSfs!(foo);
ODSfx!(zbar);
}
-----------------
Expected Output:
linefile.d:17 | zbar = 5
linefile.d:18 | foo = hi
linefile.d:19 | zbar = 0x00000005
Actual Output:
linefile.d:3 | zbar = 5
linefile.d:6 | foo = hi
linefile.d:9 | zbar = 0x00000005
Comment #3 by code — 2012-03-20T22:10:46Z
*** Issue 5686 has been marked as a duplicate of this issue. ***
Comment #4 by code — 2012-03-20T22:29:35Z
This only happens with explicit instantiations foo!(targs)(args).
In that case semantic is run in a different scope.
A workaround for functions is to split the parameters into explicit
and default parameters.
template foo(T)
{
void foo(size_t line = __LINE__)()
{
pragma(msg, line);
}
}
(In reply to comment #6)
> *** Issue 11158 has been marked as a duplicate of this issue. ***
Oh, I thought this one was already fixed.
Now it is, thanks a lot Kenji.
Comment #9 by k.hara.pg — 2015-01-05T15:31:11Z
*** Issue 11074 has been marked as a duplicate of this issue. ***