Skip to content

Commit 45f2764

Browse files
committed
prepare moving HardwiredLints to rustc_session
1 parent 7dbccf5 commit 45f2764

File tree

15 files changed

+187
-199
lines changed

15 files changed

+187
-199
lines changed

src/librustc/lint/builtin.rs

Lines changed: 2 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
//! compiler code, rather than using their own custom pass. Those
55
//! lints are all available in `rustc_lint::builtin`.
66
7-
use crate::lint::{FutureIncompatibleInfo, LateLintPass, LintArray, LintPass};
8-
use crate::middle::stability;
9-
use crate::session::Session;
10-
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder};
11-
use rustc_session::declare_lint;
12-
use rustc_session::lint::BuiltinLintDiagnostics;
7+
use rustc_session::lint::FutureIncompatibleInfo;
8+
use rustc_session::{declare_lint, declare_lint_pass};
139
use rustc_span::edition::Edition;
14-
use rustc_span::source_map::Span;
1510
use syntax::early_buffered_lints::{ILL_FORMED_ATTRIBUTE_INPUT, META_VARIABLE_MISUSE};
1611

1712
declare_lint! {
@@ -512,125 +507,3 @@ declare_lint_pass! {
512507
SOFT_UNSTABLE,
513508
]
514509
}
515-
516-
impl LateLintPass<'_, '_> for HardwiredLints {}
517-
518-
pub fn add_elided_lifetime_in_path_suggestion(
519-
sess: &Session,
520-
db: &mut DiagnosticBuilder<'_>,
521-
n: usize,
522-
path_span: Span,
523-
incl_angl_brckt: bool,
524-
insertion_span: Span,
525-
anon_lts: String,
526-
) {
527-
let (replace_span, suggestion) = if incl_angl_brckt {
528-
(insertion_span, anon_lts)
529-
} else {
530-
// When possible, prefer a suggestion that replaces the whole
531-
// `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
532-
// at a point (which makes for an ugly/confusing label)
533-
if let Ok(snippet) = sess.source_map().span_to_snippet(path_span) {
534-
// But our spans can get out of whack due to macros; if the place we think
535-
// we want to insert `'_` isn't even within the path expression's span, we
536-
// should bail out of making any suggestion rather than panicking on a
537-
// subtract-with-overflow or string-slice-out-out-bounds (!)
538-
// FIXME: can we do better?
539-
if insertion_span.lo().0 < path_span.lo().0 {
540-
return;
541-
}
542-
let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize;
543-
if insertion_index > snippet.len() {
544-
return;
545-
}
546-
let (before, after) = snippet.split_at(insertion_index);
547-
(path_span, format!("{}{}{}", before, anon_lts, after))
548-
} else {
549-
(insertion_span, anon_lts)
550-
}
551-
};
552-
db.span_suggestion(
553-
replace_span,
554-
&format!("indicate the anonymous lifetime{}", pluralize!(n)),
555-
suggestion,
556-
Applicability::MachineApplicable,
557-
);
558-
}
559-
560-
pub fn run_builtin_lint_diagnostics(
561-
this: BuiltinLintDiagnostics,
562-
sess: &Session,
563-
db: &mut DiagnosticBuilder<'_>,
564-
) {
565-
match this {
566-
BuiltinLintDiagnostics::Normal => (),
567-
BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
568-
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
569-
Ok(s) if is_global => (format!("dyn ({})", s), Applicability::MachineApplicable),
570-
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
571-
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
572-
};
573-
db.span_suggestion(span, "use `dyn`", sugg, app);
574-
}
575-
BuiltinLintDiagnostics::AbsPathWithModule(span) => {
576-
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
577-
Ok(ref s) => {
578-
// FIXME(Manishearth) ideally the emitting code
579-
// can tell us whether or not this is global
580-
let opt_colon = if s.trim_start().starts_with("::") { "" } else { "::" };
581-
582-
(format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
583-
}
584-
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
585-
};
586-
db.span_suggestion(span, "use `crate`", sugg, app);
587-
}
588-
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
589-
db.span_label(
590-
span,
591-
"names from parent modules are not accessible without an explicit import",
592-
);
593-
}
594-
BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def) => {
595-
db.span_note(span_def, "the macro is defined here");
596-
}
597-
BuiltinLintDiagnostics::ElidedLifetimesInPaths(
598-
n,
599-
path_span,
600-
incl_angl_brckt,
601-
insertion_span,
602-
anon_lts,
603-
) => {
604-
add_elided_lifetime_in_path_suggestion(
605-
sess,
606-
db,
607-
n,
608-
path_span,
609-
incl_angl_brckt,
610-
insertion_span,
611-
anon_lts,
612-
);
613-
}
614-
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
615-
db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
616-
}
617-
BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
618-
if !replaces.is_empty() {
619-
db.tool_only_multipart_suggestion(
620-
&message,
621-
replaces,
622-
Applicability::MachineApplicable,
623-
);
624-
}
625-
}
626-
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
627-
for (span, is_imported) in spans {
628-
let introduced = if is_imported { "imported" } else { "defined" };
629-
db.span_label(span, format!("the item `{}` is already {} here", ident, introduced));
630-
}
631-
}
632-
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
633-
stability::deprecation_suggestion(db, suggestion, span)
634-
}
635-
}
636-
}

