www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22872] New: __FUNCTION__ with opApply gives name of generated

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

          Issue ID: 22872
           Summary: __FUNCTION__ with opApply gives name of generated
                    delegate instead of enclosing function
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

As of DMD 2.099.0, the following program fails to compile:

---
module example;

struct HasOpApply
{
    int opApply(int delegate(int) dg)
    {
        if (int result = dg(0)) return result;
        return 0;
    }
}

void main()
{
    foreach (x; 0 .. 1)
    {
        // ok
        static assert(__FUNCTION__ == "example.main");
    }

    foreach (x; HasOpApply())
    {
        // error
        static assert(__FUNCTION__ == "example.main");
    }
}
---

The error is:

---
example.d(23): Error: static assert:  `"example.main.__foreachbody4" ==
"example.main"` is false
---

This happens because __FUNCTION__ is evaluated after the compiler has
transformed the foreach loop into a call to HasOpApply.opApply.

--
Mar 11 2022