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

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
  • Loading branch information
goar5670 committed Mar 6, 2023
1 parent 914b938 commit 029b09c
Show file tree
Hide file tree
Showing 3 changed files with 20 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
11 changes: 9 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,15 @@ 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

0 comments on commit 029b09c

Please sign in to comment.