diff options
| author | Alex <huangalex409@gmail.com> | 2025-09-22 15:10:41 -0400 |
|---|---|---|
| committer | Alex <huangalex409@gmail.com> | 2025-09-22 15:10:41 -0400 |
| commit | 2d18c886f5754258109fad2e1e0c3bcca0cc9fa5 (patch) | |
| tree | 994ccb0713fba755aa0b1db991889d5b4c6a37fc | |
| parent | 29005cb128e6d447e6bd9c110c9a684665f95985 (diff) | |
| download | rust-2d18c886f5754258109fad2e1e0c3bcca0cc9fa5.tar.gz rust-2d18c886f5754258109fad2e1e0c3bcca0cc9fa5.zip | |
Fix a crash/mislex when more than one frontmatter closing possibility is considered
| -rw-r--r-- | compiler/rustc_lexer/src/lib.rs | 4 | ||||
| -rw-r--r-- | tests/ui/frontmatter/unclosed-6.rs | 12 | ||||
| -rw-r--r-- | tests/ui/frontmatter/unclosed-6.stderr | 19 |
3 files changed, 34 insertions, 1 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index c29ab569f47..f6790f7ed1e 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -599,14 +599,16 @@ impl Cursor<'_> { if potential_closing.is_none() { // a less fortunate recovery if all else fails which finds any dashes preceded by whitespace // on a standalone line. Might be wrong. + let mut base_index = 0; while let Some(closing) = rest.find("---") { let preceding_chars_start = rest[..closing].rfind("\n").map_or(0, |i| i + 1); if rest[preceding_chars_start..closing].chars().all(is_horizontal_whitespace) { // candidate found - potential_closing = Some(closing); + potential_closing = Some(closing + base_index); break; } else { rest = &rest[closing + 3..]; + base_index += closing + 3; } } } diff --git a/tests/ui/frontmatter/unclosed-6.rs b/tests/ui/frontmatter/unclosed-6.rs new file mode 100644 index 00000000000..ea8d4702f63 --- /dev/null +++ b/tests/ui/frontmatter/unclosed-6.rs @@ -0,0 +1,12 @@ +--- +//~^ ERROR unclosed frontmatter +🦀--- + --- + +// This test checks the location of the --- recovered by the parser is not +// incorrectly tracked during the less fortunate recovery case and multiple +// candidates are found, as seen with #146847 + +#![feature(frontmatter)] + +fn main() {} diff --git a/tests/ui/frontmatter/unclosed-6.stderr b/tests/ui/frontmatter/unclosed-6.stderr new file mode 100644 index 00000000000..01a13e87268 --- /dev/null +++ b/tests/ui/frontmatter/unclosed-6.stderr @@ -0,0 +1,19 @@ +error: unclosed frontmatter + --> $DIR/unclosed-6.rs:1:1 + | +LL | / --- +LL | | +LL | | 🦀--- +LL | | --- +... | +LL | | + | |_^ + | +note: frontmatter opening here was not closed + --> $DIR/unclosed-6.rs:1:1 + | +LL | --- + | ^^^ + +error: aborting due to 1 previous error + |
