Skip to content

Commit

Permalink
gccrs: New Error Code Framework
Browse files Browse the repository at this point in the history
Updated ErrorCode struct to enum class to enforce proper
error codes, similiar to rustc. For converting the enum
to the respective error code, I used a map and updated
make_description & make_url function accordingly and
also removes the memory leak from the previous frame-
work.
Also, added macro to safely convert the enum number
to string.

gcc/rust/ChangeLog:

	* backend/rust-compile-intrinsic.cc (Intrinsics::compile):
	Formatted according to enum class.
	* checks/errors/rust-feature-gate.cc (FeatureGate::gate): likewise.
	* checks/errors/rust-unsafe-checker.cc (check_unsafe_call): likewise.
	* hir/rust-ast-lower-base.cc (struct_field_name_exists): likewise.
	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): likewise.
	* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): likewise.
	* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go): likewise.
	(PatternDeclaration::add_new_binding): likewise.
	* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): likewise.
	* resolve/rust-ast-verify-assignee.h: likewise.
	* rust-diagnostics.cc: updated make_desc & url function for enum class.
	* rust-diagnostics.h (struct ErrorCode): removed struct to switch to enum.
	(enum class): Switched from errorcode struct to enum class.
	(XSTR): Macro for converting enum to string.
	(STR): macro Used by XSTR for converting to string.
	(ERROR_CODE): macro for appending `E` to the start.
	(TABLE_TO_MAP): macro used by map for check.
	* typecheck/rust-casts.cc (TypeCastRules::emit_cast_error):
	Formatted according to enum class.
	* typecheck/rust-hir-path-probe.h: likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit):
	likewise.
	* typecheck/rust-hir-type-check-item.cc: likewise.
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): likewise.
	(emit_invalid_field_error): likewise.
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve):
	likewise.
	* typecheck/rust-tyty-call.cc (emit_unexpected_argument_error): likewise.
	(TypeCheckCallExpr::visit): likewise.
	* typecheck/rust-tyty-subst.cc (SubstitutionRef::get_mappings_from_generic_args):
	likewise.
	* typecheck/rust-tyty.cc (BaseType::bounds_compatible): likewise.

Signed-off-by: Muhammad Mahad <[email protected]>
  • Loading branch information
MahadMuhammad committed Jul 26, 2023
1 parent ccac7ed commit 89dd433
Show file tree
Hide file tree
Showing 21 changed files with 1,067 additions and 40 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-compile-intrinsic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Intrinsics::compile (TyTy::FnType *fntype)
return it->second (ctx, fntype);

