www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5274] New: Impure function call inside impure function nested inside pure function

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

           Summary: Impure function call inside impure function nested
                    inside pure function
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-11-24 16:50:31 PST ---
This D2 code is formally correct because bar() is never called:


import core.stdc.stdio: putchar;
pure void foo() {
    static void bar() {
        putchar('a');
    }
}
void main() {}


DMD 2.050 prints:

test.d(4): Error: pure function 'foo' cannot call impure function 'putchar'

Despite foo() doesn't contain calls to putchar().

Even if I modify this program, adding a call to bar() inside foo(), that
strange error message doesn't change.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug yahoo.com.au
         Resolution|                            |INVALID


--- Comment #1 from Don <clugdbug yahoo.com.au> 2010-11-24 19:06:38 PST ---
(In reply to comment #0)
 This D2 code is formally correct because bar() is never called:
No, the code is incorrect. The error message occurs because bar() cannot be compiled. Think about it -- its mangled name must have 'pure' in it. The fact that there's no way that the function can actually be called, is irrelevant: it was marked as pure, but it violates pure. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 24 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5274



--- Comment #2 from bearophile_hugs eml.cc 2010-11-24 19:34:49 PST ---
I have not expressed myself well enough, I am sorry. This bug report is a
"diagnostic" one, it's not a "rejects valid". So I agree that this code needs
to be refused at compile time, but is this a good message?

test.d(4): Error: pure function 'foo' cannot call impure function 'putchar'

foo() doesn't contain putchar(), it's bar() that calls it.

I don't know what is a good error message for this situation.
This is a reduced test case:


void foo() {}
pure void bar() {
    void spam() {
        foo();
    }
}
void main() {}


Maybe you are right, there is no much better error message to be invented here.

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



--- Comment #3 from Steven Schveighoffer <schveiguy yahoo.com> 2010-11-29
07:43:03 PST ---
(In reply to comment #2)

 Maybe you are right, there is no much better error message to be invented here.
Hm... I'd expect a message like: Error: pure function 'foo.bar' cannot call impure function 'putchar' But this doesn't seem to be a very critical problem, the line number is correct, so you can see where the problem is. -- 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=5274



--- Comment #4 from Don <clugdbug yahoo.com.au> 2010-11-29 14:25:47 PST ---
(In reply to comment #3)
 (In reply to comment #2)
 
 Maybe you are right, there is no much better error message to be invented here.
Hm... I'd expect a message like: Error: pure function 'foo.bar' cannot call impure function 'putchar'
At present, you get that error message if you explicitly mark bar as pure. But, the existing error message tells you why bar is pure. It's because foo is pure. This is helpful if there are several levels of nesting: it tells you the level which was pure.
 
 But this doesn't seem to be a very critical problem, the line number is
 correct, so you can see where the problem is.
-- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 29 2010