-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix missing destruction when discarding complex expression (#1422)
## Summary Fix the result of `if`, `case`, `block`, and `try` expression not being destroyed when `discard`ing the expression. Fixes #1421. ## Details * in `mirgen`, a value-like PMIR expression is now requested for the discarded expression * `toValue` expects a properly value-like PMIR expression, which a complex expression is not, hence the missing destructor * the expression is now properly materialized, which means that the a destructor call is registered for the temporary
- Loading branch information
Showing
2 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/lang_objects/destructor/tdestroy_discarded_complex.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
discard """ | ||
description: ''' | ||
Ensure that the result of complex expressions is properly destroyed when | ||
the full expression is discarded | ||
''' | ||
targets: "c js vm" | ||
""" | ||
|
||
type Object = object | ||
has: bool | ||
|
||
var destroyed = 0 | ||
|
||
proc `=destroy`(x: var Object) = | ||
if x.has: | ||
inc destroyed | ||
|
||
proc make(): Object = Object(has: true) | ||
|
||
# --------------- | ||
# test procedures | ||
|
||
proc test_block() = | ||
discard (block: make()) | ||
|
||
proc test_if(cond: bool) = | ||
discard (if cond: make() else: make()) | ||
|
||
proc test_case(cond: bool) = | ||
discard ( | ||
case cond | ||
of true: make() | ||
of false: make() | ||
) | ||
|
||
proc test_try() = | ||
discard ( | ||
try: make() | ||
except: make() | ||
) | ||
|
||
test_block() | ||
doAssert destroyed == 1 | ||
test_if(true) | ||
doAssert destroyed == 2 | ||
test_case(true) | ||
doAssert destroyed == 3 | ||
test_try() | ||
doAssert destroyed == 4 |