www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Purity tree trace error?

In DMD 2.054 if I have a wrong program like this, a function that's not pure
called from a pure function:

import std.conv;
void main() pure {
    to!int("12");
}


DMD 2.054 prints:
test.d(3): Error: pure function 'main' cannot call impure function 'to'


Instead of that error message, do you like the idea of a kind of "stack trace"
for purity errors? I think the compiler already has the information to show
such "purity tree trace" because it is needed to verifity the transitive nature
of the pure attribute.

Such tree-shaped error for purity is useful to better understand why a function
is not pure, and eventually try to fix this.

The disadvantage is that error messages get longer.


The idea comes from part of an answer written by KennyTM~ (edited):


to!int("12") is not pure because of:
  - std.array.front and popFront, because of:
     - std.utf.decode (Phobos pull #80 was rejected), because of:
        - std.exception.enforce (bug 5750 / DMD pull #227)
        - std.conv.to!(string, size_t), because of:
           - GC.malloc, or std.array.uninitializedArray if Phobos pull #144 was
accepted
        - std.conv.to!(string, const(ubyte)[]), because of:
           - std.conv.to!(string, ubyte), because of std.conv.to!(string, uint)
           - std.array.appender, because of:
              - GC.extend
              - GC.qalloc
              - memcpy (just need a 'pure' annotation)
  - ConvOverflowException.raise, just lacking a 'pure' annotation
  - convError, because of std.conv.to!(string, uint).

Bye,
bearophile
Jul 18 2011