-
Notifications
You must be signed in to change notification settings - Fork 226
gccrs: Add Drop support for block-local variable #4564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6e5aa1e
e89372e
16e5c74
056292d
51d8211
d35c6aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (C) 2026 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #ifndef RUST_COMPILE_DROP_CANDIDATE_H | ||
| #define RUST_COMPILE_DROP_CANDIDATE_H | ||
|
|
||
| #include "rust-system.h" | ||
| #include "rust-hir-map.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Compile { | ||
|
|
||
| struct DropCandidate | ||
| { | ||
| DropCandidate (HirId hirid, location_t locus) : hirid (hirid), locus (locus) | ||
| {} | ||
|
|
||
| HirId hirid; | ||
| location_t locus; | ||
| }; | ||
|
|
||
| } // namespace Compile | ||
| } // namespace Rust | ||
|
|
||
| #endif // RUST_COMPILE_DROP_CANDIDATE_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (C) 2026 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-compile-drop.h" | ||
|
P-E-P marked this conversation as resolved.
|
||
| #include "rust-compile-base.h" | ||
| #include "rust-compile-context.h" | ||
| #include "rust-compile-implitem.h" | ||
| #include "rust-hir-path-probe.h" | ||
| #include "rust-hir-trait-reference.h" | ||
| #include "rust-hir-type-bounds.h" | ||
| #include "rust-lang-item.h" | ||
| #include "rust-tyty.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Compile { | ||
|
|
||
| bool | ||
| CompileDrop::type_has_drop_impl (Context *ctx, TyTy::BaseType *ty) | ||
| { | ||
| auto drop_lang_item | ||
| = ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP); | ||
|
|
||
| if (!drop_lang_item.has_value ()) | ||
| return false; | ||
|
|
||
| DefId drop_id = drop_lang_item.value (); | ||
|
|
||
| auto candidates = Resolver::TypeBoundsProbe::Probe (ty); | ||
| for (auto &candidate : candidates) | ||
| { | ||
| Resolver::TraitReference *trait_ref = candidate.first; | ||
| if (trait_ref != nullptr && trait_ref->get_defid () == drop_id) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // Find the Drop trait, look for the drop method, and build the function call. | ||
| tree | ||
| CompileDrop::compile_drop_call (Context *ctx, Bvariable *var, | ||
| TyTy::BaseType *ty, location_t locus) | ||
| { | ||
| auto drop_lang = ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP); | ||
| if (!drop_lang.has_value ()) | ||
| return NULL_TREE; | ||
|
|
||
| Resolver::TraitReference *drop_ref = nullptr; | ||
| bool ok | ||
| = ctx->get_tyctx ()->lookup_trait_reference (drop_lang.value (), &drop_ref); | ||
| if (!ok) | ||
| return NULL_TREE; | ||
|
|
||
| HIR::PathIdentSegment segment ("drop"); | ||
| auto candidates | ||
| = Resolver::PathProbeImplTrait::Probe (ty->get_root (), segment, drop_ref); | ||
|
|
||
| rust_assert (candidates.size () == 1); | ||
|
|
||
| auto &candidate = *candidates.begin (); | ||
| rust_assert (candidate.is_impl_candidate ()); | ||
| rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF); | ||
|
|
||
| auto *fn_type = static_cast<TyTy::FnType *> (candidate.ty); | ||
| tree fn_addr | ||
| = CompileInherentImplItem::Compile (candidate.item.impl.impl_item, ctx, | ||
| fn_type, locus); | ||
|
|
||
| tree var_expr = Backend::var_expression (var, locus); | ||
| tree var_addr = HIRCompileBase::address_expression (var_expr, locus); | ||
|
|
||
| return Backend::call_expression (fn_addr, {var_addr}, nullptr, locus); | ||
| } | ||
|
|
||
| void | ||
| CompileDrop::emit_current_scope_drop_calls (Context *ctx) | ||
| { | ||
| auto &drop_candidates = ctx->peek_block_drop_candidates (); | ||
|
|
||
| for (auto it = drop_candidates.rbegin (); it != drop_candidates.rend (); ++it) | ||
| { | ||
| TyTy::BaseType *ty = nullptr; | ||
| Bvariable *var = nullptr; | ||
|
|
||
| bool ok = ctx->get_tyctx ()->lookup_type (it->hirid, &ty); | ||
| rust_assert (ok); | ||
|
|
||
| ok = ctx->lookup_var_decl (it->hirid, &var); | ||
| rust_assert (ok); | ||
|
|
||
| tree drop_call = CompileDrop::compile_drop_call (ctx, var, ty, it->locus); | ||
| if (drop_call != NULL_TREE) | ||
| ctx->add_statement (convert_to_void (drop_call, ICV_STATEMENT)); | ||
| } | ||
| } | ||
|
|
||
| } // namespace Compile | ||
| } // namespace Rust | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (C) 2026 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #ifndef RUST_COMPILE_DROP_H | ||
| #define RUST_COMPILE_DROP_H | ||
|
|
||
| #include "rust-compile-context.h" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reworked this to avoid the forward declarations.
Does this look good to you?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it does! |
||
|
|
||
| namespace Rust { | ||
| namespace Compile { | ||
|
|
||
| class CompileDrop | ||
| { | ||
| public: | ||
| static bool type_has_drop_impl (Context *ctx, TyTy::BaseType *ty); | ||
|
|
||
| static tree compile_drop_call (Context *ctx, Bvariable *var, | ||
| TyTy::BaseType *ty, location_t locus); | ||
|
|
||
| static void emit_current_scope_drop_calls (Context *ctx); | ||
|
Comment on lines
+30
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are always passing the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that makes sense. I’ll keep the current approach for now and consider this in a follow-up cleanup PR. |
||
| }; | ||
|
|
||
| } // namespace Compile | ||
| } // namespace Rust | ||
|
|
||
| #endif // RUST_COMPILE_DROP_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,18 @@ | |
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-compile-pattern.h" | ||
| #include "print-tree.h" | ||
| #include "rust-compile-drop.h" | ||
| #include "rust-compile-expr.h" | ||
| #include "rust-compile-resolve-path.h" | ||
| #include "rust-constexpr.h" | ||
| #include "rust-compile-type.h" | ||
| #include "print-tree.h" | ||
| #include "rust-constexpr.h" | ||
| #include "rust-diagnostics.h" | ||
| #include "rust-hir-pattern-abstract.h" | ||
| #include "rust-hir-pattern.h" | ||
| #include "rust-hir-trait-reference.h" | ||
| #include "rust-hir-type-bounds.h" | ||
| #include "rust-lang-item.h" | ||
| #include "rust-system.h" | ||
| #include "rust-tyty.h" | ||
| #include "tree.h" | ||
|
|
@@ -1343,6 +1347,24 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern) | |
| auto s = Backend::init_statement (fnctx.fndecl, var, init_expr); | ||
| ctx->add_statement (s); | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now I only handle the simple case like:
I skipped more complex patterns for this first version, like ref and subpatterns.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then you might want to add a placeholder error message for now: else if (type_has_drop_impl (ctx, ty))
rust_sorry_at (pattern.get_locus (), "drop trait not supported for subpatterns & ref patterns");
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing this out. I’ll add a |
||
| TyTy::BaseType *drop_ty = ty; | ||
| if (pattern.get_is_ref ()) | ||
| { | ||
| auto ref_ty = ty->try_as<TyTy::ReferenceType> (); | ||
| rust_assert (ref_ty != nullptr); | ||
| drop_ty = ref_ty->get_base (); | ||
| } | ||
|
|
||
| if (!CompileDrop::type_has_drop_impl (ctx, drop_ty)) | ||
| return; | ||
|
|
||
| if (!pattern.has_subpattern () && !pattern.get_is_ref ()) | ||
| ctx->note_simple_drop_candidate (pattern.get_mappings ().get_hirid (), | ||
| pattern.get_locus ()); | ||
| else | ||
| rust_sorry_at (pattern.get_locus (), | ||
| "drop trait not supported for subpatterns and ref patterns"); | ||
| } | ||
|
|
||
| void | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #![feature(no_core)] | ||
| #![feature(lang_items)] | ||
| #![no_core] | ||
|
|
||
| #[lang = "sized"] | ||
| pub trait Sized {} | ||
|
|
||
| #[lang = "drop"] | ||
| pub trait Drop { | ||
| fn drop (&mut self); | ||
| } | ||
|
|
||
| struct Droppable; | ||
|
|
||
| impl Drop for Droppable { | ||
| fn drop(&mut self) {} | ||
| } | ||
|
|
||
| fn main() { | ||
| { | ||
| let ref _x = Droppable; // { dg-message "sorry, unimplemented: drop trait not supported for subpatterns and ref patterns" } | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine for now, but we should think about what future APIs will look like and start building towards that in a later PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll keep the current approach for now and think about what future APIs should look like in a follow-up PR.
Thank you!