Skip to content

Commit 3ce43f3

Browse files
jdmbrson
authored andcommitted
Fix pre-existing problem with filemap line positions always starting at 0. Fix error line output to only retrieve up to the nearest newline.
1 parent a5ac8f1 commit 3ce43f3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/comp/syntax/codemap.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn new_codemap() -> codemap {
2626
}
2727

2828
fn new_filemap(filename filename, uint start_pos) -> filemap {
29-
ret @rec(name=filename, start_pos=start_pos, mutable lines=~[0u]);
29+
ret @rec(name=filename, start_pos=start_pos, mutable lines=[start_pos]);
3030
}
3131

3232
fn next_line(filemap file, uint pos) { file.lines += ~[pos]; }
@@ -170,10 +170,18 @@ fn span_to_lines(span sp, codemap::codemap cm) -> @file_lines {
170170
fn get_line(filemap fm, int line, &str file) -> str {
171171
let uint begin = fm.lines.(line) - fm.start_pos;
172172
let uint end;
173-
if ((line as uint) + 1u >= ivec::len(fm.lines)) {
174-
end = str::byte_len(file);
175-
} else {
173+
if (line as uint < ivec::len(fm.lines) - 1u) {
176174
end = fm.lines.(line + 1) - fm.start_pos;
175+
} else {
176+
// If we're not done parsing the file, we're at the limit of what's
177+
// parsed. If we just slice the rest of the string, we'll print out
178+
// the remainder of the file, which is undesirable.
179+
end = str::byte_len(file);
180+
auto rest = str::slice(file, begin, end);
181+
auto newline = str::index(rest, '\n' as u8);
182+
if (newline != -1) {
183+
end = begin + (newline as uint);
184+
}
177185
}
178186
ret str::slice(file, begin, end);
179187
}

0 commit comments

Comments
 (0)