Skip to content

Commit d7beee5

Browse files
Rollup merge of rust-lang#154224 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer
Remove more `BuiltinLintDiag` variants Part of rust-lang#153099. r? @JonathanBrouwer
2 parents e158c29 + 2fcd8a7 commit d7beee5

8 files changed

Lines changed: 132 additions & 132 deletions

File tree

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ use rustc_ast::*;
2727
use rustc_ast_pretty::pprust::{self, State};
2828
use rustc_attr_parsing::validate_attr;
2929
use rustc_data_structures::fx::FxIndexMap;
30-
use rustc_errors::{DiagCtxtHandle, LintBuffer};
30+
use rustc_errors::{DiagCtxtHandle, Diagnostic, LintBuffer};
3131
use rustc_feature::Features;
3232
use rustc_session::Session;
33-
use rustc_session::lint::BuiltinLintDiag;
3433
use rustc_session::lint::builtin::{
3534
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,
3635
PATTERNS_IN_FNS_WITHOUT_BODY, UNUSED_VISIBILITIES,
@@ -1424,7 +1423,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14241423
UNUSED_VISIBILITIES,
14251424
item.id,
14261425
item.vis.span,
1427-
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1426+
errors::UnusedVisibility { span: item.vis.span },
14281427
)
14291428
}
14301429

@@ -1731,14 +1730,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
17311730
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
17321731
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
17331732
if let Some(ident) = ident {
1734-
self.lint_buffer.buffer_lint(
1733+
let is_foreign = matches!(ctxt, FnCtxt::Foreign);
1734+
self.lint_buffer.dyn_buffer_lint(
17351735
PATTERNS_IN_FNS_WITHOUT_BODY,
17361736
id,
17371737
span,
1738-
BuiltinLintDiag::PatternsInFnsWithoutBody {
1739-
span,
1740-
ident,
1741-
is_foreign: matches!(ctxt, FnCtxt::Foreign),
1738+
move |dcx, level| {
1739+
let sub = errors::PatternsInFnsWithoutBodySub { ident, span };
1740+
if is_foreign {
1741+
errors::PatternsInFnsWithoutBody::Foreign { sub }
1742+
} else {
1743+
errors::PatternsInFnsWithoutBody::Bodiless { sub }
1744+
}
1745+
.into_diag(dcx, level)
17421746
},
17431747
)
17441748
}
@@ -1828,11 +1832,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
18281832
Some((right, snippet))
18291833
}
18301834
};
1831-
self.lint_buffer.buffer_lint(
1835+
let left_sp = err.span;
1836+
self.lint_buffer.dyn_buffer_lint(
18321837
DEPRECATED_WHERE_CLAUSE_LOCATION,
18331838
item.id,
18341839
err.span,
1835-
BuiltinLintDiag::DeprecatedWhereclauseLocation(err.span, sugg),
1840+
move |dcx, level| {
1841+
let suggestion = match sugg {
1842+
Some((right_sp, sugg)) => {
1843+
errors::DeprecatedWhereClauseLocationSugg::MoveToEnd {
1844+
left: left_sp,
1845+
right: right_sp,
1846+
sugg,
1847+
}
1848+
}
1849+
None => {
1850+
errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp }
1851+
}
1852+
};
1853+
errors::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level)
1854+
},
18361855
);
18371856
}
18381857

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,72 @@ pub(crate) struct RequiresRustAbi {
11501150
#[label("not using the Rust ABI because of this")]
11511151
pub extern_abi_span: Span,
11521152
}
1153+
1154+
#[derive(Diagnostic)]
1155+
#[diag("visibility qualifiers have no effect on `const _` declarations")]
1156+
#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")]
1157+
pub(crate) struct UnusedVisibility {
1158+
#[suggestion(
1159+
"remove the qualifier",
1160+
style = "short",
1161+
code = "",
1162+
applicability = "machine-applicable"
1163+
)]
1164+
pub span: Span,
1165+
}
1166+
1167+
#[derive(Subdiagnostic)]
1168+
#[suggestion(
1169+
"remove `mut` from the parameter",
1170+
code = "{ident}",
1171+
applicability = "machine-applicable"
1172+
)]
1173+
pub(crate) struct PatternsInFnsWithoutBodySub {
1174+
#[primary_span]
1175+
pub span: Span,
1176+
1177+
pub ident: Ident,
1178+
}
1179+
1180+
#[derive(Diagnostic)]
1181+
pub(crate) enum PatternsInFnsWithoutBody {
1182+
#[diag("patterns aren't allowed in foreign function declarations")]
1183+
Foreign {
1184+
#[subdiagnostic]
1185+
sub: PatternsInFnsWithoutBodySub,
1186+
},
1187+
#[diag("patterns aren't allowed in functions without bodies")]
1188+
Bodiless {
1189+
#[subdiagnostic]
1190+
sub: PatternsInFnsWithoutBodySub,
1191+
},
1192+
}
1193+
1194+
#[derive(Diagnostic)]
1195+
#[diag("where clause not allowed here")]
1196+
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
1197+
pub(crate) struct DeprecatedWhereClauseLocation {
1198+
#[subdiagnostic]
1199+
pub suggestion: DeprecatedWhereClauseLocationSugg,
1200+
}
1201+
1202+
#[derive(Subdiagnostic)]
1203+
pub(crate) enum DeprecatedWhereClauseLocationSugg {
1204+
#[multipart_suggestion(
1205+
"move it to the end of the type declaration",
1206+
applicability = "machine-applicable"
1207+
)]
1208+
MoveToEnd {
1209+
#[suggestion_part(code = "")]
1210+
left: Span,
1211+
#[suggestion_part(code = "{sugg}")]
1212+
right: Span,
1213+
1214+
sugg: String,
1215+
},
1216+
#[suggestion("remove this `where`", code = "", applicability = "machine-applicable")]
1217+
RemoveWhere {
1218+
#[primary_span]
1219+
span: Span,
1220+
},
1221+
}

