Comment #0 by snarwin+bugzilla — 2024-01-23T17:21:47Z
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
---
Comment #1 by dlang-bot — 2024-11-16T21:51:26Z
@royalpinto007 created dlang/dmd pull request #17071 "fix Bugzilla 24353 - add mutability check for foreach with opApply" fixing this issue:
- fix Bugzilla 24353 - add mutability check for foreach with opApply
Signed-off-by: royalpinto007 <[email protected]>
https://github.com/dlang/dmd/pull/17071
Comment #2 by dlang-bot — 2024-11-16T23:44:25Z
dlang/dmd pull request #17071 "fix Bugzilla 24353 - add mutability check for foreach with opApply" was merged into master:
- 7ce408a4370a7f308c173b9f233ed7f242b926dc by royalpinto007:
fix Bugzilla 24353 - add mutability check for foreach with opApply
Signed-off-by: royalpinto007 <[email protected]>
https://github.com/dlang/dmd/pull/17071
Comment #3 by snarwin+bugzilla — 2024-11-17T01:28:41Z
The linked PR is an incomplete fix. The example with `const` now works, but the same problem still exists with other qualifiers, such as `shared` and `immutable`.
Comment #4 by robert.schadek — 2024-12-13T19:32:44Z