[Issue 15651] filter: only parameters or stack based variables can be inout

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 25 06:55:04 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=15651

--- Comment #6 from Harry Vennik <htvennik at gmail.com> ---
(In reply to Steven Schveighoffer from comment #3)
> In fact, we should allow inout to be used ANYWHERE, except global variables
> (as putting inout variables in global space can change the meaning from call
> to call). They will just be another type of immutable in functions which don't
> have inout parameters, or when used in structs outside of inout functions.

There have to be more restrictions. The problem is that inout is not really a
type modifier like const or immutable, rather it is a type modifier
placeholder. Even when disallowing inout variables in global space, structs or
class instances with inout members might still reference global data. Those may
even have inout member functions, redefining the meaning of inout on every
call. Inout would become the perfect way to modify immutable data with no way
for the compiler to detect it:

----

class C
{
    private inout(int)[] _arr;

    this(inout int[] a)
    {
        _arr = a;
    }

    ref inout(int) getRef(size_t i) inout
    {
        return _arr[i];
    }
}

void main()
{
    immutable int[] iArr = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

    /* inout resolves to immutable in constructor call */
    auto c = new C(iArr);

    /* inout resolves to mutable in call to getRef */
    c.getRef(5) = 8;  // Undefined behavior
}

----

--


More information about the Digitalmars-d-bugs mailing list