Skip to content

Commit 1fab03e

Browse files
committed
Move lint levels machanism in librustc_lint.
1 parent 96180ff commit 1fab03e

File tree

5 files changed

+129
-122
lines changed

5 files changed

+129
-122
lines changed

src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub struct LintLevelsBuilder<'a> {
155155

156156
pub struct BuilderPush {
157157
prev: u32,
158-
pub(super) changed: bool,
158+
pub changed: bool,
159159
}
160160

161161
impl<'a> LintLevelsBuilder<'a> {

src/librustc/lint/mod.rs

+1-120
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ pub use self::LintSource::*;
2424
use rustc_data_structures::sync;
2525

2626
use crate::hir;
27-
use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
28-
use crate::hir::intravisit;
2927
use crate::lint::builtin::BuiltinLintDiagnostics;
3028
use crate::session::{DiagnosticMessageId, Session};
31-
use crate::ty::query::Providers;
3229
use crate::ty::TyCtxt;
3330
use crate::util::nodemap::NodeMap;
3431
use errors::{DiagnosticBuilder, DiagnosticId};
@@ -375,7 +372,7 @@ mod context;
375372
pub mod internal;
376373
mod levels;
377374

378-
pub use self::levels::{LintLevelMap, LintLevelSets};
375+
pub use self::levels::{LintLevelMap, LintLevelSets, LintLevelsBuilder};
379376

380377
#[derive(Default)]
381378
pub struct LintBuffer {
@@ -563,122 +560,6 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
563560
attrs.iter().any(|attr| Level::from_symbol(attr.name_or_empty()).is_some())
564561
}
565562

566-
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
567-
assert_eq!(cnum, LOCAL_CRATE);
568-
let store = &tcx.lint_store;
569-
let mut builder = LintLevelMapBuilder {
570-
levels: LintLevelSets::builder(tcx.sess, false, &store),
571-
tcx: tcx,
572-
store: store,
573-
};
574-
let krate = tcx.hir().krate();
575-
576-
let push = builder.levels.push(&krate.attrs, &store);
577-
builder.levels.register_id(hir::CRATE_HIR_ID);
578-
for macro_def in krate.exported_macros {
579-
builder.levels.register_id(macro_def.hir_id);
580-
}
581-
intravisit::walk_crate(&mut builder, krate);
582-
builder.levels.pop(push);
583-
584-
tcx.arena.alloc(builder.levels.build_map())
585-
}
586-
587-
struct LintLevelMapBuilder<'a, 'tcx> {
588-
levels: levels::LintLevelsBuilder<'tcx>,
589-
tcx: TyCtxt<'tcx>,
590-
store: &'a LintStore,
591-
}
592-
593-
impl LintLevelMapBuilder<'_, '_> {
594-
fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
595-
where
596-
F: FnOnce(&mut Self),
597-
{
598-
let push = self.levels.push(attrs, self.store);
599-
if push.changed {
600-
self.levels.register_id(id);
601-
}
602-
f(self);
603-
self.levels.pop(push);
604-
}
605-
}
606-
607-
impl intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
608-
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
609-
intravisit::NestedVisitorMap::All(&self.tcx.hir())
610-
}
611-
612-
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
613-
self.with_lint_attrs(param.hir_id, &param.attrs, |builder| {
614-
intravisit::walk_param(builder, param);
615-
});
616-
}
617-
618-
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
619-
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
620-
intravisit::walk_item(builder, it);
621-
});
622-
}
623-
624-
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
625-
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
626-
intravisit::walk_foreign_item(builder, it);
627-
})
628-
}
629-
630-
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
631-
self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
632-
intravisit::walk_expr(builder, e);
633-
})
634-
}
635-
636-
fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
637-
self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
638-
intravisit::walk_struct_field(builder, s);
639-
})
640-
}
641-
642-
fn visit_variant(
643-
&mut self,
644-
v: &'tcx hir::Variant<'tcx>,
645-
g: &'tcx hir::Generics<'tcx>,
646-
item_id: hir::HirId,
647-
) {
648-
self.with_lint_attrs(v.id, &v.attrs, |builder| {
649-
intravisit::walk_variant(builder, v, g, item_id);
650-
})
651-
}
652-
653-
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
654-
self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
655-
intravisit::walk_local(builder, l);
656-
})
657-
}
658-
659-
fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
660-
self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
661-
intravisit::walk_arm(builder, a);
662-
})
663-
}
664-
665-
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
666-
self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
667-
intravisit::walk_trait_item(builder, trait_item);
668-
});
669-
}
670-
671-
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
672-
self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
673-
intravisit::walk_impl_item(builder, impl_item);
674-
});
675-
}
676-
}
677-
678-
pub fn provide(providers: &mut Providers<'_>) {
679-
providers.lint_levels = lint_levels;
680-
}
681-
682563
/// Returns whether `span` originates in a foreign crate's external macro.
683564
///
684565
/// This is used to test whether a lint should not even begin to figure out whether it should

