Skip to content

Commit 5d16ff4

Browse files
Lishin1215philberty
authored andcommitted
gccrs: fix ICE in convert_tree for tuple destructuring with ref
gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternLet::visit): Handle tuple destructuring containing by-ref. gcc/testsuite/ChangeLog: * rust/compile/issue-3645.rs: New test. Signed-off-by: lishin <[email protected]>
1 parent 051429a commit 5d16ff4

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

gcc/rust/backend/rust-compile-pattern.cc

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,11 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
11651165
rust_assert (
11661166
ctx->lookup_var_decl (pattern.get_mappings ().get_hirid (), &var));
11671167

1168+
if (pattern.get_is_ref ())
1169+
{
1170+
init_expr = address_expression (init_expr, EXPR_LOCATION (init_expr));
1171+
}
1172+
11681173
auto fnctx = ctx->peek_fn ();
11691174
if (ty->is_unit ())
11701175
{
@@ -1204,11 +1209,54 @@ CompilePatternLet::visit (HIR::TuplePattern &pattern)
12041209
{
12051210
rust_assert (pattern.has_tuple_pattern_items ());
12061211

1207-
tree tuple_type = TyTyResolveCompile::compile (ctx, ty);
1212+
bool has_by_ref = false;
1213+
auto check_refs
1214+
= [] (const std::vector<std::unique_ptr<HIR::Pattern>> &patterns) {
1215+
for (const auto &sub : patterns)
1216+
{
1217+
switch (sub->get_pattern_type ())
1218+
{
1219+
case HIR::Pattern::PatternType::IDENTIFIER:
1220+
{
1221+
auto id = static_cast<HIR::IdentifierPattern *> (sub.get ());
1222+
if (id->get_is_ref ())
1223+
return true;
1224+
break;
1225+
}
1226+
case HIR::Pattern::PatternType::REFERENCE:
1227+
return true;
1228+
default:
1229+
break;
1230+
}
1231+
}
1232+
return false;
1233+
};
1234+
switch (pattern.get_items ().get_item_type ())
1235+
{
1236+
case HIR::TuplePatternItems::ItemType::NO_REST:
1237+
{
1238+
auto &items
1239+
= static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
1240+
has_by_ref = check_refs (items.get_patterns ());
1241+
break;
1242+
}
1243+
case HIR::TuplePatternItems::ItemType::HAS_REST:
1244+
{
1245+
auto &items
1246+
= static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
1247+
has_by_ref = check_refs (items.get_lower_patterns ())
1248+
|| check_refs (items.get_upper_patterns ());
1249+
break;
1250+
}
1251+
default:
1252+
break;
1253+
}
1254+
1255+
tree rhs_tuple_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
12081256
tree init_stmt;
12091257
Bvariable *tmp_var
12101258
= Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
1211-
tuple_type, init_expr, false,
1259+
rhs_tuple_type, init_expr, has_by_ref,
12121260
pattern.get_locus (), &init_stmt);
12131261
tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
12141262
ctx->add_statement (init_stmt);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// { dg-warning "unused name 'y'" "" { target *-*-* } 5 }
2+
// { dg-warning "unused name 'z'" "" { target *-*-* } 5 }
3+
4+
fn main() {
5+
let (ref y,z) = (1i32, 2u32);
6+
}

0 commit comments

Comments
 (0)