Skip to content

Commit d1e6416

Browse files
Polygonalrphilberty
authored andcommitted
gccrs: Implement let statement support for IdentifierPattern's subpatterns
Trimmed GIMPLE code gen for let-identifierpattern-subpattern.rs ... RUSTTMP.1.__0 = 0; RUSTTMP.1.__1 = 2; RUSTTMP.1.__2 = 3; bar = RUSTTMP.1.__0; RUSTTMP.2 = RUSTTMP.1.__1; RUSTTMP.3 = RUSTTMP.1.__2; foo.__0 = 0; foo.__1 = 2; foo.__2 = 3; ret = 1; RUSTTMP.5 = foo; _1 = RUSTTMP.5.__0; _2 = _1 == 0; _3 = RUSTTMP.5.__1; _4 = _3 == 2; _5 = _2 & _4; _6 = RUSTTMP.5.__2; _7 = _6 == 3; _8 = _5 & _7; if (_8 != 0) goto <D.143>; else goto <D.144>; <D.143>: { { ret = bar; } goto <D.137>; } ... gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc(CompilePatternLet::visit(IdentifierPattern)): Add support for subpatterns. * backend/rust-compile-var-decl.h(CompileVarDecl::visit(IdentifierPattern)): Implement compilation for subpatterns. Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent c8fcb33 commit d1e6416

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,11 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
992992
}
993993
else
994994
{
995+
if (pattern.has_subpattern ())
996+
{
997+
CompilePatternLet::Compile (&pattern.get_subpattern (), init_expr, ty,
998+
rval_locus, ctx);
999+
}
9951000
auto s = Backend::init_statement (fnctx.fndecl, var, init_expr);
9961001
ctx->add_statement (s);
9971002
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
6464
ctx->insert_var_decl (stmt_id, var);
6565

6666
vars.push_back (var);
67+
68+
if (pattern.has_subpattern ())
69+
{
70+
auto subpattern_vars
71+
= CompileVarDecl::compile (fndecl, translated_type,
72+
&pattern.get_subpattern (), ctx);
73+
vars.insert (vars.end (), subpattern_vars.begin (),
74+
subpattern_vars.end ());
75+
}
6776
}
6877

6978
void visit (HIR::TuplePattern &pattern) override
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() -> i32 {
2+
let foo @ (bar, _, _) = (0, 2, 3);
3+
let mut ret = 1;
4+
5+
match foo {
6+
(0, 2, 3) => { ret = bar },
7+
_ => {}
8+
}
9+
10+
ret
11+
}

0 commit comments

Comments
 (0)