Issue 4232 - Local struct member variable can be returned by ref
Summary: Local struct member variable can be returned by ref
Status: RESOLVED DUPLICATE of issue 13902
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-05-24 23:21 UTC by Shin Fujishiro
Modified: 2015-01-11 05:44 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2010-05-24 23:21:52 UTC
Using struct members bypasses the compiler check for escaping reference to local variables.

This code should not compile:
--------------------
struct S { int a; }

ref int test()
{
    S s;
    s.a = 42;
    return s.a;
}
void main()
{
    auto p = &test(); // reference to the local variable
    assert(test() == 42); // assertion fails with -O
}
--------------------

Moreover, test() in the above example returnes a corrupted (or uninitialized) value when optimization is on. This causes unittests for std.functional.toDelegate() to segfault.
Comment 1 Kenji Hara 2015-01-11 05:44:21 UTC

*** This issue has been marked as a duplicate of issue 13902 ***