Skip to content

Commit 1c41dd6

Browse files
committed
diagnostics: fix crash on completely empty included file
1 parent 1b73c7d commit 1c41dd6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

compiler/rustc_errors/src/emitter.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,9 @@ impl HumanEmitter {
15131513
for line_idx in 0..annotated_file.lines.len() {
15141514
let file = annotated_file.file.clone();
15151515
let line = &annotated_file.lines[line_idx];
1516-
if let Some(source_string) = file.get_line(line.line_index - 1) {
1516+
if let Some(source_string) =
1517+
line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
1518+
{
15171519
let leading_whitespace = source_string
15181520
.chars()
15191521
.take_while(|c| c.is_whitespace())
@@ -1553,7 +1555,10 @@ impl HumanEmitter {
15531555
for line in &annotated_file.lines {
15541556
max_line_len = max(
15551557
max_line_len,
1556-
annotated_file.file.get_line(line.line_index - 1).map_or(0, |s| s.len()),
1558+
line.line_index
1559+
.checked_sub(1)
1560+
.and_then(|l| annotated_file.file.get_line(l))
1561+
.map_or(0, |s| s.len()),
15571562
);
15581563
for ann in &line.annotations {
15591564
span_right_margin = max(span_right_margin, ann.start_col.display);

tests/ui/include-macros/mismatched-types.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0308]: mismatched types
2-
--> $DIR/mismatched-types.rs:2:20
2+
--> $DIR/file.txt:0:1
3+
|
4+
|
5+
::: $DIR/mismatched-types.rs:2:12
36
|
47
LL | let b: &[u8] = include_str!("file.txt");
5-
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `&str`
8+
| ----- ------------------------ in this macro invocation
69
| |
710
| expected due to this
811
|

0 commit comments

Comments
 (0)