Skip to content

gccrs: Add Drop support for block-local variable#4564

Draft
Lishin1215 wants to merge 2 commits into
Rust-GCC:masterfrom
Lishin1215:drop-let-tracking-and-logic
Draft

gccrs: Add Drop support for block-local variable#4564
Lishin1215 wants to merge 2 commits into
Rust-GCC:masterfrom
Lishin1215:drop-let-tracking-and-logic

Conversation

@Lishin1215
Copy link
Copy Markdown
Contributor

This PR consists of 2 commits:

For now, it handles cases like:

fn main() -> i32 {
    {
        let _x = Droppable;
    }

    0
}

commit 1
gccrs: add block drop candidate tracking infrastructure

gcc/rust/ChangeLog:

        * backend/rust-compile-context.h (struct DropCandidate): New struct
        for tracking block-local drop candidates.

commit 2
gccrs: add block-exit Drop calls

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.

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>
@Lishin1215 Lishin1215 force-pushed the drop-let-tracking-and-logic branch from e21459d to dab0a99 Compare May 29, 2026 21:19
auto s = Backend::init_statement (fnctx.fndecl, var, init_expr);
ctx->add_statement (s);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this helper to avoid getting variables whose type does not implement Drop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant