[Issue 24795] New: emplace mutates immutable data in @safe code
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Oct 4 16:45:52 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24795
Issue ID: 24795
Summary: emplace mutates immutable data in @safe code
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: druntime
Assignee: nobody at puremagic.com
Reporter: snarwin+bugzilla at gmail.com
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();
}
---
--
More information about the Digitalmars-d-bugs
mailing list