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