Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure variable declarations at the end of try blocks are cleaned up (#…
…25919) Closes #25548, in which `try! whatever()` caused the result of `whatever()` to not be cleaned up, even if no exception occurred. To see why this happens, consider the following (simplified) code desugaring `try! expr`: ```Chapel var x1 = ... var x2 = ... var x3 = ... if (error) { goto error handling; } ``` `x3 = …` is presumably the call that creates the error, so `x3` may be uninitialized at the time we do `if (error)`. When traversing this code linearly and inserting autodestroys, to avoid processing `x3` (which may be uninit'ed), we go right into the `if (error)` just before we visit `x3` (going out of order). This way, when we insert auto-destroys for error handling (which happens to auto-destroy all variables in scope, since we are unwinding), we don’t auto-destroy `x3`, which may be uninit’ed. However, we use the same code to do this early visit into `if (error)` as we do for any other statement. And other logic in that code says “well if this statement is the last mention of any variables, insert auto-destroys”. Thus, we insert auto-destroys while doing this early processing of `if (error)`, and mark all variables as having been deinited… except that we haven’t marked `x3` as inited yet (previous paragraph), so it doesn’t get an auto-destroy, so it leaks. We mark it as deinited without having inserted the deinitialization code. interestingly we visit `if (error)` again, normally, after `x3`, and it tries to do “insert auto-destroys” as well, but it’s a nop-op since they’ve already been inserted and all the variables have been marked already-uninitialized. This PR just makes the early `if (error)` not do the cleanup (since it will miss `x3`), and lets that cleanup fall through to the non-early if (error) handling. The early `if (error)` still inserts auto-destroys for unwinding (as one would expect). Reviewed by @jabraham17 -- thanks! ## Testing - [x] paratest - [x] paratest (memleaks)
- Loading branch information