Skip to content

Commit fd98ea8

Browse files
committed
Fix for issue 2174
The function that formats and prints the squigly line that hilights errors counted tabs as spaces, which resulted in incorrect error messages when tabs were used for indentation. This change compares the highlight line with the previous line and inserts a tab instead of a space whenever such a tab exists on the previous line. Note that error messages will still highlight incorrectly when the previous line include characters that require more than one utf8 code point, as mentioned in issue 3260.
1 parent 0c2b4ed commit fd98ea8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libsyntax/diagnostic.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,26 @@ fn highlight_lines(cm: @codemap::CodeMap,
263263
// indent past |name:## | and the 0-offset column location
264264
let mut left = str::len(fm.name) + digits + lo.col.to_uint() + 3u;
265265
let mut s = ~"";
266-
while left > 0u { str::push_char(&mut s, ' '); left -= 1u; }
267-
266+
// Skip is the number of characters we need to skip because they are
267+
// part of the 'filename:line ' part of the previous line.
268+
let skip = str::len(fm.name) + digits + 3u;
269+
for skip.times() {
270+
s += ~" ";
271+
}
272+
let orig = fm.get_line(lines.lines[0] as int);
273+
for uint::range(0u,left-skip) |pos| {
274+
let curChar = (orig[pos] as char);
275+
s += match curChar { // Whenever a tab occurs on the previous
276+
'\t' => "\t", // line, we insert one on the error-point-
277+
_ => " " // -squigly-line as well (instead of a
278+
}; // space). This way the squigly-line will
279+
} // usually appear in the correct position.
268280
s += ~"^";
269281
let hi = cm.lookup_char_pos(sp.hi);
270282
if hi.col != lo.col {
271283
// the ^ already takes up one space
272-
let mut width = hi.col.to_uint() - lo.col.to_uint() - 1u;
273-
while width > 0u { str::push_char(&mut s, '~'); width -= 1u; }
284+
let num_squiglies = hi.col.to_uint()-lo.col.to_uint()-1u;
285+
for num_squiglies.times() { s += ~"~"; }
274286
}
275287
io::stderr().write_str(s + ~"\n");
276288
}

0 commit comments

Comments
 (0)