Skip to content

Commit c902bc1

Browse files
Rollup merge of #153091 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer
Migration of `LintDiagnostic` - part 4 Follow-up of: * #152933 * #153016 * #153051 More `LintDiagnostic` items being migrated to `Diagnostic`. Since there is no remaining `emit_node_span_lint` calls, I replaced the method with the code of `emit_diag_node_span_lint`. r? @JonathanBrouwer
2 parents 51492d0 + f9b70f0 commit c902bc1

24 files changed

+358
-276
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_abi::FieldIdx;
2727
use rustc_data_structures::frozen::Frozen;
2828
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2929
use rustc_data_structures::graph::dominators::Dominators;
30-
use rustc_errors::LintDiagnostic;
3130
use rustc_hir as hir;
3231
use rustc_hir::CRATE_HIR_ID;
3332
use rustc_hir::def_id::LocalDefId;
@@ -715,7 +714,7 @@ impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
715714
}
716715
}
717716

718-
struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
717+
pub(crate) struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
719718
root_cx: &'a mut BorrowCheckRootCtxt<'tcx>,
720719
infcx: &'infcx BorrowckInferCtxt<'tcx>,
721720
body: &'a Body<'tcx>,
@@ -1428,13 +1427,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
14281427
borrow,
14291428
Some((WriteKind::StorageDeadOrDrop, place)),
14301429
);
1431-
this.infcx.tcx.node_span_lint(
1430+
this.infcx.tcx.emit_node_span_lint(
14321431
TAIL_EXPR_DROP_ORDER,
14331432
CRATE_HIR_ID,
14341433
borrowed,
1435-
|diag| {
1436-
session_diagnostics::TailExprDropOrder { borrowed }.decorate_lint(diag);
1437-
explain.add_explanation_to_diagnostic(&this, diag, "", None, None);
1434+
session_diagnostics::TailExprDropOrder {
1435+
borrowed,
1436+
callback: |diag| {
1437+
explain.add_explanation_to_diagnostic(&this, diag, "", None, None);
1438+
},
14381439
},
14391440
);
14401441
// We may stop at the first case

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use rustc_errors::MultiSpan;
21
use rustc_errors::codes::*;
3-
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
2+
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
3+
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_middle::ty::{GenericArg, Ty};
55
use rustc_span::Span;
66

@@ -595,9 +595,20 @@ pub(crate) struct SimdIntrinsicArgConst {
595595
pub intrinsic: String,
596596
}
597597

598-
#[derive(LintDiagnostic)]
599-
#[diag("relative drop order changing in Rust 2024")]
600-
pub(crate) struct TailExprDropOrder {
601-
#[label("this temporary value will be dropped at the end of the block")]
598+
pub(crate) struct TailExprDropOrder<F: FnOnce(&mut Diag<'_, ()>)> {
602599
pub borrowed: Span,
600+
pub callback: F,
601+
}
602+
603+
impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for TailExprDropOrder<F> {
604+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
605+
let Self { borrowed, callback } = self;
606+
let mut diag = Diag::new(dcx, level, "relative drop order changing in Rust 2024")
607+
.with_span_label(
608+
borrowed,
609+
"this temporary value will be dropped at the end of the block",
610+
);
611+
callback(&mut diag);
612+
diag
613+
}
603614
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_ast::visit::{FnCtxt, FnKind};
2222
use rustc_ast::{self as ast, *};
2323
use rustc_ast_pretty::pprust::expr_to_string;
2424
use rustc_attr_parsing::AttributeParser;
25-
use rustc_errors::{Applicability, LintDiagnostic, msg};
25+
use rustc_errors::{Applicability, Diagnostic, msg};
2626
use rustc_feature::GateIssue;
2727
use rustc_hir::attrs::{AttributeKind, DocAttribute};
2828
use rustc_hir::def::{DefKind, Res};
@@ -235,7 +235,7 @@ impl UnsafeCode {
235235
&self,
236236
cx: &EarlyContext<'_>,
237237
span: Span,
238-
decorate: impl for<'a> LintDiagnostic<'a, ()>,
238+
decorate: impl for<'a> Diagnostic<'a, ()>,
239239
) {
240240
// This comes from a macro that has `#[allow_internal_unsafe]`.
241241
if span.allows_unsafe() {

compiler/rustc_lint/src/context.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::util::parser::ExprPrecedence;
1111
use rustc_data_structures::fx::FxIndexMap;
1212
use rustc_data_structures::sync;
1313
use rustc_data_structures::unord::UnordMap;
14-
use rustc_errors::{Diag, LintBuffer, LintDiagnostic, MultiSpan};
14+
use rustc_errors::{Diag, Diagnostic, LintBuffer, LintDiagnostic, MultiSpan};
1515
use rustc_feature::Features;
1616
use rustc_hir::def::Res;
1717
use rustc_hir::def_id::{CrateNum, DefId};
@@ -522,17 +522,28 @@ pub trait LintContext {
522522
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
523523
);
524524

525+
// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
526+
// set the span in their `decorate` function (preferably using set_span).
527+
/// Emit a lint at the appropriate level, with an optional associated span.
528+
///
529+
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
530+
#[track_caller]
531+
fn opt_span_diag_lint<S: Into<MultiSpan>>(
532+
&self,
533+
lint: &'static Lint,
534+
span: Option<S>,
535+
decorate: impl for<'a> Diagnostic<'a, ()>,
536+
);
537+
525538
/// Emit a lint at `span` from a lint struct (some type that implements `LintDiagnostic`,
526539
/// typically generated by `#[derive(LintDiagnostic)]`).
527540
fn emit_span_lint<S: Into<MultiSpan>>(
528541
&self,
529542
lint: &'static Lint,
530543
span: S,
531-
decorator: impl for<'a> LintDiagnostic<'a, ()>,
544+
decorator: impl for<'a> Diagnostic<'a, ()>,
532545
) {
533-
self.opt_span_lint(lint, Some(span), |lint| {
534-
decorator.decorate_lint(lint);
535-
});
546+
self.opt_span_diag_lint(lint, Some(span), decorator);
536547
}
537548

538549
/// Emit a lint at `span` from a lazily-constructed lint struct (some type that implements
@@ -570,6 +581,12 @@ pub trait LintContext {
570581
});
571582
}
572583

584+
/// Emit a lint from a lint struct (some type that implements `LintDiagnostic`, typically
585+
/// generated by `#[derive(LintDiagnostic)]`).
586+
fn emit_diag_lint(&self, lint: &'static Lint, decorator: impl for<'a> Diagnostic<'a, ()>) {
587+
self.opt_span_diag_lint(lint, None as Option<Span>, decorator);
588+
}
589+
573590
/// Emit a lint at the appropriate level, with no associated span.
574591
///
575592
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
@@ -644,6 +661,20 @@ impl<'tcx> LintContext for LateContext<'tcx> {
644661
}
645662
}
646663

664+
fn opt_span_diag_lint<S: Into<MultiSpan>>(
665+
&self,
666+
lint: &'static Lint,
667+
span: Option<S>,
668+
decorate: impl for<'a> Diagnostic<'a, ()>,
669+
) {
670+
let hir_id = self.last_node_with_lint_attrs;
671+
672+
match span {
673+
Some(s) => self.tcx.emit_node_span_lint(lint, hir_id, s, decorate),
674+
None => self.tcx.emit_node_lint(lint, hir_id, decorate),
675+
}
676+
}
677+
647678
fn get_lint_level(&self, lint: &'static Lint) -> LevelAndSource {
648679
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs)
649680
}
@@ -664,6 +695,15 @@ impl LintContext for EarlyContext<'_> {
664695
self.builder.opt_span_lint(lint, span.map(|s| s.into()), decorate)
665696
}
666697

