Bug 4915 – auto return type escapes function purity
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2010-09-22T08:03:00Z
Last change time
2015-06-09T01:27:23Z
Keywords
accepts-invalid, patch
Assigned to
nobody
Creator
tomash.brechko
Comments
Comment #0 by tomash.brechko — 2010-09-22T08:03:34Z
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 by clugdbug — 2010-09-22T08:18:15Z
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 by clugdbug — 2010-11-08T17:43:54Z
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 by clugdbug — 2010-11-09T00:14:32Z
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(); }
}