Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ GRS_OBJS = \
rust/rust-compile-struct-field-expr.o \
rust/rust-constexpr.o \
rust/rust-compile-base.o \
rust/rust-compile-drop.o \
rust/rust-tree.o \
rust/rust-compile-context.o \
rust/rust-export-metadata.o \
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "rust-abi.h"
#include "rust-compile-stmt.h"
#include "rust-compile-expr.h"
#include "rust-compile-drop.h"
#include "rust-compile-fnparam.h"
#include "rust-compile-var-decl.h"
#include "rust-compile-type.h"
Expand Down Expand Up @@ -634,6 +635,8 @@ HIRCompileBase::compile_function_body (tree fndecl,
return_value = coercion_site (id, return_value, actual, expected,
lvalue_locus, rvalue_locus);

CompileDrop::emit_current_scope_drop_calls (ctx);

tree return_stmt
= Backend::return_statement (fndecl, return_value, locus);
ctx->add_statement (return_stmt);
Expand All @@ -656,6 +659,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
// errors should have occurred
location_t locus = function_body.get_locus ();
tree return_value = unit_expression (locus);
CompileDrop::emit_current_scope_drop_calls (ctx);
tree return_stmt
= Backend::return_statement (fndecl, return_value, locus);
ctx->add_statement (return_stmt);
Expand Down
4 changes: 3 additions & 1 deletion gcc/rust/backend/rust-compile-block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
// <http://www.gnu.org/licenses/>.

#include "rust-compile-block.h"
#include "rust-compile-stmt.h"
#include "rust-compile-drop.h"
#include "rust-compile-expr.h"
#include "rust-compile-stmt.h"
#include "rust-hir-expr.h"

namespace Rust {
Expand Down Expand Up @@ -84,6 +85,7 @@ CompileBlock::visit (HIR::BlockExpr &expr)
expr.get_locus ());
ctx->add_statement (assignment);
}
CompileDrop::emit_current_scope_drop_calls (ctx);

ctx->pop_block ();
translated = new_block;
Expand Down
18 changes: 18 additions & 0 deletions gcc/rust/backend/rust-compile-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define RUST_COMPILE_CONTEXT

#include "rust-system.h"
#include "rust-compile-drop-candidate.h"
#include "rust-hir-map.h"
#include "rust-name-resolver.h"
#include "rust-hir-type-check.h"
Expand Down Expand Up @@ -101,6 +102,7 @@ class Context
{
scope_stack.push_back (scope);
statements.push_back ({});
block_drop_candidates.emplace_back ();
}

tree pop_block ()
Expand All @@ -111,6 +113,9 @@ class Context
auto stmts = statements.back ();
statements.pop_back ();

rust_assert (!block_drop_candidates.empty ());
block_drop_candidates.pop_back ();

Backend::block_add_statements (block, stmts);

return block;
Expand All @@ -131,6 +136,18 @@ class Context

void add_statement (tree stmt) { statements.back ().push_back (stmt); }

std::vector<DropCandidate> &peek_block_drop_candidates ()
{
rust_assert (!block_drop_candidates.empty ());
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.

Copy link
Copy Markdown
Member

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

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'll keep the current approach for now and think about what future APIs should look like in a follow-up PR.
Thank you!

{
rust_assert (!block_drop_candidates.empty ());
block_drop_candidates.back ().emplace_back (hirid, locus);
}

void insert_var_decl (HirId id, ::Bvariable *decl)
{
compiled_var_decls[id] = decl;
Expand Down Expand Up @@ -419,6 +436,7 @@ class Context
std::map<HirId, tree> compiled_labels;
std::vector<::std::vector<tree>> statements;
std::vector<tree> scope_stack;
std::vector<::std::vector<DropCandidate>> block_drop_candidates;
std::vector<::Bvariable *> loop_value_stack;
std::vector<tree> loop_begin_labels;
std::map<DefId, std::vector<std::pair<const TyTy::BaseType *, tree>>>
Expand Down
40 changes: 40 additions & 0 deletions gcc/rust/backend/rust-compile-drop-candidate.h
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
113 changes: 113 additions & 0 deletions gcc/rust/backend/rust-compile-drop.cc
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"
Comment thread
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
41 changes: 41 additions & 0 deletions gcc/rust/backend/rust-compile-drop.h
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"

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 reworked this to avoid the forward declarations. rust-compile-drop.h now includes the required context header directly.

DropCandidate now lives in rust-compile-drop-candidate.h, so it remains in a drop-related file, while rust-compile-context.h only includes that small state type instead of the full rust-compile-drop.h header.

Does this look good to you?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since we are always passing the Context *ctx argument in each of these functions, I think it would be worth in a later PR to keep it as a class member and reuse it. But don't do it now as this is fine for now 👍

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.

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
26 changes: 24 additions & 2 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1343,6 +1347,24 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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");

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.

Thank you for pointing this out. I’ll add a rust_sorry_at for Drop types with ref bindings and subpatterns.

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
Expand Down
23 changes: 23 additions & 0 deletions gcc/testsuite/rust/compile/drop-ref-pattern.rs
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" }
}
}
Loading
Loading