Issue 24433 - Array value assignment is incorrect for structs with destructors and copy constructors
Summary: Array value assignment is incorrect for structs with destructors and copy con...
Status: RESOLVED DUPLICATE of issue 24872
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-03-08 16:11 UTC by artha
Modified: 2024-11-22 19:48 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description artha 2024-03-08 16:11:32 UTC
If a struct defines both (1) a destructor and (2) a postblit or copy constructor, assigning it as a value to a slice will produce an invalid result:

```
import std.stdio;

struct Uncopiable {
    int value;
    
    ~this() {
    }
    
    @disable this(const ref Uncopiable);
    
}

void testCopy(Uncopiable unc) {}

void main() {
    
    auto a = Uncopiable(1);
    auto b = Uncopiable(2);    
    //testCopy(a);  // fails, alright
    
    auto c = [Uncopiable(3)];
    c[] = a;   
    writeln(c[0].value);  // Prints 3 instead of 1
    writeln(a.value); // Prints 3 instead of 1
}
```

Removing either the destructor or copy constructor fixes the above problem. The issue also occurs if the copy constructor is replaced with a postblit.

The bug appears to have been introduced in #14382 
https://github.com/dlang/dmd/commit/d0a367e98cc31a2675a5fc41a150dd38089f1982#diff-c9a721f453e1e17d997f8603ca3e4e65c765f0ebf0dd1cfb972ddddc959bdbb1
Comment 1 Steven Schveighoffer 2024-11-22 19:48:42 UTC
Marking this as a duplicate, since the other has a PR already.

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