Thread overview
[Issue 9230] New: Incorrect implicit immutable conversion occurs in pure function
Dec 28, 2012
Kenji Hara
Dec 28, 2012
Kenji Hara
Dec 28, 2012
Kenji Hara
Dec 28, 2012
yebblies
Dec 28, 2012
yebblies
Dec 28, 2012
Kenji Hara
Dec 28, 2012
Kenji Hara
Jan 16, 2013
yebblies
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230

           Summary: Incorrect implicit immutable conversion occurs in pure
                    function
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: k.hara.pg@gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-27 19:02:24 PST ---
From: http://d.puremagic.com/issues/show_bug.cgi?id=6553

In git head, following code compiles without errors, and asserts in runtime.

string foo(in char[] s) pure {
    return s; // conversion from const(char[]) to string
}
void main() {
    char[] buf = ['a'];
    immutable str = foo(buf);
    assert(str[0] == 'a');
    buf[0] = 'b';
    assert(str[0] == 'a');  // fails!?
}

There is a problem on the return statement in foo. Complier deduces foo to a strong purity function, but its parameter s might refer outer mutable state, so the conversion const(char[]) to string should not be applied.

This is an implementation bug of issue 5081.

https://github.com/D-Programming-Language/dmd/commit/99e0f21d2a7a19b655d97fa08394a3bc623f10a0

https://github.com/D-Programming-Language/dmd/commit/99e0f21d2a7a19b655d97fa08394a3bc623f10a0#L3R3524

The implicit immutable conversion on return statement is valid only if the strong purity function has *no parameters*.

This bug has appeared by the purity calculation improvement by issue 8408.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-27 20:19:47 PST ---
(In reply to comment #0)
> The implicit immutable conversion on return statement is valid only if the strong purity function has *no parameters*.

...No, this is incorrect. I found a disproof.

Correct requirement is:
"If all parameters have no mutable indirections, implicit immutable conversion
on return statement is allowed."

For example:

immutable(int[]) foo(int n) { return new int[](n); }
immutable(int[]) bar(immutable int[] arr) { return new int[](arr.length); }
void main() {
    immutable a = foo(10);
    immutable b = bar([1,2]);
}

Both foo and bar should be allowed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230


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

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


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-27 20:22:36 PST ---
https://github.com/D-Programming-Language/dmd/pull/1418

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230


yebblies <yebblies@gmail.com> changed:

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


--- Comment #3 from yebblies <yebblies@gmail.com> 2012-12-28 16:47:33 EST ---
(In reply to comment #0)
> From: http://d.puremagic.com/issues/show_bug.cgi?id=6553
> 
> In git head, following code compiles without errors, and asserts in runtime.
> 
> string foo(in char[] s) pure {
>     return s; // conversion from const(char[]) to string
> }
> void main() {
>     char[] buf = ['a'];
>     immutable str = foo(buf);
>     assert(str[0] == 'a');
>     buf[0] = 'b';
>     assert(str[0] == 'a');  // fails!?
> }
> 
> There is a problem on the return statement in foo. Complier deduces foo to a strong purity function, but its parameter s might refer outer mutable state, so the conversion const(char[]) to string should not be applied.
> 
> This is an implementation bug of issue 5081.
> 
> This bug has appeared by the purity calculation improvement by issue 8408.

The patch for issue 5081 was corrent, the problem is that the definition of strong purity has been changed since then.

(In reply to comment #1)
> (In reply to comment #0)
> > The implicit immutable conversion on return statement is valid only if the strong purity function has *no parameters*.
> 
> ...No, this is incorrect. I found a disproof.
> 
> Correct requirement is:
> "If all parameters have no mutable indirections, implicit immutable conversion
> on return statement is allowed."
> 

This is the old definition of strong purity.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #4 from yebblies <yebblies@gmail.com> 2012-12-28 16:48:43 EST ---
(In reply to comment #1)
> Correct requirement is:
> "If all parameters have no mutable indirections, implicit immutable conversion
> on return statement is allowed."
> 

Maybe it should say 'no non-immutable indirections'?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-27 23:15:58 PST ---
(In reply to comment #3)
> The patch for issue 5081 was corrent, the problem is that the definition of strong purity has been changed since then.

> This is the old definition of strong purity.

Hmm...interesting, and it might be true. I have thought that "the old definition" was a conservative rule, and did proposed issue 8408 to improve it. But, based on your talk, I have *changed* the definition of strong purity more strictly.

I withdraw the claim that it was an implementation bug.

(In reply to comment #4)
> (In reply to comment #1)
> > Correct requirement is:
> > "If all parameters have no mutable indirections, implicit immutable conversion
> > on return statement is allowed."
> 
> Maybe it should say 'no non-immutable indirections'?

Yes, it is more exact explanation.
(I sometimes called the const qualified indirections to "mutable indirection"
- e.g. const(int)[]. Because it is an opposite of "immutable indirection".)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #6 from github-bugzilla@puremagic.com 2012-12-28 02:11:30 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/afa7a54422dd3ee7d6622e0b83c5c56c238023fb
fix Issue 9230 - Incorrect implicit immutable conversion occurs in pure
function

`hasMutableIndirectionParams()` is based on the old
`TypeFunction::purityLevel()` (before fixing issue 8408). So its result is
consistent with `TypeFunction::purityLevel() != PUREstrong` in 2.060. Then it
*fixes* the regression.

https://github.com/D-Programming-Language/dmd/commit/c42d35bcf92cd26c2b83c3d2e5fc21e5a0f2d296 Merge pull request #1418 from 9rnsr/fix9230

Issue 9230 - Incorrect implicit immutable conversion occurs in pure function

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230


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

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #7 from github-bugzilla@puremagic.com 2012-12-28 11:11:14 PST ---
Commit pushed to staging at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/260ec594ad14acf72812c2563ab249b71a7c55d2 Merge pull request #1418 from 9rnsr/fix9230

Issue 9230 - Incorrect implicit immutable conversion occurs in pure function

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9230



--- Comment #8 from yebblies <yebblies@gmail.com> 2013-01-17 01:11:52 EST ---
Issue 8998 is related

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------