January 23, 2024
https://issues.dlang.org/show_bug.cgi?id=24353

          Issue ID: 24353
           Summary: Misleading error for foreach when opApply has wrong
                    qualifier
           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.106.1, attempting to compile the following program produces a misleading error message:

---
struct S
{
    int opApply(int delegate(int) dg)
    {
        return 0;
    }
}

void example()
{
    const S s;
    foreach (e; s) {}
}
---

The message is:

---
bug.d(12): Error: cannot uniquely infer `foreach` argument types
---

However, the real cause of the error is that the program is attempting to call a mutable opApply method on a const object.

Calling opApply directly instead of using foreach produces the correct message:

---
bug.d(12): Error: mutable method `bug.S.opApply` is not callable using a
`const` object
bug.d(3):        Consider adding `const` or `inout` here
---

--