Skip to content

Commit 0524161

Browse files
author
Jakub Bukaj
committed
Fix an ICE on diagnostics originating in external macros
1 parent 96c8f2b commit 0524161

File tree

4 files changed

+84
-33
lines changed

4 files changed

+84
-33
lines changed

src/libsyntax/codemap.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,17 @@ impl FileMap {
293293

294294
/// get a line from the list of pre-computed line-beginnings
295295
///
296-
pub fn get_line(&self, line: int) -> String {
296+
pub fn get_line(&self, line_number: uint) -> Option<String> {
297297
let lines = self.lines.borrow();
298-
let begin: BytePos = (*lines)[line as uint] - self.start_pos;
299-
let begin = begin.to_uint();
300-
let slice = self.src.as_slice().slice_from(begin);
301-
match slice.find('\n') {
302-
Some(e) => slice.slice_to(e).to_string(),
303-
None => slice.to_string()
304-
}
298+
lines.get(line_number).map(|&line| {
299+
let begin: BytePos = line - self.start_pos;
300+
let begin = begin.to_uint();
301+
let slice = self.src.as_slice().slice_from(begin);
302+
match slice.find('\n') {
303+
Some(e) => slice.slice_to(e),
304+
None => slice
305+
}.to_string()
306+
})
305307
}
306308

307309
pub fn record_multibyte_char(&self, pos: BytePos, bytes: uint) {
@@ -578,10 +580,10 @@ mod test {
578580
let fm = cm.new_filemap("blork.rs".to_string(),
579581
"first line.\nsecond line".to_string());
580582
fm.next_line(BytePos(0));
581-
assert_eq!(&fm.get_line(0),&"first line.".to_string());
583+
assert_eq!(fm.get_line(0), Some("first line.".to_string()));
582584
// TESTING BROKEN BEHAVIOR:
583585
fm.next_line(BytePos(10));
584-
assert_eq!(&fm.get_line(1), &".".to_string());
586+
assert_eq!(fm.get_line(1), Some(".".to_string()));
585587
}
586588

587589
#[test]

src/libsyntax/diagnostic.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,11 @@ fn highlight_lines(err: &mut EmitterWriter,
436436
elided = true;
437437
}
438438
// Print the offending lines
439-
for line in display_lines.iter() {
440-
try!(write!(&mut err.dst, "{}:{} {}\n", fm.name, *line + 1,
441-
fm.get_line(*line as int)));
439+
for &line_number in display_lines.iter() {
440+
if let Some(line) = fm.get_line(line_number) {
441+
try!(write!(&mut err.dst, "{}:{} {}\n", fm.name,
442+
line_number + 1, line));
443+
}
442444
}
443445
if elided {
444446
let last_line = display_lines[display_lines.len() - 1u];
@@ -465,24 +467,26 @@ fn highlight_lines(err: &mut EmitterWriter,
465467
for _ in range(0, skip) {
466468
s.push(' ');
467469
}
468-
let orig = fm.get_line(lines.lines[0] as int);
469-
for pos in range(0u, left-skip) {
470-
let cur_char = orig.as_bytes()[pos] as char;
471-
// Whenever a tab occurs on the previous line, we insert one on
472-
// the error-point-squiggly-line as well (instead of a space).
473-
// That way the squiggly line will usually appear in the correct
474-
// position.
475-
match cur_char {
476-
'\t' => s.push('\t'),
477-
_ => s.push(' '),
478-
};
470+
if let Some(orig) = fm.get_line(lines.lines[0]) {
471+
for pos in range(0u, left - skip) {
472+
let cur_char = orig.as_bytes()[pos] as char;
473+
// Whenever a tab occurs on the previous line, we insert one on
474+
// the error-point-squiggly-line as well (instead of a space).
475+
// That way the squiggly line will usually appear in the correct
476+
// position.
477+
match cur_char {
478+
'\t' => s.push('\t'),
479+
_ => s.push(' '),
480+
};
481+
}
479482
}
483+
480484
try!(write!(&mut err.dst, "{}", s));
481485
let mut s = String::from_str("^");
482486
let hi = cm.lookup_char_pos(sp.hi);
483487
if hi.col != lo.col {
484488
// the ^ already takes up one space
485-
let num_squigglies = hi.col.to_uint()-lo.col.to_uint()-1u;
489+
let num_squigglies = hi.col.to_uint() - lo.col.to_uint() - 1u;
486490
for _ in range(0, num_squigglies) {
487491
s.push('~');
488492
}
@@ -510,16 +514,22 @@ fn custom_highlight_lines(w: &mut EmitterWriter,
510514

511515
let lines = lines.lines.as_slice();
512516
if lines.len() > MAX_LINES {
513-
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
514-
lines[0] + 1, fm.get_line(lines[0] as int)));
517+
if let Some(line) = fm.get_line(lines[0]) {
518+
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
519+
lines[0] + 1, line));
520+
}
515521
try!(write!(&mut w.dst, "...\n"));
516-
let last_line = lines[lines.len()-1];
517-
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
518-
last_line + 1, fm.get_line(last_line as int)));
519-
} else {
520-
for line in lines.iter() {
522+
let last_line_number = lines[lines.len() - 1];
523+
if let Some(last_line) = fm.get_line(last_line_number) {
521524
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
522-
*line + 1, fm.get_line(*line as int)));
525+
last_line_number + 1, last_line));
526+
}
527+
} else {
528+
for &line_number in lines.iter() {
529+
if let Some(line) = fm.get_line(line_number) {
530+
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
531+
line_number + 1, line));
532+
}
523533
}
524534
}
525535
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: cannot apply unary operator `!` to type `BytePos`
12+
13+
// Very
14+
15+
// sensitive
16+
pub struct BytePos(pub u32);
17+
18+
// to particular
19+
20+
// line numberings / offsets
21+
22+
fn main() {
23+
let x = BytePos(1);
24+
25+
assert!(x, x);
26+
}

src/test/compile-fail/issue-14091.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: expected `bool`, found `_` (expected bool, found integral variable)
12+
13+
fn main(){assert!(1,1);}

0 commit comments

Comments
 (0)