Bug 22290 – Disallow assignment to a non-elaborate field of an rvalue

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-09-08T11:50:57Z
Last change time
2023-12-16T21:45:33Z
Assigned to
No Owner
Creator
Tejas_Garhewal

Comments

Comment #0 by scienticman — 2021-09-08T11:50:57Z
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.
Comment #1 by b2.temp — 2023-12-16T21:45:33Z
*** This issue has been marked as a duplicate of issue 11920 ***