Skip to content

Commit

Permalink
gccrs: add support for ref literal patterns
Browse files Browse the repository at this point in the history
Fixes #3174

gcc/rust/ChangeLog:

	* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): make recursive
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): handle ref flag

gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude: nr2 cant handle this
	* rust/compile/issue-3174.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
  • Loading branch information
philberty committed Jan 23, 2025
1 parent b87fd67 commit 7f67cd3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
18 changes: 12 additions & 6 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ CompilePatternBindings::visit (HIR::TupleStructPattern &pattern)
tuple_field_index++,
pattern->get_locus ());

ctx->insert_pattern_binding (
pattern->get_mappings ().get_hirid (), binding);
CompilePatternBindings::Compile (*pattern, binding, ctx);
}
}
else
Expand All @@ -497,8 +496,7 @@ CompilePatternBindings::visit (HIR::TupleStructPattern &pattern)
tuple_field_index++,
pattern->get_locus ());

ctx->insert_pattern_binding (
pattern->get_mappings ().get_hirid (), binding);
CompilePatternBindings::Compile (*pattern, binding, ctx);
}
}
}
Expand Down Expand Up @@ -607,8 +605,16 @@ CompilePatternBindings::visit (HIR::ReferencePattern &pattern)
void
CompilePatternBindings::visit (HIR::IdentifierPattern &pattern)
{
ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
match_scrutinee_expr);
if (!pattern.get_is_ref ())
{
ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
match_scrutinee_expr);
return;
}

tree ref = address_expression (match_scrutinee_expr,
EXPR_LOCATION (match_scrutinee_expr));
ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (), ref);
}

void
Expand Down
13 changes: 11 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,18 @@ TypeCheckPattern::visit (HIR::RangePattern &pattern)
}

void
TypeCheckPattern::visit (HIR::IdentifierPattern &)
TypeCheckPattern::visit (HIR::IdentifierPattern &pattern)
{
infered = parent;
if (!pattern.get_is_ref ())
{
infered = parent;
return;
}

infered = new TyTy::ReferenceType (pattern.get_mappings ().get_hirid (),
TyTy::TyVar (parent->get_ref ()),
pattern.is_mut () ? Mutability::Mut
: Mutability::Imm);
}

void
Expand Down
28 changes: 28 additions & 0 deletions gcc/testsuite/rust/compile/issue-3174.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern "C" {
fn printf(s: *const i8, ...);
}

enum Option {
Some(i32),
None,
}

impl Option {
fn add(&mut self) {
match *self {
Option::Some(ref mut a) => *a += 1,
Option::None => {}
}
}
}

fn main() {
unsafe {
let mut a = Option::None;
a.add();
let _s = "%d\n\0";
let _s = _s as *const str;
let s = _s as *const i8;
printf(s, a);
}
}
1 change: 1 addition & 0 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,5 @@ derive_macro6.rs
issue-2987.rs
issue-3139-1.rs
issue-3139-3.rs
issue-3174.rs
# please don't delete the trailing newline

0 comments on commit 7f67cd3

Please sign in to comment.