8000 fix: Ensure empty sources have one "line" · rust-lang/annotate-snippets-rs@2324214 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 2324214

Browse files
committed
fix: Ensure empty sources have one "line"
1 parent f410552 commit 2324214

File tree

3 files changed

+16
-1
lines changed

3 files changed

+1 8000 6
-1
lines changed

src/renderer/source_map.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ pub(crate) struct SourceMap<'a> {
1111

1212
impl<'a> SourceMap<'a> {
1313
pub(crate) fn new(source: &'a str, line_start: usize) -> Self {
14+
// Empty sources do have a "line", but it is empty, so we need to add
15+
// a line with an empty string to the source map.
16+
if source.is_empty() {
17+
return Self {
18+
lines: vec![LineInfo {
19+
line: "",
20+
line_index: line_start,
21+
start_byte: 0,
22+
end_byte: 0,
23+
end_line_size: 0,
24+
}],
25+
source,
26+
};
27+
}
28+
1429
let mut current_index = 0;
1530

1631
let mut mapping = vec![];

tests/formatter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ fn test_only_source() {
303303
error:
304304
--> file.rs
305305
|
306+
1 |
306307
"#]];
307308
let renderer = Renderer::plain();
308309
assert_data_eq!(renderer.render(input), expected);

tests/rustc_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2682,7 +2682,6 @@ LL | | */
26822682
}
26832683

26842684
#[test]
2685-
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
26862685
fn mismatched_types1() {
26872686
// tests/ui/include-macros/mismatched-types.rs
26882687

0 commit comments

Comments
 (0)
0