www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5191] New: Combination of pure and nothrow result in a function that does nothing

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

           Summary: Combination of pure and nothrow result in a function
                    that does nothing
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmx.com



PST ---
import std.stdio;

struct Foo
{
    void add(T)(T value) pure nothrow
    {
        this.value += value;
    }

    this(int value)
    {
        this.value = value;
    }

    int value;
}

void main()
{
    auto foo = Foo(5);
    assert(foo.value == 5);

    foo.add(2);
    writeln(foo.value);
    assert(foo.value == 7);

    foo.add(3);
    writeln(foo.value);
    assert(foo.value == 10);

    foo.add(3);
    writeln(foo.value);
    assert(foo.value == 13);
}


If you remove either the pure _or_ the nothrow (or both) from add(), you get
this output:

7
10
13


However, if add() is both pure and nothrow, then you get an assertion failure:

5
core.exception.AssertError test(25): Assertion failure
----------------
./test(onAssertError+0x2e) [0x808d40e]
./test(_d_assertm+0x16) [0x8085536]
./test(void test.__assert(int)) [0x80828d6]
./test(_Dmain+0x43) [0x807f7a7]
./test(extern (C) int rt.dmain2.main(int, char**)) [0x8085746]
./test(extern (C) int rt.dmain2.main(int, char**)) [0x80856a0]
./test(extern (C) int rt.dmain2.main(int, char**)) [0x808578a]
./test(extern (C) int rt.dmain2.main(int, char**)) [0x80856a0]
./test(main+0x96) [0x8085646]
/usr/lib32/libc.so.6(__libc_start_main+0xe6) [0xf75d7c76]
./test() [0x807f6a1]


For some reason, the combination of pure and nothrow results in the function
being a no-op. Personally, since I try to make as many functions nothrow and as
many functions pure as possible, this is a serious fly in the ointment of both
pure and nothrow.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |clugdbug yahoo.com.au
           Severity|normal                      |critical



Nasty. It's because pure member functions are internally being marked as const
somehow. That needs to be fixed. In DMD2.049, this code didn't compile.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch



Extra test case (add to the end of the original test case):
---
    void delegate (int) pure nothrow dg = &foo.add!(int);    
    dg(7);
    assert(foo.value == 20);
--

PATCH: e2ir.c, callfunc(), line 294.  To determine if a function is strongly
pure, you need to look at the function declaration, not just the function type,
since the 'this' parameter isn't part of the type.

-----

    else if (ep)
    {   /* Do not do "no side effect" calls if a hidden parameter is passed,
         * as the return value is stored through the hidden parameter, which
         * is a side effect.
         */
-        e = el_bin((tf->purity == PUREstrong && tf->isnothrow && (retmethod !=
RETstack)) ?
+        e = el_bin(((fd ? fd->isPure() : tf->purity) == PUREstrong &&
tf->isnothrow && (retmethod != RETstack)) ?
                OPcallns : OPcall,tyret,ec,ep);
        if (tf->varargs)
            e->Eflags |= EFLAGS_variadic;
    }
    else
-    {   e = el_una((tf->purity == PUREstrong && tf->isnothrow && (retmethod !=
RETstack)) ?
+    {   e = el_una(((fd ? fd->isPure() : tf->purity) == PUREstrong &&
tf->isnothrow && (retmethod != RETstack)) ?
                OPucallns : OPucall,tyret,ec);

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


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw ubuntu.com



*** Issue 5277 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 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5191


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha yahoo.com



Note that inlining must be turned off for this test case to fail.

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


Walter Bright <bugzilla digitalmars.com> changed:

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



19:35:21 PST ---
http://www.dsource.org/projects/dmd/changeset/775

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