Skip to content

Commit 75bc8bf

Browse files
committed
introduce EarlyLint type
For now, this type just replaces a tuple, but it will eventually grow the ability to carry more structured information.
1 parent bd5fa75 commit 75bc8bf

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

src/librustc/lint/context.rs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use util::nodemap::FnvHashMap;
3838
use std::cmp;
3939
use std::default::Default as StdDefault;
4040
use std::mem;
41+
use std::fmt;
4142
use syntax::attr;
4243
use syntax::parse::token::InternedString;
4344
use syntax::ast;
@@ -80,6 +81,41 @@ pub struct LintStore {
8081
lint_cap: Option<Level>,
8182
}
8283

84+
/// When you call `add_lint` on the session, you wind up storing one
85+
/// of these, which records a "potential lint" at a particular point.
86+
pub struct EarlyLint {
87+
/// what lint is this? (e.g., `dead_code`)
88+
pub id: LintId,
89+
90+
/// the span where the lint will be reported at
91+
pub span: Span,
92+
93+
/// the main message
94+
pub msg: String,
95+
}
96+
97+
impl fmt::Debug for EarlyLint {
98+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99+
f.debug_struct("EarlyLint")
100+
.field("id", &self.id)
101+
.field("span", &self.span)
102+
.field("msg", &self.msg)
103+
.finish()
104+
}
105+
}
106+
107+
impl EarlyLint {
108+
pub fn new(id: LintId, span: Span, msg: String) -> Self {
109+
EarlyLint { id: id, span: span, msg: msg }
110+
}
111+
112+
pub fn matches(&self, other: &EarlyLint) -> bool {
113+
self.id == other.id && self.span == other.span && self.msg == other.msg
114+
}
115+
}
116+
117+
118+
83119
/// Extra information for a future incompatibility lint. See the call
84120
/// to `register_future_incompatible` in `librustc_lint/lib.rs` for
85121
/// guidelines.
@@ -514,6 +550,11 @@ pub trait LintContext: Sized {
514550
self.lookup_and_emit(lint, Some(span), msg);
515551
}
516552

553+
fn early_lint(&self, early_lint: EarlyLint) {
554+
let mut err = self.struct_span_lint(early_lint.id.lint, early_lint.span, &early_lint.msg);
555+
err.emit();
556+
}
557+
517558
fn struct_span_lint(&self,
518559
lint: &'static Lint,
519560
span: Span,
@@ -1065,8 +1106,8 @@ impl<'a, 'b, 'tcx, 'v> hir_visit::Visitor<'v> for IdVisitor<'a, 'b, 'tcx> {
10651106
fn visit_id(&mut self, id: ast::NodeId) {
10661107
if let Some(lints) = self.cx.sess().lints.borrow_mut().remove(&id) {
10671108
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
1068-
for (lint_id, span, msg) in lints {
1069-
self.cx.span_lint(lint_id.lint, span, &msg[..])
1109+
for early_lint in lints {
1110+
self.cx.early_lint(early_lint);
10701111
}
10711112
}
10721113
}
@@ -1211,10 +1252,10 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12111252
// If we missed any lints added to the session, then there's a bug somewhere
12121253
// in the iteration code.
12131254
for (id, v) in tcx.sess.lints.borrow().iter() {
1214-
for &(lint, span, ref msg) in v {
1215-
span_bug!(span,
1216-
"unprocessed lint {} at {}: {}",
1217-
lint.to_string(), tcx.map.node_to_string(*id), *msg)
1255+
for early_lint in v {
1256+
span_bug!(early_lint.span,
1257+
"unprocessed lint {:?} at {}",
1258+
early_lint, tcx.map.node_to_string(*id));
12181259
}
12191260
}
12201261

@@ -1229,8 +1270,8 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12291270
cx.with_lint_attrs(&krate.attrs, |cx| {
12301271
// Lints may be assigned to the whole crate.
12311272
if let Some(lints) = cx.sess.lints.borrow_mut().remove(&ast::CRATE_NODE_ID) {
1232-
for (lint_id, span, msg) in lints {
1233-
cx.span_lint(lint_id.lint, span, &msg[..])
1273+
for early_lint in lints {
1274+
cx.early_lint(early_lint);
12341275
}
12351276
}
12361277

@@ -1249,8 +1290,8 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12491290
// If we missed any lints added to the session, then there's a bug somewhere
12501291
// in the iteration code.
12511292
for (_, v) in sess.lints.borrow().iter() {
1252-
for &(lint, span, ref msg) in v {
1253-
span_bug!(span, "unprocessed lint {}: {}", lint.to_string(), *msg)
1293+
for early_lint in v {
1294+
span_bug!(early_lint.span, "unprocessed lint {:?}", early_lint);
12541295
}
12551296
}
12561297
}

src/librustc/lint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use hir;
4141

4242
pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
4343
raw_emit_lint, check_crate, check_ast_crate, gather_attrs,
44-
raw_struct_lint, FutureIncompatibleInfo};
44+
raw_struct_lint, FutureIncompatibleInfo, EarlyLint};
4545

4646
/// Specification of a single lint.
4747
#[derive(Copy, Clone, Debug)]

src/librustc/session/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct Session {
7474
pub local_crate_source_file: Option<PathBuf>,
7575
pub working_dir: PathBuf,
7676
pub lint_store: RefCell<lint::LintStore>,
77-
pub lints: RefCell<NodeMap<Vec<(lint::LintId, Span, String)>>>,
77+
pub lints: RefCell<NodeMap<Vec<lint::EarlyLint>>>,
7878
/// Set of (LintId, span, message) tuples tracking lint (sub)diagnostics
7979
/// that have been set once, but should not be set again, in order to avoid
8080
/// redundantly verbose output (Issue #24690).
@@ -265,14 +265,14 @@ impl Session {
265265
msg: String) {
266266
let lint_id = lint::LintId::of(lint);
267267
let mut lints = self.lints.borrow_mut();
268+
let early_lint = lint::EarlyLint::new(lint_id, sp, msg);
268269
if let Some(arr) = lints.get_mut(&id) {
269-
let tuple = (lint_id, sp, msg);
270-
if !arr.contains(&tuple) {
271-
arr.push(tuple);
270+
if !arr.iter().any(|l| l.matches(&early_lint)) {
271+
arr.push(early_lint);
272272
}
273273
return;
274274
}
275-
lints.insert(id, vec![(lint_id, sp, msg)]);
275+
lints.insert(id, vec![early_lint]);
276276
}
277277
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
278278
let id = self.next_node_id.get();

0 commit comments

Comments
 (0)