Skip to content

Commit f962ca4

Browse files
authored
Unrolled build for #154912
Rollup merge of #154912 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer Remove `BuiltinLintDiag` Part of #153099. We're finally getting rid of `BuiltinLintDiag`! \o/ Next step, `AttributeLint`. :3 r? @JonathanBrouwer
2 parents 9004856 + caeaf19 commit f962ca4

File tree

13 files changed

+170
-257
lines changed

13 files changed

+170
-257
lines changed

‎compiler/rustc_attr_parsing/src/interface.rs‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::attrs::AttributeKind;
1010
use rustc_hir::lints::AttributeLintKind;
1111
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
1212
use rustc_session::Session;
13-
use rustc_session::lint::{BuiltinLintDiag, LintId};
13+
use rustc_session::lint::LintId;
1414
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
1515

1616
use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
@@ -124,14 +124,7 @@ impl<'sess> AttributeParser<'sess, Early> {
124124
target,
125125
OmitDoc::Skip,
126126
std::convert::identity,
127-
|lint_id, span, kind| {
128-
sess.psess.buffer_lint(
129-
lint_id.lint,
130-
span,
131-
target_node_id,
132-
BuiltinLintDiag::AttributeLint(kind),
133-
)
134-
},
127+
|lint_id, span, kind| sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind),
135128
)
136129
}
137130

@@ -230,12 +223,7 @@ impl<'sess> AttributeParser<'sess, Early> {
230223
let mut parser =
231224
Self { features, tools: None, parse_only: None, sess, stage: Early { emit_errors } };
232225
let mut emit_lint = |lint_id: LintId, span: Span, kind: AttributeLintKind| {
233-
sess.psess.buffer_lint(
234-
lint_id.lint,
235-
span,
236-
target_node_id,
237-
BuiltinLintDiag::AttributeLint(kind),
238-
)
226+
sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind)
239227
};
240228
if let Some(safety) = attr_safety {
241229
parser.check_attribute_safety(&attr_path, inner_span, safety, &mut emit_lint)

‎compiler/rustc_attr_parsing/src/validate_attr.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_hir::AttrPath;
1313
use rustc_hir::lints::AttributeLintKind;
1414
use rustc_parse::parse_in;
1515
use rustc_session::errors::report_lit_error;
16-
use rustc_session::lint::BuiltinLintDiag;
1716
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1817
use rustc_session::parse::ParseSess;
1918
use rustc_span::{Span, Symbol, sym};
@@ -187,11 +186,11 @@ pub fn emit_malformed_attribute(
187186
ILL_FORMED_ATTRIBUTE_INPUT,
188187
span,
189188
ast::CRATE_NODE_ID,
190-
BuiltinLintDiag::AttributeLint(AttributeLintKind::IllFormedAttributeInput {
189+
AttributeLintKind::IllFormedAttributeInput {
191190
suggestions: suggestions.clone(),
192191
docs: template.docs,
193192
help: None,
194-
}),
193+
},
195194
);
196195
} else {
197196
suggestions.sort();

‎compiler/rustc_builtin_macros/src/errors.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,3 +1141,22 @@ pub(crate) struct EiiMacroExpectedMaxOneArgument {
11411141
pub span: Span,
11421142
pub name: String,
11431143
}
1144+
1145+
#[derive(Diagnostic)]
1146+
#[diag("named argument `{$named_arg_name}` is not used by name")]
1147+
pub(crate) struct NamedArgumentUsedPositionally {
1148+
#[label("this named argument is referred to by position in formatting string")]
1149+
pub named_arg_sp: Span,
1150+
#[label("this formatting argument uses named argument `{$named_arg_name}` by position")]
1151+
pub position_label_sp: Option<Span>,
1152+
#[suggestion(
1153+
"use the named argument by name to avoid ambiguity",
1154+
style = "verbose",
1155+
code = "{name}",
1156+
applicability = "maybe-incorrect"
1157+
)]
1158+
pub suggestion: Option<Span>,
1159+
1160+
pub name: String,
1161+
pub named_arg_name: String,
1162+
}

