Jump to page: 1 2
Thread overview
[Issue 15660] breack "immutable" with pure function and mutable params
Feb 08, 2016
Iakh
Feb 08, 2016
Iakh
[Issue 15660] break "immutable" with pure function and mutable reference params
Feb 09, 2016
Iakh
Feb 09, 2016
Iakh
Jul 14, 2016
Iakh
Aug 26, 2016
Walter Bright
[Issue 15660] break immutable with pure function and mutable reference params
Mar 18, 2018
Walter Bright
Mar 18, 2018
Walter Bright
February 08, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Iakh <iaktakh@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|result of pure function     |breack "immutable" with
                   |                            |pure function and mutable
                   |                            |params

--
February 08, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

--- Comment #1 from Iakh <iaktakh@gmail.com> ---
Also this works

import std.stdio;

class A
{
    int i;
}

class B : A {}
struct S
{
    A a;

    auto f() pure @safe
    {
        B b = new B;
        a = b;
        return b;
    }
}

void main() @safe
{
    S s;
    immutable a = s.f();
    A b = s.a;
    writeln(a.i);
    b.i = 1;
    writeln(a.i);
}

--
February 09, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
                 CC|                            |schveiguy@yahoo.com
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--- Comment #2 from Steven Schveighoffer <schveiguy@yahoo.com> ---
This doesn't require member functions.

int[] f(ref void[] m) pure
{
    auto result = new int[5];
    m = result;
    return result;
}

void main()
{
    void[] v;
    immutable x = f(v);
}

One significant problem here is that the compiler may not consider the parameter to f in these cases to be a *return* avenue, only a parameter. The compiler should take into account references to ensure that they cannot escape the same data that is being returned.

Fixing this may break a lot of code, but probably for the better. May need a deprecation cycle for this.

--
February 09, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|breack "immutable" with     |break "immutable" with pure
                   |pure function and mutable   |function and mutable
                   |params                      |reference params

--
February 09, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Iakh <iaktakh@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |http://forum.dlang.org/thre
                   |                            |ad/rdiwdveqfhevqugddwwd@for
                   |                            |um.dlang.org

--- Comment #3 from Iakh <iaktakh@gmail.com> ---
(In reply to Steven Schveighoffer from comment #2)
Even this works:
import std.stdio;

int[] f(void[] a) @safe pure
{
    return cast(int[])a;
}

void main() @safe
{
    int[] a = new int[4];

    immutable b = a.f();
    writeln(b);
    a[0] = 1;
    writeln(b);
}

> One significant problem here is that the compiler may not consider the parameter to f in these cases to be a *return* avenue, only a parameter. The compiler should take into account references to ensure that they cannot escape the same data that is being returned.

So not only escape. Compiler just traverses params's AST and search for exactly match with ReturnType.

> Fixing this may break a lot of code, but probably for the better. May need a deprecation cycle for this.

--
February 09, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

--- Comment #4 from Steven Schveighoffer <schveiguy@yahoo.com> ---
(In reply to Iakh from comment #3)
> (In reply to Steven Schveighoffer from comment #2)
> Even this works:
> import std.stdio;
> 
> int[] f(void[] a) @safe pure
> {
>     return cast(int[])a;
> }

In this case, you are using a cast. The compiler pretty much gives up trying to ensure anything when you cast, so I think it's OK to allow that.

Although, I thought such a cast wouldn't work in @safe code.

--
February 09, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

--- Comment #5 from Iakh <iaktakh@gmail.com> ---
(In reply to Steven Schveighoffer from comment #4)

> In this case, you are using a cast. The compiler pretty much gives up trying to ensure anything when you cast, so I think it's OK to allow that.
> 
> Although, I thought such a cast wouldn't work in @safe code.

Me too. Casting of array types does in reinterpret_cast manner. So it's a bug in @safe definition.

--
July 14, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Iakh <iaktakh@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe

--
August 26, 2016
https://issues.dlang.org/show_bug.cgi?id=15660

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to Steven Schveighoffer from comment #4)
> (In reply to Iakh from comment #3)
> > (In reply to Steven Schveighoffer from comment #2)
> > Even this works:
> > import std.stdio;
> > 
> > int[] f(void[] a) @safe pure
> > {
> >     return cast(int[])a;
> > }
> 
> In this case, you are using a cast. The compiler pretty much gives up trying to ensure anything when you cast, so I think it's OK to allow that.
> 
> Although, I thought such a cast wouldn't work in @safe code.

And indeed, it no longer is compilable:

test.d(3): Error: cast from void[] to int[] not allowed in safe code

--
March 18, 2018
https://issues.dlang.org/show_bug.cgi?id=15660

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|break "immutable" with pure |break immutable with pure
                   |function and mutable        |function and mutable
                   |reference params            |reference params

--
« First   ‹ Prev
1 2