698+
fn opt_span_diag_lint<S: Into<MultiSpan>>(
699+
&self,
700+
lint: &'static Lint,
701+
span: Option<S>,
702+
decorator: impl for<'a> Diagnostic<'a, ()>,
703+
) {
704+
self.builder.opt_span_diag_lint(lint, span.map(|s| s.into()), decorator)
705+
}
706+
667707
fn get_lint_level(&self, lint: &'static Lint) -> LevelAndSource {
668708
self.builder.lint_level(lint)
669709
}

compiler/rustc_lint/src/dangling.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
269269
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
270270
&& find_attr!(cx.tcx, fn_id, RustcAsPtr(_))
271271
{
272-
// FIXME: use `emit_node_lint` when `#[primary_span]` is added.
273272
cx.tcx.emit_node_span_lint(
274273
DANGLING_POINTERS_FROM_TEMPORARIES,
275274
expr.hir_id,

compiler/rustc_lint/src/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ impl EarlyLintPass for BadUseOfFindAttr {
638638
cx.emit_span_lint(
639639
BAD_USE_OF_FIND_ATTR,
640640
segment.span(),
641-
AttributeKindInFindAttr {},
641+
AttributeKindInFindAttr,
642642
);
643643
}
644644
}

compiler/rustc_lint/src/levels.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ use rustc_ast::attr::AttributeExt;
22
use rustc_ast_pretty::pprust;
33
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
44
use rustc_data_structures::unord::UnordSet;
5-
use rustc_errors::{Diag, LintDiagnostic, MultiSpan, msg};
5+
use rustc_errors::{Diag, Diagnostic, MultiSpan, msg};
66
use rustc_feature::{Features, GateIssue};
77
use rustc_hir::HirId;
88
use rustc_hir::intravisit::{self, Visitor};
99
use rustc_index::IndexVec;
1010
use rustc_middle::bug;
1111
use rustc_middle::hir::nested_filter;
1212
use rustc_middle::lint::{
13-
LevelAndSource, LintExpectation, LintLevelSource, ShallowLintLevelMap, lint_level,
14-
reveal_actual_level,
13+
LevelAndSource, LintExpectation, LintLevelSource, ShallowLintLevelMap, diag_lint_level,
14+
lint_level, reveal_actual_level,
1515
};
1616
use rustc_middle::query::Providers;
1717
use rustc_middle::ty::{RegisteredTools, TyCtxt};
@@ -822,8 +822,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
822822
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
823823
let name =
824824
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
825-
let lint = RenamedLint { name: name.as_str(), replace, suggestion };
826-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
825+
self.emit_span_lint(
826+
RENAMED_AND_REMOVED_LINTS,
827+
sp.into(),
828+
RenamedLint { name: name.as_str(), replace, suggestion },
829+
);
827830
}
828831