‎compiler/rustc_builtin_macros/src/format.rs‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use rustc_ast::{
1010
};
1111
use rustc_data_structures::fx::FxHashSet;
1212
use rustc_errors::{
13-
Applicability, BufferedEarlyLint, Diag, MultiSpan, PResult, SingleLabelManySpans, listify,
14-
pluralize,
13+
Applicability, BufferedEarlyLint, DecorateDiagCompat, Diag, Diagnostic, MultiSpan, PResult,
14+
SingleLabelManySpans, listify, 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};
@@ -611,14 +611,39 @@ fn make_format_args(
611611
span: Some(arg_name.span.into()),
612612
node_id: rustc_ast::CRATE_NODE_ID,
613613
lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
614-
diagnostic: BuiltinLintDiag::NamedArgumentUsedPositionally {
615-
position_sp_to_replace,
616-
position_sp_for_msg,
617-
named_arg_sp: arg_name.span,
618-
named_arg_name: arg_name.name.to_string(),
619-
is_formatting_arg: matches!(used_as, Width | Precision),
620-
}
621-
.into(),
614+
diagnostic: DecorateDiagCompat::Dynamic(Box::new(move |dcx, level, sess| {
615+
let (suggestion, name) =
616+
if let Some(positional_arg_to_replace) = position_sp_to_replace {
617+
let mut name = arg_name.name.to_string();
618+
let is_formatting_arg = matches!(used_as, Width | Precision);
619+
if is_formatting_arg {
620+
name.push('$')
621+
};
622+
let span_to_replace = if let Ok(positional_arg_content) = sess
623+
.downcast_ref::<rustc_session::Session>()
624+
.expect("expected a `Session`")
625+
.source_map()
626+
.span_to_snippet(positional_arg_to_replace)
627+
&& positional_arg_content.starts_with(':')
628+
{
629+
positional_arg_to_replace.shrink_to_lo()
630+
} else {
631+
positional_arg_to_replace
632+
};
633+
(Some(span_to_replace), name)
634+
} else {
635+
(None, String::new())
636+
};
637+
638+
errors::NamedArgumentUsedPositionally {
639+
named_arg_sp: arg_name.span,
640+
position_label_sp: position_sp_for_msg,
641+
suggestion,
642+
name,
643+
named_arg_name: arg_name.name.to_string(),
644+
}
645+
.into_diag(dcx, level)
646+
})),
622647
});
623648
}
624649
}

‎compiler/rustc_errors/src/decorate_diag.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use rustc_ast::node_id::NodeId;
55
use rustc_data_structures::fx::FxIndexMap;
66
use rustc_data_structures::sync::{DynSend, DynSync};
77
use rustc_error_messages::MultiSpan;
8-
use rustc_lint_defs::{BuiltinLintDiag, Lint, LintId};
8+
use rustc_lint_defs::{AttributeLintKind, Lint, LintId};
99

1010
use crate::{Diag, DiagCtxtHandle, Diagnostic, Level};
1111

12-
/// We can't implement `Diagnostic` for `BuiltinLintDiag`, because decorating some of its
12+
/// We can't implement `Diagnostic` for `AttributeLintKind`, because decorating some of its
1313
/// variants requires types we don't have yet. So, handle that case separately.
1414
pub enum DecorateDiagCompat {
1515
/// The third argument of the closure is a `Session`. However, due to the dependency tree,
@@ -22,7 +22,7 @@ pub enum DecorateDiagCompat {
2222
+ 'static,
2323
>,
2424
),
25-
Builtin(BuiltinLintDiag),
25+
Builtin(AttributeLintKind),
2626
}
2727

2828
impl std::fmt::Debug for DecorateDiagCompat {
@@ -38,9 +38,9 @@ impl<D: for<'a> Diagnostic<'a, ()> + DynSync + DynSend + 'static> From<D> for De
3838
}
3939
}
4040

41-
impl From<BuiltinLintDiag> for DecorateDiagCompat {
41+
impl From<AttributeLintKind> for DecorateDiagCompat {
4242
#[inline]
43-
fn from(b: BuiltinLintDiag) -> Self {
43+
fn from(b: AttributeLintKind) -> Self {
4444
Self::Builtin(b)
4545
}
4646
}

‎compiler/rustc_lint/src/early.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tracing::debug;
1717

1818
use crate::context::{EarlyContext, LintContext, LintStore};
1919
use crate::passes::{EarlyLintPass, EarlyLintPassObject};
20-
use crate::{DecorateBuiltinLint, DiagAndSess};
20+
use crate::{DecorateAttrLint, DiagAndSess};
2121

2222
pub(super) mod diagnostics;
2323

@@ -42,10 +42,10 @@ impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
4242
self.context.opt_span_lint(
4343
lint_id.lint,
4444
span,
45-
DecorateBuiltinLint {
45+
DecorateAttrLint {
4646
sess: self.context.sess(),
4747
tcx: self.tcx,
48-
diagnostic: b,
48+
diagnostic: &b,
4949
},
5050
);
5151
}

‎compiler/rustc_lint/src/early/diagnostics.rs‎

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ use std::any::Any;
22
use std::borrow::Cow;
33

44
use rustc_data_structures::sync::DynSend;
5-
use rustc_errors::{
6-
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, Level,
7-
elided_lifetime_in_path_suggestion,
8-
};
5+
use rustc_errors::{Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, Level};
96
use rustc_hir::lints::{AttributeLintKind, FormatWarning};
107
use rustc_middle::ty::TyCtxt;
118
use rustc_session::Session;
12-
use rustc_session::lint::BuiltinLintDiag;
139

1410
use crate::lints;
1511

@@ -28,101 +24,6 @@ impl<'a> Diagnostic<'a, ()> for DiagAndSess<'_> {
2824
}
2925
}
3026

