Bug 24010 – Destructor called before end of scope for tuples
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-06-22T22:03:17Z
Last change time
2023-06-30T15:35:11Z
Keywords
industry, pull
Assigned to
No Owner
Creator
johanengelen
Comments
Comment #0 by johanengelen — 2023-06-22T22:03:17Z
Testcase:
```
alias AliasSeq(TList...) = TList;
import core.stdc.stdio;
int i = 0;
struct A {
~this() {
printf("~this\n");
i *= 2;
}
}
void main() {
{
AliasSeq!(A, A) params;
printf("statement\n");
i = 1;
}
printf("%d\n", i);
assert(i == 4);
}
```
The assertion fails.
Looking at the printout:
```
~this
~this
statement
1
```
you can see that the dtors are called before the statement `i = 1;`, instead of at the end of the scope.
Comment #1 by johanengelen — 2023-06-24T11:06:39Z
The problem seems to be that the frontend treats/sets up the tuple as a temporary variable (valid for the duration of that expression only), instead of generating the ~this() calls at end of scope like it does for normal variables.
Comment #2 by bugzilla — 2023-06-26T08:13:07Z
You're right. I'll look into why this is happening.
Comment #3 by boris2.9 — 2023-06-27T02:55:20Z
*** Issue 17128 has been marked as a duplicate of this issue. ***
Comment #4 by boris2.9 — 2023-06-27T02:55:30Z
*** Issue 20135 has been marked as a duplicate of this issue. ***
Comment #5 by dlang-bot — 2023-06-27T03:29:27Z
@BorisCarvajal created dlang/dmd pull request #15351 "Fix Issue 24010 - Destructor called before end of scope for tuples" fixing this issue:
- Fix Issue 24010 - Destructor called before end of scope for tuples
https://github.com/dlang/dmd/pull/15351
Comment #6 by dlang-bot — 2023-06-29T05:15:33Z
@WalterBright created dlang/dmd pull request #15366 "fix Issue 24010 - Destructor called before end of scope for tuples" fixing this issue:
- fix Issue 24010 - Destructor called before end of scope for tuples
https://github.com/dlang/dmd/pull/15366
Comment #7 by dlang-bot — 2023-06-30T15:35:11Z
dlang/dmd pull request #15351 "Fix Issue 24010 - Destructor called before end of scope for tuples" was merged into master:
- 597e133324fac8c1ded4d93186969727fd095fa7 by Boris Carvajal:
Fix Issue 24010 - Destructor called before end of scope for tuples
Co-authored-by: Walter Bright <[email protected]>
https://github.com/dlang/dmd/pull/15351