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 1 commit
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
Prev Previous commit
fix: Ensure empty sources have one "line"
  • Loading branch information
Muscraft committed Jun 24, 2025
commit 23242144f51cc7902d6bcc5d69420600f9426459
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
1 change: 0 additions & 1 deletion tests/rustc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2682,7 +2682,6 @@ LL | | */
}

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

Expand Down