[Issue 18567] immutability hole related to context pointers accessed through const pure methods

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 29 10:00:30 UTC 2022


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

--- Comment #2 from timon.gehr at gmx.ch ---
Well, the compiler is just compiling the code differently now (which it can do,
as it exhibits UB), the CSE is pretty easy to defeat though:

void main(){
    int i = 0;
    struct S{
        const(int)* fun()const pure{
            return &i;
        }
    }
    immutable S s;
    static const(int)* foo(immutable(S) s)pure{
            return s.fun();
    }
    immutable(int) *pi=foo(s);
    import std.stdio;
    writeln(*pi); // 0
    i+=1;
    int x=*pi;
    writeln(x); // 1
}


The problem is that this line compiles:

immutable S s;

This requires an immutable context pointer, but a mutable one is provided.

In general, context pointers should be type checked essentially as if we were
passing around explicit pointers.

--


More information about the Digitalmars-d-bugs mailing list