digitalmars.D.bugs - [Issue 22290] New: Disallow assignment to a non-elaborate field of
- d-bugmail puremagic.com (31/31) Sep 08 2021 https://issues.dlang.org/show_bug.cgi?id=22290
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 puremagic.com Reporter: scienticman 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. --
Sep 08 2021