src/librustc/lint/context.rs

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData};
2020
use crate::lint::levels::{LintLevelSets, LintLevelsBuilder};
2121
use crate::lint::{EarlyLintPassObject, LateLintPassObject};
2222
use crate::middle::privacy::AccessLevels;
23+
use crate::middle::stability;
2324
use crate::session::Session;
2425
use crate::ty::layout::{LayoutError, LayoutOf, TyLayout};
2526
use crate::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
2627
use rustc_data_structures::fx::FxHashMap;
2728
use rustc_data_structures::sync;
2829
use rustc_error_codes::*;
29-
use rustc_errors::{struct_span_err, DiagnosticBuilder};
30+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
3031
use rustc_hir as hir;
3132
use rustc_hir::def_id::{CrateNum, DefId};
3233
use rustc_session::lint::BuiltinLintDiagnostics;
@@ -466,6 +467,48 @@ impl LintPassObject for EarlyLintPassObject {}
466467

467468
impl LintPassObject for LateLintPassObject {}
468469

470+
pub fn add_elided_lifetime_in_path_suggestion(
471+
sess: &Session,
472+
db: &mut DiagnosticBuilder<'_>,
473+
n: usize,
474+
path_span: Span,
475+
incl_angl_brckt: bool,
476+
insertion_span: Span,
477+
anon_lts: String,
478+
) {
479+
let (replace_span, suggestion) = if incl_angl_brckt {
480+
(insertion_span, anon_lts)
481+
} else {
482+
// When possible, prefer a suggestion that replaces the whole
483+
// `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
484+
// at a point (which makes for an ugly/confusing label)
485+
if let Ok(snippet) = sess.source_map().span_to_snippet(path_span) {
486+
// But our spans can get out of whack due to macros; if the place we think
487+
// we want to insert `'_` isn't even within the path expression's span, we
488+
// should bail out of making any suggestion rather than panicking on a
489+
// subtract-with-overflow or string-slice-out-out-bounds (!)
490+
// FIXME: can we do better?
491+
if insertion_span.lo().0 < path_span.lo().0 {
492+
return;
493+
}
494+
let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize;
495+
if insertion_index > snippet.len() {
496+
return;
497+
}
498+
let (before, after) = snippet.split_at(insertion_index);
499+
(path_span, format!("{}{}{}", before, anon_lts, after))
500+
} else {
501+
(insertion_span, anon_lts)
502+
}
503+
};
504+
db.span_suggestion(
505+
replace_span,
506+
&format!("indicate the anonymous lifetime{}", pluralize!(n)),
507+
suggestion,
508+
Applicability::MachineApplicable,
509+
);
510+
}
511+
469512
pub trait LintContext: Sized {
470513
type PassObject: LintPassObject;
471514

@@ -484,7 +527,85 @@ pub trait LintContext: Sized {
484527
diagnostic: BuiltinLintDiagnostics,
485528
) {
486529
let mut db = self.lookup(lint, span, msg);
487-
super::builtin::run_builtin_lint_diagnostics(diagnostic, self.sess(), &mut db);
530+
531+
let sess = self.sess();
532+
match diagnostic {
533+
BuiltinLintDiagnostics::Normal => (),
534+
BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
535+
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
536+
Ok(s) if is_global => {
537+
(format!("dyn ({})", s), Applicability::MachineApplicable)
538+
}
539+
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
540+
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
541+
};
542+
db.span_suggestion(span, "use `dyn`", sugg, app);
543+
}
544+
BuiltinLintDiagnostics::AbsPathWithModule(span) => {
545+
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
546+
Ok(ref s) => {
547+
// FIXME(Manishearth) ideally the emitting code
548+
// can tell us whether or not this is global
549+
let opt_colon = if s.trim_start().starts_with("::") { "" } else { "::" };
550+
551+
(format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
552+
}
553+
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
554+
};
555+
db.span_suggestion(span, "use `crate`", sugg, app);
556+
}
557+
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
558+
db.span_label(
559+
span,
560+
"names from parent modules are not accessible without an explicit import",
561+
);
562+
}
563+
BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def) => {
564+
db.span_note(span_def, "the macro is defined here");
565+
}
566+
BuiltinLintDiagnostics::ElidedLifetimesInPaths(
567+
n,
568+
path_span,
569+
incl_angl_brckt,
570+
insertion_span,
571+
anon_lts,
572+
) => {
573+
add_elided_lifetime_in_path_suggestion(
574+
sess,
575+
&mut db,
576+
n,
577+
path_span,
578+
incl_angl_brckt,
579+
insertion_span,
580+
anon_lts,
581+
);
582+
}
583+
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
584+
db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
585+
}
586+
BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
587+
if !replaces.is_empty() {
588+
db.tool_only_multipart_suggestion(
589+
&message,
590+
replaces,
591+
Applicability::MachineApplicable,
592+
);
593+
}
594+
}
595+
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
596+
for (span, is_imported) in spans {
597+
let introduced = if is_imported { "imported" } else { "defined" };
598+
db.span_label(
599+
span,
600+
format!("the item `{}` is already {} here", ident, introduced),
601+
);
602+
}
603+
}
604+
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
605+
stability::deprecation_suggestion(&mut db, suggestion, span)
606+
}
607+
}
608+
488609
db.emit();
489610
}
490611

src/librustc/lint/internal.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
22
//! Clippy.
33
4-
use crate::lint::{
5-
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
6-
};
4+
use crate::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
75
use rustc_data_structures::fx::FxHashMap;
86
use rustc_errors::Applicability;
97
use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, Ty, TyKind};
10-
use rustc_session::declare_tool_lint;
8+
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
119
use rustc_span::symbol::{sym, Symbol};
1210
use syntax::ast::{Ident, Item, ItemKind};
1311

src/librustc/lint/mod.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
pub use self::Level::*;
2222
pub use self::LintSource::*;
2323

24+
use crate::lint::builtin::HardwiredLints;
2425
use crate::ty::TyCtxt;
2526
use rustc_data_structures::sync;
2627
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
@@ -33,48 +34,12 @@ use rustc_span::Span;
3334
use syntax::ast;
3435

3536
pub use crate::lint::context::{
36-
CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore,
37+
add_elided_lifetime_in_path_suggestion, CheckLintNameResult, EarlyContext, LateContext,
38+
LintContext, LintStore,
3739
};
3840

3941
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Level, Lint, LintId};
40-
41-
/// Declares a static `LintArray` and return it as an expression.
42-
#[macro_export]
43-
macro_rules! lint_array {
44-
($( $lint:expr ),* ,) => { lint_array!( $($lint),* ) };
45-
($( $lint:expr ),*) => {{
46-
vec![$($lint),*]
47-
}}
48-
}
49-
50-
pub type LintArray = Vec<&'static Lint>;
51-
52-
pub trait LintPass {
53-
fn name(&self) -> &'static str;
54-
}
55-
56-
/// Implements `LintPass for $name` with the given list of `Lint` statics.
57-
#[macro_export]
58-
macro_rules! impl_lint_pass {
59-
($name:ident => [$($lint:expr),* $(,)?]) => {
60-
impl LintPass for $name {
61-
fn name(&self) -> &'static str { stringify!($name) }
62-
}
63-
impl $name {
64-
pub fn get_lints() -> LintArray { $crate::lint_array!($($lint),*) }
65-
}
66-
};
67-
}
68-
69-
/// Declares a type named `$name` which implements `LintPass`.
70-
/// To the right of `=>` a comma separated list of `Lint` statics is given.
71-
#[macro_export]
72-
macro_rules! declare_lint_pass {
73-
($(#[$m:meta])* $name:ident => [$($lint:expr),* $(,)?]) => {
74-
$(#[$m])* #[derive(Copy, Clone)] pub struct $name;
75-
$crate::impl_lint_pass!($name => [$($lint),*]);
76-
};
77-
}
42+
pub use rustc_session::lint::{LintArray, LintPass};
7843

7944
#[macro_export]
8045
macro_rules! late_lint_methods {
@@ -166,6 +131,8 @@ macro_rules! declare_late_lint_pass {
166131

167132
late_lint_methods!(declare_late_lint_pass, [], ['tcx]);
168133

134+
impl LateLintPass<'_, '_> for HardwiredLints {}
135+
169136
#[macro_export]
170137
macro_rules! expand_combined_late_lint_pass_method {
171138
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({

src/librustc_ast_lowering/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
305305
E0726,
306306
"implicit elided lifetime not allowed here"
307307
);
308-
rustc::lint::builtin::add_elided_lifetime_in_path_suggestion(
308+
rustc::lint::add_elided_lifetime_in_path_suggestion(
309309
&self.sess,
310310
&mut err,
311311
expected_lifetimes,

src/librustc_lint/array_into_iter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
2-
use rustc::lint::FutureIncompatibleInfo;
1+
use rustc::lint::{FutureIncompatibleInfo, LateContext, LateLintPass, LintContext};
32
use rustc::ty;
43
use rustc::ty::adjustment::{Adjust, Adjustment};
54
use rustc_errors::Applicability;

0 commit comments

Comments
 (0)