Bug 22017 – with() on struct method that returns this destroys too early

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-06-11T09:04:53Z
Last change time
2024-12-13T19:16:59Z
Keywords
industry, pull
Assigned to
No Owner
Creator
FeepingCreature
Moved to GitHub: dmd#18039 →

Comments

Comment #0 by default_357-line — 2021-06-11T09:04:53Z
Consider the following code: struct S { bool destroyed; ~this() { destroyed = true; } ref S foo() return { return this; } } void main() { with (S().foo) { assert(!destroyed); } } Even if the with() expression is copied, the copied version should not see the results of the destructor call. However, the assert fails.
Comment #1 by default_357-line — 2021-06-11T11:20:22Z
As far as I can tell, what happens is that with() thinks that it doesn't need to generate a temporary variable because its expression is an lvalue, due to the ref return. Since the expression *does* result in a temporary cleanup with destructor, however, the destructor is inserted immediately after the with() expression, instead of after the body.
Comment #2 by dlang-bot — 2021-06-15T12:58:35Z
@RazvanN7 created dlang/dmd pull request #12688 "Fix Issue 22017 - with() on struct method that returns this destroys too early" fixing this issue: - Fix Issue 22017 - with() on struct method that returns this destroys too early https://github.com/dlang/dmd/pull/12688
Comment #3 by robert.schadek — 2024-12-13T19:16:59Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18039 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB