D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4915 - auto return type escapes function purity
Summary: auto return type escapes function purity
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, patch
Depends on:
Blocks:
 
Reported: 2010-09-22 08:03 UTC by Tomash Brechko
Modified: 2015-06-09 01:27 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomash Brechko 2010-09-22 08:03:34 UTC
dmd 2.039 doesn't produce error for pure function with auto return type that violates purity:

int global;

pure auto f()
{
  global = 1;
  return 0;
}

compiles without errors.
Comment 1 Don 2010-09-22 08:18:15 UTC
Actually, the bug is something different:

pure auto f() {  return 0; }
pure int g() { return f(); }

bug.d(3): Error: pure function 'g' cannot call impure function 'f'

The bug is that for 'auto' functions, 'pure' is ignored.
Bug 3359 is another aspect of the same thing. In fact I think there are about five bugs which probably all have the same root cause.
Comment 2 Don 2010-11-08 17:43:54 UTC
This patch also fixes bug 4640, bug 5006, and cases 2 and 3 of bug 3573.

PATCH: func.c,  FuncDeclaration::semantic, line 164. All of the function-related storage classes need to be applied to the function.
(Possibly STCsynchronised as well? Maybe there should be a #define which puts all of these together, in case the relevant list gets longer).


    if (!type->deco)
    {
        sc = sc->push();
-        sc->stc |= storage_class & STCref;      // forward to function type
+        sc->stc |= storage_class & (STCref | STCnothrow | STCpure | STCdisable | STCproperty | STCsafe | STCtrusted | STCsystem);      // forward to function type
        if (isCtorDeclaration())
            sc->flags |= SCOPEctor;
Comment 3 Don 2010-11-09 00:14:32 UTC
Test case (should compile with no errors):

pure nothrow @safe auto bug4915a() {  return 0; }
pure nothrow @safe int  bug4915b() { return bug4915a(); }

void bug4915c()
{
    pure nothrow @safe int d() { return 0; }
    int e() pure nothrow @safe { return d(); }    
}
Comment 4 Don 2010-11-09 05:26:29 UTC
oops, STCproperty shouldn't be in that list.
Comment 5 Walter Bright 2010-11-10 22:21:09 UTC
http://www.dsource.org/projects/dmd/changeset/748