[Issue 19621] New: The specification is self-contradictory on immutability
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jan 26 18:50:52 UTC 2019
https://issues.dlang.org/show_bug.cgi?id=19621
Issue ID: 19621
Summary: The specification is self-contradictory on
immutability
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: enhancement
Priority: P1
Component: dlang.org
Assignee: nobody at puremagic.com
Reporter: porton at narod.ru
https://dlang.org/spec/const3.html says both of the following:
---
The second way is to cast data to immutable. When doing so, it is up to the
programmer to ensure that no other mutable references to the same data exist.
char[] s = ...;
immutable(char)[] p = cast(immutable)s; // undefined behavior
immutable(char)[] p = cast(immutable)s.dup; // ok, unique reference
---
and
---
An immutable or const type qualifier can be removed with a cast:
immutable int* p = ...;
int* q = cast(int*)p;
This does not mean, however, that one can change the data:
*q = 3; // allowed by compiler, but result is undefined behavior
---
The first says that the existence of mutable reference is enough for the
undefined behavior but the second says that for wrong behavior one needs to
actually change the data through a mutable reference.
The second of the two should be accepted. Having a mutable reference to
immutable data should be well-defined (and undefined only if one actually uses
this reference to change the data), because otherwise it is too easy to make an
error leading to undefined behavior what is bad for software reliability.
--
More information about the Digitalmars-d-bugs
mailing list