www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23554] New: Can break immutable with delegate

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

          Issue ID: 23554
           Summary: Can break immutable with delegate
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andy.pj.hanson gmail.com

This code is able to mutate an `immutable int[][]`:

```
 safe:

import std.stdio : writeln;

void main() {
        immutable int[][] xs = [[1]];
        writeln("xs[0][0] is ", xs[0][0]); // 1
        f(xs);
        writeln("xs[0][0] is ", xs[0][0]); // 2
}

void f(immutable int[][] xs) {
        each(xs, (ref immutable(int)[] x) {
                x = [2];
        });
}

void each(immutable int[][] xs, void delegate(ref immutable int[])  safe pure
nothrow cb) {
        foreach (ref x; xs)
                cb(x);
}
```

The provided delegate `ref immutable(int)[] x` should not be allowed, since it
should take a `ref immutable int[]` instead.

--
Dec 13 2022