Thread overview
[Issue 22614] Wrong copy constructor is called depending on context
Dec 21
RazvanN
December 21
https://issues.dlang.org/show_bug.cgi?id=22614

RazvanN <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |[email protected]

--- Comment #1 from RazvanN <[email protected]> ---
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
https://issues.dlang.org/show_bug.cgi?id=22614

Stanislav Blinov <[email protected]> changed:

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

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

[email protected] changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |[email protected]

--- Comment #2 from [email protected] ---
(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.

--