compiler/rustc_errors/src/decorate_diag.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,21 @@ impl LintBuffer {
8181
diagnostic: decorate.into(),
8282
});
8383
}
84+
85+
pub fn dyn_buffer_lint<
86+
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static,
87+
>(
88+
&mut self,
89+
lint: &'static Lint,
90+
node_id: NodeId,
91+
span: impl Into<MultiSpan>,
92+
callback: F,
93+
) {
94+
self.add_early_lint(BufferedEarlyLint {
95+
lint_id: LintId::of(lint),
96+
node_id,
97+
span: Some(span.into()),
98+
diagnostic: DecorateDiagCompat::Dynamic(Box::new(callback)),
99+
});
100+
}
84101
}

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,35 +116,16 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
116116
stability::Deprecated { sub, kind: "macro".to_owned(), path, note, since_kind }
117117
.into_diag(dcx, level)
118118
}
119-
BuiltinLintDiag::PatternsInFnsWithoutBody { span: remove_span, ident, is_foreign } => {
120-
let sub = lints::PatternsInFnsWithoutBodySub { ident, span: remove_span };
121-
if is_foreign {
122-
lints::PatternsInFnsWithoutBody::Foreign { sub }
123-
} else {
124-
lints::PatternsInFnsWithoutBody::Bodiless { sub }
125-
}
126-
.into_diag(dcx, level)
127-
}
128-
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
129-
let suggestion = match sugg {
130-
Some((right_sp, sugg)) => lints::DeprecatedWhereClauseLocationSugg::MoveToEnd {
131-
left: left_sp,
132-
right: right_sp,
133-
sugg,
134-
},
135-
None => lints::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp },
136-
};
137-
lints::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level)
138-
}
139119
BuiltinLintDiag::SingleUseLifetime {
140120
param_span,
141-
use_span: Some((use_span, elide)),
121+
use_span,
122+
elidable,
142123
deletion_span,
143124
ident,
144125
} => {
145126
debug!(?param_span, ?use_span, ?deletion_span);
146127
let suggestion = if let Some(deletion_span) = deletion_span {
147-
let (use_span, replace_lt) = if elide {
128+
let (use_span, replace_lt) = if elidable {
148129
let use_span =
149130
self.sess.source_map().span_extend_while_whitespace(use_span);
150131
(use_span, String::new())
@@ -165,9 +146,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
165146
lints::SingleUseLifetime { suggestion, param_span, use_span, ident }
166147
.into_diag(dcx, level)
167148
}
168-
BuiltinLintDiag::SingleUseLifetime { use_span: None, deletion_span, ident, .. } => {
169-
lints::UnusedLifetime { deletion_span, ident }.into_diag(dcx, level)
170-
}
171149
BuiltinLintDiag::NamedArgumentUsedPositionally {
172150
position_sp_to_replace,
173151
position_sp_for_msg,
@@ -257,9 +235,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
257235
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
258236
lints::UnusedCrateDependency { extern_crate, local_crate }.into_diag(dcx, level)
259237
}
260-
BuiltinLintDiag::UnusedVisibility(span) => {
261-
lints::UnusedVisibility { span }.into_diag(dcx, level)
262-
}
263238
BuiltinLintDiag::AttributeLint(kind) => {
264239
DecorateAttrLint { sess: self.sess, tcx: self.tcx, diagnostic: &kind }
265240
.into_diag(dcx, level)

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,62 +3125,6 @@ pub(crate) enum RedundantImportSub {
31253125
},
31263126
}
31273127

3128-
#[derive(Diagnostic)]
3129-
pub(crate) enum PatternsInFnsWithoutBody {
3130-
#[diag("patterns aren't allowed in foreign function declarations")]
3131-
Foreign {
3132-
#[subdiagnostic]
3133-
sub: PatternsInFnsWithoutBodySub,
3134-
},
3135-
#[diag("patterns aren't allowed in functions without bodies")]
3136-
Bodiless {
3137-
#[subdiagnostic]
3138-
sub: PatternsInFnsWithoutBodySub,
3139-
},
3140-
}
3141-
3142-
#[derive(Subdiagnostic)]
3143-
#[suggestion(
3144-
"remove `mut` from the parameter",
3145-
code = "{ident}",
3146-
applicability = "machine-applicable"
3147-
)]
3148-
pub(crate) struct PatternsInFnsWithoutBodySub {
3149-
#[primary_span]
3150-
pub span: Span,
3151-
3152-
pub ident: Ident,
3153-
}
3154-
3155-
#[derive(Diagnostic)]
3156-
#[diag("where clause not allowed here")]
3157-
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
3158-
pub(crate) struct DeprecatedWhereClauseLocation {
3159-
#[subdiagnostic]
3160-
pub suggestion: DeprecatedWhereClauseLocationSugg,
3161-
}
3162-
3163-
#[derive(Subdiagnostic)]
3164-
pub(crate) enum DeprecatedWhereClauseLocationSugg {
3165-
#[multipart_suggestion(
3166-
"move it to the end of the type declaration",
3167-
applicability = "machine-applicable"
3168-
)]
3169-
MoveToEnd {
3170-
#[suggestion_part(code = "")]
3171-
left: Span,
3172-
#[suggestion_part(code = "{sugg}")]
3173-
right: Span,
3174-
3175-
sugg: String,
3176-
},
3177-
#[suggestion("remove this `where`", code = "", applicability = "machine-applicable")]
3178-
RemoveWhere {
3179-
#[primary_span]
3180-
span: Span,
3181-
},
3182-
}
3183-
31843128
#[derive(Diagnostic)]
31853129
#[diag("lifetime parameter `{$ident}` only used once")]
31863130
pub(crate) struct SingleUseLifetime {
@@ -3205,15 +3149,6 @@ pub(crate) struct SingleUseLifetimeSugg {
32053149
pub replace_lt: String,
32063150
}
32073151

3208-
#[derive(Diagnostic)]
3209-
#[diag("lifetime parameter `{$ident}` never used")]
3210-
pub(crate) struct UnusedLifetime {
3211-
#[suggestion("elide the unused lifetime", code = "", applicability = "machine-applicable")]
3212-
pub deletion_span: Option<Span>,
3213-
3214-
pub ident: Ident,
3215-
}
3216-
32173152
#[derive(Diagnostic)]
32183153
#[diag("named argument `{$named_arg_name}` is not used by name")]
32193154
pub(crate) struct NamedArgumentUsedPositionally {
@@ -3644,19 +3579,6 @@ pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
36443579
pub right: Span,
36453580
}
36463581

3647-
#[derive(Diagnostic)]
3648-
#[diag("visibility qualifiers have no effect on `const _` declarations")]
3649-
#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")]
3650-
pub(crate) struct UnusedVisibility {
3651-
#[suggestion(
3652-
"remove the qualifier",
3653-
style = "short",
3654-
code = "",
3655-
applicability = "machine-applicable"
3656-
)]
3657-
pub span: Span,
3658-
}
3659-
36603582
#[derive(Diagnostic)]
36613583
#[diag("doc alias is duplicated")]
36623584
pub(crate) struct DocAliasDuplicated {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,6 @@ pub enum BuiltinLintDiag {
673673
path: String,
674674
since_kind: DeprecatedSinceKind,
675675
},
676-
PatternsInFnsWithoutBody {
677-
span: Span,
678-
ident: Ident,
679-
is_foreign: bool,
680-
},
681-
/// `##` or `#"` in edition < 2024.
682-
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
683676
SingleUseLifetime {
684677
/// Span of the parameter which declares this lifetime.
685678
param_span: Span,
@@ -688,7 +681,8 @@ pub enum BuiltinLintDiag {
688681
deletion_span: Option<Span>,
689682
/// Span of the single use, or None if the lifetime is never used.
690683
/// If true, the lifetime will be fully elided.
691-
use_span: Option<(Span, bool)>,
684+
use_span: Span,
685+
elidable: bool,
692686
ident: Ident,
693687
},
694688
NamedArgumentUsedPositionally {
@@ -737,7 +731,6 @@ pub enum BuiltinLintDiag {
737731
extern_crate: Symbol,
738732
local_crate: Symbol,
739733
},
740-
UnusedVisibility(Span),
741734
AttributeLint(AttributeLintKind),
742735
UnreachableCfg {
743736
span: Span,

compiler/rustc_resolve/src/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,3 +1583,12 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Ambiguity {
15831583
diag
15841584
}
15851585
}
1586+
1587+
#[derive(Diagnostic)]
1588+
#[diag("lifetime parameter `{$ident}` never used")]
1589+
pub(crate) struct UnusedLifetime {
1590+
#[suggestion("elide the unused lifetime", code = "", applicability = "machine-applicable")]
1591+
pub deletion_span: Option<Span>,
1592+
1593+
pub ident: Ident,
1594+
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,7 +3653,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
36533653
param.ident.span,
36543654
lint::BuiltinLintDiag::SingleUseLifetime {
36553655
param_span: param.ident.span,
3656-
use_span: Some((use_span, elidable)),
3656+
use_span,
3657+
elidable,
36573658
deletion_span,
36583659
ident: param.ident,
36593660
},
@@ -3669,12 +3670,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
36693670
lint::builtin::UNUSED_LIFETIMES,
36703671
param.id,
36713672
param.ident.span,
3672-
lint::BuiltinLintDiag::SingleUseLifetime {
3673-
param_span: param.ident.span,
3674-
use_span: None,
3675-
deletion_span,
3676-
ident: param.ident,
3677-
},
3673+
errors::UnusedLifetime { deletion_span, ident: param.ident },
36783674
);
36793675
}
36803676
}

0 commit comments

Comments
 (0)