Bug 24433 – Array value assignment is incorrect for structs with destructors and copy constructors
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-03-08T16:11:32Z
Last change time
2024-11-22T19:48:42Z
Assigned to
No Owner
Creator
artha
Comments
Comment #0 by artha — 2024-03-08T16:11:32Z
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 by schveiguy — 2024-11-22T19:48:42Z
Marking this as a duplicate, since the other has a PR already.
*** This issue has been marked as a duplicate of issue 24872 ***