www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8408] New: Purity calculation should be improved

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408

           Summary: Purity calculation should be improved
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          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-07-22 05:27:35 PDT ---
If the argument values that given to a function marked as pure doesn't appear
in its return value, then the function should become 'strong purity'.

An example:

int[] func(const(int)[] arr) pure;

The parameter 'arr' refers const integers through its slice, but func returns
int[], so func cannot return arr directly (without unsafe cast) and becomes
'strong purity' function.

The parameter 'arr' refers const integers through its slice, but func returns
int[]. const(int)[] is not implicitly convertible to int[], then func cannot
return arr directly (without unsafe cast like cast(int[]) arr) and becomes
'strong purity' function.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 22 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408


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

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


--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2012-09-09 06:21:21 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1110

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #2 from bearophile_hugs eml.cc 2012-09-09 07:03:15 PDT ---
This rule makes more functions (tagged as pure) become strongly pure, this is
positive.

On the other hand for the programmer it's increasingly harder to know if a
function is weak pure or strongly pure just looking at it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #3 from Kenji Hara <k.hara.pg gmail.com> 2012-09-09 07:24:46 PDT ---
(In reply to comment #2)
 This rule makes more functions (tagged as pure) become strongly pure, this is
 positive.
Thanks. But, I've been filed this as a part of issue 8409, so the pull request doesn't cover all cases.
 On the other hand for the programmer it's increasingly harder to know if a
 function is weak pure or strongly pure just looking at it.
I think it is not so difficult if you summarize it. - If the function can modify function argument through its parameters, it is weakly pure. - If the function arguments don't appear in the part of the returned value, or the returned value is not a part of arguments, then it is strongly pure. - Otherwise, it is constant pure. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #4 from bearophile_hugs eml.cc 2012-11-12 20:23:55 PST ---
*** Issue 9011 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 12 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #5 from github-bugzilla puremagic.com 2012-12-03 22:00:20 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/41c52a324d0526a079039041c64afc1d3983eb58
fix Issue 8408 - Purity calculation should be improved

https://github.com/D-Programming-Language/dmd/commit/b6a809346a43c7fbf350bd4181d350dd9b2cd4e6
Merge pull request #1110 from 9rnsr/fix8408

Issue 8408 - Purity calculation should be improved

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #6 from bearophile_hugs eml.cc 2012-12-05 17:36:09 PST ---
Is it correct that x1 refused and x2 accepted?


char[] foo1(int[] arr) pure {
    return new char[10];
}
immutable(char)[] foo2(int[] arr) pure {
    return new char[10];
}
void main(string[] args) {
    immutable x1 = foo1([1, 2]); // Error: cannot implicitly convert
    immutable x2 = foo2([1, 2]); // OK
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #7 from bearophile_hugs eml.cc 2012-12-06 18:07:32 PST ---
(In reply to comment #6)
 Is it correct that x1 refused and x2 accepted?
I guess I have to wait or Issue 8409 to be fixed before looking for possible troubles. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #8 from Kenji Hara <k.hara.pg gmail.com> 2012-12-06 19:33:50 PST ---
(In reply to comment #6)
 Is it correct that x1 refused and x2 accepted?
 
 
 char[] foo1(int[] arr) pure {
     return new char[10];
 }
 immutable(char)[] foo2(int[] arr) pure {
     return new char[10];
 }
 void main(string[] args) {
     immutable x1 = foo1([1, 2]); // Error: cannot implicitly convert
     immutable x2 = foo2([1, 2]); // OK
 }
I'm not sure that this should be allowed. foo1 can rewrite the elements referred from arr, then it is deduced to weak purity. In current principle, the returned value from weak purity function cannot be converted to immutable implicitly (it is only allowed for strong purity function). If you change the signature of foo1 to: char[] foo1(const int[] arr) pure; Then foo1 will be deduced to strong purity, and implicit conversion to immutable for initializing x1 will be succeeded. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #9 from bearophile_hugs eml.cc 2012-12-10 18:31:16 PST ---
(In reply to comment #8)

 I'm not sure that this should be allowed.
 foo1 can rewrite the elements referred from arr, then it is deduced to weak
 purity. In current principle, the returned value from weak purity function
 cannot be converted to immutable implicitly (it is only allowed for strong
 purity function).
 
 If you change the signature of foo1 to:
 
   char[] foo1(const int[] arr) pure;
 
 Then foo1 will be deduced to strong purity, and implicit conversion to
 immutable for initializing x1 will be succeeded.
You are right, thank you for your answer. (If you want me/us to try to suggest improvements in how you write in English, I am willing to help you, despite I am not a good English teacher.) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 10 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch


--- Comment #10 from timon.gehr gmx.ch 2012-12-10 18:37:41 PST ---
(In reply to comment #8)
 (In reply to comment #6)
 Is it correct that x1 refused and x2 accepted?
 
 
 char[] foo1(int[] arr) pure {
     return new char[10];
 }
 immutable(char)[] foo2(int[] arr) pure {
     return new char[10];
 }
 void main(string[] args) {
     immutable x1 = foo1([1, 2]); // Error: cannot implicitly convert
     immutable x2 = foo2([1, 2]); // OK
 }
I'm not sure that this should be allowed. ...
Why not? It is known at the call site that anything foo1 will return is newly allocated. Strong or weak purity is irrelevant. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 10 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #11 from Kenji Hara <k.hara.pg gmail.com> 2012-12-10 19:39:00 PST
---
(In reply to comment #10)
 (In reply to comment #8)
 (In reply to comment #6)
 I'm not sure that this should be allowed.
 ...
Why not? It is known at the call site that anything foo1 will return is newly allocated. Strong or weak purity is irrelevant.
Your argument had be true. I had re-read issue 5081, and could be believed that between the purity level (strong, constant, weak) and the conversion possibility to immutable of returned value are irrelevant. From: http://d.puremagic.com/issues/show_bug.cgi?id=5081#c2
 Note actually that as long as you can verify the return value did not come
 directly from the parameters, it's also possible to implicitly cast to
 immutable.
 
 For example:
 
 pure T[] mydup(T)(const(T)[] param) {...}
 
 It's provable that the return value did not come from param (without a cast),
 because you can't implicitly cast param to T[].  So you can cast the result to
 immutable, >>>>even if param began as mutable<<<<.
The last sentence describes the ideal behavior. Thanks a lot! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 10 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #12 from yebblies <yebblies gmail.com> 2013-01-17 01:07:31 EST ---
*** Issue 6783 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408


yebblies <yebblies gmail.com> changed:

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


--- Comment #13 from yebblies <yebblies gmail.com> 2013-01-17 01:14:02 EST ---
Is issue 8998 a regression caused by this patch?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #14 from github-bugzilla puremagic.com 2013-03-03 16:07:38 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/ee06e4a38cb7e35fe046ffd4c5a938063c48dc30
Merge pull request #1519 from 9rnsr/fix_purity

Refactoring/improvement of issue 8408, and additionally fixing Issue 8998

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 03 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8408



--- Comment #15 from github-bugzilla puremagic.com 2013-03-24 20:57:04 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/b6a809346a43c7fbf350bd4181d350dd9b2cd4e6
Merge pull request #1110 from 9rnsr/fix8408

https://github.com/D-Programming-Language/dmd/commit/ee06e4a38cb7e35fe046ffd4c5a938063c48dc30
Merge pull request #1519 from 9rnsr/fix_purity

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