[Issue 24353] New: Misleading error for foreach when opApply has wrong qualifier
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Jan 23 17:21:47 UTC 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 at puremagic.com
Reporter: snarwin+bugzilla at 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
---
--
More information about the Digitalmars-d-bugs
mailing list