Skip to content

Commit c2769b7

Browse files
committed
Auto merge of rust-lang#125410 - fmease:adj-lint-diag-api, r=<try>
[perf] Delay construction of early lint diag structs cc rust-lang#124417 (comment) r? ghost
2 parents f0038a7 + 7ef19da commit c2769b7

File tree

31 files changed

+451
-638
lines changed

31 files changed

+451
-638
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
564564
lint::builtin::INLINE_NO_SANITIZE,
565565
hir_id,
566566
no_sanitize_span,
567-
"`no_sanitize` will have no effect after inlining",
568567
|lint| {
568+
lint.primary_message("`no_sanitize` will have no effect after inlining");
569569
lint.span_note(inline_span, "inlining requested here");
570570
},
571571
)

compiler/rustc_errors/src/diagnostic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ pub trait SubdiagMessageOp<G: EmissionGuarantee> =
200200
pub trait LintDiagnostic<'a, G: EmissionGuarantee> {
201201
/// Decorate and emit a lint.
202202
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, G>);
203-
204-
fn msg(&self) -> DiagMessage;
205203
}
206204

207205
#[derive(Clone, Debug, Encodable, Decodable)]

compiler/rustc_hir_analysis/src/check/check.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
4646
.emit();
4747
}
4848
None => {
49-
tcx.node_span_lint(
50-
UNSUPPORTED_CALLING_CONVENTIONS,
51-
hir_id,
52-
span,
53-
"use of calling convention not supported on this target",
54-
|_| {},
55-
);
49+
tcx.node_span_lint(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |diag| {
50+
diag.primary_message("use of calling convention not supported on this target");
51+
});
5652
}
5753
}
5854

