Skip to content

Commit 65b93eb

Browse files
committed
introduce LintTable
1 parent 93e0bc6 commit 65b93eb

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

src/librustc/lint/context.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,10 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
773773

774774
// Output any lints that were previously added to the session.
775775
fn visit_id(&mut self, id: ast::NodeId) {
776-
if let Some(lints) = self.sess().lints.borrow_mut().remove(&id) {
777-
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
778-
for early_lint in lints {
779-
self.early_lint(early_lint);
780-
}
776+
let lints = self.sess().lints.borrow_mut().take(id);
777+
for early_lint in lints {
778+
debug!("LateContext::visit_id: id={:?} early_lint={:?}", id, early_lint);
779+
self.early_lint(early_lint);
781780
}
782781
}
783782

@@ -1232,7 +1231,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12321231

12331232
// If we missed any lints added to the session, then there's a bug somewhere
12341233
// in the iteration code.
1235-
for (id, v) in tcx.sess.lints.borrow().iter() {
1234+
if let Some((id, v)) = tcx.sess.lints.borrow().get_any() {
12361235
for early_lint in v {
12371236
span_bug!(early_lint.diagnostic.span.clone(),
12381237
"unprocessed lint {:?} at {}",
@@ -1250,10 +1249,9 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12501249
// Visit the whole crate.
12511250
cx.with_lint_attrs(&krate.attrs, |cx| {
12521251
// Lints may be assigned to the whole crate.
1253-
if let Some(lints) = cx.sess.lints.borrow_mut().remove(&ast::CRATE_NODE_ID) {
1254-
for early_lint in lints {
1255-
cx.early_lint(early_lint);
1256-
}
1252+
let lints = cx.sess.lints.borrow_mut().take(ast::CRATE_NODE_ID);
1253+
for early_lint in lints {
1254+
cx.early_lint(early_lint);
12571255
}
12581256

12591257
// since the root module isn't visited as an item (because it isn't an
@@ -1270,7 +1268,7 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12701268

12711269
// If we missed any lints added to the session, then there's a bug somewhere
12721270
// in the iteration code.
1273-
for (_, v) in sess.lints.borrow().iter() {
1271+
for (_, v) in sess.lints.borrow().get_any() {
12741272
for early_lint in v {
12751273
span_bug!(early_lint.diagnostic.span.clone(), "unprocessed lint {:?}", early_lint);
12761274
}

src/librustc/lint/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
4343
raw_emit_lint, check_crate, check_ast_crate, gather_attrs,
4444
raw_struct_lint, FutureIncompatibleInfo, EarlyLint, IntoEarlyLint};
4545

46+
pub use lint::table::LintTable;
47+
4648
/// Specification of a single lint.
4749
#[derive(Copy, Clone, Debug)]
4850
pub struct Lint {
@@ -346,3 +348,4 @@ pub type LevelSource = (Level, LintSource);
346348

347349
pub mod builtin;
348350
mod context;
351+
mod table;

src/librustc/lint/table.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use syntax::ast;
2+
use syntax_pos::MultiSpan;
3+
use util::nodemap::NodeMap;
4+
5+
use super::{Lint, LintId, EarlyLint, IntoEarlyLint};
6+
7+
pub struct LintTable {
8+
map: NodeMap<Vec<EarlyLint>>
9+
}
10+
11+
impl LintTable {
12+
pub fn new() -> Self {
13+
LintTable { map: NodeMap() }
14+
}
15+
16+
pub fn add_lint<S: Into<MultiSpan>>(&mut self,
17+
lint: &'static Lint,
18+
id: ast::NodeId,
19+
sp: S,
20+
msg: String)
21+
{
22+
self.add_lint_diagnostic(lint, id, (sp, &msg[..]))
23+
}
24+
25+
pub fn add_lint_diagnostic<M>(&mut self,
26+
lint: &'static Lint,
27+
id: ast::NodeId,
28+
msg: M)
29+
where M: IntoEarlyLint,
30+
{
31+
let lint_id = LintId::of(lint);
32+
let early_lint = msg.into_early_lint(lint_id);
33+
let arr = self.map.entry(id).or_insert(vec![]);
34+
if !arr.contains(&early_lint) {
35+
arr.push(early_lint);
36+
}
37+
}
38+
39+
pub fn get(&self, id: ast::NodeId) -> &[EarlyLint] {
40+
self.map.get(&id).map(|v| &v[..]).unwrap_or(&[])
41+
}
42+
43+
pub fn take(&mut self, id: ast::NodeId) -> Vec<EarlyLint> {
44+
self.map.remove(&id).unwrap_or(vec![])
45+
}
46+
47+
/// Returns the first (id, lint) pair that is non-empty. Used to
48+
/// implement a sanity check in lints that all node-ids are
49+
/// visited.
50+
pub fn get_any(&self) -> Option<(&ast::NodeId, &Vec<EarlyLint>)> {
51+
self.map.iter()
52+
.filter(|&(_, v)| !v.is_empty())
53+
.next()
54+
}
55+
}
56+

src/librustc/session/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use middle::dependency_format;
2020
use session::search_paths::PathKind;
2121
use session::config::DebugInfoLevel;
2222
use ty::tls;
23-
use util::nodemap::{NodeMap, FxHashMap, FxHashSet};
23+
use util::nodemap::{FxHashMap, FxHashSet};
2424
use util::common::duration_to_secs_str;
2525
use mir::transform as mir_pass;
2626

@@ -78,7 +78,7 @@ pub struct Session {
7878
pub local_crate_source_file: Option<PathBuf>,
7979
pub working_dir: PathBuf,
8080
pub lint_store: RefCell<lint::LintStore>,
81-
pub lints: RefCell<NodeMap<Vec<lint::EarlyLint>>>,
81+
pub lints: RefCell<lint::LintTable>,
8282
/// Set of (LintId, span, message) tuples tracking lint (sub)diagnostics
8383
/// that have been set once, but should not be set again, in order to avoid
8484
/// redundantly verbose output (Issue #24690).
@@ -270,13 +270,14 @@ impl Session {
270270
pub fn unimpl(&self, msg: &str) -> ! {
271271
self.diagnostic().unimpl(msg)
272272
}
273+
273274
pub fn add_lint<S: Into<MultiSpan>>(&self,
274275
lint: &'static lint::Lint,
275276
id: ast::NodeId,
276277
sp: S,
277278
msg: String)
278279
{
279-
self.add_lint_diagnostic(lint, id, (sp, &msg[..]))
280+
self.lints.borrow_mut().add_lint(lint, id, sp, msg);
280281
}
281282

282283
pub fn add_lint_diagnostic<M>(&self,
@@ -285,17 +286,9 @@ impl Session {
285286
msg: M)
286287
where M: lint::IntoEarlyLint,
287288
{
288-
let lint_id = lint::LintId::of(lint);
289-
let mut lints = self.lints.borrow_mut();
290-
let early_lint = msg.into_early_lint(lint_id);
291-
if let Some(arr) = lints.get_mut(&id) {
292-
if !arr.contains(&early_lint) {
293-
arr.push(early_lint);
294-
}
295-
return;
296-
}
297-
lints.insert(id, vec![early_lint]);
289+
self.lints.borrow_mut().add_lint_diagnostic(lint, id, msg);
298290
}
291+
299292
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
300293
let id = self.next_node_id.get();
301294

@@ -617,7 +610,7 @@ pub fn build_session_(sopts: config::Options,
617610
local_crate_source_file: local_crate_source_file,
618611
working_dir: env::current_dir().unwrap(),
619612
lint_store: RefCell::new(lint::LintStore::new()),
620-
lints: RefCell::new(NodeMap()),
613+
lints: RefCell::new(lint::LintTable::new()),
621614
one_time_diagnostics: RefCell::new(FxHashSet()),
622615
plugin_llvm_passes: RefCell::new(Vec::new()),
623616
mir_passes: RefCell::new(mir_pass::Passes::new()),

0 commit comments

Comments
 (0)