Thread overview
[Issue 22614] Wrong copy constructor is called depending on context
Dec 21, 2021
RazvanN
Dec 21, 2021
Stanislav Blinov
Jan 14, 2022
timon.gehr@gmx.ch
December 21, 2021
https://issues.dlang.org/show_bug.cgi?id=22614

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

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

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
It seems that for some reason annotating with @safe the unittest enables the compiler to infer attributes for the nested struct members. This has the result of inferring pure for the copy constructors and therefore the immutable constructor becomes a unique constructor (which means it can be implicitly convertible to mutable). Next, the compiler thinks that the unique copy constructor is better than const copy constructor.

I think that the fundamental problem here is that @safe enables attribute inference. I haven't seen this anywhere explicitly stated in the spec.

The unique conversion rules are another black box for me.

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

Stanislav Blinov <stanislav.blinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=16500

--
January 14, 2022
https://issues.dlang.org/show_bug.cgi?id=22614

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch

--- Comment #2 from timon.gehr@gmx.ch ---
(In reply to RazvanN from comment #1)
> It seems that for some reason annotating with @safe the unittest enables the compiler to infer attributes for the nested struct members. This has the result of inferring pure for the copy constructors and therefore the immutable constructor becomes a unique constructor (which means it can be implicitly convertible to mutable).

In general, unique `immutable` values _cannot_ convert to mutable. Therefore, I do not understand why the behavior would change here. The struct is convertible to mutable because there are no mutable indirections, it should not matter whether or not the copy constructors are pure.

> Next, the compiler thinks that the
> unique copy constructor is better than const copy constructor.
> 
> I think that the fundamental problem here is that @safe enables attribute inference. I haven't seen this anywhere explicitly stated in the spec. ...

That's a problem, but clearly the code should work even if annotated `pure`, so it does not seem like this is the _fundamental_ issue.

> The unique conversion rules are another black box for me.

Unique values should be able to implicitly convert to `immutable`, but that's it.

--