From d1fcf611175e695c35c6cc0537d710277c1a5c6f Mon Sep 17 00:00:00 2001 From: David Wood Date: Thu, 13 Oct 2022 10:13:02 +0100 Subject: errors: generate typed identifiers in each crate Instead of loading the Fluent resources for every crate in `rustc_error_messages`, each crate generates typed identifiers for its own diagnostics and creates a static which are pulled together in the `rustc_driver` crate and provided to the diagnostic emitter. Signed-off-by: David Wood --- compiler/rustc_passes/src/errors.rs | 92 ++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 47 deletions(-) (limited to 'compiler/rustc_passes/src/errors.rs') diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 68b098e3457..82fc3eeff94 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -3,6 +3,7 @@ use std::{ path::{Path, PathBuf}, }; +use crate::fluent_generated as fluent; use rustc_ast::Label; use rustc_errors::{ error_code, Applicability, DiagnosticSymbolList, ErrorGuaranteed, IntoDiagnostic, MultiSpan, @@ -261,7 +262,7 @@ pub struct DocKeywordConflict { pub struct DocInlineOnlyUse { #[label] pub attr_span: Span, - #[label(not_a_use_item_label)] + #[label(passes_not_a_use_item_label)] pub item_span: Option, } @@ -300,7 +301,7 @@ pub struct DocTestUnknownAny { #[derive(LintDiagnostic)] #[diag(passes_doc_test_unknown_spotlight)] #[note] -#[note(no_op_note)] +#[note(passes_no_op_note)] pub struct DocTestUnknownSpotlight { pub path: String, #[suggestion(style = "short", applicability = "machine-applicable", code = "notable_trait")] @@ -573,9 +574,9 @@ pub struct DebugVisualizerPlacement { #[derive(Diagnostic)] #[diag(passes_debug_visualizer_invalid)] -#[note(note_1)] -#[note(note_2)] -#[note(note_3)] +#[note(passes_note_1)] +#[note(passes_note_2)] +#[note(passes_note_3)] pub struct DebugVisualizerInvalid { #[primary_span] pub span: Span, @@ -782,7 +783,7 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel { self, handler: &'_ rustc_errors::Handler, ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { - let mut diag = handler.struct_err(rustc_errors::fluent::passes_invalid_attr_at_crate_level); + let mut diag = handler.struct_err(fluent::passes_invalid_attr_at_crate_level); diag.set_span(self.span); diag.set_arg("name", self.name); // Only emit an error with a suggestion if we can create a string out @@ -791,7 +792,7 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel { let replacement = src.replace("#!", "#"); diag.span_suggestion_verbose( self.span, - rustc_errors::fluent::suggestion, + fluent::passes_suggestion, replacement, rustc_errors::Applicability::MachineApplicable, ); @@ -917,17 +918,17 @@ impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> { ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { let mut diag = handler.struct_span_err_with_code( self.span, - rustc_errors::fluent::passes_break_non_loop, + fluent::passes_break_non_loop, error_code!(E0571), ); diag.set_arg("kind", self.kind); - diag.span_label(self.span, rustc_errors::fluent::label); + diag.span_label(self.span, fluent::passes_label); if let Some(head) = self.head { - diag.span_label(head, rustc_errors::fluent::label2); + diag.span_label(head, fluent::passes_label2); } diag.span_suggestion( self.span, - rustc_errors::fluent::suggestion, + fluent::passes_suggestion, self.suggestion, Applicability::MaybeIncorrect, ); @@ -945,7 +946,7 @@ impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> { _ => { diag.span_suggestion( self.break_expr_span, - rustc_errors::fluent::break_expr_suggestion, + fluent::passes_break_expr_suggestion, label.ident, Applicability::MaybeIncorrect, ); @@ -962,7 +963,7 @@ pub struct ContinueLabeledBlock { #[primary_span] #[label] pub span: Span, - #[label(block_label)] + #[label(passes_block_label)] pub block_span: Span, } @@ -972,7 +973,7 @@ pub struct BreakInsideClosure<'a> { #[primary_span] #[label] pub span: Span, - #[label(closure_label)] + #[label(passes_closure_label)] pub closure_span: Span, pub name: &'a str, } @@ -983,7 +984,7 @@ pub struct BreakInsideAsyncBlock<'a> { #[primary_span] #[label] pub span: Span, - #[label(async_block_label)] + #[label(passes_async_block_label)] pub closure_span: Span, pub name: &'a str, } @@ -1056,14 +1057,14 @@ impl IntoDiagnostic<'_> for NakedFunctionsAsmBlock { ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { let mut diag = handler.struct_span_err_with_code( self.span, - rustc_errors::fluent::passes_naked_functions_asm_block, + fluent::passes_naked_functions_asm_block, error_code!(E0787), ); for span in self.multiple_asms.iter() { - diag.span_label(*span, rustc_errors::fluent::label_multiple_asm); + diag.span_label(*span, fluent::passes_label_multiple_asm); } for span in self.non_asms.iter() { - diag.span_label(*span, rustc_errors::fluent::label_non_asm); + diag.span_label(*span, fluent::passes_label_non_asm); } diag } @@ -1122,9 +1123,9 @@ pub struct AttrOnlyInFunctions { pub struct MultipleRustcMain { #[primary_span] pub span: Span, - #[label(first)] + #[label(passes_first)] pub first: Span, - #[label(additional)] + #[label(passes_additional)] pub additional: Span, } @@ -1135,7 +1136,7 @@ pub struct MultipleStartFunctions { pub span: Span, #[label] pub labeled: Span, - #[label(previous)] + #[label(passes_previous)] pub previous: Span, } @@ -1180,7 +1181,7 @@ impl<'a> IntoDiagnostic<'a> for NoMainErr { ) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> { let mut diag = handler.struct_span_err_with_code( DUMMY_SP, - rustc_errors::fluent::passes_no_main_function, + fluent::passes_no_main_function, error_code!(E0601), ); diag.set_arg("crate_name", self.crate_name); @@ -1188,16 +1189,16 @@ impl<'a> IntoDiagnostic<'a> for NoMainErr { diag.set_arg("has_filename", self.has_filename); let note = if !self.non_main_fns.is_empty() { for &span in &self.non_main_fns { - diag.span_note(span, rustc_errors::fluent::here_is_main); + diag.span_note(span, fluent::passes_here_is_main); } - diag.note(rustc_errors::fluent::one_or_more_possible_main); - diag.help(rustc_errors::fluent::consider_moving_main); + diag.note(fluent::passes_one_or_more_possible_main); + diag.help(fluent::passes_consider_moving_main); // There were some functions named `main` though. Try to give the user a hint. - rustc_errors::fluent::main_must_be_defined_at_crate + fluent::passes_main_must_be_defined_at_crate } else if self.has_filename { - rustc_errors::fluent::consider_adding_main_to_file + fluent::passes_consider_adding_main_to_file } else { - rustc_errors::fluent::consider_adding_main_at_crate + fluent::passes_consider_adding_main_at_crate }; if self.file_empty { diag.note(note); @@ -1208,11 +1209,11 @@ impl<'a> IntoDiagnostic<'a> for NoMainErr { if let Some(main_def) = self.main_def_opt && main_def.opt_fn_def_id().is_none(){ // There is something at `crate::main`, but it is not a function definition. - diag.span_label(main_def.span, rustc_errors::fluent::non_function_main); + diag.span_label(main_def.span, fluent::passes_non_function_main); } if self.add_teach_note { - diag.note(rustc_errors::fluent::teach_note); + diag.note(fluent::passes_teach_note); } diag } @@ -1241,12 +1242,9 @@ impl IntoDiagnostic<'_> for DuplicateLangItem { ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { let mut diag = handler.struct_err_with_code( match self.duplicate { - Duplicate::Plain => rustc_errors::fluent::passes_duplicate_lang_item, - - Duplicate::Crate => rustc_errors::fluent::passes_duplicate_lang_item_crate, - Duplicate::CrateDepends => { - rustc_errors::fluent::passes_duplicate_lang_item_crate_depends - } + Duplicate::Plain => fluent::passes_duplicate_lang_item, + Duplicate::Crate => fluent::passes_duplicate_lang_item_crate, + Duplicate::CrateDepends => fluent::passes_duplicate_lang_item_crate_depends, }, error_code!(E0152), ); @@ -1261,24 +1259,24 @@ impl IntoDiagnostic<'_> for DuplicateLangItem { diag.set_span(span); } if let Some(span) = self.first_defined_span { - diag.span_note(span, rustc_errors::fluent::first_defined_span); + diag.span_note(span, fluent::passes_first_defined_span); } else { if self.orig_dependency_of.is_empty() { - diag.note(rustc_errors::fluent::first_defined_crate); + diag.note(fluent::passes_first_defined_crate); } else { - diag.note(rustc_errors::fluent::first_defined_crate_depends); + diag.note(fluent::passes_first_defined_crate_depends); } if self.orig_is_local { - diag.note(rustc_errors::fluent::first_definition_local); + diag.note(fluent::passes_first_definition_local); } else { - diag.note(rustc_errors::fluent::first_definition_path); + diag.note(fluent::passes_first_definition_path); } if self.is_local { - diag.note(rustc_errors::fluent::second_definition_local); + diag.note(fluent::passes_second_definition_local); } else { - diag.note(rustc_errors::fluent::second_definition_path); + diag.note(fluent::passes_second_definition_path); } } diag @@ -1389,7 +1387,7 @@ pub struct UselessStability { #[primary_span] #[label] pub span: Span, - #[label(item)] + #[label(passes_item)] pub item_sp: Span, } @@ -1399,7 +1397,7 @@ pub struct InvalidStability { #[primary_span] #[label] pub span: Span, - #[label(item)] + #[label(passes_item)] pub item_sp: Span, } @@ -1409,7 +1407,7 @@ pub struct CannotStabilizeDeprecated { #[primary_span] #[label] pub span: Span, - #[label(item)] + #[label(passes_item)] pub item_sp: Span, } @@ -1419,7 +1417,7 @@ pub struct InvalidDeprecationVersion { #[primary_span] #[label] pub span: Span, - #[label(item)] + #[label(passes_item)] pub item_sp: Span, } -- cgit 1.4.1-3-g733a5 From e39fe374df14334be3b5eb081a006fad8e4419f7 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Feb 2023 00:53:19 +0100 Subject: Add check for invalid \`#[macro_export]\` arguments --- compiler/rustc_lint_defs/src/builtin.rs | 30 +++++++++++++++++++++ compiler/rustc_passes/locales/en-US.ftl | 4 +++ compiler/rustc_passes/src/check_attr.rs | 31 ++++++++++++++++++++-- compiler/rustc_passes/src/errors.rs | 12 +++++++-- .../ui/attributes/invalid_macro_export_argument.rs | 26 ++++++++++++++++++ .../invalid_macro_export_argument.stderr | 16 +++++++++++ 6 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 tests/ui/attributes/invalid_macro_export_argument.rs create mode 100644 tests/ui/attributes/invalid_macro_export_argument.stderr (limited to 'compiler/rustc_passes/src/errors.rs') diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 9d8ad9d9ed9..46ec1a2dca1 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -4103,3 +4103,33 @@ declare_lint! { }; report_in_external_macro } + +declare_lint! { + /// The `invalid_macro_export_arguments` lint detects cases where `#[macro_export]` is being used with invalid arguments. + /// + /// ### Example + /// + /// ```rust,compile_fail + /// #![deny(invalid_macro_export_arguments)] + /// + /// #[macro_export(invalid_parameter)] + /// macro_rules! myMacro { + /// () => { + /// // [...] + /// } + /// } + /// + /// #[macro_export(too, many, items)] + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// The only valid argument is `#[macro_export(local_inner_macros)]` or no argument (`#[macro_export]`). + /// You can't have multiple arguments in a `#[macro_export(..)]`, or mention arguments other than `local_inner_macros`. + /// + pub INVALID_MACRO_EXPORT_ARGUMENTS, + Warn, + "\"invalid_parameter\" isn't a valid argument for `#[macro_export]`", +} diff --git a/compiler/rustc_passes/locales/en-US.ftl b/compiler/rustc_passes/locales/en-US.ftl index 0c7e02912d4..789a7cef4ae 100644 --- a/compiler/rustc_passes/locales/en-US.ftl +++ b/compiler/rustc_passes/locales/en-US.ftl @@ -745,3 +745,7 @@ passes_proc_macro_invalid_abi = proc macro functions may not be `extern "{$abi}" passes_proc_macro_unsafe = proc macro functions may not be `unsafe` passes_skipping_const_checks = skipping const checks + +passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]` argument + +passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index bb09dcbdd69..5ef3e13eff8 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -23,7 +23,8 @@ use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{ParamEnv, TyCtxt}; use rustc_session::lint::builtin::{ - CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES, + CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS, + UNUSED_ATTRIBUTES, }; use rustc_session::parse::feature_err; use rustc_span::symbol::{kw, sym, Symbol}; @@ -2102,7 +2103,33 @@ impl CheckAttrVisitor<'_> { fn check_macro_export(&self, hir_id: HirId, attr: &Attribute, target: Target) { if target != Target::MacroDef { - self.tcx.emit_spanned_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, errors::MacroExport); + self.tcx.emit_spanned_lint( + UNUSED_ATTRIBUTES, + hir_id, + attr.span, + errors::MacroExport::Normal, + ); + } else if let Some(meta_item_list) = attr.meta_item_list() && + !meta_item_list.is_empty() { + if meta_item_list.len() > 1 { + self.tcx.emit_spanned_lint( + INVALID_MACRO_EXPORT_ARGUMENTS, + hir_id, + attr.span, + errors::MacroExport::TooManyItems, + ); + } else { + if meta_item_list[0].name_or_empty() != sym::local_inner_macros { + self.tcx.emit_spanned_lint( + INVALID_MACRO_EXPORT_ARGUMENTS, + hir_id, + meta_item_list[0].span(), + errors::MacroExport::UnknownItem { + name: meta_item_list[0].name_or_empty(), + }, + ); + } + } } } diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 82fc3eeff94..2c0d21b4798 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -641,8 +641,16 @@ pub struct MacroUse { } #[derive(LintDiagnostic)] -#[diag(passes_macro_export)] -pub struct MacroExport; +pub enum MacroExport { + #[diag(passes_macro_export)] + Normal, + + #[diag(passes_invalid_macro_export_arguments)] + UnknownItem { name: Symbol }, + + #[diag(passes_invalid_macro_export_arguments_too_many_items)] + TooManyItems, +} #[derive(LintDiagnostic)] #[diag(passes_plugin_registrar)] diff --git a/tests/ui/attributes/invalid_macro_export_argument.rs b/tests/ui/attributes/invalid_macro_export_argument.rs new file mode 100644 index 00000000000..85d009f11a6 --- /dev/null +++ b/tests/ui/attributes/invalid_macro_export_argument.rs @@ -0,0 +1,26 @@ +// check-pass +#[macro_export(hello, world)] //~ WARN `#[macro_export]` can only take 1 or 0 arguments +macro_rules! a { + () => () +} + +#[macro_export(not_local_inner_macros)] //~ WARN `not_local_inner_macros` isn't a valid `#[macro_export]` argument +macro_rules! b { + () => () +} + +#[macro_export] +macro_rules! c { + () => () +} +#[macro_export(local_inner_macros)] +macro_rules! d { + () => () +} + +#[macro_export()] +macro_rules! e { + () => () +} + +fn main() {} diff --git a/tests/ui/attributes/invalid_macro_export_argument.stderr b/tests/ui/attributes/invalid_macro_export_argument.stderr new file mode 100644 index 00000000000..a4e17642c2a --- /dev/null +++ b/tests/ui/attributes/invalid_macro_export_argument.stderr @@ -0,0 +1,16 @@ +warning: `#[macro_export]` can only take 1 or 0 arguments + --> $DIR/invalid_macro_export_argument.rs:2:1 + | +LL | #[macro_export(hello, world)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(invalid_macro_export_arguments)]` on by default + +warning: `not_local_inner_macros` isn't a valid `#[macro_export]` argument + --> $DIR/invalid_macro_export_argument.rs:7:16 + | +LL | #[macro_export(not_local_inner_macros)] + | ^^^^^^^^^^^^^^^^^^^^^^ + +warning: 2 warnings emitted + -- cgit 1.4.1-3-g733a5