Thread overview
[Issue 5798] New: Swap and comma operator
Mar 30, 2011
timon.gehr@gmx.ch
[Issue 5798] Swap and comma operator silently fails
Mar 30, 2011
kennytm@gmail.com
[Issue 5798] Weakly pure function calls skipped inside a comma expression
Apr 19, 2011
timon.gehr@gmx.ch
Apr 25, 2011
kennytm@gmail.com
May 05, 2011
Walter Bright
Jun 24, 2011
kennytm@gmail.com
Jun 24, 2011
kennytm@gmail.com
Jul 15, 2011
yebblies
March 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798

           Summary: Swap and comma operator
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: timon.gehr@gmx.ch


--- Comment #0 from timon.gehr@gmx.ch 2011-03-30 13:19:58 PDT ---
Hello,

The following code fails the assertion:

import std.algorithm;
import std.stdio;

int main(){
    int a=1,b=0,c;
    swap(a,b),c=b;
    assert(a==0);
    return 0;
}



(This one passes:

import std.stdio;

int main(){
    int a=1,b=0,c;
    tmp=b,b=a,a=tmp,c=b;
    assert(a==0);
    return 0;
}

As well as this one:

import std.algorithm;
import std.stdio;

int main(){
    int a=1,b=0,c;
    swap(a,b);c=b;
    assert(a==0);
    return 0;
} )

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


kennytm@gmail.com changed:

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


--- Comment #1 from kennytm@gmail.com 2011-03-30 14:08:55 PDT ---
Test case that does not depend on Phobos:


void assign9(ref int lhs) pure {
    lhs = 9;
}

void assign8(ref int rhs) pure {
    rhs = 8;
}

int main(){
    int a=1,b=2;
    assign8(b),assign9(a);
    assert(a == 9);
    assert(b == 8);   // <-- fail

    assign9(b),assign8(a);
    assert(a == 8);
    assert(b == 9);   // <-- fail

    return 0;
}


Removing the 'pure' attributes make all asserts pass.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 19, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Severity|normal                      |major


--- Comment #2 from timon.gehr@gmx.ch 2011-04-19 12:57:50 PDT ---
I changed importance to mayor, because this also affects some compiler rewrites, and the fix is trivial.

Eg:
int weakly_pure_function(out param)pure{...}
1^^weakly_pure_function(param), will be optimized away to
(weakly_pure_function(param),1) and then only 1, without setting the param.

This bug exists because the compiler incorrectly assumes that a weakly pure function has no side effects.

Suggested fix:
in expression.c, function CallExp::checkSideEffect, replace

(~line 7278)

-     if (t->ty == Tfunction && ((TypeFunction *)t)->purity)
-         return 0;

by

+     if (t->ty == Tfunction && ((TypeFunction *)t)->purity > PUREweak)
+         return 0;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798



--- Comment #3 from kennytm@gmail.com 2011-04-25 02:14:51 PDT ---
Comma expression is also used in for-loops, which may trigger this bug. In
particular, the equal() function since commit ec2c8460* does not work with a
range with pure popFront() since it says

      for (; !r1.empty; r1.popFront(), r2.popFront())
    //                               ^

For instance, the following code, which works in 2.052, no longer work in the git master version:

-----------------------------------------------------------------
import std.array, std.algorithm;

struct X {
    int _front;
pure nothrow:
    @property int front() const { return _front; }
    void popFront() { ++ _front; }
    @property bool empty() const { return _front == 10; }
}

void main() {
    X x;
    //assert(equal(array(x), [0,1,2,3,4,5,6,7,8,9])); // ok
    x._front = 0;
    assert(equal(x, [0,1,2,3,4,5,6,7,8,9])); // asserts
}
-----------------------------------------------------------------

* https://github.com/D-Programming-Language/phobos/commit/ec2c8460#L0R4324

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 05, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2011-05-04 23:56:25 PDT ---
https://github.com/D-Programming-Language/dmd/commit/125e81535a2473f485df4f09fe90a900ea2e054b

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


kennytm@gmail.com changed:

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


--- Comment #5 from kennytm@gmail.com 2011-06-24 02:26:41 PDT ---
I thought this in a 'for' loop (comment 3) has been fixed, but it doesn't.
Re-opening.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@kyllingen.net


--- Comment #6 from kennytm@gmail.com 2011-06-24 02:28:28 PDT ---
*** Issue 6206 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: -------
June 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798



--- Comment #7 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2011-06-24 02:39:10 PDT ---
Adding my test case from issue 6206 here, since it's much smaller and doesn't pull in any Phobos modules.

    struct S
    {
        int i = 0;
        void incr() pure { ++i; }
    }

    void main()
    {
        S s;
        for (int j=0; j<10; s.incr(), ++j) { }
        assert (s.i == 10); // fails
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 15, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5798


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |FIXED


--- Comment #8 from yebblies <yebblies@gmail.com> 2011-07-15 14:07:52 EST ---
These all seem to be working with dmd 2.054

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