Skip to content
Open
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
25 changes: 23 additions & 2 deletions compiler/rustc_errors/src/decorate_diag.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::any::Any;

/// This module provides types and traits for buffering lints until later in compilation.
use rustc_ast::node_id::NodeId;
use rustc_data_structures::fx::FxIndexMap;
Expand All @@ -10,7 +12,9 @@ use crate::{Diag, DiagCtxtHandle, Diagnostic, Level};
/// We can't implement `Diagnostic` for `BuiltinLintDiag`, because decorating some of its
/// variants requires types we don't have yet. So, handle that case separately.
pub enum DecorateDiagCompat {
Dynamic(Box<dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>),
/// The third argument of the closure is a `Session`. However, due to the dependency tree,
/// we don't have access to `rustc_session` here, so we downcast it when needed.
Dynamic(Box<dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level, &dyn Any) -> Diag<'a, ()> + DynSend>),
Builtin(BuiltinLintDiag),
}

Expand All @@ -23,7 +27,7 @@ impl std::fmt::Debug for DecorateDiagCompat {
impl<D: for<'a> Diagnostic<'a, ()> + DynSend + 'static> From<D> for DecorateDiagCompat {
#[inline]
fn from(d: D) -> Self {
Self::Dynamic(Box::new(|dcx, level| d.into_diag(dcx, level)))
Self::Dynamic(Box::new(|dcx, level, _| d.into_diag(dcx, level)))
}
}

Expand Down Expand Up @@ -90,6 +94,23 @@ impl LintBuffer {
node_id: NodeId,
span: impl Into<MultiSpan>,
callback: F,
) {
self.add_early_lint(BufferedEarlyLint {
lint_id: LintId::of(lint),
node_id,
span: Some(span.into()),
diagnostic: DecorateDiagCompat::Dynamic(Box::new(|dcx, level, _| callback(dcx, level))),
});
}

pub fn dyn_buffer_lint_any<
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level, &dyn Any) -> Diag<'a, ()> + DynSend + 'static,
>(
&mut self,
lint: &'static Lint,
node_id: NodeId,
span: impl Into<MultiSpan>,
callback: F,
) {
self.add_early_lint(BufferedEarlyLint {
lint_id: LintId::of(lint),
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::panic;
use std::path::PathBuf;
use std::thread::panicking;

use rustc_data_structures::sync::DynSend;
use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg};
use rustc_lint_defs::{Applicability, LintExpectationId};
use rustc_macros::{Decodable, Encodable};
Expand Down Expand Up @@ -119,14 +118,6 @@ where
}
}

impl<'a> Diagnostic<'a, ()>
for Box<dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSend + 'static>
{
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
self(dcx, level)
}
}

/// Type used to emit diagnostic through a closure instead of implementing the `Diagnostic` trait.
pub struct DiagDecorator<F: FnOnce(&mut Diag<'_, ()>)>(pub F);

Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use rustc_session::lint::LintPass;
use rustc_span::{Ident, Span};
use tracing::debug;

use crate::DecorateBuiltinLint;
use crate::context::{EarlyContext, LintContext, LintStore};
use crate::passes::{EarlyLintPass, EarlyLintPassObject};
use crate::{DecorateBuiltinLint, DiagAndSess};

pub(super) mod diagnostics;

Expand Down Expand Up @@ -49,8 +49,12 @@ impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
},
);
}
DecorateDiagCompat::Dynamic(d) => {
self.context.opt_span_lint(lint_id.lint, span, d);
DecorateDiagCompat::Dynamic(callback) => {
self.context.opt_span_lint(
lint_id.lint,
span,
DiagAndSess { callback, sess: self.context.sess() },
);
}
}
}
Expand Down
68 changes: 15 additions & 53 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::any::Any;
use std::borrow::Cow;

use rustc_data_structures::sync::DynSend;
use rustc_errors::{
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, Level,
elided_lifetime_in_path_suggestion,
Expand All @@ -8,12 +10,24 @@ use rustc_hir::lints::{AttributeLintKind, FormatWarning};
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::lint::BuiltinLintDiag;
use tracing::debug;

use crate::lints;

mod check_cfg;

pub struct DiagAndSess<'sess> {
pub callback: Box<
dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level, &dyn Any) -> Diag<'b, ()> + DynSend + 'static,
>,
pub sess: &'sess Session,
}

