Issue 21525 - Spurious "copying &... into allocated memory escapes a reference to parameter variable" with associative array iteration
Summary: Spurious "copying &... into allocated memory escapes a reference to parameter...
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: diagnostic, pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2021-01-05 00:29 UTC by Vladimir Panteleev
Modified: 2023-02-28 10:36 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2021-01-05 00:29:33 UTC
/////////////////////////////// test.d ///////////////////////////////
void main()
{
    int[string] aa;
    aa["name"] = 5;

    int*[] pvalues;
    foreach (name, ref value; aa)
    {
        // Deprecation: copying `&value` into allocated memory escapes
        // a reference to parameter variable `value`
        pvalues ~= &value;
    }

    // The reference is, in fact, valid.
    *pvalues[0] = 7;
    assert(aa["name"] == 7);
}
//////////////////////////////////////////////////////////////////////

The error message, besides apparently being spurious, is additionally misleading: `value` is not a parameter as far as the programmer is concerned.

This is a regression caused by enabling DIP25 by default (https://github.com/dlang/dmd/pull/10805).
Comment 1 Dlang Bot 2021-02-01 16:20:03 UTC
@RazvanN7 created dlang/dmd pull request #12173 "Fix Issue 21525 - Spurious copying into allocated memory escapes a re…" fixing this issue:

- Fix Issue 21525 - Spurious copying into allocated memory escapes a reference to parameter variable with associative array iteration

https://github.com/dlang/dmd/pull/12173
Comment 2 RazvanN 2023-02-28 10:36:31 UTC
I cannot reproduce this. I think it has been fixed.