location_t locus = ctx->get_mappings ()->lookup_location (fntype->get_ref ());
rust_error_at (locus, ErrorCode ("E0093"),
rust_error_at (locus, ErrorCode::E0093,
"unrecognized intrinsic function: %<%s%>",
fntype->get_identifier ().c_str ());

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/checks/errors/rust-feature-gate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ FeatureGate::gate (Feature::Name name, Location loc,
"<https://github.com/rust-lang/rust/issues/%u> for more "
"information. add `#![feature(%s)]` to the crate attributes to "
"enable.";
rust_error_at (loc, ErrorCode ("E0658"), fmt_str, error_msg.c_str (),
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
issue, issue, feature.as_string ().c_str ());
}
else
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/checks/errors/rust-unsafe-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void
check_unsafe_call (HIR::Function *fn, location_t locus, const std::string &kind)
{
if (fn->get_qualifiers ().is_unsafe ())
rust_error_at (locus, ErrorCode ("E0133"),
rust_error_at (locus, ErrorCode::E0133,
"call to unsafe %s requires unsafe function or block",
kind.c_str ());
}
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,7 @@ struct_field_name_exists (std::vector<HIR::StructField> &fields,
{
rich_location r (line_table, new_field.get_locus ());
r.add_range (field.get_locus ());
rust_error_at (r, ErrorCode ("E0124"),
"field %qs is already declared",
rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
field.get_field_name ().as_string ().c_str ());
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ ResolveExpr::visit (AST::IdentifierExpr &expr)
}
else
{
rust_error_at (expr.get_locus (), ErrorCode ("E0425"),
rust_error_at (expr.get_locus (), ErrorCode::E0425,
"cannot find value %qs in this scope",
expr.as_string ().c_str ());
}
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/resolve/rust-ast-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
bool in_middle_of_path = i > 0;
if (in_middle_of_path && segment.is_lower_self_seg ())
{
rust_error_at (segment.get_locus (), ErrorCode ("E0433"),
rust_error_at (segment.get_locus (), ErrorCode::E0433,
"failed to resolve: %<%s%> in paths can only be used "
"in start position",
segment.as_string ().c_str ());
Expand Down Expand Up @@ -206,7 +206,7 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
}
else if (is_first_segment)
{
rust_error_at (segment.get_locus (), ErrorCode ("E0433"),
rust_error_at (segment.get_locus (), ErrorCode::E0433,
"Cannot find path %<%s%> in this scope",
segment.as_string ().c_str ());
return UNKNOWN_NODEID;
Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/resolve/rust-ast-resolve-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ PatternDeclaration::go (AST::Pattern *pattern, Rib::ItemType type,
auto ident = map_entry.first; // key
auto info = map_entry.second; // value

rust_error_at (info.get_locus (), ErrorCode ("E0408"),
rust_error_at (info.get_locus (), ErrorCode::E0408,
"variable '%s' is not bound in all patterns",
ident.as_string ().c_str ());
}
Expand All @@ -53,7 +53,7 @@ PatternDeclaration::go (AST::Pattern *pattern, Rib::ItemType type,
auto info = map_entry.second; // value

rust_error_at (
info.get_locus (), ErrorCode ("E0409"),
info.get_locus (), ErrorCode::E0409,
"variable '%s' is bound inconsistently across pattern alternatives",
ident.as_string ().c_str ());
}
Expand Down Expand Up @@ -278,15 +278,15 @@ PatternDeclaration::add_new_binding (Identifier ident, NodeId node_id,
{
if (type == Rib::ItemType::Param)
{
rust_error_at (info.get_locus (), ErrorCode ("E0415"),
rust_error_at (info.get_locus (), ErrorCode::E0415,
"identifier '%s' is bound more than once in the "
"same parameter list",
ident.as_string ().c_str ());
}
else
{
rust_error_at (
info.get_locus (), ErrorCode ("E0416"),
info.get_locus (), ErrorCode::E0416,
"identifier '%s' is bound more than once in the same pattern",
ident.as_string ().c_str ());
}
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
bool in_middle_of_path = i > 0;
if (in_middle_of_path && segment->is_lower_self_seg ())
{
rust_error_at (segment->get_locus (), ErrorCode ("E0433"),
rust_error_at (segment->get_locus (), ErrorCode::E0433,
"failed to resolve: %<%s%> in paths can only be used "
"in start position",
segment->as_string ().c_str ());
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-verify-assignee.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VerifyAsignee : public ResolverBase
VerifyAsignee checker;
assignee->accept_vis (checker);
if (!checker.ok)
rust_error_at (assignee->get_locus (), ErrorCode ("E0070"),
rust_error_at (assignee->get_locus (), ErrorCode::E0070,
"invalid left-hand side of assignment");
return checker.ok;
}
Expand Down
9 changes: 6 additions & 3 deletions gcc/rust/rust-diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,16 @@ class rust_error_code_rule : public diagnostic_metadata::rule

char *make_description () const final override
{
return xstrdup (m_code.m_str);
std::string errstring (ErrorCodeString.at (m_code));
char *desc = new char[errstring.size () + 1];
std::strcpy (desc, errstring.c_str ());
return desc;
}

char *make_url () const final override
{
return concat ("https://doc.rust-lang.org/error-index.html#", m_code.m_str,
NULL);
return concat ("https://doc.rust-lang.org/error-index.html#",
ErrorCodeString.at (m_code), NULL);
}

private:
Expand Down
Loading

0 comments on commit 89dd433

Please sign in to comment.