impl<'a> Diagnostic<'a, ()> for DiagAndSess<'_> {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
(self.callback)(dcx, level, self.sess)
}
}

/// This is a diagnostic struct that will decorate a `BuiltinLintDiag`
/// Directly creating the lint structs is expensive, using this will only decorate the lint structs when needed.
pub struct DecorateBuiltinLint<'sess, 'tcx> {
Expand All @@ -25,28 +39,6 @@ pub struct DecorateBuiltinLint<'sess, 'tcx> {
impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
match self.diagnostic {
BuiltinLintDiag::AbsPathWithModule(mod_span) => {
let (replacement, applicability) =
match self.sess.source_map().span_to_snippet(mod_span) {
Ok(ref s) => {
// FIXME(Manishearth) ideally the emitting code
// can tell us whether or not this is global
let opt_colon =
if s.trim_start().starts_with("::") { "" } else { "::" };

(format!("crate{opt_colon}{s}"), Applicability::MachineApplicable)
}
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
};
lints::AbsPathWithModule {
sugg: lints::AbsPathWithModuleSugg {
span: mod_span,
applicability,
replacement,
},
}
.into_diag(dcx, level)
}
BuiltinLintDiag::ElidedLifetimesInPaths(
n,
path_span,
Expand Down Expand Up @@ -87,36 +79,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
}
.into_diag(dcx, level)
}
BuiltinLintDiag::SingleUseLifetime {
param_span,
use_span,
elidable,
deletion_span,
ident,
} => {
debug!(?param_span, ?use_span, ?deletion_span);
let suggestion = if let Some(deletion_span) = deletion_span {
let (use_span, replace_lt) = if elidable {
let use_span =
self.sess.source_map().span_extend_while_whitespace(use_span);
(use_span, String::new())
} else {
(use_span, "'_".to_owned())
};
debug!(?deletion_span, ?use_span);

// issue 107998 for the case such as a wrong function pointer type
// `deletion_span` is empty and there is no need to report lifetime uses here
let deletion_span =
if deletion_span.is_empty() { None } else { Some(deletion_span) };
Some(lints::SingleUseLifetimeSugg { deletion_span, use_span, replace_lt })
} else {
None
};

lints::SingleUseLifetime { suggestion, param_span, use_span, ident }
.into_diag(dcx, level)
}
BuiltinLintDiag::NamedArgumentUsedPositionally {
position_sp_to_replace,
position_sp_for_msg,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ use unused::*;
#[rustfmt::skip]
pub use builtin::{MissingDoc, SoftLints};
pub use context::{CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore};
pub use early::diagnostics::{DecorateAttrLint, DecorateBuiltinLint};
pub use early::diagnostics::{DecorateAttrLint, DecorateBuiltinLint, DiagAndSess};
pub use early::{EarlyCheckNode, check_ast_node};
pub use late::{check_crate, late_lint_mod, unerased_lint_store};
pub use levels::LintLevelsBuilder;
Expand Down
43 changes: 0 additions & 43 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3010,25 +3010,6 @@ pub(crate) struct IllFormedAttributeInput {
pub docs: &'static str,
}

#[derive(Diagnostic)]
#[diag(
"absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition"
)]
pub(crate) struct AbsPathWithModule {
#[subdiagnostic]
pub sugg: AbsPathWithModuleSugg,
}

#[derive(Subdiagnostic)]
#[suggestion("use `crate`", code = "{replacement}")]
pub(crate) struct AbsPathWithModuleSugg {
#[primary_span]
pub span: Span,
#[applicability]
pub applicability: Applicability,
pub replacement: String,
}

#[derive(Diagnostic)]
#[diag("hidden lifetime parameters in types are deprecated")]
pub(crate) struct ElidedLifetimesInPaths {
Expand Down Expand Up @@ -3080,30 +3061,6 @@ pub(crate) enum UnusedImportsSugg {
},
}

#[derive(Diagnostic)]
#[diag("lifetime parameter `{$ident}` only used once")]
pub(crate) struct SingleUseLifetime {
#[label("this lifetime...")]
pub param_span: Span,
#[label("...is used only here")]
pub use_span: Span,
#[subdiagnostic]
pub suggestion: Option<SingleUseLifetimeSugg>,

pub ident: Ident,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion("elide the single-use lifetime", applicability = "machine-applicable")]
pub(crate) struct SingleUseLifetimeSugg {
#[suggestion_part(code = "")]
pub deletion_span: Option<Span>,
#[suggestion_part(code = "{replace_lt}")]
pub use_span: Span,

pub replace_lt: String,
}

#[derive(Diagnostic)]
#[diag("named argument `{$named_arg_name}` is not used by name")]
pub(crate) struct NamedArgumentUsedPositionally {
Expand Down
13 changes: 0 additions & 13 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ pub enum DeprecatedSinceKind {
// becomes hacky (and it gets allocated).
#[derive(Debug)]
pub enum BuiltinLintDiag {
AbsPathWithModule(Span),
ElidedLifetimesInPaths(usize, Span, bool, Span),
UnusedImports {
remove_whole_use: bool,
Expand All @@ -665,18 +664,6 @@ pub enum BuiltinLintDiag {
test_module_span: Option<Span>,
span_snippets: Vec<String>,
},
SingleUseLifetime {
/// Span of the parameter which declares this lifetime.
param_span: Span,
/// Span of the code that should be removed when eliding this lifetime.
/// This span should include leading or trailing comma.
deletion_span: Option<Span>,
/// Span of the single use, or None if the lifetime is never used.
/// If true, the lifetime will be fully elided.
use_span: Span,
elidable: bool,
ident: Ident,
},
NamedArgumentUsedPositionally {
/// Span where the named argument is used by position and will be replaced with the named
/// argument name
Expand Down
32 changes: 27 additions & 5 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
Applicability, Diag, DiagCtxtHandle, Diagnostic, ErrorGuaranteed, MultiSpan, SuggestionStyle,
struct_span_code_err,
};
use rustc_feature::BUILTIN_ATTRIBUTES;
Expand All @@ -23,7 +23,6 @@ use rustc_hir::{PrimTy, Stability, StabilityLevel, find_attr};
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS, AMBIGUOUS_IMPORT_VISIBILITIES,
AMBIGUOUS_PANIC_IMPORTS, MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
Expand Down Expand Up @@ -510,12 +509,35 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return;
}

let diag = BuiltinLintDiag::AbsPathWithModule(root_span);
self.lint_buffer.buffer_lint(
self.lint_buffer.dyn_buffer_lint_any(
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
node_id,
root_span,
diag,
move |dcx, level, sess| {
let (replacement, applicability) = match sess
.downcast_ref::<Session>()
.expect("expected a `Session`")
.source_map()
.span_to_snippet(root_span)
{
Ok(ref s) => {
// FIXME(Manishearth) ideally the emitting code
// can tell us whether or not this is global
let opt_colon = if s.trim_start().starts_with("::") { "" } else { "::" };

(format!("crate{opt_colon}{s}"), Applicability::MachineApplicable)
}
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
};
errors::AbsPathWithModule {
sugg: errors::AbsPathWithModuleSugg {
span: root_span,
applicability,
replacement,
},
}
.into_diag(dcx, level)
},
);
}

Expand Down
43 changes: 43 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,3 +1688,46 @@ pub(crate) struct AssociatedConstElidedLifetime {
#[note("cannot automatically infer `'static` because of other lifetimes in scope")]
pub lifetimes_in_scope: MultiSpan,
}

#[derive(Diagnostic)]
#[diag("lifetime parameter `{$ident}` only used once")]
pub(crate) struct SingleUseLifetime {
#[label("this lifetime...")]
pub param_span: Span,
#[label("...is used only here")]
pub use_span: Span,
#[subdiagnostic]
pub suggestion: Option<SingleUseLifetimeSugg>,

pub ident: Ident,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion("elide the single-use lifetime", applicability = "machine-applicable")]
pub(crate) struct SingleUseLifetimeSugg {
#[suggestion_part(code = "")]
pub deletion_span: Option<Span>,
#[suggestion_part(code = "{replace_lt}")]
pub use_span: Span,

pub replace_lt: String,
}

#[derive(Diagnostic)]
#[diag(
"absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition"
)]
pub(crate) struct AbsPathWithModule {
#[subdiagnostic]
pub sugg: AbsPathWithModuleSugg,
}

#[derive(Subdiagnostic)]
#[suggestion("use `crate`", code = "{replacement}")]
pub(crate) struct AbsPathWithModuleSugg {
#[primary_span]
pub span: Span,
#[applicability]
pub applicability: Applicability,
pub replacement: String,
}
Loading
Loading