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 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 29, 2023
1 parent 2ddf542 commit f2fac89
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 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 @@ -201,16 +210,14 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
= static_cast<HIR::StructPatternFieldIdentPat &> (*field.get ());

TyTy::StructFieldType *field = nullptr;
if (!variant->lookup_field (ident.get_identifier ().as_string (),
&field, nullptr))
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 ().as_string ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ());
break;
}
named_fields.push_back (ident.get_identifier ().as_string ());
named_fields.push_back (ident.get_identifier ());

TyTy::BaseType *fty = field->get_field_type ();
TypeCheckPattern::Resolve (ident.get_pattern ().get (), fty);
Expand All @@ -222,16 +229,14 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
= static_cast<HIR::StructPatternFieldIdent &> (*field.get ());

TyTy::StructFieldType *field = nullptr;
if (!variant->lookup_field (ident.get_identifier ().as_string (),
&field, nullptr))
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 ().as_string ().c_str ());
emit_invalid_field_error (ident.get_locus (), variant,
ident.get_identifier ());
break;
}
named_fields.push_back (ident.get_identifier ().as_string ());
named_fields.push_back (ident.get_identifier ());

// setup the type on this pattern
TyTy::BaseType *fty = field->get_field_type ();
Expand Down

0 comments on commit f2fac89

Please sign in to comment.