Skip to content

Commit a21216f

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

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

compiler/rustc_errors/src/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ 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) = line.line_index.checked_sub(1).and_then(|l| file.get_line(l)) {
15171517
let leading_whitespace = source_string
15181518
.chars()
15191519
.take_while(|c| c.is_whitespace())

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)