Skip to content

Commit 37c2bb8

Browse files
committed
Use MultiSpan for lints
1 parent 6dc112d commit 37c2bb8

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/librustc/lint/context.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::default::Default as StdDefault;
4141
use std::mem;
4242
use syntax::ast_util::{self, IdVisitingOperation};
4343
use syntax::attr::{self, AttrMetaMethods};
44-
use syntax::codemap::Span;
44+
use syntax::codemap::{Span, MultiSpan};
4545
use syntax::errors::DiagnosticBuilder;
4646
use syntax::parse::token::InternedString;
4747
use syntax::ast;
@@ -398,20 +398,20 @@ pub fn gather_attr(attr: &ast::Attribute)
398398
/// in trans that run after the main lint pass is finished. Most
399399
/// lints elsewhere in the compiler should call
400400
/// `Session::add_lint()` instead.
401-
pub fn raw_emit_lint(sess: &Session,
401+
pub fn raw_emit_lint<S: Into<MultiSpan>>(sess: &Session,
402402
lints: &LintStore,
403403
lint: &'static Lint,
404404
lvlsrc: LevelSource,
405-
span: Option<Span>,
405+
span: Option<S>,
406406
msg: &str) {
407407
raw_struct_lint(sess, lints, lint, lvlsrc, span, msg).emit();
408408
}
409409

410-
pub fn raw_struct_lint<'a>(sess: &'a Session,
410+
pub fn raw_struct_lint<'a, S: Into<MultiSpan>>(sess: &'a Session,
411411
lints: &LintStore,
412412
lint: &'static Lint,
413413
lvlsrc: LevelSource,
414-
span: Option<Span>,
414+
span: Option<S>,
415415
msg: &str)
416416
-> DiagnosticBuilder<'a> {
417417
let (mut level, source) = lvlsrc;
@@ -442,10 +442,15 @@ pub fn raw_struct_lint<'a>(sess: &'a Session,
442442
// For purposes of printing, we can treat forbid as deny.
443443
if level == Forbid { level = Deny; }
444444

445-
let mut err = match (level, span) {
446-
(Warn, Some(sp)) => sess.struct_span_warn(sp, &msg[..]),
445+
let span = span.map(|s| s.into());
446+
let mut err = match (level, span.clone()) {
447+
(Warn, Some(sp)) => {
448+
sess.struct_span_warn(sp, &msg[..])
449+
},
447450
(Warn, None) => sess.struct_warn(&msg[..]),
448-
(Deny, Some(sp)) => sess.struct_span_err(sp, &msg[..]),
451+
(Deny, Some(sp)) => {
452+
sess.struct_span_err(sp, &msg[..])
453+
},
449454
(Deny, None) => sess.struct_err(&msg[..]),
450455
_ => sess.bug("impossible level in raw_emit_lint"),
451456
};
@@ -458,7 +463,7 @@ pub fn raw_struct_lint<'a>(sess: &'a Session,
458463
let citation = format!("for more information, see {}",
459464
future_incompatible.reference);
460465
if let Some(sp) = span {
461-
err.fileline_warn(sp, &explanation);
466+
err.fileline_warn(sp.clone(), &explanation);
462467
err.fileline_note(sp, &citation);
463468
} else {
464469
err.warn(&explanation);
@@ -497,7 +502,7 @@ pub trait LintContext: Sized {
497502
})
498503
}
499504

500-
fn lookup_and_emit(&self, lint: &'static Lint, span: Option<Span>, msg: &str) {
505+
fn lookup_and_emit(&self, lint: &'static Lint, span: Option<MultiSpan>, msg: &str) {
501506
let (level, src) = match self.level_src(lint) {
502507
None => return,
503508
Some(pair) => pair,
@@ -520,8 +525,8 @@ pub trait LintContext: Sized {
520525
}
521526

522527
/// Emit a lint at the appropriate level, for a particular span.
523-
fn span_lint(&self, lint: &'static Lint, span: Span, msg: &str) {
524-
self.lookup_and_emit(lint, Some(span), msg);
528+
fn span_lint<S: Into<MultiSpan>>(&self, lint: &'static Lint, span: S, msg: &str) {
529+
self.lookup_and_emit(lint, Some(span.into()), msg);
525530
}
526531

527532
fn struct_span_lint(&self,
@@ -1258,8 +1263,8 @@ pub fn check_crate(tcx: &ty::ctxt, access_levels: &AccessLevels) {
12581263
// If we missed any lints added to the session, then there's a bug somewhere
12591264
// in the iteration code.
12601265
for (id, v) in tcx.sess.lints.borrow().iter() {
1261-
for &(lint, span, ref msg) in v {
1262-
tcx.sess.span_bug(span,
1266+
for &(lint, ref span, ref msg) in v {
1267+
tcx.sess.span_bug(span.clone(),
12631268
&format!("unprocessed lint {} at {}: {}",
12641269
lint.as_str(), tcx.map.node_to_string(*id), *msg))
12651270
}
@@ -1292,8 +1297,8 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12921297
// If we missed any lints added to the session, then there's a bug somewhere
12931298
// in the iteration code.
12941299
for (_, v) in sess.lints.borrow().iter() {
1295-
for &(lint, span, ref msg) in v {
1296-
sess.span_bug(span,
1300+
for &(lint, ref span, ref msg) in v {
1301+
sess.span_bug(span.clone(),
12971302
&format!("unprocessed lint {}: {}",
12981303
lint.as_str(), *msg))
12991304
}

src/librustc/session/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct Session {
5757
pub local_crate_source_file: Option<PathBuf>,
5858
pub working_dir: PathBuf,
5959
pub lint_store: RefCell<lint::LintStore>,
60-
pub lints: RefCell<NodeMap<Vec<(lint::LintId, Span, String)>>>,
60+
pub lints: RefCell<NodeMap<Vec<(lint::LintId, MultiSpan, String)>>>,
6161
pub plugin_llvm_passes: RefCell<Vec<String>>,
6262
pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
6363
pub crate_types: RefCell<Vec<config::CrateType>>,
@@ -236,18 +236,18 @@ impl Session {
236236
pub fn unimpl(&self, msg: &str) -> ! {
237237
self.diagnostic().unimpl(msg)
238238
}
239-
pub fn add_lint(&self,
239+
pub fn add_lint<S: Into<MultiSpan>>(&self,
240240
lint: &'static lint::Lint,
241241
id: ast::NodeId,
242-
sp: Span,
242+
sp: S,
243243
msg: String) {
244244
let lint_id = lint::LintId::of(lint);
245245
let mut lints = self.lints.borrow_mut();
246246
match lints.get_mut(&id) {
247-
Some(arr) => { arr.push((lint_id, sp, msg)); return; }
247+
Some(arr) => { arr.push((lint_id, sp.into(), msg)); return; }
248248
None => {}
249249
}
250-
lints.insert(id, vec!((lint_id, sp, msg)));
250+
lints.insert(id, vec!((lint_id, sp.into(), msg)));
251251
}
252252
pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
253253
let id = self.next_node_id.get();

src/libsyntax/codemap.rs

+8
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ impl MultiSpan {
364364
}
365365
}
366366

367+
impl fmt::Debug for MultiSpan {
368+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
369+
write!(f,
370+
"MultiSpan ({})",
371+
self.spans.iter().map(|s| format!("{:?}", s)).collect::<Vec<_>>().join(", "))
372+
}
373+
}
374+
367375
impl From<Span> for MultiSpan {
368376
fn from(span: Span) -> MultiSpan {
369377
MultiSpan { spans: vec![span] }

0 commit comments

Comments
 (0)