@@ -243,8 +239,8 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
243239
UNINHABITED_STATIC,
244240
tcx.local_def_id_to_hir_id(def_id),
245241
span,
246-
"static of uninhabited type",
247242
|lint| {
243+
lint.primary_message("static of uninhabited type");
248244
lint
249245
.note("uninhabited statics cannot be initialized, and any access would be an immediate error");
250246
},
@@ -1315,9 +1311,11 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
13151311
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
13161312
tcx.local_def_id_to_hir_id(adt.did().expect_local()),
13171313
span,
1318-
"zero-sized fields in `repr(transparent)` cannot \
1319-
contain external non-exhaustive types",
13201314
|lint| {
1315+
lint.primary_message(
1316+
"zero-sized fields in `repr(transparent)` cannot \
1317+
contain external non-exhaustive types",
1318+
);
13211319
let note = if non_exhaustive {
13221320
"is marked with `#[non_exhaustive]`"
13231321
} else {

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
281281
lint::builtin::ASM_SUB_REGISTER,
282282
expr.hir_id,
283283
spans,
284-
"formatting may not be suitable for sub-register argument",
285284
|lint| {
285+
lint.primary_message("formatting may not be suitable for sub-register argument");
286286
lint.span_label(expr.span, "for this argument");
287287
lint.help(format!(
288288
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",

compiler/rustc_hir_analysis/src/check_unused.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ fn check_unused_traits(tcx: TyCtxt<'_>, (): ()) {
3535
continue;
3636
}
3737
let (path, _) = item.expect_use();
38-
let msg = if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(path.span) {
39-
format!("unused import: `{snippet}`")
40-
} else {
41-
"unused import".to_owned()
42-
};
43-
tcx.node_span_lint(lint::builtin::UNUSED_IMPORTS, item.hir_id(), path.span, msg, |_| {});
38+
tcx.node_span_lint(lint::builtin::UNUSED_IMPORTS, item.hir_id(), path.span, |lint| {
39+
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(path.span) {
40+
lint.primary_message(format!("unused import: `{snippet}`"));
41+
} else {
42+
lint.primary_message("unused import");
43+
}
44+
});
4445
}
4546
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
317317
lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
318318
param.hir_id,
319319
param.span,
320-
TYPE_DEFAULT_NOT_ALLOWED,
321-
|_| {},
320+
|lint| {
321+
lint.primary_message(TYPE_DEFAULT_NOT_ALLOWED);
322+
},
322323
);
323324
}
324325
Defaults::Deny => {

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,9 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
646646
LATE_BOUND_LIFETIME_ARGUMENTS,
647647
args.args[0].hir_id(),
648648
multispan,
649-
msg,
650-
|_| {},
649+
|lint| {
650+
lint.primary_message(msg);
651+
},
651652
);
652653
}
653654

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7272
self.maybe_suggest_assoc_ty_bound(self_ty, &mut diag);
7373
diag.stash(self_ty.span, StashKey::TraitMissingMethod);
7474
} else {
75-
let msg = "trait objects without an explicit `dyn` are deprecated";
76-
tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, msg, |lint| {
75+
tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, |lint| {
76+
lint.primary_message("trait objects without an explicit `dyn` are deprecated");
7777
if self_ty.span.can_be_used_for_suggestions() {
7878
lint.multipart_suggestion_verbose(
7979
"if this is an object-safe trait, use `dyn`",

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -1165,33 +1165,28 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11651165
let ty = self.lower_assoc_ty(span, assoc_ty_did, assoc_segment, bound);
11661166

11671167
if let Some(variant_def_id) = variant_resolution {
1168-
tcx.node_span_lint(
1169-
AMBIGUOUS_ASSOCIATED_ITEMS,
1170-
hir_ref_id,
1171-
span,
1172-
"ambiguous associated item",
1173-
|lint| {
1174-
let mut could_refer_to = |kind: DefKind, def_id, also| {
1175-
let note_msg = format!(
1176-
"`{}` could{} refer to the {} defined here",
1177-
assoc_ident,
1178-
also,
1179-
tcx.def_kind_descr(kind, def_id)
1180-
);
1181-
lint.span_note(tcx.def_span(def_id), note_msg);
1182-
};
1168+
tcx.node_span_lint(AMBIGUOUS_ASSOCIATED_ITEMS, hir_ref_id, span, |lint| {
1169+
lint.primary_message("ambiguous associated item");
1170+
let mut could_refer_to = |kind: DefKind, def_id, also| {
1171+
let note_msg = format!(
1172+
"`{}` could{} refer to the {} defined here",
1173+
assoc_ident,
1174+
also,
1175+
tcx.def_kind_descr(kind, def_id)
1176+
);
1177+
lint.span_note(tcx.def_span(def_id), note_msg);
1178+
};
11831179

1184-
could_refer_to(DefKind::Variant, variant_def_id, "");
1185-
could_refer_to(DefKind::AssocTy, assoc_ty_did, " also");
1180+
could_refer_to(DefKind::Variant, variant_def_id, "");
1181+
could_refer_to(DefKind::AssocTy, assoc_ty_did, " also");
11861182

1187-
lint.span_suggestion(
1188-
span,
1189-
"use fully-qualified syntax",
1190-
format!("<{} as {}>::{}", qself_ty, tcx.item_name(trait_did), assoc_ident),
1191-
Applicability::MachineApplicable,
1192-
);
1193-
},
1194-
);
1183+
lint.span_suggestion(
1184+
span,
1185+
"use fully-qualified syntax",
1186+
format!("<{} as {}>::{}", qself_ty, tcx.item_name(trait_did), assoc_ident),
1187+
Applicability::MachineApplicable,
1188+
);
1189+
});
11951190
}
11961191
Ok((ty, DefKind::AssocTy, assoc_ty_did))
11971192
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6363
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
6464

6565
let msg = format!("unreachable {kind}");
66-
self.tcx().node_span_lint(
67-
lint::builtin::UNREACHABLE_CODE,
68-
id,
69-
span,
70-
msg.clone(),
71-
|lint| {
72-
lint.span_label(span, msg).span_label(
73-
orig_span,
74-
custom_note
75-
.unwrap_or("any code following this expression is unreachable"),
76-
);
77-
},
78-
)
66+
self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
67+
lint.primary_message(msg.clone());
68+
lint.span_label(span, msg).span_label(
69+
orig_span,
70+
custom_note.unwrap_or("any code following this expression is unreachable"),
71+
);
72+
})
7973
}
8074
}
8175
}

compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

