www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22792] New: Wrong inference of opApply

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

          Issue ID: 22792
           Summary: Wrong inference of opApply
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: zan77137 nifty.com

Why is following code different behavior between int and string?

-----------------
import std;

struct S
{
    int opApply(int delegate(ref const(int), ref int) dg)
    { writeln("mutable int"); return 0; }
    int opApply(int delegate(ref const(int), ref const(int)) dg) const
    { writeln("const int"); return 0; }
    int opApply(int delegate(ref const(int), ref string) dg)
    { writeln("mutable str"); return 0; }
    int opApply(int delegate(ref const(int), ref const(string)) dg) const
    { writeln("const str"); return 0; }
}
void main()
{
    S s;
    foreach (ref const(int) x, ref const(int) y; s) {}
    foreach (ref const(int) x, ref const(string) y; s) {}
}
-----------------
rdmd playground.d
const int
mutable str
-----------------

The function on the const one that matches better should be called.

This problem was discovered by investigating the cause of Botan(*) build
failure in 2.099.1-beta.1.
However, the above code shows the same behavior since before 2.098.1.

* https://github.com/etcimon/botan

--
Feb 18 2022