D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21546 - covariant return checks for functions wrong if returning by ref
Summary: covariant return checks for functions wrong if returning by ref
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 critical
Assignee: No Owner
URL:
Keywords: accepts-invalid, pull, safe
Depends on:
Blocks:
 
Reported: 2021-01-13 21:50 UTC by Bolpat
Modified: 2022-02-26 09:36 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Bolpat 2021-01-13 21:50:44 UTC
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 Walter Bright 2022-02-26 08:37:17 UTC
This reveals a bug in the covariant return checks when ref returns are involved.
Comment 2 Dlang Bot 2022-02-26 08:42:17 UTC
@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 Dlang Bot 2022-02-26 09:36:53 UTC
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