www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13574] New: incorrect code for assignment to dollar in slice


          Issue ID: 13574
           Summary: incorrect code for assignment to dollar in slice
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: code dawg.eu

cat > bug.d << CODE
extern (C) int printf(const char*, ...);

struct Foo
    void opSlice(size_t a, size_t b) { printf("%zu %zu\n", a, b); }
    alias opDollar = length;
    size_t length;

void main()
    Foo foo;
    foo[0 .. foo.length = 1];
    assert(foo.length == 1);
    foo[0 .. $ = 2]; // assigns to the temporary dollar variable
    assert(foo.length == 2);

dmd -run bug


AFAIK we're currently caching the result of opDollar to avoid reevaluating of
side-effects. If that should remain the case the opDollar tmp variable needs to
be const.
Alternatively we could also allow side-effects in opDollar (requires strict
left to right evaluation) and not cache the value. This might be a bad choice
when length is costly to compute.

Oct 04 2014