[Issue 23554] New: Can break immutable with delegate
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Dec 14 04:48:01 UTC 2022
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 at puremagic.com
Reporter: andy.pj.hanson at 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.
--
More information about the Digitalmars-d-bugs
mailing list