Thread overview
[Issue 13488] implicit conversions to immutable broken
[Issue 13488] [reg] implicit conversions to immutable broken
Sep 17, 2014
Walter Bright
Sep 17, 2014
Kenji Hara
Sep 17, 2014
Kenji Hara
Sep 17, 2014
Sobirari Muhomori
Sep 17, 2014
Kenji Hara
Sep 18, 2014
Sobirari Muhomori
Nov 01, 2014
Sobirari Muhomori
Dec 17, 2022
Iain Buclaw
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|regression                  |enhancement

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
I take it back, this is an enhancement not a regression. Changing the
const(int) to immutable(int) will cause the examples to work.

But I'd still like the examples to work as is.

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[reg] implicit conversions  |implicit conversions to
                   |to immutable broken         |immutable broken

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
There's

>     struct T {
>       const(int)* p = null;
>       this(const(int)* q) pure {
>         p = q;
>       }
>     }

When we see only the signature (const(int*)) pure, the constructor cannot
construct immutable object in general, because the parameter q may receive
mutable object.

If you want to restrict constructed object qualifier by using constructor argument qualifiers, you can use inout constructor.

struct T
{
    const(int)* p = null;
    this(inout(int)* q) inout// pure
    {
        p = q;
    }
}

void foo()
{
    int x;
    T mt1 = T(&x);            // ok
    T* mp1 = new T(&x);       // ok
  //T mt2 = immutable T(&x);            // NG
  //T* mp2 = new immutable T(&x);       // NG
    immutable int y;
  //immutable(T) it1 = T(&y);       // NG
  //immutable(T)* ip1 = new T(&y);  // NG
    immutable(T) it2 = immutable T(&y);      // ok
    immutable(T)* ip2 = new immutable T(&y); // ok
}

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to Kenji Hara from comment #2)
> There's

Sorry, ignore this part.

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

--- Comment #4 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
Doesn't work:
---
struct B
{
    const(int)[] a;
}

immutable(B) b(immutable int[] a) pure
{
    return B(a);
}
---
Error: cannot implicitly convert expression (B(cast(const(int)[])a)) of type B
to immutable(B)

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to Sobirari Muhomori from comment #4)
> Doesn't work:
> ---
> struct B
> {
> 	const(int)[] a;
> }
> 
> immutable(B) b(immutable int[] a) pure
> {
> 	return B(a);
> }
> ---
> Error: cannot implicitly convert expression (B(cast(const(int)[])a)) of type
> B to immutable(B)

It's equivalent with:

struct B
{
    const(int)[] a;
}
void main()
{
    immutable int[] a;
    immutable(B) b = B(a);
    // <-- B is not implicitly convertible to immutable(B)
}

--
September 18, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

--- Comment #6 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
It feels like everything constructed in a strongly pure call can be implicitly casted to immutable. A call to pure constructor can be seen as a call to pure factory, so its result can be casted to immutable if the call is strongly pure.

This works:
---
int[] a() pure
{
    return new int[2];
}

void b()
{
    immutable int[] c = a();
}
---

--
November 01, 2014
https://issues.dlang.org/show_bug.cgi?id=13488

Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

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

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--