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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 11 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 6 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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`",

0 commit comments

Comments
 (0)