Jump to page: 1 2
Thread overview
[Issue 14781] [REG2.067] impure delegate to pure function context should be able to modify context
[Issue 14781] impure delegate to pure function context should be able to modify context
Jul 07, 2015
Kenji Hara
Jul 08, 2015
Kenji Hara
Jul 08, 2015
Denis Shelomovskij
Jul 08, 2015
Denis Shelomovskij
Jul 08, 2015
ag0aep6g@gmail.com
Aug 28, 2015
Kenji Hara
Aug 28, 2015
Kenji Hara
July 07, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> ---
Thanks.

--
July 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
https://github.com/D-Programming-Language/dmd/pull/4808

--
July 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

Denis Shelomovskij <verylonglogin.reg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg@gmail.com

--- Comment #3 from Denis Shelomovskij <verylonglogin.reg@gmail.com> ---
(In reply to Steven Schveighoffer from comment #0)
> In a followup to issue 9148, which made this illegal:
> 
> int impureFuncCall() { static int g; return g; }
> auto foo(out int delegate() pure pureDg) pure {
>     int x;
>     auto bar()() {
>         impureFuncCall();
>             x = 1;  // Error: impure function 'bar' cannot access variable
> 'x'
>         // declared in enclosing pure function 'foo'
>     }
> 
>     pureDg = &(bar!());
> 
>     int dg() pure { return x;}
>     return &dg;
> }
> 
> 
> This should be allowed.
> ...

Do you mean this code should compile? If so, either pure function `bar` can
call an impure function or impure delegate is implicitly convertible to a pure
one.
If you are opening an issue with an enhancement please provide a clear and
concise testcase. Thanks.

--
July 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

--- Comment #4 from Steven Schveighoffer <schveiguy@yahoo.com> ---
(In reply to Denis Shelomovskij from comment #3)

> Do you mean this code should compile?

Yes.

> If so, either pure function `bar` can
> call an impure function or impure delegate is implicitly convertible to a
> pure one.

No, but I made an error in the example from what I meant.

should be:

auto foo(out int delegate() impureDg) pure {

...

impureDg = &(bar!());

> If you are opening an issue with an enhancement please provide a clear and concise testcase. Thanks.

It's hard to make a test case that "should compile" without making an error with no compiler to help you :D

Really, the issue is that bar doesn't compile, not that the impure delegate can't be assigned.

--
July 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

--- Comment #5 from Denis Shelomovskij <verylonglogin.reg@gmail.com> ---
(In reply to Steven Schveighoffer from comment #4)
> (In reply to Denis Shelomovskij from comment #3)
> 
> > Do you mean this code should compile?
> 
> Yes.
> 
> > If so, either pure function `bar` can
> > call an impure function or impure delegate is implicitly convertible to a
> > pure one.
> 
> No, but I made an error in the example from what I meant.
> 
> should be:
> 
> auto foo(out int delegate() impureDg) pure {
> 
> ...
> 
> impureDg = &(bar!());
> 
> > If you are opening an issue with an enhancement please provide a clear and concise testcase. Thanks.
> 
> It's hard to make a test case that "should compile" without making an error with no compiler to help you :D
> 
> Really, the issue is that bar doesn't compile, not that the impure delegate can't be assigned.

Then this function isn't strongly pure anymore:
---
T f() pure;
---
if `T` is `int delegate()`.

Is everybody OK with this?

--
July 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g@gmail.com

--- Comment #6 from ag0aep6g@gmail.com ---
(In reply to Denis Shelomovskij from comment #5)
> Then this function isn't strongly pure anymore:
> ---
> T f() pure;
> ---
> if `T` is `int delegate()`.
> 
> Is everybody OK with this?

I think that's already not strongly pure. `int delegate()` has a mutable indirection in the context pointer. So a function that returns such a delegate can't be strongly pure.

See http://klickverbot.at/blog/2012/05/purity-in-d/#indirections_in_the_return_type

An example with the delegate type:
----
struct S
{
    int x = 1;
    int method() {return x++;}
}

int delegate() f() pure
{
    return &(new S).method;
}

void main()
{
   auto dg1 = f();
   version(all) auto dg2 = f();
   else auto dg2 = dg1; /* This would be equivalent to the above if f were
strongly pure. */

   import std.stdio;
   writeln(dg1()); /* "1" */
   writeln(dg1()); /* "2" */
   writeln(dg2()); /* "1" or "3", depending on the version above */
}
----

--
August 28, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
            Summary|impure delegate to pure     |[REG2.067] impure delegate
                   |function context should be  |to pure function context
                   |able to modify context      |should be able to modify
                   |                            |context
           Severity|enhancement                 |regression

--- Comment #7 from Kenji Hara <k.hara.pg@gmail.com> ---
I change the importance to regression, because the introduced behavior since 2.067 rejects valid code.

--
August 28, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

--- Comment #8 from Kenji Hara <k.hara.pg@gmail.com> ---
New PR targeting 2.068.1 release: https://github.com/D-Programming-Language/dmd/pull/4970

--
August 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

--- Comment #9 from github-bugzilla@puremagic.com ---
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/922d79a43ff60ed324e32e2f4be1430de209746f
fix Issue 14781 - impure delegate to pure function context should be able to
modify context

https://github.com/D-Programming-Language/dmd/commit/b30556bf2ce1b4e49fdba51ea759725c2fc6e48f Merge pull request #4970 from 9rnsr/fix14781

[REG2.067/2.068] Issue 14781 & 14962 - fix problematic purity inference introduced in #3626

--
August 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14781

github-bugzilla@puremagic.com changed:

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

--
« First   ‹ Prev
1 2