Skip to content

gccrs: Implement compilation of IdentifierPattern's subpattern bindings #3822

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 3 commits into from
Jun 15, 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
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2485,7 +2485,7 @@ TokenCollector::visit (IdentifierPattern &pattern)
if (pattern.has_subpattern ())
{
push (Rust::Token::make (PATTERN_BIND, UNDEF_LOCATION));
visit (pattern.get_pattern_to_bind ());
visit (pattern.get_subpattern ());
}
}

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ void
DefaultASTVisitor::visit (AST::IdentifierPattern &pattern)
{
if (pattern.has_subpattern ())
visit (pattern.get_pattern_to_bind ());
visit (pattern.get_subpattern ());
}

void
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ class IdentifierPattern : public Pattern

void accept_vis (ASTVisitor &vis) override;

// TODO: is this better? Or is a "vis_pattern" better?
Pattern &get_pattern_to_bind ()
Pattern &get_subpattern ()
{
rust_assert (has_subpattern ());
return *subpattern;
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,12 @@ CompilePatternBindings::visit (HIR::ReferencePattern &pattern)
void
CompilePatternBindings::visit (HIR::IdentifierPattern &pattern)
{
if (pattern.has_subpattern ())
{
CompilePatternBindings::Compile (pattern.get_subpattern (),
match_scrutinee_expr, ctx);
}

if (!pattern.get_is_ref ())
{
ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ CfgStrip::visit (AST::IdentifierPattern &pattern)

AST::DefaultASTVisitor::visit (pattern);

auto &sub_pattern = pattern.get_pattern_to_bind ();
auto &sub_pattern = pattern.get_subpattern ();
if (sub_pattern.is_marked_for_strip ())
rust_error_at (sub_pattern.get_locus (),
"cannot strip pattern in this position");
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/hir/rust-ast-lower-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ASTLoweringPattern::visit (AST::IdentifierPattern &pattern)
if (pattern.has_subpattern ())
{
subpattern = std::unique_ptr<Pattern> (
ASTLoweringPattern::translate (pattern.get_pattern_to_bind ()));
ASTLoweringPattern::translate (pattern.get_subpattern ()));
}
translated
= new HIR::IdentifierPattern (mapping, pattern.get_ident (),
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ PatternDeclaration::go (AST::Pattern &pattern, Rib::ItemType type,
void
PatternDeclaration::visit (AST::IdentifierPattern &pattern)
{
if (pattern.has_subpattern ())
{
pattern.get_subpattern ().accept_vis (*this);
}

Mutability mut = pattern.get_is_mut () ? Mutability::Mut : Mutability::Imm;
add_new_binding (pattern.get_ident (), pattern.get_node_id (),
BindingTypeInfo (mut, pattern.get_is_ref (),
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/resolve/rust-late-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ visit_identifier_as_pattern (NameResolutionContext &ctx,
void
Late::visit (AST::IdentifierPattern &identifier)
{
if (identifier.has_subpattern ())
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need a condition check over this ? Calling DefaultResolver::visit(identifier) directly would help future changes and let the condition in the default visitor.

{
DefaultResolver::visit (identifier.get_subpattern ());
}

visit_identifier_as_pattern (ctx, identifier.get_ident (),
identifier.get_locus (),
identifier.get_node_id ());
Expand Down
12 changes: 12 additions & 0 deletions gcc/testsuite/rust/compile/match-identifierpattern-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
enum Foo {
I(i32),
}

fn main() {
let x = Foo::I(1);

match x {
a @ Foo::I(b) => {},
_ => {},
};
}
15 changes: 15 additions & 0 deletions gcc/testsuite/rust/execute/match-identifierpattern-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enum Foo {
I(i32),
}

fn main() -> i32 {
let x = Foo::I(0);
let ret = 1;

match x {
_ @ Foo::I(b) => { ret = b },
_ => {},
};

ret
}