Skip to content

gccrs: fix ICE in convert_tree for tuple destructuring with ref #4059

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

Merged
merged 1 commit into from
Aug 17, 2025
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
52 changes: 50 additions & 2 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,11 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
rust_assert (
ctx->lookup_var_decl (pattern.get_mappings ().get_hirid (), &var));

if (pattern.get_is_ref ())
{
init_expr = address_expression (init_expr, EXPR_LOCATION (init_expr));
}

auto fnctx = ctx->peek_fn ();
if (ty->is_unit ())
{
Expand Down Expand Up @@ -1013,11 +1018,54 @@ CompilePatternLet::visit (HIR::TuplePattern &pattern)
{
rust_assert (pattern.has_tuple_pattern_items ());

tree tuple_type = TyTyResolveCompile::compile (ctx, ty);
bool has_by_ref = false;
auto check_refs
= [] (const std::vector<std::unique_ptr<HIR::Pattern>> &patterns) {
for (const auto &sub : patterns)
{
switch (sub->get_pattern_type ())
{
case HIR::Pattern::PatternType::IDENTIFIER:
{
auto id = static_cast<HIR::IdentifierPattern *> (sub.get ());
if (id->get_is_ref ())
return true;
break;
}
case HIR::Pattern::PatternType::REFERENCE:
return true;
default:
break;
}
}
return false;
};
switch (pattern.get_items ().get_item_type ())
{
case HIR::TuplePatternItems::ItemType::NO_REST:
{
auto &items
= static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
has_by_ref = check_refs (items.get_patterns ());
break;
}
case HIR::TuplePatternItems::ItemType::HAS_REST:
{
auto &items
= static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
has_by_ref = check_refs (items.get_lower_patterns ())
|| check_refs (items.get_upper_patterns ());
break;
}
default:
break;
}

tree rhs_tuple_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
tree init_stmt;
Bvariable *tmp_var
= Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
tuple_type, init_expr, false,
rhs_tuple_type, init_expr, has_by_ref,
pattern.get_locus (), &init_stmt);
tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
ctx->add_statement (init_stmt);
Expand Down
6 changes: 6 additions & 0 deletions gcc/testsuite/rust/compile/issue-3645.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// { dg-warning "unused name 'y'" "" { target *-*-* } 5 }
// { dg-warning "unused name 'z'" "" { target *-*-* } 5 }

fn main() {
let (ref y,z) = (1i32, 2u32);
}
Loading