Bug 21546 – covariant return checks for functions wrong if returning by ref
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-01-13T21:50:44Z
Last change time
2022-02-26T09:36:53Z
Keywords
accepts-invalid, pull, safe
Assigned to
No Owner
Creator
Bolpat
Comments
Comment #0 by qs.il.paperinik — 2021-01-13T21:50:44Z
A delegate with return type ref immutable(T) can be used to reassign an immutable(T) as follows:
alias DGm = ref int delegate() @safe;
alias DGi = ref immutable(int) delegate() @safe;
@safe void f()
{
immutable int i = 0;
DGi dgi = ref() => i;
DGm[] dgms = [ dgi ]; // why?
dgms[0]() = 1;
assert(i == 1); // fails, because optimization
}
@safe void g()
{
DGm[] dgms;
foreach (immutable int i; [0])
{
assert(i == 0); // just to be sure
DGi dgi = ref() => i;
dgms ~= dgi; // why?
dgms[0]() = 1;
assert(i == 1); // passes, meaning immutable i was changed
}
}
The lines marked with // why? should be rejected by the type system.
Comment #1 by bugzilla — 2022-02-26T08:37:17Z
This reveals a bug in the covariant return checks when ref returns are involved.
Comment #2 by dlang-bot — 2022-02-26T08:42:17Z
@WalterBright created dlang/dmd pull request #13722 "fix Issue 21546 - covariant return checks for functions wrong if retu…" fixing this issue:
- fix Issue 21546 - covariant return checks for functions wrong if returning by ref
https://github.com/dlang/dmd/pull/13722
Comment #3 by dlang-bot — 2022-02-26T09:36:53Z
dlang/dmd pull request #13722 "fix Issue 21546 - covariant return checks for functions wrong if retu…" was merged into master:
- ddbf02545c73a06bf4db1c0d32aa10b186806e22 by Walter Bright:
fix Issue 21546 - covariant return checks for functions wrong if returning by ref
https://github.com/dlang/dmd/pull/13722