From dc48704f98f77822e8f6aea7fdac255c5aea101f Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 31 Jan 2026 02:05:07 +0000 Subject: [PATCH] Fix ICE when parsing frontmatter without newline --- compiler/rustc_parse/src/lexer/mod.rs | 2 +- .../frontmatter-no-trailing-newline/rmake.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/frontmatter-no-trailing-newline/rmake.rs diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index f9bf50de091a..76f610df1eb0 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -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') { diff --git a/tests/run-make/frontmatter-no-trailing-newline/rmake.rs b/tests/run-make/frontmatter-no-trailing-newline/rmake.rs new file mode 100644 index 000000000000..204062201ad3 --- /dev/null +++ b/tests/run-make/frontmatter-no-trailing-newline/rmake.rs @@ -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"); +}