Skip to content

Commit 8fb83bb

Browse files
Polygonalrphilberty
authored andcommitted
Implement missing var decl case for TuplePatternItemsHasRest
GIMPLE dump from compiling issue-3930.rs: ... const i32 b; const i32 a; D.114.__0 = 2; D.114.__1 = 3; RUSTTMP.1 = D.114; a = RUSTTMP.1.__0; b = RUSTTMP.1.__1; ... gcc/rust/ChangeLog: * backend/rust-compile-var-decl.h (CompileVarDecl::visit(TuplePattern)): Implement variable declaration bindings for tuple patterns with rest pattern (i.e. TuplePatternItemsHasRest). Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent 3e52769 commit 8fb83bb

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

gcc/rust/backend/rust-compile-var-decl.h

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,66 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
7777

7878
void visit (HIR::TuplePattern &pattern) override
7979
{
80+
rust_assert (TREE_CODE (translated_type) == RECORD_TYPE);
8081
switch (pattern.get_items ().get_item_type ())
8182
{
8283
case HIR::TuplePatternItems::ItemType::NO_REST:
8384
{
84-
rust_assert (TREE_CODE (translated_type) == RECORD_TYPE);
85-
auto &items = static_cast<HIR::TuplePatternItemsNoRest &> (
85+
auto &items_no_rest = static_cast<HIR::TuplePatternItemsNoRest &> (
8686
pattern.get_items ());
8787

88-
size_t offs = 0;
89-
for (auto &sub : items.get_patterns ())
88+
tree field = TYPE_FIELDS (translated_type);
89+
for (auto &sub : items_no_rest.get_patterns ())
9090
{
91-
tree sub_ty = error_mark_node;
92-
tree field = TYPE_FIELDS (translated_type);
93-
for (size_t i = 0; i < offs; i++)
91+
gcc_assert (field != NULL_TREE);
92+
tree sub_ty = TREE_TYPE (field);
93+
CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
94+
field = DECL_CHAIN (field);
95+
}
96+
}
97+
break;
98+
99+
case HIR::TuplePatternItems::ItemType::HAS_REST:
100+
{
101+
auto &items_has_rest = static_cast<HIR::TuplePatternItemsHasRest &> (
102+
pattern.get_items ());
103+
104+
// count total fields in translated_type
105+
size_t total_fields = 0;
106+
for (tree t = TYPE_FIELDS (translated_type); t; t = DECL_CHAIN (t))
107+
{
108+
total_fields++;
109+
}
110+
111+
// process lower patterns
112+
tree field = TYPE_FIELDS (translated_type);
113+
for (auto &sub : items_has_rest.get_lower_patterns ())
114+
{
115+
gcc_assert (field != NULL_TREE);
116+
tree sub_ty = TREE_TYPE (field);
117+
CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
118+
field = DECL_CHAIN (field);
119+
}
120+
121+
// process upper patterns
122+
if (!items_has_rest.get_upper_patterns ().empty ())
123+
{
124+
size_t upper_start
125+
= total_fields - items_has_rest.get_upper_patterns ().size ();
126+
field = TYPE_FIELDS (translated_type);
127+
for (size_t i = 0; i < upper_start; i++)
94128
{
95129
field = DECL_CHAIN (field);
96130
gcc_assert (field != NULL_TREE);
97131
}
98-
sub_ty = TREE_TYPE (field);
99-
CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
100-
offs++;
132+
133+
for (auto &sub : items_has_rest.get_upper_patterns ())
134+
{
135+
gcc_assert (field != NULL_TREE);
136+
tree sub_ty = TREE_TYPE (field);
137+
CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
138+
field = DECL_CHAIN (field);
139+
}
101140
}
102141
}
103142
break;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// { dg-additional-options "-w" }
2+
fn main() {
3+
let (a, .., b) = (2, 3);
4+
}

0 commit comments

Comments
 (0)