Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use rustc_abi::FieldIdx;
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::graph::dominators::Dominators;
use rustc_errors::LintDiagnostic;
use rustc_hir as hir;
use rustc_hir::CRATE_HIR_ID;
use rustc_hir::def_id::LocalDefId;
Expand Down Expand Up @@ -715,7 +714,7 @@ impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
}
}

struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
pub(crate) struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
root_cx: &'a mut BorrowCheckRootCtxt<'tcx>,
infcx: &'infcx BorrowckInferCtxt<'tcx>,
body: &'a Body<'tcx>,
Expand Down Expand Up @@ -1428,13 +1427,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
borrow,
Some((WriteKind::StorageDeadOrDrop, place)),
);
this.infcx.tcx.node_span_lint(
this.infcx.tcx.emit_node_span_lint(
TAIL_EXPR_DROP_ORDER,
CRATE_HIR_ID,
borrowed,
|diag| {
session_diagnostics::TailExprDropOrder { borrowed }.decorate_lint(diag);
explain.add_explanation_to_diagnostic(&this, diag, "", None, None);
session_diagnostics::TailExprDropOrder {
borrowed,
callback: |diag| {
explain.add_explanation_to_diagnostic(&this, diag, "", None, None);
},
},
);
// We may stop at the first case
Expand Down
23 changes: 17 additions & 6 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_errors::MultiSpan;
use rustc_errors::codes::*;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_middle::ty::{GenericArg, Ty};
use rustc_span::Span;

Expand Down Expand Up @@ -595,9 +595,20 @@ pub(crate) struct SimdIntrinsicArgConst {
pub intrinsic: String,
}

#[derive(LintDiagnostic)]
#[diag("relative drop order changing in Rust 2024")]
pub(crate) struct TailExprDropOrder {
#[label("this temporary value will be dropped at the end of the block")]
pub(crate) struct TailExprDropOrder<F: FnOnce(&mut Diag<'_, ()>)> {
pub borrowed: Span,
pub callback: F,
}

impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for TailExprDropOrder<F> {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
let Self { borrowed, callback } = self;
let mut diag = Diag::new(dcx, level, "relative drop order changing in Rust 2024")
.with_span_label(
borrowed,
"this temporary value will be dropped at the end of the block",
);
callback(&mut diag);
diag
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_ast::visit::{FnCtxt, FnKind};
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_attr_parsing::AttributeParser;
use rustc_errors::{Applicability, LintDiagnostic, msg};
use rustc_errors::{Applicability, Diagnostic, msg};
use rustc_feature::GateIssue;
use rustc_hir::attrs::{AttributeKind, DocAttribute};
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -235,7 +235,7 @@ impl UnsafeCode {
&self,
cx: &EarlyContext<'_>,
span: Span,
decorate: impl for<'a> LintDiagnostic<'a, ()>,
decorate: impl for<'a> Diagnostic<'a, ()>,
) {
// This comes from a macro that has `#[allow_internal_unsafe]`.
if span.allows_unsafe() {
Expand Down
50 changes: 45 additions & 5 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_ast::util::parser::ExprPrecedence;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Diag, LintBuffer, LintDiagnostic, MultiSpan};
use rustc_errors::{Diag, Diagnostic, LintBuffer, LintDiagnostic, MultiSpan};
use rustc_feature::Features;
use rustc_hir::def::Res;
use rustc_hir::def_id::{CrateNum, DefId};
Expand Down Expand Up @@ -522,17 +522,28 @@ pub trait LintContext {
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
);

// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this FIXME something that is realistic to fix in one or multiple of these PRs (not this one), or is that way too much work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. You'll see a lot of FIXME comments copied from existing code. :')

// set the span in their `decorate` function (preferably using set_span).
/// Emit a lint at the appropriate level, with an optional associated span.
///
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
#[track_caller]
fn opt_span_diag_lint<S: Into<MultiSpan>>(
&self,
lint: &'static Lint,
span: Option<S>,
decorate: impl for<'a> Diagnostic<'a, ()>,
);

/// Emit a lint at `span` from a lint struct (some type that implements `LintDiagnostic`,
/// typically generated by `#[derive(LintDiagnostic)]`).
fn emit_span_lint<S: Into<MultiSpan>>(
&self,
lint: &'static Lint,
span: S,
decorator: impl for<'a> LintDiagnostic<'a, ()>,
decorator: impl for<'a> Diagnostic<'a, ()>,
) {
self.opt_span_lint(lint, Some(span), |lint| {
decorator.decorate_lint(lint);
});
self.opt_span_diag_lint(lint, Some(span), decorator);
}

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

/// Emit a lint from a lint struct (some type that implements `LintDiagnostic`, typically
/// generated by `#[derive(LintDiagnostic)]`).
fn emit_diag_lint(&self, lint: &'static Lint, decorator: impl for<'a> Diagnostic<'a, ()>) {
self.opt_span_diag_lint(lint, None as Option<Span>, decorator);
}

/// Emit a lint at the appropriate level, with no associated span.
///
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
Expand Down Expand Up @@ -644,6 +661,20 @@ impl<'tcx> LintContext for LateContext<'tcx> {
}
}

fn opt_span_diag_lint<S: Into<MultiSpan>>(
&self,
lint: &'static Lint,
span: Option<S>,
decorate: impl for<'a> Diagnostic<'a, ()>,
) {
let hir_id = self.last_node_with_lint_attrs;

match span {
Some(s) => self.tcx.emit_node_span_lint(lint, hir_id, s, decorate),
None => self.tcx.emit_node_lint(lint, hir_id, decorate),
}
}

fn get_lint_level(&self, lint: &'static Lint) -> LevelAndSource {
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs)
}
Expand All @@ -664,6 +695,15 @@ impl LintContext for EarlyContext<'_> {
self.builder.opt_span_lint(lint, span.map(|s| s.into()), decorate)
}

fn opt_span_diag_lint<S: Into<MultiSpan>>(
&self,
lint: &'static Lint,
span: Option<S>,
decorator: impl for<'a> Diagnostic<'a, ()>,
) {
self.builder.opt_span_diag_lint(lint, span.map(|s| s.into()), decorator)
}

fn get_lint_level(&self, lint: &'static Lint) -> LevelAndSource {
self.builder.lint_level(lint)
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint/src/dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& find_attr!(cx.tcx, fn_id, RustcAsPtr(_))
{
// FIXME: use `emit_node_lint` when `#[primary_span]` is added.
cx.tcx.emit_node_span_lint(
DANGLING_POINTERS_FROM_TEMPORARIES,
expr.hir_id,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ impl EarlyLintPass for BadUseOfFindAttr {
cx.emit_span_lint(
BAD_USE_OF_FIND_ATTR,
segment.span(),
AttributeKindInFindAttr {},
AttributeKindInFindAttr,
);
}
}
Expand Down
54 changes: 35 additions & 19 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use rustc_ast::attr::AttributeExt;
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{Diag, LintDiagnostic, MultiSpan, msg};
use rustc_errors::{Diag, Diagnostic, MultiSpan, msg};
use rustc_feature::{Features, GateIssue};
use rustc_hir::HirId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_index::IndexVec;
use rustc_middle::bug;
use rustc_middle::hir::nested_filter;
use rustc_middle::lint::{
LevelAndSource, LintExpectation, LintLevelSource, ShallowLintLevelMap, lint_level,
reveal_actual_level,
LevelAndSource, LintExpectation, LintLevelSource, ShallowLintLevelMap, diag_lint_level,
lint_level, reveal_actual_level,
};
use rustc_middle::query::Providers;
use rustc_middle::ty::{RegisteredTools, TyCtxt};
Expand Down Expand Up @@ -822,8 +822,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
let name =
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
let lint = RenamedLint { name: name.as_str(), replace, suggestion };
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
self.emit_span_lint(
RENAMED_AND_REMOVED_LINTS,
sp.into(),
RenamedLint { name: name.as_str(), replace, suggestion },
);
}

// If this lint was renamed, apply the new lint instead of ignoring the
Expand All @@ -844,8 +847,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
if self.lint_added_lints {
let name =
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
let lint = RemovedLint { name: name.as_str(), reason };
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
self.emit_span_lint(
RENAMED_AND_REMOVED_LINTS,
sp.into(),
RemovedLint { name: name.as_str(), reason },
);
}
continue;
}
Expand All @@ -861,8 +867,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
from_rustc,
}
});
let lint = UnknownLint { name, suggestion };
self.emit_span_lint(UNKNOWN_LINTS, sp.into(), lint);
self.emit_span_lint(
UNKNOWN_LINTS,
sp.into(),
UnknownLint { name, suggestion },
);
}
continue;
}
Expand Down Expand Up @@ -967,8 +976,6 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {

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

/// Used to emit a lint-related diagnostic based on the current state of
/// this lint context.
#[track_caller]
pub(crate) fn opt_span_diag_lint(
&self,
lint: &'static Lint,
span: Option<MultiSpan>,
decorator: impl for<'a> Diagnostic<'a, ()>,
) {
let level = self.lint_level(lint);
diag_lint_level(self.sess, lint, level, span, decorator)
}

#[track_caller]
pub fn emit_span_lint(
&self,
lint: &'static Lint,
span: MultiSpan,
decorate: impl for<'a> LintDiagnostic<'a, ()>,
decorator: impl for<'a> Diagnostic<'a, ()>,
) {
let level = self.lint_level(lint);
lint_level(self.sess, lint, level, Some(span), |lint| {
decorate.decorate_lint(lint);
});
diag_lint_level(self.sess, lint, level, Some(span), decorator);
}

#[track_caller]
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> LintDiagnostic<'a, ()>) {
pub fn emit_lint(&self, lint: &'static Lint, decorator: impl for<'a> Diagnostic<'a, ()>) {
let level = self.lint_level(lint);
lint_level(self.sess, lint, level, None, |lint| {
decorate.decorate_lint(lint);
});
diag_lint_level(self.sess, lint, level, None, decorator);
}
}

Expand Down
Loading
Loading