Drop foundation and block scopes ยท PR #4564
Add Drop support for block-local variables
Objective
Add automatic Drop calls for supported local bindings at block exit and implicit function exit.
Why it was needed
Registering the Drop trait did not run cleanup by itself.
gccrs still did not call Drop::drop when a supported local binding reached the end of its block.
This PR added the first automatic cleanup path for simple local names.
Main changes
- Adds tracking for block-local Drop candidates.
- Builds a Drop call for each supported candidate when its block ends.
- Reports unsupported Drop
refpatterns and subpatterns. - Moves the Drop helpers into separate
rust-compile-dropfiles. - Adds Drop calls at the end of function bodies for implicit returns.
Test cases
- drop-block-scope.rs: Creates
_x,x, andmut xin separate blocks and checks that all three Drop calls run. - drop-ref-pattern.rs: Checks the unimplemented diagnostic for
let ref _x = Droppable. - drop-function-scope-unit.rs: Checks that a local in a unit-returning function runs its Drop call.
- drop-function-scope.rs: Checks that a local in a function with the tail value
0runs its Drop call.
Scope of this PR
Supported bindings in this patch use simple names such as
x,mut x, and_x.
refbindings and subpatterns are not supported.