www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19621] New: The specification is self-contradictory on


          Issue ID: 19621
           Summary: The specification is self-contradictory on
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: porton 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


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.

Jan 26