Skip to content

Commit

Permalink
typecheck: Add basic typechecking for ReferenceType
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:
	* hir/tree/rust-hir-pattern.h (HIR::ReferencePattern):
	added get_referenced_pattern function.
	* resolve/rust-ast-resolve-pattern.h
	(Resolve::PatternDeclaration): add visit function for
	AST::ReferencePattern
	* typecheck/rust-hir-type-check-pattern.cc
	(Resolver::TypeCheckPattern): add visit function for
	HIR::ReferencePattern

gcc/testsuite/ChangeLog:
	* rust/compile/ref_pattern_fn_param.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
  • Loading branch information
goar5670 committed Mar 7, 2023
1 parent 94cbaa2 commit 27ae0fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions gcc/rust/hir/tree/rust-hir-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ class ReferencePattern : public Pattern
return PatternType::REFERENCE;
}

std::unique_ptr<Pattern> &get_referenced_pattern ()
{
rust_assert (pattern != nullptr);
return pattern;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class PatternDeclaration : public ResolverBase
pattern.get_pattern_in_parens ()->accept_vis (*this);
}

void visit (AST::ReferencePattern &pattern) override
{
pattern.get_referenced_pattern ()->accept_vis (*this);
}

// cases in a match expression
void visit (AST::PathInExpression &pattern) override;

Expand Down
9 changes: 7 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ TypeCheckPattern::visit (HIR::QualifiedPathInExpression &pattern)
void
TypeCheckPattern::visit (HIR::ReferencePattern &pattern)
{
rust_sorry_at (pattern.get_locus (),
"type checking qualified path patterns not supported");
if (parent->get_kind () != TyTy::TypeKind::REF)
rust_error_at (pattern.get_locus (), "expected %s, found reference",
parent->as_string ().c_str ());

TyTy::ReferenceType *ref_ty_ty = static_cast<TyTy::ReferenceType *> (parent);
TypeCheckPattern::Resolve (pattern.get_referenced_pattern ().get (),
ref_ty_ty->get_base ());
}

void
Expand Down
1 change: 1 addition & 0 deletions gcc/testsuite/rust/compile/ref_pattern_fn_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f(&b: i32) {} // { dg-error "expected i32, found reference" }

0 comments on commit 27ae0fb

Please sign in to comment.