----
struct S
{
int* x;
this(this) @safe { *x = 13; }
}
void main() @safe
{
immutable int* x = new int(42);
assert(*x == 42); /* passes */
auto s = immutable S(x);
auto s2 = s; /* should be rejected */
assert(*x == 42); /* fails */
}
----
Comment #1 by ag0aep6g — 2018-03-06T12:56:29Z
*** Issue 18561 has been marked as a duplicate of this issue. ***
Comment #2 by andrei — 2018-03-19T15:57:47Z
A similar example shows fetching a mutable pointer to immutable data:
----
int* g;
struct S
{
int* x;
this(this) { g = x; }
}
void main()
{
immutable int* x = new int(42);
assert(*x == 42); /* passes */
auto s = immutable S(x);
auto s2 = s; /* should be rejected */
}
Comment #3 by dfj1esp02 — 2018-04-03T07:45:44Z
Postblit should respect type qualifiers the same way const constructor does.
Comment #4 by robert.schadek — 2024-12-13T18:56:35Z