Skip to content

Commit 9ff5ba0

Browse files
committed
Fix bad line printing for parse errors
The code that extracted lines from partially-parsed files was broken. Closes #1848
1 parent 6627890 commit 9ff5ba0

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/comp/driver/diagnostic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
204204
// Print the offending lines
205205
for line: uint in display_lines {
206206
io::stderr().write_str(#fmt["%s:%u ", fm.name, line + 1u]);
207-
let s = codemap::get_line(fm, line as int);
208-
if !str::ends_with(s, "\n") { s += "\n"; }
207+
let s = codemap::get_line(fm, line as int) + "\n";
209208
io::stderr().write_str(s);
210209
}
211210
if elided {

src/comp/syntax/codemap.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,11 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
157157

158158
fn get_line(fm: filemap, line: int) -> str unsafe {
159159
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
160-
let end: uint;
161-
if line as uint < vec::len(fm.lines) - 1u {
162-
end = fm.lines[line + 1].byte - fm.start_pos.byte;
163-
ret str::unsafe::slice_bytes(*fm.src, begin, end);
164-
} else {
165-
// If we're not done parsing the file, we're at the limit of what's
166-
// parsed. If we just slice the rest of the string, we'll print out
167-
// the remainder of the file, which is undesirable.
168-
ret str::splitn_char(*fm.src, '\n', 1u)[0];
169-
}
160+
let end = alt str::byte_index(*fm.src, '\n' as u8, begin) {
161+
some(e) { e }
162+
none { str::len(*fm.src) }
163+
};
164+
str::unsafe::slice_bytes(*fm.src, begin, end)
170165
}
171166

172167
fn lookup_byte_offset(cm: codemap::codemap, chpos: uint)

src/libcore/str.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export
6868

6969
// Searching
7070
index,
71+
byte_index,
7172
rindex,
7273
find,
7374
find_bytes,
@@ -852,6 +853,19 @@ fn index(ss: str, cc: char) -> option<uint> {
852853
ret option::none;
853854
}
854855

856+
// Function: byte_index
857+
//
858+
// Returns the index of the first matching byte
859+
// (as option some/none)
860+
fn byte_index(s: str, b: u8, start: uint) -> option<uint> {
861+
let i = start, l = len_bytes(s);
862+
while i < l {
863+
if s[i] == b { ret some(i); }
864+
i += 1u;
865+
}
866+
ret none;
867+
}
868+
855869
// Function: rindex
856870
//
857871
// Returns the index of the first matching char

0 commit comments

Comments
 (0)