Skip to content

Commit 9d322e2

Browse files
Auto merge of #149984 - fmease:mv-var-to-dyn-buf-lints-final-ish, r=<try>
[WIP] Yet again move some more early buffered lints to dyn lint diagnostics
2 parents 08de25c + 475c181 commit 9d322e2

File tree

23 files changed

+359
-615
lines changed

23 files changed

+359
-615
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ ast_passes_coroutine_and_c_variadic = functions cannot be both `{$coroutine_kind
107107
.const = `{$coroutine_kind}` because of this
108108
.variadic = C-variadic because of this
109109
110+
ast_passes_deprecated_where_clause_location = where clause not allowed here
111+
.note = see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
112+
110113
ast_passes_equality_in_where = equality constraints are not yet supported in `where` clauses
111114
.label = not supported
112115
.suggestion = if `{$ident}` is an associated type you're trying to set, use the associated type binding syntax
@@ -237,6 +240,8 @@ ast_passes_missing_unsafe_on_extern_lint = extern blocks should be unsafe
237240
ast_passes_module_nonascii = trying to load file for module `{$name}` with non-ascii identifier name
238241
.help = consider using the `#[path]` attribute to specify filesystem path
239242
243+
ast_passes_move_leading_ty_alias_where_clause = move it to the end of the type declaration
244+
240245
ast_passes_negative_bound_not_supported =
241246
negative bounds are not supported
242247
@@ -259,6 +264,7 @@ ast_passes_out_of_order_params = {$param_ord} parameters must be declared prior
259264
260265
ast_passes_pattern_in_bodiless = patterns aren't allowed in functions without bodies
261266
.label = pattern not allowed in function without body
267+
.remove_mut_sugg = remove `mut` from the parameter
262268
263269
ast_passes_pattern_in_fn_pointer = patterns aren't allowed in function pointer types
264270
@@ -270,6 +276,8 @@ ast_passes_precise_capturing_duplicated = duplicate `use<...>` precise capturing
270276
271277
ast_passes_precise_capturing_not_allowed_here = `use<...>` precise capturing syntax not allowed in {$loc}
272278
279+
ast_passes_remove_leading_ty_alias_where_clause = remove this `where`
280+
273281
ast_passes_static_without_body =
274282
free static item without body
275283
.suggestion = provide a definition for the static
@@ -322,6 +330,10 @@ ast_passes_unsafe_negative_impl = negative impls cannot be unsafe
322330
ast_passes_unsafe_static =
323331
static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
324332
333+
ast_passes_unused_visibilities = visibility qualifiers have no effect on `const _` declarations
334+
.note = `const _` does not declare a name, so there is nothing for the qualifier to apply to
335+
.suggestion = remove the qualifier
336+
325337
ast_passes_visibility_not_permitted =
326338
visibility qualifiers are not permitted here
327339
.enum_variant = enum variants and their fields always share the visibility of the enum they are in
@@ -336,5 +348,3 @@ ast_passes_where_clause_after_type_alias = where clauses are not allowed after t
336348
337349
ast_passes_where_clause_before_type_alias = where clauses are not allowed before the type for type aliases
338350
.note = see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
339-
.remove_suggestion = remove this `where`
340-
.move_suggestion = move it to the end of the type declaration

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use rustc_data_structures::fx::FxIndexMap;
3030
use rustc_errors::{DiagCtxtHandle, 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,
@@ -166,13 +165,13 @@ impl<'a> AstValidator<'a> {
166165
state.print_where_predicate(p);
167166
}
168167

169-
errors::WhereClauseBeforeTypeAliasSugg::Move {
168+
errors::ModifyLeadingTyAliasWhereClause::Move {
170169
left: span,
171170
snippet: state.s.eof(),
172171
right: ty_alias.after_where_clause.span.shrink_to_hi(),
173172
}
174173
} else {
175-
errors::WhereClauseBeforeTypeAliasSugg::Remove { span }
174+
errors::ModifyLeadingTyAliasWhereClause::Remove { span }
176175
};
177176

178177
Err(errors::WhereClauseBeforeTypeAlias { span, sugg })
@@ -230,14 +229,12 @@ impl<'a> AstValidator<'a> {
230229
});
231230
}
232231

