From 33fbecc394c54faa8a5fcdd72603ddfe6eb5f1f8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 22 Mar 2026 18:35:02 +0100 Subject: [PATCH] Remove `BuiltinLintDiag` usage in `rustc_ast_passes` --- .../rustc_ast_passes/src/ast_validation.rs | 39 ++++++++--- compiler/rustc_ast_passes/src/errors.rs | 69 +++++++++++++++++++ compiler/rustc_lint/src/early/diagnostics.rs | 23 ------- compiler/rustc_lint/src/lints.rs | 69 ------------------- compiler/rustc_lint_defs/src/lib.rs | 8 --- 5 files changed, 98 insertions(+), 110 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index bdb09813112..dd14e914356 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -27,10 +27,9 @@ use rustc_ast::*; use rustc_ast_pretty::pprust::{self, State}; use rustc_attr_parsing::validate_attr; use rustc_data_structures::fx::FxIndexMap; -use rustc_errors::{DiagCtxtHandle, LintBuffer}; +use rustc_errors::{DiagCtxtHandle, Diagnostic, LintBuffer}; use rustc_feature::Features; use rustc_session::Session; -use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::builtin::{ DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN, PATTERNS_IN_FNS_WITHOUT_BODY, UNUSED_VISIBILITIES, @@ -1424,7 +1423,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { UNUSED_VISIBILITIES, item.id, item.vis.span, - BuiltinLintDiag::UnusedVisibility(item.vis.span), + errors::UnusedVisibility { span: item.vis.span }, ) } @@ -1731,14 +1730,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> { Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| { if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) { if let Some(ident) = ident { - self.lint_buffer.buffer_lint( + let is_foreign = matches!(ctxt, FnCtxt::Foreign); + self.lint_buffer.dyn_buffer_lint( PATTERNS_IN_FNS_WITHOUT_BODY, id, span, - BuiltinLintDiag::PatternsInFnsWithoutBody { - span, - ident, - is_foreign: matches!(ctxt, FnCtxt::Foreign), + move |dcx, level| { + let sub = errors::PatternsInFnsWithoutBodySub { ident, span }; + if is_foreign { + errors::PatternsInFnsWithoutBody::Foreign { sub } + } else { + errors::PatternsInFnsWithoutBody::Bodiless { sub } + } + .into_diag(dcx, level) }, ) } @@ -1828,11 +1832,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> { Some((right, snippet)) } }; - self.lint_buffer.buffer_lint( + let left_sp = err.span; + self.lint_buffer.dyn_buffer_lint( DEPRECATED_WHERE_CLAUSE_LOCATION, item.id, err.span, - BuiltinLintDiag::DeprecatedWhereclauseLocation(err.span, sugg), + move |dcx, level| { + let suggestion = match sugg { + Some((right_sp, sugg)) => { + errors::DeprecatedWhereClauseLocationSugg::MoveToEnd { + left: left_sp, + right: right_sp, + sugg, + } + } + None => { + errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp } + } + }; + errors::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level) + }, ); } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 390c1556f19..b3a22c0c995 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -1150,3 +1150,72 @@ pub(crate) struct RequiresRustAbi { #[label("not using the Rust ABI because of this")] pub extern_abi_span: Span, } + +#[derive(Diagnostic)] +#[diag("visibility qualifiers have no effect on `const _` declarations")] +#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")] +pub(crate) struct UnusedVisibility { + #[suggestion( + "remove the qualifier", + style = "short", + code = "", + applicability = "machine-applicable" + )] + pub span: Span, +} + +#[derive(Subdiagnostic)] +#[suggestion( + "remove `mut` from the parameter", + code = "{ident}", + applicability = "machine-applicable" +)] +pub(crate) struct PatternsInFnsWithoutBodySub { + #[primary_span] + pub span: Span, + + pub ident: Ident, +} + +#[derive(Diagnostic)] +pub(crate) enum PatternsInFnsWithoutBody { + #[diag("patterns aren't allowed in foreign function declarations")] + Foreign { + #[subdiagnostic] + sub: PatternsInFnsWithoutBodySub, + }, + #[diag("patterns aren't allowed in functions without bodies")] + Bodiless { + #[subdiagnostic] + sub: PatternsInFnsWithoutBodySub, + }, +} + +#[derive(Diagnostic)] +#[diag("where clause not allowed here")] +#[note("see issue #89122 for more information")] +pub(crate) struct DeprecatedWhereClauseLocation { + #[subdiagnostic] + pub suggestion: DeprecatedWhereClauseLocationSugg, +} + +#[derive(Subdiagnostic)] +pub(crate) enum DeprecatedWhereClauseLocationSugg { + #[multipart_suggestion( + "move it to the end of the type declaration", + applicability = "machine-applicable" + )] + MoveToEnd { + #[suggestion_part(code = "")] + left: Span, + #[suggestion_part(code = "{sugg}")] + right: Span, + + sugg: String, + }, + #[suggestion("remove this `where`", code = "", applicability = "machine-applicable")] + RemoveWhere { + #[primary_span] + span: Span, + }, +} diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index d5cd26dd75e..c61bb7307f6 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -116,26 +116,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> { stability::Deprecated { sub, kind: "macro".to_owned(), path, note, since_kind } .into_diag(dcx, level) } - BuiltinLintDiag::PatternsInFnsWithoutBody { span: remove_span, ident, is_foreign } => { - let sub = lints::PatternsInFnsWithoutBodySub { ident, span: remove_span }; - if is_foreign { - lints::PatternsInFnsWithoutBody::Foreign { sub } - } else { - lints::PatternsInFnsWithoutBody::Bodiless { sub } - } - .into_diag(dcx, level) - } - BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => { - let suggestion = match sugg { - Some((right_sp, sugg)) => lints::DeprecatedWhereClauseLocationSugg::MoveToEnd { - left: left_sp, - right: right_sp, - sugg, - }, - None => lints::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp }, - }; - lints::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level) - } BuiltinLintDiag::SingleUseLifetime { param_span, use_span: Some((use_span, elide)), @@ -257,9 +237,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> { BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => { lints::UnusedCrateDependency { extern_crate, local_crate }.into_diag(dcx, level) } - BuiltinLintDiag::UnusedVisibility(span) => { - lints::UnusedVisibility { span }.into_diag(dcx, level) - } BuiltinLintDiag::AttributeLint(kind) => { DecorateAttrLint { sess: self.sess, tcx: self.tcx, diagnostic: &kind } .into_diag(dcx, level) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index d5b5a50d281..c66654afc9d 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -3125,62 +3125,6 @@ pub(crate) enum RedundantImportSub { }, } -#[derive(Diagnostic)] -pub(crate) enum PatternsInFnsWithoutBody { - #[diag("patterns aren't allowed in foreign function declarations")] - Foreign { - #[subdiagnostic] - sub: PatternsInFnsWithoutBodySub, - }, - #[diag("patterns aren't allowed in functions without bodies")] - Bodiless { - #[subdiagnostic] - sub: PatternsInFnsWithoutBodySub, - }, -} - -#[derive(Subdiagnostic)] -#[suggestion( - "remove `mut` from the parameter", - code = "{ident}", - applicability = "machine-applicable" -)] -pub(crate) struct PatternsInFnsWithoutBodySub { - #[primary_span] - pub span: Span, - - pub ident: Ident, -} - -#[derive(Diagnostic)] -#[diag("where clause not allowed here")] -#[note("see issue #89122 for more information")] -pub(crate) struct DeprecatedWhereClauseLocation { - #[subdiagnostic] - pub suggestion: DeprecatedWhereClauseLocationSugg, -} - -#[derive(Subdiagnostic)] -pub(crate) enum DeprecatedWhereClauseLocationSugg { - #[multipart_suggestion( - "move it to the end of the type declaration", - applicability = "machine-applicable" - )] - MoveToEnd { - #[suggestion_part(code = "")] - left: Span, - #[suggestion_part(code = "{sugg}")] - right: Span, - - sugg: String, - }, - #[suggestion("remove this `where`", code = "", applicability = "machine-applicable")] - RemoveWhere { - #[primary_span] - span: Span, - }, -} - #[derive(Diagnostic)] #[diag("lifetime parameter `{$ident}` only used once")] pub(crate) struct SingleUseLifetime { @@ -3644,19 +3588,6 @@ pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion { pub right: Span, } -#[derive(Diagnostic)] -#[diag("visibility qualifiers have no effect on `const _` declarations")] -#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")] -pub(crate) struct UnusedVisibility { - #[suggestion( - "remove the qualifier", - style = "short", - code = "", - applicability = "machine-applicable" - )] - pub span: Span, -} - #[derive(Diagnostic)] #[diag("doc alias is duplicated")] pub(crate) struct DocAliasDuplicated { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index e2e4ffc64be..f3317017daa 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -673,13 +673,6 @@ pub enum BuiltinLintDiag { path: String, since_kind: DeprecatedSinceKind, }, - PatternsInFnsWithoutBody { - span: Span, - ident: Ident, - is_foreign: bool, - }, - /// `##` or `#"` in edition < 2024. - DeprecatedWhereclauseLocation(Span, Option<(Span, String)>), SingleUseLifetime { /// Span of the parameter which declares this lifetime. param_span: Span, @@ -737,7 +730,6 @@ pub enum BuiltinLintDiag { extern_crate: Symbol, local_crate: Symbol, }, - UnusedVisibility(Span), AttributeLint(AttributeLintKind), UnreachableCfg { span: Span,