Skip to content

Commit 0d73f08

Browse files
committed
gccrs: New Error Code Framework
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 used by map for check. (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 (TypeCheckTopLevelExternItem::visit): likewise. (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]>
1 parent 27f5146 commit 0d73f08

21 files changed

+1068
-42
lines changed

gcc/rust/backend/rust-compile-intrinsic.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Intrinsics::compile (TyTy::FnType *fntype)
228228
return it->second (ctx, fntype);
229229

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

gcc/rust/checks/errors/rust-feature-gate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ FeatureGate::gate (Feature::Name name, Location loc,
7979
"<https://github.com/rust-lang/rust/issues/%u> for more "
8080
"information. add `#![feature(%s)]` to the crate attributes to "
8181
"enable.";
82-
rust_error_at (loc, ErrorCode ("E0658"), fmt_str, error_msg.c_str (),
82+
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
8383
issue, issue, feature.as_string ().c_str ());
8484
}
8585
else

gcc/rust/checks/errors/rust-unsafe-checker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void
8686
check_unsafe_call (HIR::Function *fn, location_t locus, const std::string &kind)
8787
{
8888
if (fn->get_qualifiers ().is_unsafe ())
89-
rust_error_at (locus, ErrorCode ("E0133"),
89+
rust_error_at (locus, ErrorCode::E0133,
9090
"call to unsafe %s requires unsafe function or block",
9191
kind.c_str ());
9292
}

gcc/rust/hir/rust-ast-lower-base.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ struct_field_name_exists (std::vector<HIR::StructField> &fields,
681681
{
682682
rich_location r (line_table, new_field.get_locus ());
683683
r.add_range (field.get_locus ());
684-
rust_error_at (r, ErrorCode ("E0124"),
685-
"field %qs is already declared",
684+
rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
686685
field.get_field_name ().as_string ().c_str ());
687686
return true;
688687
}

gcc/rust/resolve/rust-ast-resolve-expr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ ResolveExpr::visit (AST::IdentifierExpr &expr)
173173
}
174174
else
175175
{
176-
rust_error_at (expr.get_locus (), ErrorCode ("E0425"),
176+
rust_error_at (expr.get_locus (), ErrorCode::E0425,
177177
"cannot find value %qs in this scope",
178178
expr.as_string ().c_str ());
179179
}

gcc/rust/resolve/rust-ast-resolve-path.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
6262
bool in_middle_of_path = i > 0;
6363
if (in_middle_of_path && segment.is_lower_self_seg ())
6464
{
65-
rust_error_at (segment.get_locus (), ErrorCode ("E0433"),
65+
rust_error_at (segment.get_locus (), ErrorCode::E0433,
6666
"failed to resolve: %<%s%> in paths can only be used "
6767
"in start position",
6868
segment.as_string ().c_str ());
@@ -206,7 +206,7 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
206206
}
207207
else if (is_first_segment)
208208
{
209-
rust_error_at (segment.get_locus (), ErrorCode ("E0433"),
209+
rust_error_at (segment.get_locus (), ErrorCode::E0433,
210210
"Cannot find path %<%s%> in this scope",
211211
segment.as_string ().c_str ());
212212
return UNKNOWN_NODEID;

gcc/rust/resolve/rust-ast-resolve-pattern.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PatternDeclaration::go (AST::Pattern *pattern, Rib::ItemType type,
4242
auto ident = map_entry.first; // key
4343
auto info = map_entry.second; // value
4444

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

5555
rust_error_at (
56-
info.get_locus (), ErrorCode ("E0409"),
56+
info.get_locus (), ErrorCode::E0409,
5757
"variable '%s' is bound inconsistently across pattern alternatives",
5858
ident.as_string ().c_str ());
5959
}
@@ -278,15 +278,15 @@ PatternDeclaration::add_new_binding (Identifier ident, NodeId node_id,
278278
{
279279
if (type == Rib::ItemType::Param)
280280
{
281-
rust_error_at (info.get_locus (), ErrorCode ("E0415"),
281+
rust_error_at (info.get_locus (), ErrorCode::E0415,
282282
"identifier '%s' is bound more than once in the "
283283
"same parameter list",
284284
ident.as_string ().c_str ());
285285
}
286286
else
287287
{
288288
rust_error_at (
289-
info.get_locus (), ErrorCode ("E0416"),
289+
info.get_locus (), ErrorCode::E0416,
290290
"identifier '%s' is bound more than once in the same pattern",
291291
ident.as_string ().c_str ());
292292
}

gcc/rust/resolve/rust-ast-resolve-type.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
9898
bool in_middle_of_path = i > 0;
9999
if (in_middle_of_path && segment->is_lower_self_seg ())
100100
{
101-
rust_error_at (segment->get_locus (), ErrorCode ("E0433"),
101+
rust_error_at (segment->get_locus (), ErrorCode::E0433,
102102
"failed to resolve: %<%s%> in paths can only be used "
103103
"in start position",
104104
segment->as_string ().c_str ());

gcc/rust/resolve/rust-ast-verify-assignee.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class VerifyAsignee : public ResolverBase
3535
VerifyAsignee checker;
3636
assignee->accept_vis (checker);
3737
if (!checker.ok)
38-
rust_error_at (assignee->get_locus (), ErrorCode ("E0070"),
38+
rust_error_at (assignee->get_locus (), ErrorCode::E0070,
3939
"invalid left-hand side of assignment");
4040
return checker.ok;
4141
}

gcc/rust/rust-diagnostics.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ class rust_error_code_rule : public diagnostic_metadata::rule
199199

200200
char *make_description () const final override
201201
{
202-
return xstrdup (m_code.m_str);
202+
return xstrdup (error_code_strings.at (m_code));
203203
}
204204

205205
char *make_url () const final override
206206
{
207-
return concat ("https://doc.rust-lang.org/error-index.html#", m_code.m_str,
208-
NULL);
207+
return concat ("https://doc.rust-lang.org/error-index.html#",
208+
error_code_strings.at (m_code), NULL);
209209
}
210210

211211
private:

0 commit comments

Comments
 (0)