233-
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
232+
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>)) {
234233
for Param { pat, .. } in &decl.inputs {
235234
match pat.kind {
236235
PatKind::Missing | PatKind::Ident(BindingMode::NONE, _, None) | PatKind::Wild => {}
237-
PatKind::Ident(BindingMode::MUT, ident, None) => {
238-
report_err(pat.span, Some(ident), true)
239-
}
240-
_ => report_err(pat.span, None, false),
236+
PatKind::Ident(BindingMode::MUT, ident, None) => report_err(pat.span, Some(ident)),
237+
_ => report_err(pat.span, None),
241238
}
242239
}
243240
}
@@ -929,7 +926,7 @@ impl<'a> AstValidator<'a> {
929926
TyKind::FnPtr(bfty) => {
930927
self.check_fn_ptr_safety(bfty.decl_span, bfty.safety);
931928
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
932-
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
929+
Self::check_decl_no_pat(&bfty.decl, |span, _| {
933930
self.dcx().emit_err(errors::PatternFnPointer { span });
934931
});
935932
if let Extern::Implicit(extern_span) = bfty.ext {
@@ -1360,7 +1357,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13601357
UNUSED_VISIBILITIES,
13611358
item.id,
13621359
item.vis.span,
1363-
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1360+
errors::UnusedVisibility { span: item.vis.span },
13641361
)
13651362
}
13661363

@@ -1646,25 +1643,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16461643

16471644
// Functions without bodies cannot have patterns.
16481645
if let FnKind::Fn(ctxt, _, Fn { body: None, sig, .. }) = fk {
1649-
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
1650-
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
1651-
if let Some(ident) = ident {
1652-
self.lint_buffer.buffer_lint(
1653-
PATTERNS_IN_FNS_WITHOUT_BODY,
1654-
id,
1655-
span,
1656-
BuiltinLintDiag::PatternsInFnsWithoutBody {
1657-
span,
1658-
ident,
1659-
is_foreign: matches!(ctxt, FnCtxt::Foreign),
1660-
},
1661-
)
1662-
}
1663-
} else {
1664-
match ctxt {
1665-
FnCtxt::Foreign => self.dcx().emit_err(errors::PatternInForeign { span }),
1666-
_ => self.dcx().emit_err(errors::PatternInBodiless { span }),
1667-
};
1646+
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| match ctxt {
1647+
FnCtxt::Assoc(_) if let Some(mut_ident) = mut_ident => {
1648+
self.lint_buffer.buffer_lint(
1649+
PATTERNS_IN_FNS_WITHOUT_BODY,
1650+
id,
1651+
span,
1652+
errors::PatternInBodilessLint { removal: span.until(mut_ident.span) },
1653+
);
1654+
}
1655+
FnCtxt::Foreign => {
1656+
self.dcx().emit_err(errors::PatternInForeign { span });
1657+
}
1658+
_ => {
1659+
self.dcx().emit_err(errors::PatternInBodiless { span });
16681660
}
16691661
});
16701662
}
@@ -1728,17 +1720,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
17281720
if let AssocItemKind::Type(ty_alias) = &item.kind
17291721
&& let Err(err) = self.check_type_alias_where_clause_location(ty_alias)
17301722
{
1731-
let sugg = match err.sugg {
1732-
errors::WhereClauseBeforeTypeAliasSugg::Remove { .. } => None,
1733-
errors::WhereClauseBeforeTypeAliasSugg::Move { snippet, right, .. } => {
1734-
Some((right, snippet))
1735-
}
1736-
};
17371723
self.lint_buffer.buffer_lint(
17381724
DEPRECATED_WHERE_CLAUSE_LOCATION,
17391725
item.id,
17401726
err.span,
1741-
BuiltinLintDiag::DeprecatedWhereclauseLocation(err.span, sugg),
1727+
errors::DeprecatedWhereClauseLocation { sugg: err.sugg },
17421728
);
17431729
}
17441730

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,18 +575,30 @@ pub(crate) struct WhereClauseBeforeTypeAlias {
575575
#[primary_span]
576576
pub span: Span,
577577
#[subdiagnostic]
578-
pub sugg: WhereClauseBeforeTypeAliasSugg,
578+
pub sugg: ModifyLeadingTyAliasWhereClause,
579+
}
580+
581+
#[derive(LintDiagnostic)]
582+
#[diag(ast_passes_deprecated_where_clause_location)]
583+
#[note]
584+
pub(crate) struct DeprecatedWhereClauseLocation {
585+
#[subdiagnostic]
586+
pub sugg: ModifyLeadingTyAliasWhereClause,
579587
}
580588

581589
#[derive(Subdiagnostic)]
582-
pub(crate) enum WhereClauseBeforeTypeAliasSugg {
583-
#[suggestion(ast_passes_remove_suggestion, applicability = "machine-applicable", code = "")]
590+
pub(crate) enum ModifyLeadingTyAliasWhereClause {
591+
#[suggestion(
592+
ast_passes_remove_leading_ty_alias_where_clause,
593+
applicability = "machine-applicable",
594+
code = ""
595+
)]
584596
Remove {
585597
#[primary_span]
586598
span: Span,
587599
},
588600
#[multipart_suggestion(
589-
ast_passes_move_suggestion,
601+
ast_passes_move_leading_ty_alias_where_clause,
590602
applicability = "machine-applicable",
591603
style = "verbose"
592604
)]
@@ -743,7 +755,6 @@ pub(crate) struct CVariadicNotSupported<'a> {
743755

