Issue 24010 - Destructor called before end of scope for tuples
Summary: Destructor called before end of scope for tuples
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 critical
Assignee: No Owner
URL:
Keywords: industry, pull
: 17128 20135 (view as issue list)
Depends on:
Blocks:
 
Reported: 2023-06-22 22:03 UTC by johanengelen
Modified: 2023-06-30 15:35 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description johanengelen 2023-06-22 22:03:17 UTC
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 johanengelen 2023-06-24 11:06:39 UTC
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 Walter Bright 2023-06-26 08:13:07 UTC
You're right. I'll look into why this is happening.
Comment 3 Boris Carvajal 2023-06-27 02:55:20 UTC
*** Issue 17128 has been marked as a duplicate of this issue. ***
Comment 4 Boris Carvajal 2023-06-27 02:55:30 UTC
*** Issue 20135 has been marked as a duplicate of this issue. ***
Comment 5 Dlang Bot 2023-06-27 03:29:27 UTC
@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 Dlang Bot 2023-06-29 05:15:33 UTC
@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 Dlang Bot 2023-06-30 15:35:11 UTC
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 <walter@walterbright.com>

https://github.com/dlang/dmd/pull/15351