www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5006] New: 'pure' unenforced in a nested function

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

           Summary: 'pure' unenforced in a nested function
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-10-06 14:08:53 PDT ---
This D2 code compiles and runs with no errors with dmd 2.049, and it ignores
the "pure" attribute:


int z = 1000;
int foo(int x) {
    pure int bar(int y) {
        z++;
        return x + y + z;
    }
    return bar(x * x);
}
void main() {
    assert(foo(10) == 1111);
}


I expect this code to not compile.

Is DMD missing unit tests for this?

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


Stewart Gordon <smjg iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
                 CC|                            |smjg iname.com


--- Comment #1 from Stewart Gordon <smjg iname.com> 2010-10-07 04:33:04 PDT ---
Please remember to assign keywords to bug reports.  To everybody reading this:
Please look through issues you've reported and check for missing keywords.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 07 2010
parent Don <nospam nospam.com> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=5006
 
 
 Stewart Gordon <smjg iname.com> changed:
 
            What    |Removed                     |Added
 ----------------------------------------------------------------------------
            Keywords|                            |accepts-invalid
                  CC|                            |smjg iname.com
 
 
 --- Comment #1 from Stewart Gordon <smjg iname.com> 2010-10-07 04:33:04 PDT ---
 Please remember to assign keywords to bug reports.  To everybody reading this:
 Please look through issues you've reported and check for missing keywords.
Thanks Stewart. There are currently 395 bugs with no keywords!! I use keywords in most of my searches, so bugs without keywords tend to get overlooked.
Oct 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5006



--- Comment #2 from bearophile_hugs eml.cc 2010-10-26 15:34:16 PDT ---
It seems currently (2.050alpha) nested functions can't be pure:


import std.traits: FunctionAttribute, functionAttributes;
void main() {
    static pure int foo1(int x) { return x; }
    pure int foo2(int x) { return x; }
    static assert(functionAttributes!(foo1) & FunctionAttribute.PURE); //
asserts
    static assert(functionAttributes!(foo2) & FunctionAttribute.PURE); //
asserts

}
void main() {}


This is a problem because I'd like many functions of std.algorithm to be weakly
pure. Many high order functions take an optional delegate argument, so if
there's no handy way to build a pure delegate, they become less useful.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #3 from Don <clugdbug yahoo.com.au> 2010-10-27 01:54:01 PDT ---
This is the same issue as bug 4640 (and I think I've seen it somewhere else as
well). It's a parsing issue.
Placing the attribute after the parameter list works.

int bar(int y) pure { .... }

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


Lewis <lewis1711 gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lewis1711 gmail.com
         OS/Version|Windows                     |Linux
           Severity|normal                      |major


--- Comment #4 from Lewis <lewis1711 gmail.com> 2010-10-31 15:52:50 PDT ---
I can confirm this bug.

It especially annoying because if you copypasta the bit on pure functionns
straight out of TDPL it *will compile*, when the comment says it shouldn't.

import std.stdio;

void main()
{
    pure bool leapYear(uint y)
    {
        auto result = (y % 4) == 0 && (y % 100 || (y % 400) == 0);
        if (result) writeln(y, " is a leap year!"); // Error!
        // Cannot call impure function writeln from pure function!
        return result;
    }

    leapYear(1);
}

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


kennytm gmail.com changed:

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


--- Comment #5 from kennytm gmail.com 2011-02-21 11:41:26 PST ---
Looks like this is fixed in v2.052 or earlier.

Now bearophile's 1st program gives:

x.d(4): Error: pure function 'bar' cannot access mutable static data 'z'
x.d(5): Error: pure nested function 'bar' cannot access mutable data 'x'
x.d(5): Error: pure function 'bar' cannot access mutable static data 'z'

and Lewis' program gives:

x.d(8): Error: pure function 'leapYear' cannot call impure function 'writeln'

as expected, although I think 'bar' should be able to access 'x' in
bearophile's 1st program.

On the other hand, bearophile's 2nd program now result in ICE (at least on Mac
OS X)

Internal error: ../ztc/machobj.c 1805

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5006



--- Comment #6 from kennytm gmail.com 2011-02-21 11:50:21 PST ---
(In reply to comment #5)
 On the other hand, bearophile's 2nd program now result in ICE (at least on Mac
 OS X)
 
 Internal error: ../ztc/machobj.c 1805
Never mind, it ICE only because there are two 'main's (issue 5634). Removing the 2nd 'main' the program compiles correctly. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 21 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5006


bearophile_hugs eml.cc 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: -------
Feb 21 2011