Skip to content

Commit 0bd022c

Browse files
committed
libsyntax: add COMMAND_LINE_SP and use it for spans generated from the command line
1 parent c41cafb commit 0bd022c

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/librustc/metadata/creader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use syntax::ast;
2626
use syntax::abi;
2727
use syntax::attr;
2828
use syntax::attr::AttrMetaMethods;
29-
use syntax::codemap::{DUMMY_SP, Span, mk_sp};
29+
use syntax::codemap::{COMMAND_LINE_SP, Span, mk_sp};
3030
use syntax::parse;
3131
use syntax::parse::token::InternedString;
3232
use syntax::parse::token;
@@ -456,7 +456,7 @@ impl<'a> CrateReader<'a> {
456456
ident: s.to_string(),
457457
id: ast::DUMMY_NODE_ID,
458458
should_link: true,
459-
}, DUMMY_SP)
459+
}, COMMAND_LINE_SP)
460460
}
461461
};
462462
let target_triple = &self.sess.opts.target_triple[];

src/libsyntax/codemap.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ pub struct Span {
105105

106106
pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), expn_id: NO_EXPANSION };
107107

108+
// Generic span to be used for code originating from the command line
109+
pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
110+
hi: BytePos(0),
111+
expn_id: COMMAND_LINE_EXPN };
112+
108113
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)]
109114
pub struct Spanned<T> {
110115
pub node: T,
@@ -235,6 +240,8 @@ pub struct ExpnInfo {
235240
pub struct ExpnId(u32);
236241

237242
pub const NO_EXPANSION: ExpnId = ExpnId(-1);
243+
// For code appearing from the command line
244+
pub const COMMAND_LINE_EXPN: ExpnId = ExpnId(-2);
238245

239246
impl ExpnId {
240247
pub fn from_llvm_cookie(cookie: c_uint) -> ExpnId {

src/libsyntax/diagnostic.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::RenderSpan::*;
1313
pub use self::ColorConfig::*;
1414
use self::Destination::*;
1515

16-
use codemap::{Pos, Span};
16+
use codemap::{COMMAND_LINE_SP, Pos, Span};
1717
use codemap;
1818
use diagnostics;
1919

@@ -368,6 +368,9 @@ impl Emitter for EmitterWriter {
368368
cmsp: Option<(&codemap::CodeMap, Span)>,
369369
msg: &str, code: Option<&str>, lvl: Level) {
370370
let error = match cmsp {
371+
Some((cm, COMMAND_LINE_SP)) => emit(self, cm,
372+
FileLine(COMMAND_LINE_SP),
373+
msg, code, lvl, false),
371374
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl, false),
372375
None => print_diagnostic(self, "", lvl, msg, code),
373376
};
@@ -390,8 +393,11 @@ impl Emitter for EmitterWriter {
390393
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
391394
msg: &str, code: Option<&str>, lvl: Level, custom: bool) -> io::IoResult<()> {
392395
let sp = rsp.span();
393-
let ss = cm.span_to_string(sp);
394-
let lines = cm.span_to_lines(sp);
396+
let ss = if sp == COMMAND_LINE_SP {
397+
"<command line option>".to_string()
398+
} else {
399+
cm.span_to_string(sp)
400+
};
395401
if custom {
396402
// we want to tell compiletest/runtest to look at the last line of the
397403
// 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,
400406
let ses = cm.span_to_string(span_end);
401407
try!(print_diagnostic(dst, &ses[], lvl, msg, code));
402408
if rsp.is_full_span() {
403-
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
409+
try!(custom_highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
404410
}
405411
} else {
406412
try!(print_diagnostic(dst, &ss[], lvl, msg, code));
407413
if rsp.is_full_span() {
408-
try!(highlight_lines(dst, cm, sp, lvl, lines));
414+
try!(highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
409415
}
410416
}
411-
try!(print_macro_backtrace(dst, cm, sp));
417+
if sp != COMMAND_LINE_SP {
418+
try!(print_macro_backtrace(dst, cm, sp));
419+
}
412420
match code {
413421
Some(code) =>
414422
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {

0 commit comments

Comments
 (0)