Skip to content

Commit

Permalink
Refactored Field Error Function
Browse files Browse the repository at this point in the history
Refactored field error function into one function,
to make easier error emission. Fixes Rust-GCC#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 <mahadtxt@gmail.com>
  • Loading branch information
MahadMuhammad committed Jun 28, 2023
1 parent 2983beb commit 8e7b2fc
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 char *message)
{
rust_error_at (loc, ErrorCode ("E0026"),
"variant %s does not have a field named %s",
variant->get_identifier ().c_str (), message);
}

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 (), &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 ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ().c_str ());
break;
}
named_fields.push_back (ident.get_identifier ());
Expand All @@ -225,10 +232,8 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
if (!variant->lookup_field (ident.get_identifier (), &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 ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ().c_str ());
break;
}
named_fields.push_back (ident.get_identifier ());
Expand Down

0 comments on commit 8e7b2fc

Please sign in to comment.