diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-09-03 21:15:18 -0400 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-09-05 12:35:15 -0400 |
| commit | b437240ceefaad3cdf92ad7e9d1255b8da88dbb3 (patch) | |
| tree | ad724c143dc6cf6b20b214fa00e4572571e87399 /src | |
| parent | 74563b41666228e46f892e795108e06306b2b514 (diff) | |
| download | rust-b437240ceefaad3cdf92ad7e9d1255b8da88dbb3.tar.gz rust-b437240ceefaad3cdf92ad7e9d1255b8da88dbb3.zip | |
Replace diagnostic plugins with macro_rules
Diffstat (limited to 'src')
32 files changed, 87 insertions, 351 deletions
diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index bde0fdf3b8e..eee33846139 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -2184,11 +2184,7 @@ Examples of erroneous code: static X: u32 = 42; ``` "##, - -} - - -register_diagnostics! { +; // E0006, // merged with E0005 // E0101, // replaced with E0282 // E0102, // replaced with E0282 diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 368f5bb64fe..c5d2b2d7e7f 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -88,8 +88,6 @@ mod tests; #[macro_use] mod macros; -// N.B., this module needs to be declared first so diagnostics are -// registered before they are used. pub mod error_codes; #[macro_use] @@ -143,6 +141,3 @@ pub mod util { // Allows macros to refer to this crate as `::rustc` extern crate self as rustc; - -// Build the diagnostics array at the end so that the metadata includes error use sites. -__build_diagnostic_array! { librustc, DIAGNOSTICS } diff --git a/src/librustc_codegen_llvm/error_codes.rs b/src/librustc_codegen_llvm/error_codes.rs index c6b5dc03a6f..042e51ed2ba 100644 --- a/src/librustc_codegen_llvm/error_codes.rs +++ b/src/librustc_codegen_llvm/error_codes.rs @@ -1,4 +1,4 @@ -register_long_diagnostics! { +register_diagnostics! { E0511: r##" Invalid monomorphization of an intrinsic function was used. Erroneous code diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index 2fd78885bd0..f032d0ff046 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -256,7 +256,7 @@ impl CodegenBackend for LlvmCodegenBackend { } fn diagnostics(&self) -> &[(&'static str, &'static str)] { - &DIAGNOSTICS + &error_codes::DIAGNOSTICS } fn target_features(&self, sess: &Session) -> Vec<Symbol> { @@ -425,5 +425,3 @@ impl Drop for ModuleLlvm { } } } - -__build_diagnostic_array! { librustc_codegen_llvm, DIAGNOSTICS } diff --git a/src/librustc_codegen_ssa/error_codes.rs b/src/librustc_codegen_ssa/error_codes.rs index 8d46dcb7c09..8ff41c275a8 100644 --- a/src/librustc_codegen_ssa/error_codes.rs +++ b/src/librustc_codegen_ssa/error_codes.rs @@ -1,4 +1,4 @@ -register_long_diagnostics! { +syntax::register_diagnostics! { E0668: r##" Malformed inline assembly rejected by LLVM. diff --git a/src/librustc_codegen_ssa/lib.rs b/src/librustc_codegen_ssa/lib.rs index 68640abb043..0f98310722e 100644 --- a/src/librustc_codegen_ssa/lib.rs +++ b/src/librustc_codegen_ssa/lib.rs @@ -35,8 +35,6 @@ use rustc_data_structures::svh::Svh; use rustc::middle::cstore::{LibSource, CrateSource, NativeLibrary}; use syntax_pos::symbol::Symbol; -// N.B., this module needs to be declared first so diagnostics are -// registered before they are used. mod error_codes; pub mod common; @@ -158,5 +156,3 @@ pub struct CodegenResults { pub linker_info: back::linker::LinkerInfo, pub crate_info: CrateInfo, } - -__build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS } diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 24b44964e4f..bd9b240e24e 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -34,7 +34,7 @@ use rustc_privacy; use rustc_resolve::{Resolver, ResolverArenas}; use rustc_traits; use rustc_typeck as typeck; -use syntax::{self, ast, diagnostics, visit}; +use syntax::{self, ast, visit}; use syntax::early_buffered_lints::BufferedEarlyLint; use syntax::ext::base::{NamedSyntaxExtension, ExtCtxt}; use syntax::mut_visit::MutVisitor; @@ -292,18 +292,7 @@ pub fn register_plugins<'a>( time(sess, "plugin registration", || { if sess.features_untracked().rustc_diagnostic_macros { - registry.register_macro( - "__diagnostic_used", - diagnostics::plugin::expand_diagnostic_used, - ); - registry.register_macro( - "__register_diagnostic", - diagnostics::plugin::expand_register_diagnostic, - ); - registry.register_macro( - "__build_diagnostic_array", - diagnostics::plugin::expand_build_diagnostic_array, - ); + // FIXME: remove feature gate } for registrar in registrars { diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index f007a0cf2ab..9eaf7b77716 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -43,17 +43,17 @@ use std::{thread, panic}; pub fn diagnostics_registry() -> Registry { let mut all_errors = Vec::new(); - all_errors.extend_from_slice(&rustc::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_typeck::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_resolve::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_privacy::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_typeck::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_resolve::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_privacy::error_codes::DIAGNOSTICS); // FIXME: need to figure out a way to get these back in here // all_errors.extend_from_slice(get_codegen_backend(sess).diagnostics()); - all_errors.extend_from_slice(&rustc_metadata::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_passes::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_plugin::DIAGNOSTICS); - all_errors.extend_from_slice(&rustc_mir::DIAGNOSTICS); - all_errors.extend_from_slice(&syntax::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_metadata::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_passes::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_plugin::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&rustc_mir::error_codes::DIAGNOSTICS); + all_errors.extend_from_slice(&syntax::error_codes::DIAGNOSTICS); Registry::new(&all_errors) } diff --git a/src/librustc_lint/error_codes.rs b/src/librustc_lint/error_codes.rs index d7c39b780bf..ea2e1d9ecc5 100644 --- a/src/librustc_lint/error_codes.rs +++ b/src/librustc_lint/error_codes.rs @@ -1,5 +1,4 @@ -use syntax::register_diagnostics; - -register_diagnostics! { +syntax::register_diagnostics! { +; E0721, // `await` keyword } diff --git a/src/librustc_metadata/error_codes.rs b/src/librustc_metadata/error_codes.rs index 4d2c0291ad3..cd8e95e6c3a 100644 --- a/src/librustc_metadata/error_codes.rs +++ b/src/librustc_metadata/error_codes.rs @@ -1,6 +1,4 @@ -use syntax::{register_diagnostics, register_long_diagnostics}; - -register_long_diagnostics! { +syntax::register_diagnostics! { E0454: r##" A link name was given with an empty name. Erroneous code example: @@ -84,10 +82,7 @@ You need to link your code to the relevant crate in order to be able to use it (through Cargo or the `-L` option of rustc example). Plugins are crates as well, and you link to them the same way. "##, - -} - -register_diagnostics! { +; E0456, // plugin `..` is not available for triple `..` E0457, // plugin `..` only found in rlib format, but must be available... E0514, // metadata version mismatch diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index c96d02d9b37..3ab7d372461 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -23,7 +23,7 @@ extern crate rustc; #[macro_use] extern crate rustc_data_structures; -mod error_codes; +pub mod error_codes; mod index; mod encoder; @@ -68,5 +68,3 @@ pub fn validate_crate_name( sess.unwrap().abort_if_errors(); } } - -__build_diagnostic_array! { librustc_metadata, DIAGNOSTICS } diff --git a/src/librustc_mir/error_codes.rs b/src/librustc_mir/error_codes.rs index 86c263a447b..74b3eff0906 100644 --- a/src/librustc_mir/error_codes.rs +++ b/src/librustc_mir/error_codes.rs @@ -1,4 +1,4 @@ -register_long_diagnostics! { +syntax::register_diagnostics! { E0001: r##" @@ -2448,9 +2448,9 @@ information. There are some known bugs that trigger this message. "##, -} -register_diagnostics! { +; + // E0298, // cannot compare constants // E0299, // mismatched types between arms // E0471, // constant evaluation error (in pattern) diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index cccf7b9545b..7a5db4fc19d 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -32,7 +32,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #[macro_use] extern crate rustc_data_structures; #[macro_use] extern crate syntax; -mod error_codes; +pub mod error_codes; mod borrow_check; mod build; @@ -62,5 +62,3 @@ pub fn provide(providers: &mut Providers<'_>) { }; providers.type_name = interpret::type_name; } - -__build_diagnostic_array! { librustc_mir, DIAGNOSTICS } diff --git a/src/librustc_passes/error_codes.rs b/src/librustc_passes/error_codes.rs index a30cd8a627f..af07c790e2a 100644 --- a/src/librustc_passes/error_codes.rs +++ b/src/librustc_passes/error_codes.rs @@ -1,6 +1,4 @@ -use syntax::{register_diagnostics, register_long_diagnostics}; - -register_long_diagnostics! { +syntax::register_diagnostics! { /* E0014: r##" Constants can only be initialized by a constant value or, in a future @@ -320,10 +318,8 @@ async fn foo() {} ``` Switch to the Rust 2018 edition to use `async fn`. -"## -} - -register_diagnostics! { +"##, +; E0226, // only a single explicit lifetime bound is permitted E0472, // asm! is unsupported on this target E0561, // patterns aren't allowed in function pointer types diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index 5614b570b92..2070f306112 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -18,7 +18,7 @@ extern crate rustc; use rustc::ty::query::Providers; -mod error_codes; +pub mod error_codes; pub mod ast_validation; pub mod rvalue_promotion; @@ -26,8 +26,6 @@ pub mod hir_stats; pub mod layout_test; pub mod loops; -__build_diagnostic_array! { librustc_passes, DIAGNOSTICS } - pub fn provide(providers: &mut Providers<'_>) { rvalue_promotion::provide(providers); loops::provide(providers); diff --git a/src/librustc_plugin/error_codes.rs b/src/librustc_plugin/error_codes.rs index b5f6a8d905d..7b3f01c0ee1 100644 --- a/src/librustc_plugin/error_codes.rs +++ b/src/librustc_plugin/error_codes.rs @@ -1,9 +1,4 @@ -use syntax::{register_diagnostics, register_long_diagnostics}; - -register_long_diagnostics! { - -} - -register_diagnostics! { - E0498 // malformed plugin attribute +syntax::register_diagnostics! { +; + E0498, // malformed plugin attribute } diff --git a/src/librustc_plugin/lib.rs b/src/librustc_plugin/lib.rs index 952bc9fff6a..1cf01d481e4 100644 --- a/src/librustc_plugin/lib.rs +++ b/src/librustc_plugin/lib.rs @@ -60,9 +60,7 @@ pub use registry::Registry; -mod error_codes; +pub mod error_codes; pub mod registry; pub mod load; pub mod build; - -__build_diagnostic_array! { librustc_plugin, DIAGNOSTICS } diff --git a/src/librustc_privacy/error_codes.rs b/src/librustc_privacy/error_codes.rs index 70a799d426a..67066466f1d 100644 --- a/src/librustc_privacy/error_codes.rs +++ b/src/librustc_privacy/error_codes.rs @@ -1,4 +1,4 @@ -register_long_diagnostics! { +syntax::register_diagnostics! { E0445: r##" A private trait was used on a public type parameter bound. Erroneous code @@ -154,8 +154,5 @@ let f = Bar::Foo::new(); // ok! ``` "##, -} - -register_diagnostics! { // E0450, moved into resolve } diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 146058963b6..28ef3b794ba 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -31,7 +31,7 @@ use syntax_pos::Span; use std::{cmp, fmt, mem}; use std::marker::PhantomData; -mod error_codes; +pub mod error_codes; //////////////////////////////////////////////////////////////////////////////// /// Generic infrastructure used to implement specific visitors below. @@ -2035,5 +2035,3 @@ fn check_private_in_public(tcx: TyCtxt<'_>, krate: CrateNum) { }; krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor)); } - -__build_diagnostic_array! { librustc_privacy, DIAGNOSTICS } diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index 1faaf97e981..adbff67cc8d 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1,9 +1,7 @@ -use syntax::{register_diagnostics, register_long_diagnostics}; - // Error messages for EXXXX errors. Each message should start and end with a // new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and // use `gq` to wrap paragraphs. Use `:set tw=0` to disable. -register_long_diagnostics! { +syntax::register_diagnostics! { E0128: r##" Type parameter defaults can only use parameters that occur before them. @@ -1662,10 +1660,7 @@ fn const_id<T, const N: T>() -> T { // error: const parameter } ``` "##, - -} - -register_diagnostics! { +; // E0153, unused error code // E0157, unused error code // E0257, diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index e15d02a9f7e..4570bbbe339 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -286,18 +286,18 @@ impl<'a> PathSource<'a> { } fn error_code(self, has_unexpected_resolution: bool) -> &'static str { - __diagnostic_used!(E0404); - __diagnostic_used!(E0405); - __diagnostic_used!(E0412); - __diagnostic_used!(E0422); - __diagnostic_used!(E0423); - __diagnostic_used!(E0425); - __diagnostic_used!(E0531); - __diagnostic_used!(E0532); - __diagnostic_used!(E0573); - __diagnostic_used!(E0574); - __diagnostic_used!(E0575); - __diagnostic_used!(E0576); + syntax::diagnostic_used!(E0404); + syntax::diagnostic_used!(E0405); + syntax::diagnostic_used!(E0412); + syntax::diagnostic_used!(E0422); + syntax::diagnostic_used!(E0423); + syntax::diagnostic_used!(E0425); + syntax::diagnostic_used!(E0531); + syntax::diagnostic_used!(E0532); + syntax::diagnostic_used!(E0573); + syntax::diagnostic_used!(E0574); + syntax::diagnostic_used!(E0575); + syntax::diagnostic_used!(E0576); match (self, has_unexpected_resolution) { (PathSource::Trait(_), true) => "E0404", (PathSource::Trait(_), false) => "E0405", diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index a822fa049ca..0c86d8494fd 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -113,7 +113,7 @@ impl<'a> LateResolutionVisitor<'a, '_> { // Emit special messages for unresolved `Self` and `self`. if is_self_type(path, ns) { - __diagnostic_used!(E0411); + syntax::diagnostic_used!(E0411); err.code(DiagnosticId::Error("E0411".into())); err.span_label(span, format!("`Self` is only available in impls, traits, \ and type definitions")); @@ -122,7 +122,7 @@ impl<'a> LateResolutionVisitor<'a, '_> { if is_self_value(path, ns) { debug!("smart_resolve_path_fragment: E0424, source={:?}", source); - __diagnostic_used!(E0424); + syntax::diagnostic_used!(E0424); err.code(DiagnosticId::Error("E0424".into())); err.span_label(span, match source { PathSource::Pat => { diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 875ae449d94..6ba08ccef03 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -67,9 +67,7 @@ use macros::{LegacyBinding, LegacyScope}; type Res = def::Res<NodeId>; -// N.B., this module needs to be declared first so diagnostics are -// registered before they are used. -mod error_codes; +pub mod error_codes; mod diagnostics; mod late; mod macros; @@ -2817,5 +2815,3 @@ impl CrateLint { } } } - -__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS } diff --git a/src/librustc_typeck/error_codes.rs b/src/librustc_typeck/error_codes.rs index 4d960b416ff..e11dcfafb8f 100644 --- a/src/librustc_typeck/error_codes.rs +++ b/src/librustc_typeck/error_codes.rs @@ -1,6 +1,6 @@ // ignore-tidy-filelength -register_long_diagnostics! { +syntax::register_diagnostics! { E0023: r##" A pattern used to match against an enum variant must provide a sub-pattern for @@ -4870,10 +4870,7 @@ fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> { The `Box<...>` ensures that the result is of known size, and the pin is required to keep it in the same place in memory. "##, - -} // (end of detailed error messages) - -register_diagnostics! { +; // E0035, merged into E0087/E0089 // E0036, merged into E0087/E0089 // E0068, diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 9d9a9d9b559..91c7522280a 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -78,9 +78,7 @@ This API is completely unstable and subject to change. #[macro_use] extern crate rustc; -// N.B., this module needs to be declared first so diagnostics are -// registered before they are used. -mod error_codes; +pub mod error_codes; mod astconv; mod check; @@ -389,5 +387,3 @@ pub fn hir_trait_to_predicates<'tcx>( bounds } - -__build_diagnostic_array! { librustc_typeck, DIAGNOSTICS } diff --git a/src/librustc_typeck/structured_errors.rs b/src/librustc_typeck/structured_errors.rs index 3e3eab8cf4c..273a36edc56 100644 --- a/src/librustc_typeck/structured_errors.rs +++ b/src/librustc_typeck/structured_errors.rs @@ -48,7 +48,7 @@ impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> { fn session(&self) -> &Session { self.sess } fn code(&self) -> DiagnosticId { - __diagnostic_used!(E0617); + syntax::diagnostic_used!(E0617); DiagnosticId::Error("E0617".to_owned()) } @@ -104,7 +104,7 @@ impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> { fn session(&self) -> &Session { self.sess } fn code(&self) -> DiagnosticId { - __diagnostic_used!(E0607); + syntax::diagnostic_used!(E0607); DiagnosticId::Error("E0607".to_owned()) } diff --git a/src/libsyntax/diagnostics/macros.rs b/src/libsyntax/diagnostics/macros.rs index b754d083376..c95c5bd5d02 100644 --- a/src/libsyntax/diagnostics/macros.rs +++ b/src/libsyntax/diagnostics/macros.rs @@ -1,13 +1,14 @@ #[macro_export] -macro_rules! register_diagnostic { - ($code:tt, $description:tt) => (__register_diagnostic! { $code, $description }); - ($code:tt) => (__register_diagnostic! { $code }) +macro_rules! diagnostic_used { + ($code:ident) => ( + let _ = crate::error_codes::$code; + ) } #[macro_export] macro_rules! span_fatal { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.span_fatal_with_code( $span, &format!($($message)*), @@ -19,7 +20,7 @@ macro_rules! span_fatal { #[macro_export] macro_rules! span_err { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.span_err_with_code( $span, &format!($($message)*), @@ -31,7 +32,7 @@ macro_rules! span_err { #[macro_export] macro_rules! span_warn { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.span_warn_with_code( $span, &format!($($message)*), @@ -43,7 +44,7 @@ macro_rules! span_warn { #[macro_export] macro_rules! struct_err { ($session:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.struct_err_with_code( &format!($($message)*), $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), @@ -54,7 +55,7 @@ macro_rules! struct_err { #[macro_export] macro_rules! span_err_or_warn { ($is_warning:expr, $session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); if $is_warning { $session.span_warn_with_code( $span, @@ -74,7 +75,7 @@ macro_rules! span_err_or_warn { #[macro_export] macro_rules! struct_span_fatal { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.struct_span_fatal_with_code( $span, &format!($($message)*), @@ -86,7 +87,7 @@ macro_rules! struct_span_fatal { #[macro_export] macro_rules! struct_span_err { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.struct_span_err_with_code( $span, &format!($($message)*), @@ -98,7 +99,7 @@ macro_rules! struct_span_err { #[macro_export] macro_rules! stringify_error_code { ($code:ident) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()) }) } @@ -117,7 +118,7 @@ macro_rules! type_error_struct { #[macro_export] macro_rules! struct_span_warn { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); $session.struct_span_warn_with_code( $span, &format!($($message)*), @@ -129,7 +130,7 @@ macro_rules! struct_span_warn { #[macro_export] macro_rules! struct_span_err_or_warn { ($is_warning:expr, $session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ - __diagnostic_used!($code); + $crate::diagnostic_used!($code); if $is_warning { $session.struct_span_warn_with_code( $span, @@ -169,20 +170,22 @@ macro_rules! help { #[macro_export] macro_rules! register_diagnostics { - ($($code:tt),*) => ( - $($crate::register_diagnostic! { $code })* + ($($ecode:ident: $message:expr,)*) => ( + $crate::register_diagnostics!{$($ecode:$message,)* ;} ); - ($($code:tt),*,) => ( - $($crate::register_diagnostic! { $code })* - ) -} -#[macro_export] -macro_rules! register_long_diagnostics { - ($($code:tt: $description:tt),*) => ( - $($crate::register_diagnostic! { $code, $description })* - ); - ($($code:tt: $description:tt),*,) => ( - $($crate::register_diagnostic! { $code, $description })* + ($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => ( + pub static DIAGNOSTICS: &[(&str, &str)] = &[ + $( (stringify!($ecode), $message), )* + ]; + + $( + #[deny(unused)] + pub(crate) const $ecode: &str = $message; + )* + $( + #[deny(unused)] + pub(crate) const $code: () = (); + )* ) } diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs deleted file mode 100644 index 5de39c8d14d..00000000000 --- a/src/libsyntax/diagnostics/plugin.rs +++ /dev/null @@ -1,185 +0,0 @@ -use std::collections::BTreeMap; - -use crate::ast::{self, Ident, Name}; -use crate::source_map; -use crate::ext::base::{ExtCtxt, MacEager, MacResult}; -use crate::parse::token::{self, Token}; -use crate::ptr::P; -use crate::symbol::kw; -use crate::tokenstream::{TokenTree, TokenStream}; - -use smallvec::smallvec; -use syntax_pos::Span; - -pub use errors::*; - -// Maximum width of any line in an extended error description (inclusive). -const MAX_DESCRIPTION_WIDTH: usize = 80; - -/// Error information type. -pub struct ErrorInfo { - pub description: Option<Name>, - pub use_site: Option<Span> -} - -/// Mapping from error codes to metadata. -pub type ErrorMap = BTreeMap<Name, ErrorInfo>; - -pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt<'_>, - span: Span, - tts: TokenStream) - -> Box<dyn MacResult+'cx> { - assert_eq!(tts.len(), 1); - let code = match tts.into_trees().next() { - Some(TokenTree::Token(Token { kind: token::Ident(code, _), .. })) => code, - _ => unreachable!() - }; - - ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| { - match diagnostics.get_mut(&code) { - // Previously used errors. - Some(&mut ErrorInfo { description: _, use_site: Some(previous_span) }) => { - ecx.struct_span_warn(span, &format!( - "diagnostic code {} already used", code - )).span_note(previous_span, "previous invocation") - .emit(); - } - // Newly used errors. - Some(ref mut info) => { - info.use_site = Some(span); - } - // Unregistered errors. - None => { - ecx.span_err(span, &format!( - "used diagnostic code {} not registered", code - )); - } - } - }); - MacEager::expr(ecx.expr_tuple(span, Vec::new())) -} - -pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>, - span: Span, - tts: TokenStream) - -> Box<dyn MacResult+'cx> { - assert!(tts.len() == 1 || tts.len() == 3); - let mut cursor = tts.into_trees(); - let code = match cursor.next() { - Some(TokenTree::Token(Token { kind: token::Ident(code, _), .. })) => code, - _ => unreachable!() - }; - let description = match (cursor.next(), cursor.next()) { - (None, None) => None, - ( - Some(TokenTree::Token(Token { kind: token::Comma, .. })), - Some(TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..})) - ) => { - Some(symbol) - }, - _ => unreachable!() - }; - - // Check that the description starts and ends with a newline and doesn't - // overflow the maximum line width. - description.map(|raw_msg| { - let msg = raw_msg.as_str(); - if !msg.starts_with("\n") || !msg.ends_with("\n") { - ecx.span_err(span, &format!( - "description for error code {} doesn't start and end with a newline", - code - )); - } - - // URLs can be unavoidably longer than the line limit, so we allow them. - // Allowed format is: `[name]: https://www.rust-lang.org/` - let is_url = |l: &str| l.starts_with("[") && l.contains("]:") && l.contains("http"); - - if msg.lines().any(|line| line.len() > MAX_DESCRIPTION_WIDTH && !is_url(line)) { - ecx.span_err(span, &format!( - "description for error code {} contains a line longer than {} characters.\n\ - if you're inserting a long URL use the footnote style to bypass this check.", - code, MAX_DESCRIPTION_WIDTH - )); - } - }); - // Add the error to the map. - ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| { - let info = ErrorInfo { - description, - use_site: None - }; - if diagnostics.insert(code, info).is_some() { - ecx.span_err(span, &format!( - "diagnostic code {} already registered", code - )); - } - }); - - MacEager::items(smallvec![]) -} - -pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt<'_>, - span: Span, - tts: TokenStream) - -> Box<dyn MacResult+'cx> { - assert_eq!(tts.len(), 3); - let ident = match tts.into_trees().nth(2) { - // DIAGNOSTICS ident. - Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) - => Ident::new(name, span), - _ => unreachable!() - }; - - // Construct the output expression. - let (count, expr) = - ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| { - let descriptions: Vec<P<ast::Expr>> = - diagnostics.iter().filter_map(|(&code, info)| { - info.description.map(|description| { - ecx.expr_tuple(span, vec![ - ecx.expr_str(span, code), - ecx.expr_str(span, description) - ]) - }) - }).collect(); - (descriptions.len(), ecx.expr_vec(span, descriptions)) - }); - - let static_ = ecx.lifetime(span, Ident::with_dummy_span(kw::StaticLifetime)); - let ty_str = ecx.ty_rptr( - span, - ecx.ty_ident(span, ecx.ident_of("str")), - Some(static_), - ast::Mutability::Immutable, - ); - - let ty = ecx.ty( - span, - ast::TyKind::Array( - ecx.ty( - span, - ast::TyKind::Tup(vec![ty_str.clone(), ty_str]) - ), - ast::AnonConst { - id: ast::DUMMY_NODE_ID, - value: ecx.expr_usize(span, count), - }, - ), - ); - - MacEager::items(smallvec![ - P(ast::Item { - ident, - attrs: Vec::new(), - id: ast::DUMMY_NODE_ID, - node: ast::ItemKind::Const( - ty, - expr, - ), - vis: source_map::respan(span.shrink_to_lo(), ast::VisibilityKind::Public), - span, - tokens: None, - }) - ]) -} diff --git a/src/libsyntax/error_codes.rs b/src/libsyntax/error_codes.rs index 76b2575bfd6..9925dd8ada0 100644 --- a/src/libsyntax/error_codes.rs +++ b/src/libsyntax/error_codes.rs @@ -421,9 +421,8 @@ Delete the offending feature attribute, or add it to the list of allowed features in the `-Z allow_features` flag. "##, -} +; -register_diagnostics! { E0539, // incorrect meta item E0540, // multiple rustc_deprecated attributes E0542, // missing 'since' @@ -447,7 +446,7 @@ register_diagnostics! { // attribute E0630, E0693, // incorrect `repr(align)` attribute format - E0694, // an unknown tool name found in scoped attributes +// E0694, // an unknown tool name found in scoped attributes E0703, // invalid ABI E0717, // rustc_promotable without stability attribute } diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 1741932c1b8..75ce306df80 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -123,11 +123,8 @@ scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals); pub mod diagnostics { #[macro_use] pub mod macros; - pub mod plugin; } -// N.B., this module needs to be declared first so diagnostics are -// registered before they are used. pub mod error_codes; pub mod util { @@ -182,5 +179,3 @@ pub mod ext { } pub mod early_buffered_lints; - -__build_diagnostic_array! { libsyntax, DIAGNOSTICS } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index b1af4806e2d..981f5f1f7b0 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -8,7 +8,6 @@ use crate::parse::parser::Parser; use crate::parse::parser::emit_unclosed_delims; use crate::parse::token::TokenKind; use crate::tokenstream::{TokenStream, TokenTree}; -use crate::diagnostics::plugin::ErrorMap; use crate::print::pprust; use crate::symbol::Symbol; @@ -64,8 +63,6 @@ pub struct ParseSess { pub missing_fragment_specifiers: Lock<FxHashSet<Span>>, /// Places where raw identifiers were used. This is used for feature-gating raw identifiers. pub raw_identifier_spans: Lock<Vec<Span>>, - /// The registered diagnostics codes. - crate registered_diagnostics: Lock<ErrorMap>, /// Used to determine and report recursive module inclusions. included_mod_stack: Lock<Vec<PathBuf>>, source_map: Lrc<SourceMap>, @@ -95,7 +92,6 @@ impl ParseSess { config: FxHashSet::default(), missing_fragment_specifiers: Lock::new(FxHashSet::default()), raw_identifier_spans: Lock::new(Vec::new()), - registered_diagnostics: Lock::new(ErrorMap::new()), included_mod_stack: Lock::new(vec![]), source_map, buffered_lints: Lock::new(vec![]), diff --git a/src/libsyntax_ext/error_codes.rs b/src/libsyntax_ext/error_codes.rs index 49808fc0b60..2bc990574f7 100644 --- a/src/libsyntax_ext/error_codes.rs +++ b/src/libsyntax_ext/error_codes.rs @@ -1,5 +1,3 @@ -use syntax::register_long_diagnostics; - // Error messages for EXXXX errors. // Each message should start and end with a new line, and be wrapped to 80 // characters. In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use |
