8000 fix: Ensure empty sources have one "line" by Muscraft · Pull Request #219 · rust-lang/annotate-snippets-rs · GitHub
[go: up one dir, main page]

Skip to content

fix: Ensure empty sources have one "line" #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/renderer/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ pub(crate) struct SourceMap<'a> {

impl<'a> SourceMap<'a> {
pub(crate) fn new(source: &'a str, line_start: usize) -> Self {
// Empty sources do have a "line", but it is empty, so we need to add
// a line with an empty string to the source map.
if source.is_empty() {
return Self {
lines: vec![LineInfo {
line: "",
line_index: line_start,
start_byte: 0,
end_byte: 0,
end_line_size: 0,
}],
source,
};
}

let mut current_index = 0;

let mut mapping = vec![];
Expand Down
1 change: 1 addition & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ fn test_only_source() {
error:
--> file.rs
|
1 |
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input), expected);
Expand Down
113 changes: 113 additions & 0 deletions tests/rustc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,3 +2680,116 @@ LL | | */
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}

#[test]
fn mismatched_types1() {
// tests/ui/include-macros/mismatched-types.rs

let file_txt_source = r#""#;

let rust_source = r#"fn main() {
let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
}"#;

let input = Level::ERROR.header("mismatched types").id("E0308").group(
Group::new()
.element(
Snippet::source(file_txt_source)
.fold(true)
.line_start(3)
.origin("$DIR/file.txt")
.annotation(
AnnotationKind::Primary
.span(0..0)
.label("expected `&[u8]`, found `&str`"),
),
)
.element(
Snippet::source(rust_source)
.origin("$DIR/mismatched-types.rs")
.fold(true)
.annotation(
AnnotationKind::Context
.span(23..28)
.label("expected due to this"),
)
.annotation(
AnnotationKind::Context
.span(31..55)
.label("in this macro invocation"),
),
)
.element(
Level::NOTE.title("expected reference `&[u8]`\n found reference `&'static str`"),
),
);

let expected = str![[r#"
error[E0308]: mismatched types
--> $DIR/file.txt:3:1
|
LL |
| ^ expected `&[u8]`, found `&str`
|
::: $DIR/mismatched-types.rs:2:12
|
LL | let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
| ----- ------------------------ in this macro invocation
| |
| expected due to this
|
= note: expected reference `&[u8]`
found reference `&'static str`
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}

#[test]
fn mismatched_types2() {
// tests/ui/include-macros/mismatched-types.rs

let source = r#"fn main() {
let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
}"#;

let input = Level::ERROR.header("mismatched types").id("E0308").group(
Group::new()
.element(
Snippet::source(source)
.origin("$DIR/mismatched-types.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(105..131)
.label("expected `&str`, found `&[u8; 0]`"),
)
.annotation(
AnnotationKind::Context
.span(98..102)
.label("expected due to this"),
),
)
.element(
Level::NOTE
.title("expected reference `&str`\n found reference `&'static [u8; 0]`"),
),
);

let expected = str![[r#"
error[E0308]: mismatched types
--> $DIR/mismatched-types.rs:3:19
|
LL | let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&str`, found `&[u8; 0]`
| |
| expected due to this
|
= note: expected reference `&str`
found reference `&'static [u8; 0]`
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}
0