gccrs: Add Drop support for block-local variable#4564
Conversation
gcc/rust/ChangeLog: * backend/rust-compile-context.h (struct DropCandidate): New struct for tracking block-local drop candidates. Signed-off-by: lishin <lishin1008@gmail.com>
gcc/rust/ChangeLog: * backend/rust-compile-block.cc (compile_drop_call): New helper to build a drop call. (CompileBlock::visit): Add drop calls for candidates. * backend/rust-compile-pattern.cc (type_has_drop_impl): New helper to check whether a type implements Drop. (CompilePatternLet::visit): Save initialized simple drop candidates. gcc/testsuite/ChangeLog: * rust/execute/drop-block-scope.rs: New test. Signed-off-by: lishin <lishin1008@gmail.com>
e21459d to
dab0a99
Compare
| auto s = Backend::init_statement (fnctx.fndecl, var, init_expr); | ||
| ctx->add_statement (s); | ||
| } | ||
|
|
There was a problem hiding this comment.
For now I only handle the simple case like:
- let x = Droppable;
- let mut x = Droppable;
- let _x = Droppable;
I skipped more complex patterns for this first version, like ref and subpatterns.
I wanted to start with the smallest case first and then extend it later.
Does this scope look okay for a first draft?
| return block_drop_candidates.back (); | ||
| } | ||
|
|
||
| void note_simple_drop_candidate (HirId hirid, location_t locus) |
There was a problem hiding this comment.
I am storing the drop candidates in Context for now.
The idea is that when we encounter an eligible variable in let, I store it in the array.
Please let me know if this is an ideal place.
| namespace Compile { | ||
|
|
||
| static tree | ||
| compile_drop_call (Context *ctx, Bvariable *var, TyTy::BaseType *ty, |
There was a problem hiding this comment.
This helper finds the Drop trait, looks for the drop method, and builds the function call.
I put it in rust-compile-block.cc for now because it is only used when leaving a block.
I think this may need to move to a separate file later, since other scenarios may also need to use it.
| namespace Compile { | ||
|
|
||
| static bool | ||
| type_has_drop_impl (Context *ctx, TyTy::BaseType *ty) |
There was a problem hiding this comment.
I added this helper to avoid getting variables whose type does not implement Drop.
This PR consists of 2 commits:
For now, it handles cases like:
commit 1
gccrs: add block drop candidate tracking infrastructure
commit 2
gccrs: add block-exit Drop calls