Skip to content

Commit

Permalink
gccrs: refactored field error function
Browse files Browse the repository at this point in the history
Refactored field error function into one funciton
Fixes #2336

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-pattern.cc (emit_invalid_field_error): Refactored Field
	Error Function
	(TypeCheckPattern::visit): called emit_invalid_field_error function

Signed-off-by: Muhammad Mahad <[email protected]>
  • Loading branch information
MahadMuhammad committed Jun 29, 2023
1 parent 2ddf542 commit 41f5344
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern)
}
}

void
emit_invalid_field_error (Location loc, Rust::TyTy::VariantDef *variant,
const std::string &name)
{
rust_error_at (loc, ErrorCode ("E0026"),
"variant %s does not have a field named %s",
variant->get_identifier ().c_str (), name.c_str ());
}

void
TypeCheckPattern::visit (HIR::StructPattern &pattern)
{
Expand Down Expand Up @@ -204,10 +213,8 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
if (!variant->lookup_field (ident.get_identifier ().as_string (),
&field, nullptr))
{
rust_error_at (ident.get_locus (), ErrorCode ("E0026"),
"variant %s does not have a field named %s",
variant->get_identifier ().c_str (),
ident.get_identifier ().as_string ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ().as_string ());
break;
}
named_fields.push_back (ident.get_identifier ().as_string ());
Expand All @@ -225,10 +232,8 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
if (!variant->lookup_field (ident.get_identifier ().as_string (),
&field, nullptr))
{
rust_error_at (ident.get_locus (), ErrorCode ("E0026"),
"variant %s does not have a field named %s",
variant->get_identifier ().c_str (),
ident.get_identifier ().as_string ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ().as_string ());
break;
}
named_fields.push_back (ident.get_identifier ().as_string ());
Expand Down

0 comments on commit 41f5344

Please sign in to comment.