Consider the following code: import std.json; @safe unittest { const JSONValue innerObj = JSONValue(["foo": JSONValue(1)]); assert(innerObj["foo"] == JSONValue(1)); // Why can I do this?? JSONValue value = innerObj; value["foo"] = JSONValue(2); assert(innerObj["foo"] == JSONValue(1)); } innerObj is changed, even though we access it through a const variable. This should not be allowed.
See also bug 12885. https://issues.dlang.org/show_bug.cgi?id=12885 This bug is still relevant, as it happens in @safe code, not just @system - std.json violates @trusted by allowing bad things to happen.
Is this a duplicate of 12885 then?
I'd call it a consequence or subset of 12885, in that fixing 12885 also fixes this. But if 12885 was closed wontfix, this one would still need a separate fix.
@FeepingCreature updated dlang/phobos pull request #6716 "Fix issue 19256: prevent JSONValue const conversion" fixing this issue: - std.json: Fix issue 19256, prevent auto JSONValue = const(JSONValue) Reorder JSONValue's union members to make dmd realize that JSONValue contains const references and disallows reassigning const(JSONValue) to JSONValue, which would break constness. This works around https://issues.dlang.org/show_bug.cgi?id=12885 . https://github.com/dlang/phobos/pull/6716
Fixed by https://github.com/dlang/dmd/pull/16594 when `-preview=fixImmutableConv` is enabled.
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10350 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB