Bug 24795 – emplace mutates immutable data in @safe code

Status
NEW
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-10-04T16:45:52Z
Last change time
2024-12-07T13:43:54Z
Assigned to
No Owner
Creator
Paul Backus
Moved to GitHub: dmd#17219 →

Comments

Comment #0 by snarwin+bugzilla — 2024-10-04T16:45:52Z
As of DMD 2.109.1, the example program below compiles and runs to completion without errors. The program uses core.lifetime.emplace to mutate immutable data in @safe code. Since mutating immutable data results in undefined behavior, it is a bug for the program to compile. The cause of the bug is the use of @trusted in core.lifetime.emplace to cast away type qualifiers from the target object. --- import core.lifetime; void example1() @safe { const(int)* obj = new immutable(int)(123); assert(*obj == 123); emplace(obj, 456); assert(*obj == 456); // immutable object mutated! } void example2() @safe { static class C { int n; this(int n) pure @safe { this.n = n; } } const(C) obj = new immutable(C)(123); assert(obj.n == 123); emplace(obj, 456); assert(obj.n == 456); // immutable object mutated! } void example3() @safe { static struct S { int n; this(int n) pure @safe { this.n = n; } } const(S)* obj = new immutable(S)(123); assert(obj.n == 123); emplace(obj, 456); assert(obj.n == 456); // immutable object mutated! } void main() @safe { example1(); example2(); example3(); } ---
Comment #1 by robert.schadek — 2024-12-07T13:43:54Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17219 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB