Bug 24561 – inout on opAssign allows you to mutate member variables

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-05-24T06:49:03Z
Last change time
2024-12-13T19:35:19Z
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#18241 →

Comments

Comment #0 by issues.dlang — 2024-05-24T06:49:03Z
This code compiles --- void main() { static struct V { int _data; ref opAssign(T)(auto ref T rhs) inout if(is(immutable T == immutable V)) { this._data = rhs._data; return this; } } V v; auto u = v; v = u; } --- However, it really shouldn't, because inout could be const or immutable underneath the hood. If the opAssign is not templated, then it correctly gives an error, but it doesn't if it's templated like here. That being said, the compiler still manages to give an error for the assignment to v if v is marked as const, so that may be why the issue hasn't been caught previously. This may or may not be related to https://issues.dlang.org/show_bug.cgi?id=12793, which points to a similar problem but specifically with a this This template parameter.
Comment #1 by robert.schadek — 2024-12-13T19:35:19Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18241 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB