www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18919] New: __FILE__ and __LINE__ should work when used in

https://issues.dlang.org/show_bug.cgi?id=18919

          Issue ID: 18919
           Summary: __FILE__ and __LINE__ should work when used in default
                    argument expressions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

Currently, __FILE__ and __LINE__ expand to the *caller's* location if used
directly as default parameters in a function:

void foo(string file = __FILE__, size_t line = __LINE__)
{
   import std.stdio;
   writeln(file, " ", line);
}

void main()
{
   foo(); // line 9
}

prints "foo.d 9"

But if you wrap those into an expression, it changes the meaning:

struct Loc
{
   string file;
   size_t line;
}
void foo(Loc loc = Loc(__FILE__, __LINE__)) // line 6
{
   import std.stdio;
   writeln(loc.file, " ", loc.line);
}

void main()
{
   foo(); // line 14
}

prints "foo.d 6"

There's no reason it shouldn't just forward the original caller's file/line to
the expression itself. It violates the principal of least surprise, and the
current behavior is pretty near useless.

In addition to fixing this, default parameters in expressions called should
ALSO forward:

Loc defaultLoc(string file = __FILE__, size_t line = __LINE)
{
   return Loc(file, line);
}

void foo(Loc loc = defaultLoc)
{
   writeln(loc.file, " ", loc.line);
}

This should print wherever foo is called from.

I'd predict 0 code breakage, because anyone trying to use this would be wanting
the expected behavior, so they wouldn't use this mechanism.

--
May 30 2018