From d197c1eb5bee2d4c2c27c895f5e496df935ccbd0 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 19 Aug 2022 15:50:38 -0400 Subject: [PATCH 01/10] migrate: `UnknownTool` error to `SessionDiagnostic` --- .../locales/en-US/lint.ftl | 3 +++ compiler/rustc_lint/src/errors.rs | 13 +++++++++ compiler/rustc_lint/src/levels.rs | 27 ++++++++----------- compiler/rustc_lint/src/lib.rs | 3 +++ 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 compiler/rustc_lint/src/errors.rs diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 9731711471664..e0943323adee9 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -393,3 +393,6 @@ lint_builtin_deref_nullptr = dereferencing a null pointer .label = this code causes undefined behavior when executed lint_builtin_asm_labels = avoid using named labels in inline assembly + +lint_unknown_tool = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` + .help = add `#![register_tool({$tool_name})]` to the crate root diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs new file mode 100644 index 0000000000000..3f4d856a1ccc7 --- /dev/null +++ b/compiler/rustc_lint/src/errors.rs @@ -0,0 +1,13 @@ +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; + +#[derive(SessionDiagnostic)] +#[error(lint::unknown_tool, code = "E0710")] +pub struct UnknownTool { + #[primary_span] + pub span: Option, + pub tool_name: String, + pub lint_name: String, + #[help] + pub is_nightly_build: Option<()>, +} diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 1cabb58bbebfd..3db88b7ace439 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -1,3 +1,6 @@ +// #![deny(rustc::diagnostic_outside_of_impl)] +// #![deny(rustc::untranslatable_diagnostic)] +// use crate::context::{CheckLintNameResult, LintStore}; use crate::late::unerased_lint_store; use rustc_ast as ast; @@ -23,6 +26,8 @@ use rustc_span::symbol::{sym, Symbol}; use rustc_span::{Span, DUMMY_SP}; use tracing::debug; +use crate::errors::UnknownTool; + fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { let store = unerased_lint_store(tcx); let levels = @@ -485,22 +490,12 @@ impl<'s> LintLevelsBuilder<'s> { } &CheckLintNameResult::NoTool => { - let mut err = struct_span_err!( - sess, - tool_ident.map_or(DUMMY_SP, |ident| ident.span), - E0710, - "unknown tool name `{}` found in scoped lint: `{}::{}`", - tool_name.unwrap(), - tool_name.unwrap(), - pprust::path_to_string(&meta_item.path), - ); - if sess.is_nightly_build() { - err.help(&format!( - "add `#![register_tool({})]` to the crate root", - tool_name.unwrap() - )); - } - err.emit(); + sess.emit_err(UnknownTool { + span: tool_ident.map(|ident| ident.span), + tool_name: tool_name.unwrap().to_string(), + lint_name: pprust::path_to_string(&meta_item.path), + is_nightly_build: sess.is_nightly_build().then_some(()), + }); continue; } diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index f087c624917e8..23fd5d5eea081 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -36,6 +36,8 @@ #![feature(let_else)] #![feature(never_type)] #![recursion_limit = "256"] +// #![deny(rustc::diagnostic_outside_of_impl)] +// #![deny(rustc::untranslatable_diagnostic)] #[macro_use] extern crate rustc_middle; @@ -47,6 +49,7 @@ pub mod builtin; mod context; mod early; mod enum_intrinsics_non_enums; +mod errors; mod expect; pub mod hidden_unicode_codepoints; mod internal; From 874a79fae30ec93bcd9847187741378c405bd9da Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 19 Aug 2022 17:17:14 -0400 Subject: [PATCH 02/10] migrate: `bad_attr` to `SessionDiagnostic` --- .../locales/en-US/lint.ftl | 8 ++++ compiler/rustc_lint/src/errors.rs | 21 +++++++++- compiler/rustc_lint/src/levels.rs | 42 +++++++++++-------- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index e0943323adee9..4569ce82e3ef3 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -394,5 +394,13 @@ lint_builtin_deref_nullptr = dereferencing a null pointer lint_builtin_asm_labels = avoid using named labels in inline assembly +lint_malformed_attribute = malformed lint attribute input + +lint_bad_attribute_argument = bad attribute argument + +lint_reason_must_be_string_literal = reason must be a string literal + +lint_reason_must_come_last = reason in lint attribute must come last + lint_unknown_tool = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` .help = add `#![register_tool({$tool_name})]` to the crate root diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 3f4d856a1ccc7..679cfad4961eb 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -1,6 +1,25 @@ -use rustc_macros::SessionDiagnostic; +use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; use rustc_span::Span; +#[derive(SessionDiagnostic)] +#[error(lint::malformed_attribute, code = "E0452")] +pub struct MalformedAttribute { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub sub: MalformedAttributeSub, +} + +#[derive(SessionSubdiagnostic)] +pub enum MalformedAttributeSub { + #[label(lint::bad_attribute_argument)] + BadAttributeArgument(#[primary_span] Span), + #[label(lint::reason_must_be_string_literal)] + ReasonMustBeStringLiteral(#[primary_span] Span), + #[label(lint::reason_must_come_last)] + ReasonMustComeLast(#[primary_span] Span), +} + #[derive(SessionDiagnostic)] #[error(lint::unknown_tool, code = "E0710")] pub struct UnknownTool { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 3db88b7ace439..2dd4d2b34cc21 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -26,7 +26,7 @@ use rustc_span::symbol::{sym, Symbol}; use rustc_span::{Span, DUMMY_SP}; use tracing::debug; -use crate::errors::UnknownTool; +use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool}; fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { let store = unerased_lint_store(tcx); @@ -271,7 +271,7 @@ impl<'s> LintLevelsBuilder<'s> { self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev }); let sess = self.sess; - let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input"); + // let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input"); for (attr_index, attr) in attrs.iter().enumerate() { if attr.has_name(sym::automatically_derived) { self.current_specs_mut().insert( @@ -322,20 +322,27 @@ impl<'s> LintLevelsBuilder<'s> { } reason = Some(rationale); } else { - bad_attr(name_value.span) - .span_label(name_value.span, "reason must be a string literal") - .emit(); + sess.emit_err(MalformedAttribute { + span: name_value.span, + sub: MalformedAttributeSub::ReasonMustBeStringLiteral( + name_value.span, + ), + }); } // found reason, reslice meta list to exclude it metas.pop().unwrap(); } else { - bad_attr(item.span) - .span_label(item.span, "bad attribute argument") - .emit(); + sess.emit_err(MalformedAttribute { + span: item.span, + sub: MalformedAttributeSub::BadAttributeArgument(item.span), + }); } } ast::MetaItemKind::List(_) => { - bad_attr(item.span).span_label(item.span, "bad attribute argument").emit(); + sess.emit_err(MalformedAttribute { + span: item.span, + sub: MalformedAttributeSub::BadAttributeArgument(item.span), + }); } } } @@ -353,20 +360,21 @@ impl<'s> LintLevelsBuilder<'s> { let meta_item = match li { ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item, _ => { - let mut err = bad_attr(sp); - let mut add_label = true; if let Some(item) = li.meta_item() { if let ast::MetaItemKind::NameValue(_) = item.kind { if item.path == sym::reason { - err.span_label(sp, "reason in lint attribute must come last"); - add_label = false; + sess.emit_err(MalformedAttribute { + span: sp, + sub: MalformedAttributeSub::ReasonMustComeLast(sp), + }); + continue; } } } - if add_label { - err.span_label(sp, "bad attribute argument"); - } - err.emit(); + sess.emit_err(MalformedAttribute { + span: sp, + sub: MalformedAttributeSub::BadAttributeArgument(sp), + }); continue; } }; From 6f83ec88e63b15f9cb34e63a5e149c5b826d6310 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 19 Aug 2022 19:52:20 -0400 Subject: [PATCH 03/10] change: diagnostic `String` field to `Symbol` --- compiler/rustc_lint/src/errors.rs | 2 +- compiler/rustc_lint/src/levels.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 679cfad4961eb..4738e8e82b610 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -25,7 +25,7 @@ pub enum MalformedAttributeSub { pub struct UnknownTool { #[primary_span] pub span: Option, - pub tool_name: String, + pub tool_name: Symbol, pub lint_name: String, #[help] pub is_nightly_build: Option<()>, diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 2dd4d2b34cc21..07e9eab62f3bf 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -271,7 +271,6 @@ impl<'s> LintLevelsBuilder<'s> { self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev }); let sess = self.sess; - // let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input"); for (attr_index, attr) in attrs.iter().enumerate() { if attr.has_name(sym::automatically_derived) { self.current_specs_mut().insert( @@ -500,7 +499,7 @@ impl<'s> LintLevelsBuilder<'s> { &CheckLintNameResult::NoTool => { sess.emit_err(UnknownTool { span: tool_ident.map(|ident| ident.span), - tool_name: tool_name.unwrap().to_string(), + tool_name: tool_name.unwrap(), lint_name: pprust::path_to_string(&meta_item.path), is_nightly_build: sess.is_nightly_build().then_some(()), }); From 32e445af7499fa470961f30750942398bf51d198 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 19 Aug 2022 20:04:21 -0400 Subject: [PATCH 04/10] hotfix: add missing import --- compiler/rustc_lint/src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 4738e8e82b610..2d7ec992f2b5f 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -1,5 +1,5 @@ use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; #[derive(SessionDiagnostic)] #[error(lint::malformed_attribute, code = "E0452")] From 7a6ae2367d3b3442a7ae36e53551b58a9ece7155 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 19 Aug 2022 20:47:05 -0400 Subject: [PATCH 05/10] migrate: `OverruledAttribute` --- .../locales/en-US/lint.ftl | 10 +++++ compiler/rustc_lint/src/errors.rs | 40 +++++++++++++++++++ compiler/rustc_lint/src/levels.rs | 37 +++++++++++------ 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 4569ce82e3ef3..cb393d67193b9 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -394,6 +394,16 @@ lint_builtin_deref_nullptr = dereferencing a null pointer lint_builtin_asm_labels = avoid using named labels in inline assembly +lint_overruled_attribute = {$lint_level}({$lint_source}) incompatible with previous forbid + .label = overruled by previous forbid + +lint_default_source = `forbid` lint level is the default for {$id} + +lint_node_source = `forbid` level set here + .note = {$reason} + +lint_command_line_source = `forbid` lint level was set on command line + lint_malformed_attribute = malformed lint attribute input lint_bad_attribute_argument = bad attribute argument diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 2d7ec992f2b5f..bb4e23e29e7a3 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -1,6 +1,46 @@ +use rustc_errors::{fluent, AddSubdiagnostic}; use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; use rustc_span::{Span, Symbol}; +#[derive(SessionDiagnostic)] +#[error(lint::overruled_attribute, code = "E0453")] +pub struct OverruledAttribute { + #[primary_span] + pub span: Span, + #[label] + pub overruled: Span, + pub lint_level: String, + pub lint_source: Symbol, + #[subdiagnostic] + pub sub: OverruledAttributeSub, +} +// +pub enum OverruledAttributeSub { + DefaultSource { id: String }, + NodeSource { span: Span, reason: Option }, + CommandLineSource, +} + +impl AddSubdiagnostic for OverruledAttributeSub { + fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) { + match self { + OverruledAttributeSub::DefaultSource { id } => { + diag.note(fluent::lint::default_source); + diag.set_arg("id", id); + } + OverruledAttributeSub::NodeSource { span, reason } => { + diag.span_label(span, fluent::lint::node_source); + if let Some(rationale) = reason { + diag.note(rationale.as_str()); + } + } + OverruledAttributeSub::CommandLineSource => { + diag.note(fluent::lint::command_line_source); + } + } + } +} + #[derive(SessionDiagnostic)] #[error(lint::malformed_attribute, code = "E0452")] pub struct MalformedAttribute { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 07e9eab62f3bf..2bafe64c1394a 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -6,7 +6,7 @@ use crate::late::unerased_lint_store; use rustc_ast as ast; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::{struct_span_err, Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan}; +use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan}; use rustc_hir as hir; use rustc_hir::{intravisit, HirId}; use rustc_middle::hir::nested_filter; @@ -26,7 +26,10 @@ use rustc_span::symbol::{sym, Symbol}; use rustc_span::{Span, DUMMY_SP}; use tracing::debug; -use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool}; +use crate::errors::{ + MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub, + UnknownTool, +}; fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { let store = unerased_lint_store(tcx); @@ -191,16 +194,26 @@ impl<'s> LintLevelsBuilder<'s> { } }; if !fcw_warning { - let mut diag_builder = struct_span_err!( - self.sess, - src.span(), - E0453, - "{}({}) incompatible with previous forbid", - level.as_str(), - src.name(), - ); - decorate_diag(&mut diag_builder); - diag_builder.emit(); + self.sess.emit_err(OverruledAttribute { + span: src.span(), + overruled: src.span(), + lint_level: level.as_str().to_string(), + lint_source: src.name(), + sub: match old_src { + LintLevelSource::Default => { + OverruledAttributeSub::DefaultSource { id: id.to_string() } + } + LintLevelSource::Node(_, forbid_source_span, reason) => { + OverruledAttributeSub::NodeSource { + span: forbid_source_span, + reason, + } + } + LintLevelSource::CommandLine(_, _) => { + OverruledAttributeSub::CommandLineSource + } + }, + }); } else { self.struct_lint( FORBIDDEN_LINT_GROUPS, From 5d302d11483a7313742d24e024928a37268ed4bd Mon Sep 17 00:00:00 2001 From: Rejyr Date: Sat, 20 Aug 2022 12:11:07 -0400 Subject: [PATCH 06/10] migrate: `BuiltinEllipsisInclusiveRangePatterns` --- compiler/rustc_lint/src/builtin.rs | 36 +++++++++++------------------- compiler/rustc_lint/src/errors.rs | 10 +++++++++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index bd58021f78fc0..507abad0d5674 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1,3 +1,6 @@ +// #![deny(rustc::diagnostic_outside_of_impl)] +// #![deny(rustc::untranslatable_diagnostic)] +// //! Lints in the Rust compiler. //! //! This contains lints which can feasibly be implemented as their own @@ -21,6 +24,7 @@ //! `late_lint_methods!` invocation in `lib.rs`. use crate::{ + errors::BuiltinEllpisisInclusiveRangePatterns, types::{transparent_newtype_field, CItemKind}, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext, }; @@ -1760,18 +1764,11 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns { None => format!("&(..={})", end), }; if join.edition() >= Edition::Edition2021 { - let mut err = cx.sess().struct_span_err_with_code( - pat.span, - msg, - rustc_errors::error_code!(E0783), - ); - err.span_suggestion( - pat.span, - suggestion, + cx.sess().emit_err(BuiltinEllpisisInclusiveRangePatterns { + span: pat.span, + suggestion: pat.span, replace, - Applicability::MachineApplicable, - ) - .emit(); + }); } else { cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| { lint.build(msg) @@ -1787,18 +1784,11 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns { } else { let replace = "..="; if join.edition() >= Edition::Edition2021 { - let mut err = cx.sess().struct_span_err_with_code( - pat.span, - msg, - rustc_errors::error_code!(E0783), - ); - err.span_suggestion_short( - join, - suggestion, - replace, - Applicability::MachineApplicable, - ) - .emit(); + cx.sess().emit_err(BuiltinEllpisisInclusiveRangePatterns { + span: pat.span, + suggestion: join, + replace: replace.to_string(), + }); } else { cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| { lint.build(msg) diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index bb4e23e29e7a3..569f68bd5e53b 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -70,3 +70,13 @@ pub struct UnknownTool { #[help] pub is_nightly_build: Option<()>, } + +#[derive(SessionDiagnostic)] +#[error(lint::builtin_ellipsis_inclusive_range_patterns, code = "E0783")] +pub struct BuiltinEllpisisInclusiveRangePatterns { + #[primary_span] + pub span: Span, + #[suggestion_short(code = "{replace}", applicability = "machine-applicable")] + pub suggestion: Span, + pub replace: String, +} From dbe838079ca9ec3ea76e196cccd68754fe1bbd70 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Sat, 20 Aug 2022 12:30:49 -0400 Subject: [PATCH 07/10] rename: `UnknownTool` to `UnknownToolInScopedLint` --- compiler/rustc_error_messages/locales/en-US/lint.ftl | 2 +- compiler/rustc_lint/src/errors.rs | 4 ++-- compiler/rustc_lint/src/levels.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index cb393d67193b9..9ae226857d3ee 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -412,5 +412,5 @@ lint_reason_must_be_string_literal = reason must be a string literal lint_reason_must_come_last = reason in lint attribute must come last -lint_unknown_tool = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` +lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` .help = add `#![register_tool({$tool_name})]` to the crate root diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 569f68bd5e53b..0f8b8e174d834 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -61,8 +61,8 @@ pub enum MalformedAttributeSub { } #[derive(SessionDiagnostic)] -#[error(lint::unknown_tool, code = "E0710")] -pub struct UnknownTool { +#[error(lint::unknown_tool_in_scoped_lint, code = "E0710")] +pub struct UnknownToolInScopedLint { #[primary_span] pub span: Option, pub tool_name: Symbol, diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 2bafe64c1394a..41d89229009ba 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -28,7 +28,7 @@ use tracing::debug; use crate::errors::{ MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub, - UnknownTool, + UnknownToolInScopedLint, }; fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { @@ -510,7 +510,7 @@ impl<'s> LintLevelsBuilder<'s> { } &CheckLintNameResult::NoTool => { - sess.emit_err(UnknownTool { + sess.emit_err(UnknownToolInScopedLint { span: tool_ident.map(|ident| ident.span), tool_name: tool_name.unwrap(), lint_name: pprust::path_to_string(&meta_item.path), From 1974186d3285d5ed21e7c9427a1c5e7a93d99596 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Sat, 20 Aug 2022 15:48:03 -0400 Subject: [PATCH 08/10] migrate: `rustc_lint::context` --- .../locales/en-US/lint.ftl | 13 +++ compiler/rustc_lint/src/context.rs | 96 ++++++++----------- compiler/rustc_lint/src/errors.rs | 82 +++++++++++++++- 3 files changed, 132 insertions(+), 59 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 9ae226857d3ee..27ad3e4536601 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -414,3 +414,16 @@ lint_reason_must_come_last = reason in lint attribute must come last lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` .help = add `#![register_tool({$tool_name})]` to the crate root + +lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´ + +lint_requested_level = requested on the command line with `{$level} {$lint_name}` + +lint_check_name_unknown = unknown lint: `{$lint_name}` + .help = did you mean: `{$suggestion}` + +lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}` + +lint_check_name_warning = {$msg} + +lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name} diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index b95fc341db656..a1fdc557f9298 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -1,3 +1,6 @@ +// #![deny(rustc::diagnostic_outside_of_impl)] +// #![deny(rustc::untranslatable_diagnostic)] +// //! Implementation of lint checking. //! //! The lint checking is mostly consolidated into one pass which runs @@ -16,12 +19,16 @@ use self::TargetLint::*; +use crate::errors::{ + CheckNameDeprecated, CheckNameUnknown, CheckNameUnknownTool, CheckNameWarning, RequestedLevel, + UnsupportedGroup, +}; use crate::levels::LintLevelsBuilder; use crate::passes::{EarlyLintPassObject, LateLintPassObject}; use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync; -use rustc_errors::{add_elided_lifetime_in_path_suggestion, struct_span_err}; +use rustc_errors::add_elided_lifetime_in_path_suggestion; use rustc_errors::{ Applicability, DecorateLint, LintDiagnosticBuilder, MultiSpan, SuggestionStyle, }; @@ -39,7 +46,7 @@ use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintI use rustc_session::Session; use rustc_span::lev_distance::find_best_match_for_name; use rustc_span::symbol::{sym, Ident, Symbol}; -use rustc_span::{BytePos, Span, DUMMY_SP}; +use rustc_span::{BytePos, Span}; use rustc_target::abi; use tracing::debug; @@ -326,68 +333,41 @@ impl LintStore { ) { let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name); if lint_name_only == crate::WARNINGS.name_lower() && matches!(level, Level::ForceWarn(_)) { - struct_span_err!( - sess, - DUMMY_SP, - E0602, - "`{}` lint group is not supported with ´--force-warn´", - crate::WARNINGS.name_lower() - ) - .emit(); + sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() }); return; } - let db = match self.check_lint_name(lint_name_only, tool_name, registered_tools) { - CheckLintNameResult::Ok(_) => None, - CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)), + let lint_name = lint_name.to_string(); + match self.check_lint_name(lint_name_only, tool_name, registered_tools) { + CheckLintNameResult::Warning(msg, _) => { + sess.emit_warning(CheckNameWarning { + msg, + sub: RequestedLevel { level, lint_name }, + }); + } CheckLintNameResult::NoLint(suggestion) => { - let mut err = - struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name); - - if let Some(suggestion) = suggestion { - err.help(&format!("did you mean: `{}`", suggestion)); + sess.emit_err(CheckNameUnknown { + lint_name: lint_name.clone(), + suggestion, + sub: RequestedLevel { level, lint_name }, + }); + } + CheckLintNameResult::Tool(result) => { + if let Err((Some(_), new_name)) = result { + sess.emit_warning(CheckNameDeprecated { + lint_name: lint_name.clone(), + new_name, + sub: RequestedLevel { level, lint_name }, + }); } - - Some(err.forget_guarantee()) } - CheckLintNameResult::Tool(result) => match result { - Err((Some(_), new_name)) => Some(sess.struct_warn(&format!( - "lint name `{}` is deprecated \ - and does not have an effect anymore. \ - Use: {}", - lint_name, new_name - ))), - _ => None, - }, - CheckLintNameResult::NoTool => Some( - struct_span_err!( - sess, - DUMMY_SP, - E0602, - "unknown lint tool: `{}`", - tool_name.unwrap() - ) - .forget_guarantee(), - ), + CheckLintNameResult::NoTool => { + sess.emit_err(CheckNameUnknownTool { + tool_name: tool_name.unwrap(), + sub: RequestedLevel { level, lint_name }, + }); + } + _ => {} }; - - if let Some(mut db) = db { - let msg = format!( - "requested on the command line with `{} {}`", - match level { - Level::Allow => "-A", - Level::Warn => "-W", - Level::ForceWarn(_) => "--force-warn", - Level::Deny => "-D", - Level::Forbid => "-F", - Level::Expect(_) => { - unreachable!("lints with the level of `expect` should not run this code"); - } - }, - lint_name - ); - db.note(&msg); - db.emit(); - } } /// True if this symbol represents a lint group name. diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index 0f8b8e174d834..a6859311cfd63 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -1,5 +1,6 @@ -use rustc_errors::{fluent, AddSubdiagnostic}; +use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed}; use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; +use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic}; use rustc_span::{Span, Symbol}; #[derive(SessionDiagnostic)] @@ -80,3 +81,82 @@ pub struct BuiltinEllpisisInclusiveRangePatterns { pub suggestion: Span, pub replace: String, } + +pub struct RequestedLevel { + pub level: Level, + pub lint_name: String, +} + +impl AddSubdiagnostic for RequestedLevel { + fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) { + diag.note(fluent::lint::requested_level); + diag.set_arg( + "level", + match self.level { + Level::Allow => "-A", + Level::Warn => "-W", + Level::ForceWarn(_) => "--force-warn", + Level::Deny => "-D", + Level::Forbid => "-F", + Level::Expect(_) => { + unreachable!("lints with the level of `expect` should not run this code"); + } + }, + ); + diag.set_arg("lint_name", self.lint_name); + } +} + +#[derive(SessionDiagnostic)] +#[error(lint::unsupported_group, code = "E0602")] +pub struct UnsupportedGroup { + pub lint_group: String, +} + +pub struct CheckNameUnknown { + pub lint_name: String, + pub suggestion: Option, + pub sub: RequestedLevel, +} + +impl SessionDiagnostic<'_> for CheckNameUnknown { + fn into_diagnostic( + self, + sess: &ParseSess, + ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag = sess.struct_err(fluent::lint::check_name_unknown); + diag.code(rustc_errors::error_code!(E0602)); + if let Some(suggestion) = self.suggestion { + diag.help(fluent::lint::help); + diag.set_arg("suggestion", suggestion); + } + diag.set_arg("lint_name", self.lint_name); + diag.subdiagnostic(self.sub); + diag + } +} + +#[derive(SessionDiagnostic)] +#[error(lint::check_name_unknown_tool, code = "E0602")] +pub struct CheckNameUnknownTool { + pub tool_name: Symbol, + #[subdiagnostic] + pub sub: RequestedLevel, +} + +#[derive(SessionDiagnostic)] +#[warning(lint::check_name_warning)] +pub struct CheckNameWarning { + pub msg: String, + #[subdiagnostic] + pub sub: RequestedLevel, +} + +#[derive(SessionDiagnostic)] +#[warning(lint::check_name_deprecated)] +pub struct CheckNameDeprecated { + pub lint_name: String, + pub new_name: String, + #[subdiagnostic] + pub sub: RequestedLevel, +} From 257cf03e2caa673ca1248727c09b4a08d093d706 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Mon, 22 Aug 2022 08:33:47 -0400 Subject: [PATCH 09/10] refactor: migrate to kind-less `SessionDiagnostic` derives --- compiler/rustc_lint/src/errors.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_lint/src/errors.rs b/compiler/rustc_lint/src/errors.rs index a6859311cfd63..606d8bda8aafe 100644 --- a/compiler/rustc_lint/src/errors.rs +++ b/compiler/rustc_lint/src/errors.rs @@ -4,7 +4,7 @@ use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic}; use rustc_span::{Span, Symbol}; #[derive(SessionDiagnostic)] -#[error(lint::overruled_attribute, code = "E0453")] +#[diag(lint::overruled_attribute, code = "E0453")] pub struct OverruledAttribute { #[primary_span] pub span: Span, @@ -43,7 +43,7 @@ impl AddSubdiagnostic for OverruledAttributeSub { } #[derive(SessionDiagnostic)] -#[error(lint::malformed_attribute, code = "E0452")] +#[diag(lint::malformed_attribute, code = "E0452")] pub struct MalformedAttribute { #[primary_span] pub span: Span, @@ -62,7 +62,7 @@ pub enum MalformedAttributeSub { } #[derive(SessionDiagnostic)] -#[error(lint::unknown_tool_in_scoped_lint, code = "E0710")] +#[diag(lint::unknown_tool_in_scoped_lint, code = "E0710")] pub struct UnknownToolInScopedLint { #[primary_span] pub span: Option, @@ -73,7 +73,7 @@ pub struct UnknownToolInScopedLint { } #[derive(SessionDiagnostic)] -#[error(lint::builtin_ellipsis_inclusive_range_patterns, code = "E0783")] +#[diag(lint::builtin_ellipsis_inclusive_range_patterns, code = "E0783")] pub struct BuiltinEllpisisInclusiveRangePatterns { #[primary_span] pub span: Span, @@ -108,7 +108,7 @@ impl AddSubdiagnostic for RequestedLevel { } #[derive(SessionDiagnostic)] -#[error(lint::unsupported_group, code = "E0602")] +#[diag(lint::unsupported_group, code = "E0602")] pub struct UnsupportedGroup { pub lint_group: String, } @@ -137,7 +137,7 @@ impl SessionDiagnostic<'_> for CheckNameUnknown { } #[derive(SessionDiagnostic)] -#[error(lint::check_name_unknown_tool, code = "E0602")] +#[diag(lint::check_name_unknown_tool, code = "E0602")] pub struct CheckNameUnknownTool { pub tool_name: Symbol, #[subdiagnostic] @@ -145,7 +145,7 @@ pub struct CheckNameUnknownTool { } #[derive(SessionDiagnostic)] -#[warning(lint::check_name_warning)] +#[diag(lint::check_name_warning)] pub struct CheckNameWarning { pub msg: String, #[subdiagnostic] @@ -153,7 +153,7 @@ pub struct CheckNameWarning { } #[derive(SessionDiagnostic)] -#[warning(lint::check_name_deprecated)] +#[diag(lint::check_name_deprecated)] pub struct CheckNameDeprecated { pub lint_name: String, pub new_name: String, From 1693993d8ff16b1a5d288bc858db052902126053 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Tue, 23 Aug 2022 10:41:02 -0400 Subject: [PATCH 10/10] cleanup: commented lints --- compiler/rustc_lint/src/builtin.rs | 3 --- compiler/rustc_lint/src/context.rs | 3 --- compiler/rustc_lint/src/levels.rs | 3 --- compiler/rustc_lint/src/lib.rs | 2 -- 4 files changed, 11 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 507abad0d5674..af7ef96e485b5 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1,6 +1,3 @@ -// #![deny(rustc::diagnostic_outside_of_impl)] -// #![deny(rustc::untranslatable_diagnostic)] -// //! Lints in the Rust compiler. //! //! This contains lints which can feasibly be implemented as their own diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index a1fdc557f9298..e0679a6112a4c 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -1,6 +1,3 @@ -// #![deny(rustc::diagnostic_outside_of_impl)] -// #![deny(rustc::untranslatable_diagnostic)] -// //! Implementation of lint checking. //! //! The lint checking is mostly consolidated into one pass which runs diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 41d89229009ba..89409b58f88b9 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -1,6 +1,3 @@ -// #![deny(rustc::diagnostic_outside_of_impl)] -// #![deny(rustc::untranslatable_diagnostic)] -// use crate::context::{CheckLintNameResult, LintStore}; use crate::late::unerased_lint_store; use rustc_ast as ast; diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 23fd5d5eea081..f34e062fd12a9 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -36,8 +36,6 @@ #![feature(let_else)] #![feature(never_type)] #![recursion_limit = "256"] -// #![deny(rustc::diagnostic_outside_of_impl)] -// #![deny(rustc::untranslatable_diagnostic)] #[macro_use] extern crate rustc_middle;