Skip to content

Commit 11dc974

Browse files
committed
refactor to use new snippet code and model
Major changes: - Remove old snippet rendering code and use the new stuff. - Introduce `span_label` method to add a label - Remove EndSpan mode and replace with a fn to get the last character of a span. - Stop using `Option<MultiSpan>` and just use an empty `MultiSpan` - and probably a bunch of other stuff :)
1 parent 5b150cf commit 11dc974

File tree

7 files changed

+357
-744
lines changed

7 files changed

+357
-744
lines changed

src/librustc/session/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
567567
}
568568
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
569569
};
570-
emitter.emit(None, msg, None, errors::Level::Fatal);
570+
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Fatal);
571571
panic!(errors::FatalError);
572572
}
573573

@@ -578,7 +578,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
578578
}
579579
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
580580
};
581-
emitter.emit(None, msg, None, errors::Level::Warning);
581+
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Warning);
582582
}
583583

584584
// Err(0) means compilation was stopped, but no errors were found.

src/librustc_driver/lib.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ use std::thread;
9191

9292
use rustc::session::early_error;
9393

94-
use syntax::{ast, errors, diagnostics};
95-
use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
94+
use syntax::{ast, errors, diagnostic};
95+
use syntax::codemap::MultiSpan;
96+
use syntax::parse::{self, PResult};
9697
use syntax::errors::emitter::Emitter;
9798
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
9899
use syntax::parse::{self, PResult, token};
@@ -136,7 +137,8 @@ pub fn run(args: Vec<String>) -> isize {
136137
None => {
137138
let mut emitter =
138139
errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
139-
emitter.emit(None, &abort_msg(err_count), None, errors::Level::Fatal);
140+
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
141+
errors::Level::Fatal);
140142
exit_on_err();
141143
}
142144
}
@@ -379,7 +381,7 @@ fn check_cfg(sopts: &config::Options,
379381
match item.node {
380382
ast::MetaItemKind::List(ref pred, _) => {
381383
saw_invalid_predicate = true;
382-
emitter.emit(None,
384+
emitter.emit(&MultiSpan::new(),
383385
&format!("invalid predicate in --cfg command line argument: `{}`",
384386
pred),
385387
None,
@@ -1028,19 +1030,19 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
10281030
// a .span_bug or .bug call has already printed what
10291031
// it wants to print.
10301032
if !value.is::<errors::ExplicitBug>() {
1031-
emitter.emit(None, "unexpected panic", None, errors::Level::Bug);
1033+
emitter.emit(&MultiSpan::new(), "unexpected panic", None, errors::Level::Bug);
10321034
}
10331035

10341036
let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
10351037
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
10361038
for note in &xs {
1037-
emitter.emit(None, &note[..], None, errors::Level::Note)
1039+
emitter.emit(&MultiSpan::new(), &note[..], None, errors::Level::Note)
10381040
}
10391041
if match env::var_os("RUST_BACKTRACE") {
10401042
Some(val) => &val != "0",
10411043
None => false,
10421044
} {
1043-
emitter.emit(None,
1045+
emitter.emit(&MultiSpan::new(),
10441046
"run with `RUST_BACKTRACE=1` for a backtrace",
10451047
None,
10461048
errors::Level::Note);

src/librustc_driver/test.rs

-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ impl Emitter for ExpectErrorEmitter {
8686
lvl: Level) {
8787
remove_message(self, msg, lvl);
8888
}
89-
90-
fn custom_emit(&mut self, _sp: &RenderSpan, msg: &str, lvl: Level) {
91-
remove_message(self, msg, lvl);
92-
}
9389
}
9490

9591
fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {

src/librustc_trans/back/write.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use llvm::SMDiagnosticRef;
1919
use {CrateTranslation, ModuleTranslation};
2020
use util::common::time;
2121
use util::common::path2cstr;
22-
use syntax::codemap;
22+
use syntax::codemap::{self, MultiSpan};
2323
use syntax::errors::{self, Handler, Level};
2424
use syntax::errors::emitter::Emitter;
2525

@@ -84,13 +84,13 @@ impl SharedEmitter {
8484
for diag in &*buffer {
8585
match diag.code {
8686
Some(ref code) => {
87-
handler.emit_with_code(None,
87+
handler.emit_with_code(&MultiSpan::new(),
8888
&diag.msg,
8989
&code[..],
9090
diag.lvl);
9191
},
9292
None => {
93-
handler.emit(None,
93+
handler.emit(&MultiSpan::new(),
9494
&diag.msg,
9595
diag.lvl);
9696
},
@@ -101,9 +101,12 @@ impl SharedEmitter {
101101
}
102102

103103
impl Emitter for SharedEmitter {
104-
fn emit(&mut self, sp: Option<&codemap::MultiSpan>,
105-
msg: &str, code: Option<&str>, lvl: Level) {
106-
assert!(sp.is_none(), "SharedEmitter doesn't support spans");
104+
fn emit(&mut self,
105+
sp: &codemap::MultiSpan,
106+
msg: &str,
107+
code: Option<&str>,
108+
lvl: Level) {
109+
assert!(sp.primary_span().is_none(), "SharedEmitter doesn't support spans");
107110

108111
self.buffer.lock().unwrap().push(Diagnostic {
109112
msg: msg.to_string(),
@@ -112,8 +115,8 @@ impl Emitter for SharedEmitter {
112115
});
113116
}
114117

115-
fn custom_emit(&mut self, _sp: &errors::RenderSpan, _msg: &str, _lvl: Level) {
116-
bug!("SharedEmitter doesn't support custom_emit");
118+
fn emit_struct(&mut self, _db: &errors::DiagnosticBuilder) {
119+
bug!("SharedEmitter doesn't support emit_struct");
117120
}
118121
}
119122

src/libsyntax/codemap.rs

+13-74
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
163163
expn_id: COMMAND_LINE_EXPN };
164164

165165
impl Span {
166+
/// Returns a new span representing just the end-point of this span
167+
pub fn end_point(self) -> Span {
168+
let lo = cmp::max(self.hi.0 - 1, self.lo.0);
169+
Span { lo: BytePos(lo), hi: self.hi, expn_id: self.expn_id}
170+
}
171+
166172
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
167173
pub fn substitute_dummy(self, other: Span) -> Span {
168174
if self.source_equal(&DUMMY_SP) { other } else { self }
@@ -794,7 +800,7 @@ impl CodeMap {
794800
/// Creates a new filemap and sets its line information.
795801
pub fn new_filemap_and_lines(&self, filename: &str, src: &str) -> Rc<FileMap> {
796802
let fm = self.new_filemap(filename.to_string(), src.to_owned());
797-
let mut byte_pos: u32 = 0;
803+
let mut byte_pos: u32 = fm.start_pos.0;
798804
for line in src.lines() {
799805
// register the start of this line
800806
fm.next_line(BytePos(byte_pos));
@@ -1126,7 +1132,9 @@ impl CodeMap {
11261132
// numbers in Loc are 1-based, so we subtract 1 to get 0-based
11271133
// lines.
11281134
for line_index in lo.line-1 .. hi.line-1 {
1129-
let line_len = lo.file.get_line(line_index).map(|s| s.len()).unwrap_or(0);
1135+
let line_len = lo.file.get_line(line_index)
1136+
.map(|s| s.chars().count())
1137+
.unwrap_or(0);
11301138
lines.push(LineInfo { line_index: line_index,
11311139
start_col: start_col,
11321140
end_col: CharPos::from_usize(line_len) });
@@ -1584,13 +1592,13 @@ mod tests {
15841592
assert_eq!(file_lines.lines[0].line_index, 1);
15851593
}
15861594

1587-
/// Given a string like " ^~~~~~~~~~~~ ", produces a span
1595+
/// Given a string like " ~~~~~~~~~~~~ ", produces a span
15881596
/// coverting that range. The idea is that the string has the same
15891597
/// length as the input, and we uncover the byte positions. Note
15901598
/// that this can span lines and so on.
15911599
fn span_from_selection(input: &str, selection: &str) -> Span {
15921600
assert_eq!(input.len(), selection.len());
1593-
let left_index = selection.find('^').unwrap() as u32;
1601+
let left_index = selection.find('~').unwrap() as u32;
15941602
let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index);
15951603
Span { lo: BytePos(left_index), hi: BytePos(right_index + 1), expn_id: NO_EXPANSION }
15961604
}
@@ -1601,7 +1609,7 @@ mod tests {
16011609
fn span_to_snippet_and_lines_spanning_multiple_lines() {
16021610
let cm = CodeMap::new();
16031611
let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
1604-
let selection = " \n ^~\n~~~\n~~~~~ \n \n";
1612+
let selection = " \n ~~\n~~~\n~~~~~ \n \n";
16051613
cm.new_filemap_and_lines("blork.rs", inputtext);
16061614
let span = span_from_selection(inputtext, selection);
16071615

@@ -1751,73 +1759,4 @@ r"blork2.rs:2:1: 2:12
17511759
";
17521760
assert_eq!(sstr, res_str);
17531761
}
1754-
1755-
#[test]
1756-
fn t13() {
1757-
// Test that collecting multiple spans into line-groups works correctly
1758-
let cm = CodeMap::new();
1759-
let inp = "_aaaaa__bbb\nvv\nw\nx\ny\nz\ncccccc__ddddee__";
1760-
let sp1 = " ^~~~~ \n \n \n \n \n \n ";
1761-
let sp2 = " \n \n \n \n \n^\n ";
1762-
let sp3 = " ^~~\n~~\n \n \n \n \n ";
1763-
let sp4 = " \n \n \n \n \n \n^~~~~~ ";
1764-
let sp5 = " \n \n \n \n \n \n ^~~~ ";
1765-
let sp6 = " \n \n \n \n \n \n ^~~~ ";
1766-
let sp_trim = " \n \n \n \n \n \n ^~ ";
1767-
let sp_merge = " \n \n \n \n \n \n ^~~~~~ ";
1768-
let sp7 = " \n ^\n \n \n \n \n ";
1769-
let sp8 = " \n \n^\n \n \n \n ";
1770-
let sp9 = " \n \n \n^\n \n \n ";
1771-
let sp10 = " \n \n \n \n^\n \n ";
1772-
1773-
let span = |sp, expected| {
1774-
let sp = span_from_selection(inp, sp);
1775-
assert_eq!(&cm.span_to_snippet(sp).unwrap(), expected);
1776-
sp
1777-
};
1778-
1779-
cm.new_filemap_and_lines("blork.rs", inp);
1780-
let sp1 = span(sp1, "aaaaa");
1781-
let sp2 = span(sp2, "z");
1782-
let sp3 = span(sp3, "bbb\nvv");
1783-
let sp4 = span(sp4, "cccccc");
1784-
let sp5 = span(sp5, "dddd");
1785-
let sp6 = span(sp6, "ddee");
1786-
let sp7 = span(sp7, "v");
1787-
let sp8 = span(sp8, "w");
1788-
let sp9 = span(sp9, "x");
1789-
let sp10 = span(sp10, "y");
1790-
let sp_trim = span(sp_trim, "ee");
1791-
let sp_merge = span(sp_merge, "ddddee");
1792-
1793-
let spans = vec![sp5, sp2, sp4, sp9, sp10, sp7, sp3, sp8, sp1, sp6];
1794-
1795-
macro_rules! check_next {
1796-
($groups: expr, $expected: expr) => ({
1797-
let actual = $groups.next().map(|g|&g.spans[..]);
1798-
let expected = $expected;
1799-
println!("actual:\n{:?}\n", actual);
1800-
println!("expected:\n{:?}\n", expected);
1801-
assert_eq!(actual, expected.as_ref().map(|x|&x[..]));
1802-
});
1803-
}
1804-
1805-
let _groups = cm.group_spans(spans.clone());
1806-
let it = &mut _groups.iter();
1807-
1808-
check_next!(it, Some([sp1, sp7, sp8, sp9, sp10, sp2]));
1809-
// New group because we're exceeding MAX_HIGHLIGHT_LINES
1810-
check_next!(it, Some([sp4, sp_merge]));
1811-
check_next!(it, Some([sp3]));
1812-
check_next!(it, None::<[Span; 0]>);
1813-
1814-
let _groups = cm.end_group_spans(spans);
1815-
let it = &mut _groups.iter();
1816-
1817-
check_next!(it, Some([sp1, sp7, sp8, sp9, sp10, sp2]));
1818-
// New group because we're exceeding MAX_HIGHLIGHT_LINES
1819-
check_next!(it, Some([sp4, sp5, sp_trim]));
1820-
check_next!(it, Some([sp3]));
1821-
check_next!(it, None::<[Span; 0]>);
1822-
}
18231762
}

0 commit comments

Comments
 (0)