Skip to content
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

typecheck: Add basic typechecking for ReferenceType #1971

Merged
merged 1 commit into from
Mar 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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",
goar5670 marked this conversation as resolved.
Show resolved Hide resolved
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" }