diff --git a/src/librustc/metadata/creader.rs b/src/librustc/metadata/creader.rs index 3ab5b0a3bc46..24befbd44a10 100644 --- a/src/librustc/metadata/creader.rs +++ b/src/librustc/metadata/creader.rs @@ -26,7 +26,7 @@ use syntax::ast; use syntax::abi; use syntax::attr; use syntax::attr::AttrMetaMethods; -use syntax::codemap::{DUMMY_SP, Span, mk_sp}; +use syntax::codemap::{COMMAND_LINE_SP, Span, mk_sp}; use syntax::parse; use syntax::parse::token::InternedString; use syntax::parse::token; @@ -456,7 +456,7 @@ impl<'a> CrateReader<'a> { ident: s.to_string(), id: ast::DUMMY_NODE_ID, should_link: true, - }, DUMMY_SP) + }, COMMAND_LINE_SP) } }; let target_triple = &self.sess.opts.target_triple[]; diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index d1768867f0da..9a422e17bb4d 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -105,6 +105,11 @@ pub struct Span { pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), expn_id: NO_EXPANSION }; +// Generic span to be used for code originating from the command line +pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0), + hi: BytePos(0), + expn_id: COMMAND_LINE_EXPN }; + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub struct Spanned { pub node: T, @@ -235,6 +240,8 @@ pub struct ExpnInfo { pub struct ExpnId(u32); pub const NO_EXPANSION: ExpnId = ExpnId(-1); +// For code appearing from the command line +pub const COMMAND_LINE_EXPN: ExpnId = ExpnId(-2); impl ExpnId { pub fn from_llvm_cookie(cookie: c_uint) -> ExpnId { diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 7e57709f33d0..64fdb61f2ece 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -13,7 +13,7 @@ pub use self::RenderSpan::*; pub use self::ColorConfig::*; use self::Destination::*; -use codemap::{Pos, Span}; +use codemap::{COMMAND_LINE_SP, Pos, Span}; use codemap; use diagnostics; @@ -368,6 +368,9 @@ impl Emitter for EmitterWriter { cmsp: Option<(&codemap::CodeMap, Span)>, msg: &str, code: Option<&str>, lvl: Level) { let error = match cmsp { + Some((cm, COMMAND_LINE_SP)) => emit(self, cm, + FileLine(COMMAND_LINE_SP), + msg, code, lvl, false), Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl, false), None => print_diagnostic(self, "", lvl, msg, code), }; @@ -390,8 +393,11 @@ impl Emitter for EmitterWriter { fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan, msg: &str, code: Option<&str>, lvl: Level, custom: bool) -> io::IoResult<()> { let sp = rsp.span(); - let ss = cm.span_to_string(sp); - let lines = cm.span_to_lines(sp); + let ss = if sp == COMMAND_LINE_SP { + "".to_string() + } else { + cm.span_to_string(sp) + }; if custom { // we want to tell compiletest/runtest to look at the last line of the // span (since `custom_highlight_lines` displays an arrow to the end of @@ -400,15 +406,17 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan, let ses = cm.span_to_string(span_end); try!(print_diagnostic(dst, &ses[], lvl, msg, code)); if rsp.is_full_span() { - try!(custom_highlight_lines(dst, cm, sp, lvl, lines)); + try!(custom_highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp))); } } else { try!(print_diagnostic(dst, &ss[], lvl, msg, code)); if rsp.is_full_span() { - try!(highlight_lines(dst, cm, sp, lvl, lines)); + try!(highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp))); } } - try!(print_macro_backtrace(dst, cm, sp)); + if sp != COMMAND_LINE_SP { + try!(print_macro_backtrace(dst, cm, sp)); + } match code { Some(code) => match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {