Emit drops before explicit returns in GCC Rust
Ensures proper resource cleanup when explicit return statements are used in Rust code.
This commit ensures that destructors are correctly emitted for all active block scopes before an explicit return statement is executed in the GCC Rust compiler. If the explicit return has a value, it is saved before drops are performed. For unit returns with an expression, that expression is evaluated and its result handled before drops and the final unit return.
In Details
The CompileExpr::visit function now calls DropBuilder::get_block_drop_candidate_stack to retrieve all active scopes needing drops before emitting an HIR::ReturnExpr. If the return has a value, it's saved; otherwise, any associated expression is evaluated. This guarantees that cleanup code runs for all relevant scopes preceding the return.
- Drop emission
- The process of generating code to call destructors (or 'drop' functions) for variables that go out of scope, ensuring resources are properly released.
- HIR::ReturnExpr
- A node in the High-Level Intermediate Representation (HIR) representing an explicit return statement in Rust code.
- Block scope
- A region of code, typically enclosed in curly braces {}, where variables declared within that region are valid and exist.
- HIR
- The High-Level Intermediate Representation used by the GCC Rust front-end. It represents the code after initial parsing and some semantic analysis, before optimization.