829832
// If this lint was renamed, apply the new lint instead of ignoring the
@@ -844,8 +847,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
844847
if self.lint_added_lints {
845848
let name =
846849
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
847-
let lint = RemovedLint { name: name.as_str(), reason };
848-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
850+
self.emit_span_lint(
851+
RENAMED_AND_REMOVED_LINTS,
852+
sp.into(),
853+
RemovedLint { name: name.as_str(), reason },
854+
);
849855
}
850856
continue;
851857
}
@@ -861,8 +867,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
861867
from_rustc,
862868
}
863869
});
864-
let lint = UnknownLint { name, suggestion };
865-
self.emit_span_lint(UNKNOWN_LINTS, sp.into(), lint);
870+
self.emit_span_lint(
871+
UNKNOWN_LINTS,
872+
sp.into(),
873+
UnknownLint { name, suggestion },
874+
);
866875
}
867876
continue;
868877
}
@@ -967,8 +976,6 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
967976

968977
/// Used to emit a lint-related diagnostic based on the current state of
969978
/// this lint context.
970-
///
971-
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
972979
#[track_caller]
973980
pub(crate) fn opt_span_lint(
974981
&self,
@@ -980,25 +987,34 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
980987
lint_level(self.sess, lint, level, span, decorate)
981988
}
982989

990+
/// Used to emit a lint-related diagnostic based on the current state of
991+
/// this lint context.
992+
#[track_caller]
993+
pub(crate) fn opt_span_diag_lint(
994+
&self,
995+
lint: &'static Lint,
996+
span: Option<MultiSpan>,
997+
decorator: impl for<'a> Diagnostic<'a, ()>,
998+
) {
999+
let level = self.lint_level(lint);
1000+
diag_lint_level(self.sess, lint, level, span, decorator)
1001+
}
1002+
9831003
#[track_caller]
9841004
pub fn emit_span_lint(
9851005
&self,
9861006
lint: &'static Lint,
9871007
span: MultiSpan,
988-
decorate: impl for<'a> LintDiagnostic<'a, ()>,
1008+
decorator: impl for<'a> Diagnostic<'a, ()>,
9891009
) {
9901010
let level = self.lint_level(lint);
991-
lint_level(self.sess, lint, level, Some(span), |lint| {
992-
decorate.decorate_lint(lint);
993-
});
1011+
diag_lint_level(self.sess, lint, level, Some(span), decorator);
9941012
}
9951013

9961014
#[track_caller]
997-
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> LintDiagnostic<'a, ()>) {
1015+
pub fn emit_lint(&self, lint: &'static Lint, decorator: impl for<'a> Diagnostic<'a, ()>) {
9981016
let level = self.lint_level(lint);
999-
lint_level(self.sess, lint, level, None, |lint| {
1000-
decorate.decorate_lint(lint);
1001-
});
1017+
diag_lint_level(self.sess, lint, level, None, decorator);
10021018
}
10031019
}
10041020

0 commit comments

Comments
 (0)