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); } }
Comment #5 by k.hara.pg — 2013-10-02T08:16:14Z
Comment #6 by andrej.mitrovich — 2013-10-02T19:07:11Z
*** Issue 11158 has been marked as a duplicate of this issue. ***
Comment #7 by github-bugzilla — 2013-10-03T07:08:17Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7c4ef3310168fd6f30f704e005845c197df9cc7f fix Issue 4018 - __FILE__ and __LINE__ as default template parameters not set to instantiation point per spec Also fixes __MODULE__, __FUNCITON__, and __PRETTY_FUNCTION__ https://github.com/D-Programming-Language/dmd/commit/6c8f9ffe16a19a1ba81eef0b645b1d755329f1d9 Merge pull request #2617 from 9rnsr/fix4018 Issue 4018 - __FILE__ and __LINE__ as default template parameters not set to instantiation point per spec
Comment #8 by code — 2013-10-03T08:32:42Z
(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. ***