31-
/// This is a diagnostic struct that will decorate a `BuiltinLintDiag`
32-
/// Directly creating the lint structs is expensive, using this will only decorate the lint structs when needed.
33-
pub struct DecorateBuiltinLint<'sess, 'tcx> {
34-
pub sess: &'sess Session,
35-
pub tcx: Option<TyCtxt<'tcx>>,
36-
pub diagnostic: BuiltinLintDiag,
37-
}
38-
39-
impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
40-
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
41-
match self.diagnostic {
42-
BuiltinLintDiag::ElidedLifetimesInPaths(
43-
n,
44-
path_span,
45-
incl_angl_brckt,
46-
insertion_span,
47-
) => lints::ElidedLifetimesInPaths {
48-
subdiag: elided_lifetime_in_path_suggestion(
49-
self.sess.source_map(),
50-
n,
51-
path_span,
52-
incl_angl_brckt,
53-
insertion_span,
54-
),
55-
}
56-
.into_diag(dcx, level),
57-
BuiltinLintDiag::UnusedImports {
58-
remove_whole_use,
59-
num_to_remove,
60-
remove_spans,
61-
test_module_span,
62-
span_snippets,
63-
} => {
64-
let sugg = if remove_whole_use {
65-
lints::UnusedImportsSugg::RemoveWholeUse { span: remove_spans[0] }
66-
} else {
67-
lints::UnusedImportsSugg::RemoveImports { remove_spans, num_to_remove }
68-
};
69-
let test_module_span =
70-
test_module_span.map(|span| self.sess.source_map().guess_head_span(span));
71-
72-
lints::UnusedImports {
73-
sugg,
74-
test_module_span,
75-
num_snippets: span_snippets.len(),
76-
span_snippets: DiagArgValue::StrListSepByAnd(
77-
span_snippets.into_iter().map(Cow::Owned).collect(),
78-
),
79-
}
80-
.into_diag(dcx, level)
81-
}
82-
BuiltinLintDiag::NamedArgumentUsedPositionally {
83-
position_sp_to_replace,
84-
position_sp_for_msg,
85-
named_arg_sp,
86-
named_arg_name,
87-
is_formatting_arg,
88-
} => {
89-
let (suggestion, name) =
90-
if let Some(positional_arg_to_replace) = position_sp_to_replace {
91-
let mut name = named_arg_name.clone();
92-
if is_formatting_arg {
93-
name.push('$')
94-
};
95-
let span_to_replace = if let Ok(positional_arg_content) =
96-
self.sess.source_map().span_to_snippet(positional_arg_to_replace)
97-
&& positional_arg_content.starts_with(':')
98-
{
99-
positional_arg_to_replace.shrink_to_lo()
100-
} else {
101-
positional_arg_to_replace
102-
};
103-
(Some(span_to_replace), name)
104-
} else {
105-
(None, String::new())
106-
};
107-
108-
lints::NamedArgumentUsedPositionally {
109-
named_arg_sp,
110-
position_label_sp: position_sp_for_msg,
111-
suggestion,
112-
name,
113-
named_arg_name,
114-
}
115-
.into_diag(dcx, level)
116-
}
117-
118-
BuiltinLintDiag::AttributeLint(kind) => {
119-
DecorateAttrLint { sess: self.sess, tcx: self.tcx, diagnostic: &kind }
120-
.into_diag(dcx, level)
121-
}
122-
}
123-
}
124-
}
125-
12627
/// This is a diagnostic struct that will decorate a `AttributeLintKind`
12728
/// Directly creating the lint structs is expensive, using this will only decorate the lint structs when needed.
12829
pub struct DecorateAttrLint<'a, 'sess, 'tcx> {

‎compiler/rustc_lint/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ use unused::*;
129129
#[rustfmt::skip]
130130
pub use builtin::{MissingDoc, SoftLints};
131131
pub use context::{EarlyContext, LateContext, LintContext, LintStore};
132-
pub use early::diagnostics::{DecorateAttrLint, DecorateBuiltinLint, DiagAndSess};
132+
pub use early::diagnostics::{DecorateAttrLint, DiagAndSess};
133133
pub use early::{EarlyCheckNode, check_ast_node};
134134
pub use late::{check_crate, late_lint_mod, unerased_lint_store};
135135
pub use levels::LintLevelsBuilder;

0 commit comments

Comments
 (0)