Skip to content

Commit

Permalink
Stop using String for error codes.
Browse files Browse the repository at this point in the history
Error codes are integers, but `String` is used everywhere to represent
them. Gross!

This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.

With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123)  // macro call

struct_span_code_err!(dcx, span, E0123, "msg");  // bare ident arg to macro call

\#[diag(name, code = "E0123")]  // string
struct Diag;
```

With the new code, they all use the `E0123` constant.
```
E0123  // constant

struct_span_code_err!(dcx, span, E0123, "msg");  // constant

\#[diag(name, code = E0123)]  // constant
struct Diag;
```

The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
  used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
  moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
  code constants and the `DIAGNOSTIC_TABLES`. This is in its new
  `codes.rs` file.
  • Loading branch information
nnethercote committed Jan 21, 2024
1 parent d2fe1e5 commit 9ddc424
Show file tree
Hide file tree
Showing 110 changed files with 1,624 additions and 1,572 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,6 @@ name = "error_index_generator"
version = "0.0.0"
dependencies = [
"mdbook",
"rustc_error_codes",
]

[[package]]
Expand Down Expand Up @@ -3703,7 +3702,6 @@ dependencies = [
"rustc_codegen_ssa",
"rustc_const_eval",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
"rustc_expand",
"rustc_feature",
Expand Down Expand Up @@ -3776,9 +3774,11 @@ dependencies = [
"rustc_ast",
"rustc_ast_pretty",
"rustc_data_structures",
"rustc_error_codes",
"rustc_error_messages",
"rustc_fluent_macro",
"rustc_hir",
"rustc_index",
"rustc_lint_defs",
"rustc_macros",
"rustc_serialize",
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rustc_errors::DiagnosticArgFromDisplay;
use rustc_errors::{codes::*, DiagnosticArgFromDisplay};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{symbol::Ident, Span, Symbol};

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_generic_type_with_parentheses, code = "E0214")]
#[diag(ast_lowering_generic_type_with_parentheses, code = E0214)]
pub struct GenericTypeWithParentheses {
#[primary_span]
#[label]
Expand All @@ -22,7 +22,7 @@ pub struct UseAngleBrackets {
}

#[derive(Diagnostic)]
#[diag(ast_lowering_invalid_abi, code = "E0703")]
#[diag(ast_lowering_invalid_abi, code = E0703)]
#[note]
pub struct InvalidAbi {
#[primary_span]
Expand Down Expand Up @@ -89,7 +89,7 @@ pub enum AssocTyParenthesesSub {
}

#[derive(Diagnostic)]
#[diag(ast_lowering_misplaced_impl_trait, code = "E0562")]
#[diag(ast_lowering_misplaced_impl_trait, code = E0562)]
#[note]
pub struct MisplacedImplTrait<'a> {
#[primary_span]
Expand All @@ -114,15 +114,15 @@ pub struct UnderscoreExprLhsAssign {
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_base_expression_double_dot, code = "E0797")]
#[diag(ast_lowering_base_expression_double_dot, code = E0797)]
pub struct BaseExpressionDoubleDot {
#[primary_span]
#[suggestion(code = "/* expr */", applicability = "has-placeholders", style = "verbose")]
pub span: Span,
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_await_only_in_async_fn_and_blocks, code = "E0728")]
#[diag(ast_lowering_await_only_in_async_fn_and_blocks, code = E0728)]
pub struct AwaitOnlyInAsyncFnAndBlocks {
#[primary_span]
#[label]
Expand All @@ -132,14 +132,14 @@ pub struct AwaitOnlyInAsyncFnAndBlocks {
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_coroutine_too_many_parameters, code = "E0628")]
#[diag(ast_lowering_coroutine_too_many_parameters, code = E0628)]
pub struct CoroutineTooManyParameters {
#[primary_span]
pub fn_decl_span: Span,
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_closure_cannot_be_static, code = "E0697")]
#[diag(ast_lowering_closure_cannot_be_static, code = E0697)]
pub struct ClosureCannotBeStatic {
#[primary_span]
pub fn_decl_span: Span,
Expand All @@ -154,14 +154,14 @@ pub struct FunctionalRecordUpdateDestructuringAssignment {
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_async_coroutines_not_supported, code = "E0727")]
#[diag(ast_lowering_async_coroutines_not_supported, code = E0727)]
pub struct AsyncCoroutinesNotSupported {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_inline_asm_unsupported_target, code = "E0472")]
#[diag(ast_lowering_inline_asm_unsupported_target, code = E0472)]
pub struct InlineAsmUnsupportedTarget {
#[primary_span]
pub span: Span,
Expand Down
38 changes: 19 additions & 19 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Errors emitted by ast_passes.

use rustc_ast::ParamKindOrd;
use rustc_errors::{AddToDiagnostic, Applicability};
use rustc_errors::{codes::*, AddToDiagnostic, Applicability};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{symbol::Ident, Span, Symbol};

Expand All @@ -23,7 +23,7 @@ pub struct InvalidLabel {
}

#[derive(Diagnostic)]
#[diag(ast_passes_visibility_not_permitted, code = "E0449")]
#[diag(ast_passes_visibility_not_permitted, code = E0449)]
pub struct VisibilityNotPermitted {
#[primary_span]
pub span: Span,
Expand All @@ -44,7 +44,7 @@ pub enum VisibilityNotPermittedNote {
}

#[derive(Diagnostic)]
#[diag(ast_passes_trait_fn_const, code = "E0379")]
#[diag(ast_passes_trait_fn_const, code = E0379)]
pub struct TraitFnConst {
#[primary_span]
#[label]
Expand Down Expand Up @@ -302,14 +302,14 @@ pub struct ItemUnderscore<'a> {
}

#[derive(Diagnostic)]
#[diag(ast_passes_nomangle_ascii, code = "E0754")]
#[diag(ast_passes_nomangle_ascii, code = E0754)]
pub struct NoMangleAscii {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_module_nonascii, code = "E0754")]
#[diag(ast_passes_module_nonascii, code = E0754)]
#[help]
pub struct ModuleNonAscii {
#[primary_span]
Expand All @@ -318,7 +318,7 @@ pub struct ModuleNonAscii {
}

#[derive(Diagnostic)]
#[diag(ast_passes_auto_generic, code = "E0567")]
#[diag(ast_passes_auto_generic, code = E0567)]
pub struct AutoTraitGeneric {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
Expand All @@ -328,7 +328,7 @@ pub struct AutoTraitGeneric {
}

#[derive(Diagnostic)]
#[diag(ast_passes_auto_super_lifetime, code = "E0568")]
#[diag(ast_passes_auto_super_lifetime, code = E0568)]
pub struct AutoTraitBounds {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
Expand All @@ -338,7 +338,7 @@ pub struct AutoTraitBounds {
}

#[derive(Diagnostic)]
#[diag(ast_passes_auto_items, code = "E0380")]
#[diag(ast_passes_auto_items, code = E0380)]
pub struct AutoTraitItems {
#[primary_span]
pub spans: Vec<Span>,
Expand Down Expand Up @@ -384,28 +384,28 @@ impl AddToDiagnostic for EmptyLabelManySpans {
}

#[derive(Diagnostic)]
#[diag(ast_passes_pattern_in_fn_pointer, code = "E0561")]
#[diag(ast_passes_pattern_in_fn_pointer, code = E0561)]
pub struct PatternFnPointer {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_trait_object_single_bound, code = "E0226")]
#[diag(ast_passes_trait_object_single_bound, code = E0226)]
pub struct TraitObjectBound {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_impl_trait_path, code = "E0667")]
#[diag(ast_passes_impl_trait_path, code = E0667)]
pub struct ImplTraitPath {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_nested_impl_trait, code = "E0666")]
#[diag(ast_passes_nested_impl_trait, code = E0666)]
pub struct NestedImplTrait {
#[primary_span]
pub span: Span,
Expand Down Expand Up @@ -443,7 +443,7 @@ pub struct ObsoleteAuto {
}

#[derive(Diagnostic)]
#[diag(ast_passes_unsafe_negative_impl, code = "E0198")]
#[diag(ast_passes_unsafe_negative_impl, code = E0198)]
pub struct UnsafeNegativeImpl {
#[primary_span]
pub span: Span,
Expand All @@ -468,7 +468,7 @@ pub struct InherentImplCannot<'a> {
}

#[derive(Diagnostic)]
#[diag(ast_passes_inherent_cannot_be, code = "E0197")]
#[diag(ast_passes_inherent_cannot_be, code = E0197)]
pub struct InherentImplCannotUnsafe<'a> {
#[primary_span]
pub span: Span,
Expand Down Expand Up @@ -536,7 +536,7 @@ pub struct GenericDefaultTrailing {
}

#[derive(Diagnostic)]
#[diag(ast_passes_nested_lifetimes, code = "E0316")]
#[diag(ast_passes_nested_lifetimes, code = E0316)]
pub struct NestedLifetimes {
#[primary_span]
pub span: Span,
Expand Down Expand Up @@ -655,15 +655,15 @@ pub struct ConstAndCVariadic {
}

#[derive(Diagnostic)]
#[diag(ast_passes_pattern_in_foreign, code = "E0130")]
#[diag(ast_passes_pattern_in_foreign, code = E0130)]
pub struct PatternInForeign {
#[primary_span]
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_pattern_in_bodiless, code = "E0642")]
#[diag(ast_passes_pattern_in_bodiless, code = E0642)]
pub struct PatternInBodiless {
#[primary_span]
#[label]
Expand Down Expand Up @@ -711,14 +711,14 @@ pub struct AssociatedSuggestion2 {
}

#[derive(Diagnostic)]
#[diag(ast_passes_stability_outside_std, code = "E0734")]
#[diag(ast_passes_stability_outside_std, code = E0734)]
pub struct StabilityOutsideStd {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_feature_on_non_nightly, code = "E0554")]
#[diag(ast_passes_feature_on_non_nightly, code = E0554)]
pub struct FeatureOnNonNightly {
#[primary_span]
pub span: Span,
Expand Down
Loading

0 comments on commit 9ddc424

Please sign in to comment.