Skip to content

Commit b582f80

Browse files
committed
Auto merge of #125410 - fmease:adj-lint-diag-api, r=nnethercote
[perf] Delay the construction of early lint diag structs Attacks some of the perf regressions from #124417 (comment). See individual commits for details. The first three commits are not strictly necessary. However, the 2nd one (06bc4fc, *Remove `LintDiagnostic::msg`*) makes the main change way nicer to implement. It's also pretty sweet on its own if I may say so myself.
2 parents fec98b3 + 37bf2d2 commit b582f80

File tree

50 files changed

+600
-751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+600
-751
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
576576
lint::builtin::INLINE_NO_SANITIZE,
577577
hir_id,
578578
no_sanitize_span,
579-
"`no_sanitize` will have no effect after inlining",
580579
|lint| {
580+
lint.primary_message("`no_sanitize` will have no effect after inlining");
581581
lint.span_note(inline_span, "inlining requested here");
582582
},
583583
)

compiler/rustc_error_messages/src/lib.rs

-11
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,6 @@ impl From<Cow<'static, str>> for DiagMessage {
364364
}
365365
}
366366

367-
/// A workaround for must_produce_diag ICEs when formatting types in disabled lints.
368-
///
369-
/// Delays formatting until `.into(): DiagMessage` is used.
370-
pub struct DelayDm<F>(pub F);
371-
372-
impl<F: FnOnce() -> String> From<DelayDm<F>> for DiagMessage {
373-
fn from(DelayDm(f): DelayDm<F>) -> Self {
374-
DiagMessage::from(f())
375-
}
376-
}
377-
378367
/// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
379368
/// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagMessage` and the
380369
/// subdiagnostic derive refers to typed identifiers that are `DiagMessage`s, so need to be

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_errors/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use diagnostic_impls::{
3939
};
4040
pub use emitter::ColorConfig;
4141
pub use rustc_error_messages::{
42-
fallback_fluent_bundle, fluent_bundle, DelayDm, DiagMessage, FluentBundle, LanguageIdentifier,
42+
fallback_fluent_bundle, fluent_bundle, DiagMessage, FluentBundle, LanguageIdentifier,
4343
LazyFallbackBundle, MultiSpan, SpanLabel, SubdiagMessage,
4444
};
4545
pub use rustc_lint_defs::{pluralize, Applicability};
@@ -572,8 +572,8 @@ impl Drop for DiagCtxtInner {
572572
if let Some(backtrace) = &self.must_produce_diag {
573573
panic!(
574574
"must_produce_diag: `trimmed_def_paths` called but no diagnostics emitted; \
575-
use `DelayDm` for lints or `with_no_trimmed_paths` for debugging. \
576-
called at: {backtrace}"
575+
`with_no_trimmed_paths` for debugging. \
576+
called at: {backtrace}"
577577
);
578578
}
579579
}

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, |lint| {
50+
lint.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
},
@@ -1310,9 +1306,11 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
13101306
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
13111307
tcx.local_def_id_to_hir_id(adt.did().expect_local()),
13121308
span,
1313-
"zero-sized fields in `repr(transparent)` cannot \
1314-
contain external non-exhaustive types",
13151309
|lint| {
1310+
lint.primary_message(
1311+
"zero-sized fields in `repr(transparent)` cannot \
1312+
contain external non-exhaustive types",
1313+
);
13161314
let note = if non_exhaustive {
13171315
"is marked with `#[non_exhaustive]`"
13181316
} 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
@@ -64,19 +64,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6464
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
6565

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

0 commit comments

Comments
 (0)