src/librustc_interface/passes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
692692
rustc_resolve::provide(providers);
693693
rustc_traits::provide(providers);
694694
rustc_metadata::provide(providers);
695-
lint::provide(providers);
696695
rustc_lint::provide(providers);
697696
rustc_codegen_utils::provide(providers);
698697
rustc_codegen_ssa::provide(providers);

src/librustc_lint/levels.rs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use rustc::hir;
2+
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
3+
use rustc::hir::intravisit;
4+
use rustc::lint::{LintLevelMap, LintLevelSets, LintLevelsBuilder, LintStore};
5+
use rustc::ty::query::Providers;
6+
use rustc::ty::TyCtxt;
7+
use syntax::ast;
8+
9+
pub use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintId};
10+
11+
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
12+
assert_eq!(cnum, LOCAL_CRATE);
13+
let store = &tcx.lint_store;
14+
let mut builder = LintLevelMapBuilder {
15+
levels: LintLevelSets::builder(tcx.sess, false, &store),
16+
tcx: tcx,
17+
store: store,
18+
};
19+
let krate = tcx.hir().krate();
20+
21+
let push = builder.levels.push(&krate.attrs, &store);
22+
builder.levels.register_id(hir::CRATE_HIR_ID);
23+
for macro_def in krate.exported_macros {
24+
builder.levels.register_id(macro_def.hir_id);
25+
}
26+
intravisit::walk_crate(&mut builder, krate);
27+
builder.levels.pop(push);
28+
29+
tcx.arena.alloc(builder.levels.build_map())
30+
}
31+
32+
struct LintLevelMapBuilder<'a, 'tcx> {
33+
levels: LintLevelsBuilder<'tcx>,
34+
tcx: TyCtxt<'tcx>,
35+
store: &'a LintStore,
36+
}
37+
38+
impl LintLevelMapBuilder<'_, '_> {
39+
fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
40+
where
41+
F: FnOnce(&mut Self),
42+
{
43+
let push = self.levels.push(attrs, self.store);
44+
if push.changed {
45+
self.levels.register_id(id);
46+
}
47+
f(self);
48+
self.levels.pop(push);
49+
}
50+
}
51+
52+
impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
53+
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
54+
intravisit::NestedVisitorMap::All(&self.tcx.hir())
55+
}
56+
57+
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
58+
self.with_lint_attrs(param.hir_id, &param.attrs, |builder| {
59+
intravisit::walk_param(builder, param);
60+
});
61+
}
62+
63+
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
64+
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
65+
intravisit::walk_item(builder, it);
66+
});
67+
}
68+
69+
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
70+
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
71+
intravisit::walk_foreign_item(builder, it);
72+
})
73+
}
74+
75+
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
76+
self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
77+
intravisit::walk_expr(builder, e);
78+
})
79+
}
80+
81+
fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
82+
self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
83+
intravisit::walk_struct_field(builder, s);
84+
})
85+
}
86+
87+
fn visit_variant(
88+
&mut self,
89+
v: &'tcx hir::Variant<'tcx>,
90+
g: &'tcx hir::Generics<'tcx>,
91+
item_id: hir::HirId,
92+
) {
93+
self.with_lint_attrs(v.id, &v.attrs, |builder| {
94+
intravisit::walk_variant(builder, v, g, item_id);
95+
})
96+
}
97+
98+
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
99+
self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
100+
intravisit::walk_local(builder, l);
101+
})
102+
}
103+
104+
fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
105+
self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
106+
intravisit::walk_arm(builder, a);
107+
})
108+
}
109+
110+
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
111+
self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
112+
intravisit::walk_trait_item(builder, trait_item);
113+
});
114+
}
115+
116+
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
117+
self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
118+
intravisit::walk_impl_item(builder, impl_item);
119+
});
120+
}
121+
}
122+
123+
pub fn provide(providers: &mut Providers<'_>) {
124+
providers.lint_levels = lint_levels;
125+
}

src/librustc_lint/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod array_into_iter;
2626
pub mod builtin;
2727
mod early;
2828
mod late;
29+
mod levels;
2930
mod non_ascii_idents;
3031
mod nonstandard_style;
3132
mod redundant_semicolon;
@@ -63,6 +64,7 @@ pub use early::check_ast_crate;
6364
pub use late::check_crate;
6465

6566
pub fn provide(providers: &mut Providers<'_>) {
67+
levels::provide(providers);
6668
*providers = Providers { lint_mod, ..*providers };
6769
}
6870

0 commit comments

Comments
 (0)