Skip to content

Commit e48300e

Browse files
authored
Fix rendering errors on very long lines (#2440)
Don't render the line, just show the filename/line number. Closes #2439
1 parent 6afe0a3 commit e48300e

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

crates/wit-parser/src/ast.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,17 +1875,24 @@ impl SourceMap {
18751875
let end = end.map(|end| src.to_relative_offset(end));
18761876
let (line, col) = src.linecol(start);
18771877
let snippet = src.contents.lines().nth(line).unwrap_or("");
1878+
let line = line + 1;
1879+
let col = col + 1;
1880+
1881+
// If the snippet is too large then don't overload output on a terminal
1882+
// for example and instead just print the error. This also sidesteps
1883+
// Rust's restriction that `>0$` below has to be less than `u16::MAX`.
1884+
if snippet.len() > 500 {
1885+
return format!("{}:{line}:{col}: {err}", src.path);
1886+
}
18781887
let mut msg = format!(
18791888
"\
18801889
{err}
18811890
--> {file}:{line}:{col}
18821891
|
18831892
{line:4} | {snippet}
18841893
| {marker:>0$}",
1885-
col + 1,
1894+
col,
18861895
file = src.path,
1887-
line = line + 1,
1888-
col = col + 1,
18891896
marker = "^",
18901897
);
18911898
if let Some(end) = end {

crates/wit-parser/tests/ui/parse-fail/very-large-column.wit

Lines changed: 6 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/ui/parse-fail/very-large-column.wit:1:65543: expected '{', found an identifier

0 commit comments

Comments
 (0)