Thread overview
[Issue 22154] Pure functions should be able to use only the address of a global
Jul 28, 2021
Mathias LANG
Jul 29, 2021
Nathan S.
Jul 29, 2021
kinke
Jul 29, 2021
kinke
Dec 17, 2022
Iain Buclaw
July 28, 2021
https://issues.dlang.org/show_bug.cgi?id=22154

Mathias LANG <pro.mathias.lang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pro.mathias.lang@gmail.com

--- Comment #1 from Mathias LANG <pro.mathias.lang@gmail.com> ---
If we allow it, what prevents the function from then passing it down to another pure function that will read / write to it ?

--
July 28, 2021
https://issues.dlang.org/show_bug.cgi?id=22154

--- Comment #2 from Andrei Alexandrescu <andrei@erdani.com> ---
(In reply to Mathias LANG from comment #1)
> If we allow it, what prevents the function from then passing it down to another pure function that will read / write to it ?

Good point. There'd need to be appropriate restrictions, e.g. the address is read but not passed around.

This issue prevents the application of the Null Object Pattern (https://en.wikipedia.org/wiki/Null_object_pattern) to Appender in Phobos. I started modifying the code around https://github.com/dlang/phobos/blob/master/std/array.d#L3264 like this:

    private static __gshared Data nullData = { 0, null, false };
    private Data* _data = &nullData;

It worked really nice because subsequently I could eliminate most tests comparing _data against null - they just worked with the empty data.

The one place I had to test was at https://github.com/dlang/phobos/blob/master/std/array.d#L3356, where I replaced `if (!_data)` with `if (_data != &nullData)`. That made the function impure and caused a bunch of compile-time errors for no good reason.

--
July 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22154

Nathan S. <n8sh.secondary@hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |n8sh.secondary@hotmail.com

--- Comment #3 from Nathan S. <n8sh.secondary@hotmail.com> ---
Putting the address in an enum appears to work:

---

__gshared int a;

pure int fun(int* x) {
    enum int* addr = &a;
    if (x == addr) return 1;
    return 42;
}

---

--
July 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22154

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #4 from kinke <kinke@gmx.net> ---
> Putting the address in an enum appears to work:

As does making the global immutable:

pure int fun(int* x) {
    static immutable int a;
    if (x == &a) return 1;
    return 42;
}

--
July 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22154

--- Comment #5 from kinke <kinke@gmx.net> ---
(In reply to Nathan S. from comment #3)
> Putting the address in an enum appears to work:

Seems like a great way to violate purity:

pure int fun(int* x) {
    enum int* addr = &a;
    *addr = *x; // boom
    return 42;
}

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=22154

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
December 13
https://issues.dlang.org/show_bug.cgi?id=22154

--- Comment #6 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19962

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--