Bug 21590 – assignment inside assert accepted if -checkaction=context is given

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-01-29T02:28:19Z
Last change time
2021-02-13T12:30:01Z
Keywords
pull
Assigned to
No Owner
Creator
kdevel

Comments

Comment #0 by kdevel — 2021-01-29T02:28:19Z
```assgncnd.d unittest { int a, b = 1; assert (a = b); } ``` $ dmd -unittest -main -run assgncnd.d assgncnd.d(3): Error: assignment cannot be used as a condition, perhaps == was meant? $ dmd -unittest -checkaction=context -main -run assgncnd.d 1 modules passed unittests Tested DMD Versions: 2.095.0 bad 2.093.1 bad 2.090.1 good
Comment #1 by moonlightsentinel — 2021-01-29T11:47:31Z
That's an unintended consequence of the current rewrite for expressions with side effects. The compiler lowers the example to: unittest { int a = 0; int b = 1; (ref int __assertOp2 = a = b;) , assert(__assertOp2, _d_assert_fail(__assertOp2)); } The assignment happens for the temporary and hence becomes legal. The compiler should probably check if the expression can be used as a condition before the rewrite.
Comment #2 by dlang-bot — 2021-01-29T13:48:57Z
@MoonlightSentinel created dlang/dmd pull request #12165 "Fix 21590 - assignment inside assert accepted for -checkaction=context" fixing this issue: - Fix 21590 - assignment inside assert accepted for -checkaction=context The rewrite introduced a temporary which hid the assignment inside of `assert(...)` and hence prevented the error. The fix is to omit the additional temporary for `AssignExp` and use the assigned variable directly. The temporary is unecessary anyways and the following semantic analysis will raise an appropriate error. https://github.com/dlang/dmd/pull/12165
Comment #3 by dlang-bot — 2021-01-30T12:59:51Z
dlang/dmd pull request #12165 "Fix 21590 - assignment inside assert accepted for -checkaction=context" was merged into stable: - f9dc70cc7d57e24a05379496b17e1d150c363eb5 by MoonlightSentinel: Fix 21590 - assignment inside assert accepted for -checkaction=context The rewrite introduced a temporary which hid the assignment inside of `assert(...)` and hence prevented the error. The fix is to omit the additional temporary for `AssignExp` and use the assigned variable directly. The temporary is unecessary anyways and the following semantic analysis will raise an appropriate error. https://github.com/dlang/dmd/pull/12165
Comment #4 by dlang-bot — 2021-02-13T12:30:01Z
dlang/dmd pull request #12195 "Merge stable" was merged into master: - 8055d9deb843efeb30b2b6a5d6602b6e2384b94a by MoonlightSentinel: Fix 21590 - assignment inside assert accepted for -checkaction=context The rewrite introduced a temporary which hid the assignment inside of `assert(...)` and hence prevented the error. The fix is to omit the additional temporary for `AssignExp` and use the assigned variable directly. The temporary is unecessary anyways and the following semantic analysis will raise an appropriate error. https://github.com/dlang/dmd/pull/12195