Fix ICE when parsing frontmatter without newline

This commit is contained in:
yukang 2026-01-31 02:05:07 +00:00
parent 44e34e1ac6
commit dc48704f98
2 changed files with 20 additions and 1 deletions

View file

@ -623,7 +623,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
self.dcx().emit_err(errors::FrontmatterInvalidInfostring { span });
}
let last_line_start = real_s.rfind('\n').map_or(0, |i| i + 1);
let last_line_start = real_s.rfind('\n').map_or(line_end, |i| i + 1);
let content = &real_s[line_end..last_line_start];
if let Some(cr_offset) = content.find('\r') {

View file

@ -0,0 +1,19 @@
// Regression test for issue #151882
// See https://github.com/rust-lang/rust/issues/151882
//@ only-nightly
//@ needs-target-std
use run_make_support::{rfs, rustc};
fn main() {
rfs::write("test.rs", b"----");
// Ensure rustc does not ICE when parsing a file with frontmatter syntax
// that has no trailing newline
rustc()
.input("test.rs")
.run_fail()
.assert_stderr_contains("invalid infostring for frontmatter")
.assert_stderr_not_contains("unexpectedly panicked");
}