+63-67
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9191
prelude_or_array_lint,
9292
self_expr.hir_id,
9393
self_expr.span,
94-
format!(
95-
"trait method `{}` will become ambiguous in Rust {edition}",
96-
segment.ident.name
97-
),
9894
|lint| {
95+
lint.primary_message(format!(
96+
"trait method `{}` will become ambiguous in Rust {edition}",
97+
segment.ident.name
98+
));
99+
99100
let sp = self_expr.span;
100101

101102
let derefs = "*".repeat(pick.autoderefs);
@@ -144,11 +145,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
144145
prelude_or_array_lint,
145146
call_expr.hir_id,
146147
call_expr.span,
147-
format!(
148-
"trait method `{}` will become ambiguous in Rust {edition}",
149-
segment.ident.name
150-
),
151148
|lint| {
149+
lint.primary_message(format!(
150+
"trait method `{}` will become ambiguous in Rust {edition}",
151+
segment.ident.name
152+
));
153+
152154
let sp = call_expr.span;
153155
let trait_name = self.trait_path_or_bare_name(
154156
span,
@@ -251,73 +253,67 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
251253
return;
252254
}
253255

254-
self.tcx.node_span_lint(
255-
RUST_2021_PRELUDE_COLLISIONS,
256-
expr_id,
257-
span,
258-
format!(
256+
self.tcx.node_span_lint(RUST_2021_PRELUDE_COLLISIONS, expr_id, span, |lint| {
257+
lint.primary_message(format!(
259258
"trait-associated function `{}` will become ambiguous in Rust 2021",
260259
method_name.name
261-
),
262-
|lint| {
263-
// "type" refers to either a type or, more likely, a trait from which
264-
// the associated function or method is from.
265-
let container_id = pick.item.container_id(self.tcx);
266-
let trait_path = self.trait_path_or_bare_name(span, expr_id, container_id);
267-
let trait_generics = self.tcx.generics_of(container_id);
268-
269-
let trait_name =
270-
if trait_generics.own_params.len() <= trait_generics.has_self as usize {
271-
trait_path
272-
} else {
273-
let counts = trait_generics.own_counts();
274-
format!(
275-
"{}<{}>",
276-
trait_path,
260+
));
261+
262+
// "type" refers to either a type or, more likely, a trait from which
263+
// the associated function or method is from.
264+
let container_id = pick.item.container_id(self.tcx);
265+
let trait_path = self.trait_path_or_bare_name(span, expr_id, container_id);
266+
let trait_generics = self.tcx.generics_of(container_id);
267+
268+
let trait_name =
269+
if trait_generics.own_params.len() <= trait_generics.has_self as usize {
270+
trait_path
271+
} else {
272+
let counts = trait_generics.own_counts();
273+
format!(
274+
"{}<{}>",
275+
trait_path,
276+
std::iter::repeat("'_")
277+
.take(counts.lifetimes)
278+
.chain(std::iter::repeat("_").take(
279+
counts.types + counts.consts - trait_generics.has_self as usize
280+
))
281+
.collect::<Vec<_>>()
282+
.join(", ")
283+
)
284+
};
285+
286+
let mut self_ty_name = self_ty_span
287+
.find_ancestor_inside(span)
288+
.and_then(|span| self.sess().source_map().span_to_snippet(span).ok())
289+
.unwrap_or_else(|| self_ty.to_string());
290+
291+
// Get the number of generics the self type has (if an Adt) unless we can determine that
292+
// the user has written the self type with generics already which we (naively) do by looking
293+
// for a "<" in `self_ty_name`.
294+
if !self_ty_name.contains('<') {
295+
if let ty::Adt(def, _) = self_ty.kind() {
296+
let generics = self.tcx.generics_of(def.did());
297+
if !generics.is_own_empty() {
298+
let counts = generics.own_counts();
299+
self_ty_name += &format!(
300+
"<{}>",
277301
std::iter::repeat("'_")
278302
.take(counts.lifetimes)
279-
.chain(std::iter::repeat("_").take(
280-
counts.types + counts.consts - trait_generics.has_self as usize
281-
))
303+
.chain(std::iter::repeat("_").take(counts.types + counts.consts))
282304
.collect::<Vec<_>>()
283305
.join(", ")
284-
)
285-
};
286-
287-
let mut self_ty_name = self_ty_span
288-
.find_ancestor_inside(span)
289-
.and_then(|span| self.sess().source_map().span_to_snippet(span).ok())
290-
.unwrap_or_else(|| self_ty.to_string());
291-
292-
// Get the number of generics the self type has (if an Adt) unless we can determine that
293-
// the user has written the self type with generics already which we (naively) do by looking
294-
// for a "<" in `self_ty_name`.
295-
if !self_ty_name.contains('<') {
296-
if let ty::Adt(def, _) = self_ty.kind() {
297-
let generics = self.tcx.generics_of(def.did());
298-
if !generics.is_own_empty() {
299-
let counts = generics.own_counts();
300-
self_ty_name += &format!(
301-
"<{}>",
302-
std::iter::repeat("'_")
303-
.take(counts.lifetimes)
304-
.chain(
305-
std::iter::repeat("_").take(counts.types + counts.consts)
306-
)
307-
.collect::<Vec<_>>()
308-
.join(", ")
309-
);
310-
}
306+
);
311307
}
312308
}
313-
lint.span_suggestion(
314-
span,
315-
"disambiguate the associated function",
316-
format!("<{} as {}>::{}", self_ty_name, trait_name, method_name.name,),
317-
Applicability::MachineApplicable,
318-
);
319-
},
320-
);
309+
}
310+
lint.span_suggestion(
311+
span,
312+
"disambiguate the associated function",
313+
format!("<{} as {}>::{}", self_ty_name, trait_name, method_name.name,),
314+
Applicability::MachineApplicable,
315+
);
316+
});
321317
}
322318

323319
fn trait_path_or_bare_name(

0 commit comments

Comments
 (0)