Thread overview
[Issue 20142] Incorrect auto ref inference for manifest constant member
Aug 19, 2019
Simen Kjaeraas
Aug 19, 2019
RazvanN
Mar 21, 2020
Basile-z
Dec 07, 2021
RazvanN
August 19, 2019
https://issues.dlang.org/show_bug.cgi?id=20142

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com
            Summary|Reading member of enum      |Incorrect auto ref
                   |considered modifying access |inference for manifest
                   |                            |constant member

--- Comment #1 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
Reduced example:

struct S {
    int i;
}

void fun(T)(auto ref T x) {}

unittest {
    enum s = S();
    fun(s.i);
}

The call to fun() should infer x as int, but somehow thinks it's ref int.

--
August 19, 2019
https://issues.dlang.org/show_bug.cgi?id=20142

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #2 from RazvanN <razvan.nitu1305@gmail.com> ---
I cannot reproduce this with the latest master branch. Also, running it on dlang.io [1] seems to successfully compile. Although on the nightly branch it is indeed failing.

[1] https://run.dlang.io/is/clMnj3

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=20142

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--
December 07, 2021
https://issues.dlang.org/show_bug.cgi?id=20142

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from RazvanN <razvan.nitu1305@gmail.com> ---
(In reply to Basile-z from comment #3)
> The reduced test case is not catching the problem anymore. This one does:
> 
> ---
> void empty(T)(auto /*const*/ ref T a) { }
> 
> 
> struct Foo {
>     int i;
> }
> 
> void main() {
>     enum Foo foo = Foo(0);
>     foo.i.empty();
> }
> ---
> 
> The regression is caused by a protection on function parameters (and on assign exp too) to prevent writing member of manifest constants that are aggregates, more specifically struct literals (`enum Foo foo` from the front-end POV is actually a struct literal).
> 
> Without the protection, in the past, `i` could be modified, which made no sense and could even create crashes.
> 
> See https://github.com/dlang/dmd/pull/10115, which added the protection.

I cannot reproduce this. Running this code:

void empty(T)(auto ref T a)
{
    pragma(msg, __traits(isRef, a));
}


struct Foo {
    int i;
}

void main() {
    enum Foo foo = Foo(0);
    foo.i.empty();
}

Yields `false` which is correct. Closing as fixed, please reopen if I am missing something.

--