[Issue 15862] dmd thinks functions are strongly pure despite mutable indirections in the return type

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Oct 5 08:59:20 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=15862

--- Comment #6 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Walter Bright from comment #2)
> This is as designed and intended.
> 
> 1. Mutable indirections in the return type do not affect purity
> determination. Mutability of the parameters does.

The function can still be strong-pure, in that the result will always be the
same for the same parameters. You can elide the call, but the result must be
copied -- it cannot be identical.

That is, it is OK for the compiler to optimize by changing OP's code to this:

    int* p1 = p();
    int* p2 = new int(*p1);

    if (p1 is p2) throw new Exception("pointers same");

    int[] a1 = a();
    int[] a2 = a1.dup;

    if (a1 is a2) throw new Exception("arrays same");

    Object o1 = o();
    Object o2 = deepCopy(o);

    if (o1 is o2) throw new Exception("objects same");

> 2. An early design decision was that calling new() inside a pure function is
> an acceptable special case. (Otherwise pure functions would be nearly
> useless.)

Right, but all pure memory allocations should not return the same piece of
memory!

--


More information about the Digitalmars-d-bugs mailing list