Skip to content

Commit

Permalink
Implement MultiSpan error reporting
Browse files Browse the repository at this point in the history
This allows to render multiple spans on one line,
or to splice multiple replacements into a code suggestion.
  • Loading branch information
mitaa committed Jan 28, 2016
1 parent 5d6e8fc commit 727f959
Show file tree
Hide file tree
Showing 10 changed files with 1,187 additions and 434 deletions.
88 changes: 44 additions & 44 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use session::search_paths::PathKind;
use util::nodemap::{NodeMap, FnvHashMap};

use syntax::ast::{NodeId, NodeIdAssigner, Name};
use syntax::codemap::Span;
use syntax::codemap::{Span, MultiSpan};
use syntax::errors::{self, DiagnosticBuilder};
use syntax::errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
use syntax::errors::json::JsonEmitter;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct Session {
pub cstore: Rc<for<'a> CrateStore<'a>>,
pub parse_sess: ParseSess,
// For a library crate, this is always none
pub entry_fn: RefCell<Option<(NodeId, codemap::Span)>>,
pub entry_fn: RefCell<Option<(NodeId, Span)>>,
pub entry_type: Cell<Option<config::EntryFnType>>,
pub plugin_registrar_fn: Cell<Option<ast::NodeId>>,
pub default_sysroot: Option<PathBuf>,
Expand All @@ -57,7 +57,7 @@ pub struct Session {
pub local_crate_source_file: Option<PathBuf>,
pub working_dir: PathBuf,
pub lint_store: RefCell<lint::LintStore>,
pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
pub lints: RefCell<NodeMap<Vec<(lint::LintId, Span, String)>>>,
pub plugin_llvm_passes: RefCell<Vec<String>>,
pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
pub crate_types: RefCell<Vec<config::CrateType>>,
Expand All @@ -81,36 +81,36 @@ pub struct Session {
}

impl Session {
pub fn struct_span_warn<'a>(&'a self,
sp: Span,
msg: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_warn(sp, msg)
}
pub fn struct_span_warn_with_code<'a>(&'a self,
sp: Span,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
}
pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
self.diagnostic().struct_warn(msg)
}
pub fn struct_span_err<'a>(&'a self,
sp: Span,
msg: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str)
-> DiagnosticBuilder<'a> {
match split_msg_into_multilines(msg) {
Some(ref msg) => self.diagnostic().struct_span_err(sp, msg),
None => self.diagnostic().struct_span_err(sp, msg),
}
}
pub fn struct_span_err_with_code<'a>(&'a self,
sp: Span,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
match split_msg_into_multilines(msg) {
Some(ref msg) => self.diagnostic().struct_span_err_with_code(sp, msg, code),
None => self.diagnostic().struct_span_err_with_code(sp, msg, code),
Expand All @@ -119,46 +119,46 @@ impl Session {
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
self.diagnostic().struct_err(msg)
}
pub fn struct_span_fatal<'a>(&'a self,
sp: Span,
msg: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_fatal(sp, msg)
}
pub fn struct_span_fatal_with_code<'a>(&'a self,
sp: Span,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
}
pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
self.diagnostic().struct_fatal(msg)
}

pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
panic!(self.diagnostic().span_fatal(sp, msg))
}
pub fn span_fatal_with_code(&self, sp: Span, msg: &str, code: &str) -> ! {
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) -> ! {
panic!(self.diagnostic().span_fatal_with_code(sp, msg, code))
}
pub fn fatal(&self, msg: &str) -> ! {
panic!(self.diagnostic().fatal(msg))
}
pub fn span_err_or_warn(&self, is_warning: bool, sp: Span, msg: &str) {
pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) {
if is_warning {
self.span_warn(sp, msg);
} else {
self.span_err(sp, msg);
}
}
pub fn span_err(&self, sp: Span, msg: &str) {
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
match split_msg_into_multilines(msg) {
Some(msg) => self.diagnostic().span_err(sp, &msg),
None => self.diagnostic().span_err(sp, msg)
}
}
pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) {
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
match split_msg_into_multilines(msg) {
Some(msg) => self.diagnostic().span_err_with_code(sp, &msg, code),
None => self.diagnostic().span_err_with_code(sp, msg, code)
Expand Down Expand Up @@ -186,32 +186,32 @@ impl Session {
}
result
}
pub fn span_warn(&self, sp: Span, msg: &str) {
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_warn(sp, msg)
}
pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) {
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
self.diagnostic().span_warn_with_code(sp, msg, code)
}
pub fn warn(&self, msg: &str) {
self.diagnostic().warn(msg)
}
pub fn opt_span_warn(&self, opt_sp: Option<Span>, msg: &str) {
pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) {
match opt_sp {
Some(sp) => self.span_warn(sp, msg),
None => self.warn(msg),
}
}
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
pub fn opt_span_bug<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) -> ! {
match opt_sp {
Some(sp) => self.span_bug(sp, msg),
None => self.bug(msg),
}
}
/// Delay a span_bug() call until abort_if_errors()
pub fn delay_span_bug(&self, sp: Span, msg: &str) {
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().delay_span_bug(sp, msg)
}
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.diagnostic().span_bug(sp, msg)
}
pub fn bug(&self, msg: &str) -> ! {
Expand All @@ -220,10 +220,10 @@ impl Session {
pub fn note_without_error(&self, msg: &str) {
self.diagnostic().note_without_error(msg)
}
pub fn span_note_without_error(&self, sp: Span, msg: &str) {
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_note_without_error(sp, msg)
}
pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.diagnostic().span_unimpl(sp, msg)
}
pub fn unimpl(&self, msg: &str) -> ! {
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Session {
}
// This exists to help with refactoring to eliminate impossible
// cases later on
pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {
pub fn impossible_case<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.span_bug(sp, &format!("impossible case reached: {}", msg));
}
pub fn verbose(&self) -> bool { self.opts.debugging_opts.verbose }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
&format!("to force the closure to take ownership of {} \
(and any other referenced variables), \
use the `move` keyword, as shown:",
cmt_path_or_string),
cmt_path_or_string),
suggestion)
.emit();
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rustc::front::map as hir_map;
use rustc::session::{self, config};
use std::rc::Rc;
use syntax::{abi, ast};
use syntax::codemap::{Span, CodeMap, DUMMY_SP};
use syntax::codemap::{MultiSpan, CodeMap, DUMMY_SP};
use syntax::errors;
use syntax::errors::emitter::Emitter;
use syntax::errors::{Level, RenderSpan};
Expand Down Expand Up @@ -78,14 +78,14 @@ fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {

impl Emitter for ExpectErrorEmitter {
fn emit(&mut self,
_sp: Option<Span>,
_sp: Option<&MultiSpan>,
msg: &str,
_: Option<&str>,
lvl: Level) {
remove_message(self, msg, lvl);
}

fn custom_emit(&mut self, _sp: RenderSpan, msg: &str, lvl: Level) {
fn custom_emit(&mut self, _sp: &RenderSpan, msg: &str, lvl: Level) {
remove_message(self, msg, lvl);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl SharedEmitter {
}

impl Emitter for SharedEmitter {
fn emit(&mut self, sp: Option<codemap::Span>,
fn emit(&mut self, sp: Option<&codemap::MultiSpan>,
msg: &str, code: Option<&str>, lvl: Level) {
assert!(sp.is_none(), "SharedEmitter doesn't support spans");

Expand All @@ -120,7 +120,7 @@ impl Emitter for SharedEmitter {
});
}

fn custom_emit(&mut self, _sp: errors::RenderSpan, _msg: &str, _lvl: Level) {
fn custom_emit(&mut self, _sp: &errors::RenderSpan, _msg: &str, _lvl: Level) {
panic!("SharedEmitter doesn't support custom_emit");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ fn report_cast_to_unsized_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
match fcx.tcx().sess.codemap().span_to_snippet(t_span) {
Ok(s) => {
err.span_suggestion(t_span,
"try casting to a `Box` instead:",
format!("Box<{}>", s));
"try casting to a `Box` instead:",
format!("Box<{}>", s));
},
Err(_) =>
span_help!(err, t_span, "did you mean `Box<{}>`?", tstr),
Expand Down
Loading

0 comments on commit 727f959

Please sign in to comment.