744756
#[derive(Diagnostic)]
745757
#[diag(ast_passes_pattern_in_foreign, code = E0130)]
746-
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
747758
pub(crate) struct PatternInForeign {
748759
#[primary_span]
749760
#[label]
@@ -752,13 +763,19 @@ pub(crate) struct PatternInForeign {
752763

753764
#[derive(Diagnostic)]
754765
#[diag(ast_passes_pattern_in_bodiless, code = E0642)]
755-
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
756766
pub(crate) struct PatternInBodiless {
757767
#[primary_span]
758768
#[label]
759769
pub span: Span,
760770
}
761771

772+
#[derive(LintDiagnostic)]
773+
#[diag(ast_passes_pattern_in_bodiless)]
774+
pub(crate) struct PatternInBodilessLint {
775+
#[suggestion(ast_passes_remove_mut_sugg, code = "", applicability = "machine-applicable")]
776+
pub removal: Span,
777+
}
778+
762779
#[derive(Diagnostic)]
763780
#[diag(ast_passes_equality_in_where)]
764781
#[note]
@@ -990,3 +1007,11 @@ pub(crate) struct AbiX86Interrupt {
9901007
pub spans: Vec<Span>,
9911008
pub param_count: usize,
9921009
}
1010+
1011+
#[derive(LintDiagnostic)]
1012+
#[diag(ast_passes_unused_visibilities)]
1013+
#[note]
1014+
pub(crate) struct UnusedVisibility {
1015+
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
1016+
pub span: Span,
1017+
}

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ builtin_macros_naked_functions_testing_attribute =
270270
.label = function marked with testing attribute here
271271
.naked_attribute = `#[unsafe(naked)]` is incompatible with testing attributes
272272
273+
builtin_macros_named_argument_used_positionally = named argument `{$named_arg_name}` is not used by name
274+
.label_named_arg = this named argument is referred to by position in formatting string
275+
.label_position_arg = this formatting argument uses named argument `{$named_arg_name}` by position
276+
.suggestion = use the named argument by name to avoid ambiguity
277+
273278
builtin_macros_no_default_variant = `#[derive(Default)]` on enum with no `#[default]`
274279
.label = this enum needs a unit variant marked with `#[default]`
275280
.suggestion = make this unit variant default by placing `#[default]` on it

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use rustc_errors::codes::*;
22
use rustc_errors::{
3-
Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan, SingleLabelManySpans,
4-
Subdiagnostic,
3+
Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, LintDiagnostic,
4+
MultiSpan, SingleLabelManySpans, Subdiagnostic,
55
};
66
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
88

9+
use crate::fluent_generated as fluent;
10+
911
#[derive(LintDiagnostic)]
1012
#[diag(builtin_macros_avoid_intel_syntax)]
1113
pub(crate) struct AvoidIntelSyntax;
@@ -1058,3 +1060,35 @@ pub(crate) struct EiiMacroExpectedMaxOneArgument {
10581060
pub span: Span,
10591061
pub name: String,
10601062
}
1063+
1064+
pub(crate) struct NamedArgumentUsedPositionally {
1065+
pub position_sp_to_replace: Option<Span>,
1066+
pub position_sp_for_msg: Option<Span>,
1067+
pub named_arg_sp: Span,
1068+
pub named_arg_name: Symbol,
1069+
pub is_formatting_arg: bool,
1070+
}
1071+
1072+
impl<'a> LintDiagnostic<'a, ()> for NamedArgumentUsedPositionally {
1073+
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
1074+
diag.primary_message(fluent::builtin_macros_named_argument_used_positionally);
1075+
diag.span_label(self.named_arg_sp, fluent::builtin_macros_label_named_arg);
1076+
if let Some(span) = self.position_sp_for_msg {
1077+
diag.span_label(span, fluent::builtin_macros_label_position_arg);
1078+
}
1079+
diag.arg("named_arg_name", self.named_arg_name);
1080+
1081+
if let Some(positional_arg_to_replace) = self.position_sp_to_replace {
1082+
let mut name = self.named_arg_name.to_string();
1083+
if self.is_formatting_arg {
1084+
name.push('$')
1085+
};
1086+
diag.span_suggestion_verbose(
1087+
positional_arg_to_replace,
1088+
fluent::builtin_macros_suggestion,
1089+
name,
1090+
Applicability::MaybeIncorrect,
1091+
);
1092+
}
1093+
}
1094+
}

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_errors::{
1414
pluralize,
1515
};
1616
use rustc_expand::base::*;
17+
use rustc_lint_defs::LintId;
1718
use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
18-
use rustc_lint_defs::{BuiltinLintDiag, LintId};
1919
use rustc_parse::exp;
2020
use rustc_parse_format as parse;
2121
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
@@ -589,11 +589,11 @@ fn make_format_args(
589589
span: Some(arg_name.span.into()),
590590
node_id: rustc_ast::CRATE_NODE_ID,
591591
lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
592-
diagnostic: BuiltinLintDiag::NamedArgumentUsedPositionally {
592+
diagnostic: errors::NamedArgumentUsedPositionally {
593593
position_sp_to_replace,
594594
position_sp_for_msg,
595595
named_arg_sp: arg_name.span,
596-
named_arg_name: arg_name.name.to_string(),
596+
named_arg_name: arg_name.name,
597597
is_formatting_arg: matches!(used_as, Width | Precision),
598598
}
599599
.into(),

0 commit comments

Comments
 (0)