Bug 17977 – [DIP1000] destructor allows escaping reference to a temporary struct instance

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-11-10T22:01:36Z
Last change time
2021-11-24T12:02:11Z
Keywords
pull, safe, Vision
Assigned to
No Owner
Creator
Walter Bright
See also
https://issues.dlang.org/show_bug.cgi?id=12625

Comments

Comment #0 by bugzilla — 2017-11-10T22:01:36Z
The following code: struct List { Elem front() @safe return scope; ~this() @trusted scope; @disable this(this); void* data; } struct Elem { void* data; } void test() @safe { Elem elem; { // error: elem gets a pointer that has a lifetime limited to List() elem = List().front; } }
Comment #1 by dkorpel — 2021-08-07T18:50:58Z
The issue description looks incomplete, but I gather this is an accepts-invalid bug in DIP1000 and the comment shows the expected error? Currently I see nothing wrong: `List()` is not scope, and `front()` does not return by ref, so there's no references escaping here. I tried fixing and reducing the code, and I think this is the actual issue: ``` // compile with -preview=dip1000 @safe: struct List { int* data; ~this(); int* front() return; } void test() { auto elem = List().front; } ``` Without the destructor, there's an error: > onlineapp.d(11): Error: address of struct literal `List(null)` assigned > to longer lived variable `elem` Commenting it out makes it go away, which is wrong.
Comment #2 by dlang-bot — 2021-11-24T10:36:58Z
@dkorpel updated dlang/dmd pull request #13349 "Fix issue 17977 - destructor allows escaping reference to a temporary struct instance" fixing this issue: - Fix issue 17977 - destructor allows escaping reference to a temporary struct instance https://github.com/dlang/dmd/pull/13349
Comment #3 by dlang-bot — 2021-11-24T12:02:11Z
dlang/dmd pull request #13349 "Fix issue 17977 - destructor allows escaping reference to a temporary struct instance" was merged into master: - 18dbe73c01fa992a859b1b09de69a53c04259474 by dkorpel: Fix issue 17977 - destructor allows escaping reference to a temporary struct instance https://github.com/dlang/dmd/pull/13349