`a += b` may silently generates incorrect code
roastduck opened this issue · comments
We distinguish a = b
and a[...] = b
, where the former is an assignment in stage-1, and the latter is in stage-2. This is well defined because a = b
goes to a Python reference assignment, and a[...] = b
goes to __setitem__
.
However, there is not a clear distinction between a += b
and a[...] += b
: they both go to __iadd__
, which is in stage-2. The real problem is that when an exception raise from __iadd__
, Python will automatically fall back to a = a + b
for a += b
, which is in stage-1, and this leads to an incorrect code. An example of such exceptions is modifying a read-only tensor.
This issue is not true.