[Issue 22290] New: Disallow assignment to a non-elaborate field of an rvalue

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 8 11:50:57 UTC 2021


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

          Issue ID: 22290
           Summary: Disallow assignment to a non-elaborate field of an
                    rvalue
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: scienticman at gmail.com

struct S{
    int i;
}

S _s;

S s(){
    return _s;  //returns DUPLICATE of _s, not _s by reference
}


void main(){

    s().i = 42; //new user might think _s.i is the variable that gets modified.
    assert(!(_s.i == 42)); //They would be wrong.
}

Yes, there are valid cases when the rvalue DOES need to be mutated. However, in
cases like these, there are no side-effects, and the computation becomes
completely redundant, and the user finds out their error at runtime with no
proper diagnostic, since the program even successfully executes, not just
compiles.

--


More information about the Digitalmars-d-bugs mailing list