gccrs: Emit drops for function parameters
GCC's Rust compiler now ensures function parameters are dropped after function execution.
GCC’s Rust compiler now correctly emits drop calls for function parameters. These parameters are added to the function scope’s drop list after the function body block is constructed, ensuring they are dropped in the correct order relative to local variables upon normal function exit. This change improves resource management and prevents leaks.
In Details
The rustc_codegen_gcc backend's HIRCompileBase::compile_function now explicitly tracks droppable function parameters and adds them to the function scope's drop list. This ensures that parameters are properly cleaned up (dropped) after the function body executes, alongside local variables. This is a crucial step for maintaining correct RAII semantics in Rust code generated by GCC.
- Drop
- In Rust, a trait that can be implemented for a type to define custom logic that runs when a value of that type goes out of scope. This is primarily used for resource management.
- function scope
- The region of code within a function where its parameters and local variables are accessible. When the function returns, these variables go out of scope and are dropped.
- RAII
- Resource Acquisition Is Initialization. A programming idiom in which resource management is tied to object lifetime. In Rust, this is primarily handled via the
Droptrait.