Bug 2887 – Wrong line number reported inside string mixin

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-04-24T14:19:35Z
Last change time
2024-12-13T17:49:55Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Nick Sabalausky
Moved to GitHub: dmd#17973 →

Comments

Comment #0 by bus_dbugzilla — 2009-04-24T14:19:35Z
---------- import tango.io.Stdout; template line() { const char[] line = "__LINE__"; } template wrongLine() { const char[] wrongLine = "\n\n\n\n\n__LINE__"; } void main() { Stdout.formatln("Line: {}, {}, {}", __LINE__, mixin(line!()), mixin(wrongLine!())); Stdout.formatln("Line: {}, {}, {}", __LINE__, mixin(line!()), mixin(wrongLine!())); } ---------- Expected output: Line: 12, 12, 12 Line: 13, 13, 13 Actual output: Line: 12, 12, 17 Line: 13, 13, 18 DMD is reporting the line where __LINE__ *would* be *after* mixin expansion. However, this only hinders the programmer rather than helping because the programmer isn't actually working with the post-expansion code. Therefore, when __LINE__ appears inside a string mixin, it should resolve to the line number of the mixin instantiation. This is preventing my custom assert routines from automatically reporting the correct line number of the assertion, and is thus forcing my custom assert routines to require the user to manually pass in __LINE__.
Comment #1 by shro8822 — 2009-04-24T14:38:23Z
What I think it should do is the filename inside a mixin should be (based on outside values): __FILE__:__LINE__ and the line number should restart at 1 so that you can get both inside and outside info even with several levels of mixins Another option would be to have a __LINES__ macro that is an array of line numbers for each level of mixins. __LINES__[$-2] == __LINE__ at mixin()
Comment #2 by bus_dbugzilla — 2009-09-07T13:25:54Z
Workaround: template foo() { const char[] foo = `{ auto __foo_line = __LINE__; // must be on first line // Also, the extra surrounding {} is needed to // allow foo to be used twice in the same scope. Stdout.formatln("Foo:"); Stdout.formatln(" File {}", __FILE__); Stdout.formatln(" Line {}", __foo_line); }`; }
Comment #3 by greensunny12 — 2018-02-03T08:35:07Z
Changing this to D2 as it's still not working as expected: --- template line() { const char[] line = "__LINE__"; } template wrongLine() { const char[] wrongLine = "\n\n\n\n\n__LINE__"; } void main() { import std.stdio; writefln("Line: %s, %s, %s", __LINE__, mixin(line!()), mixin(wrongLine!())); writefln("Line: %s, %s, %s", __LINE__, mixin(line!()), mixin(wrongLine!())); } --- https://run.dlang.io/is/uZrbxu
Comment #4 by robert.schadek — 2024-12-13T17:49:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17973 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB