about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthew Jasper <mjjasper1@gmail.com>2025-06-04 15:51:36 +0000
committerMatthew Jasper <mjjasper1@gmail.com>2025-06-04 15:51:36 +0000
commit55f59fb0e329d0815a06d51dd469d774eb455cba (patch)
tree982d60db0c0d914b313712c7e37e73587fce79ef
parent52882f6522ae9f34f1d574b2efabc4b18e691ae0 (diff)
downloadrust-55f59fb0e329d0815a06d51dd469d774eb455cba.tar.gz
rust-55f59fb0e329d0815a06d51dd469d774eb455cba.zip
Fix parsing of frontmatters with inner hyphens
-rw-r--r--compiler/rustc_lexer/src/lib.rs4
-rw-r--r--tests/ui/frontmatter/frontmatter-inner-hyphens-1.rs10
-rw-r--r--tests/ui/frontmatter/frontmatter-inner-hyphens-2.rs11
3 files changed, 24 insertions, 1 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs
index 2374f388250..ece3f9107b0 100644
--- a/compiler/rustc_lexer/src/lib.rs
+++ b/compiler/rustc_lexer/src/lib.rs
@@ -545,11 +545,12 @@ impl Cursor<'_> {
 
         let mut s = self.as_str();
         let mut found = false;
+        let mut size = 0;
         while let Some(closing) = s.find(&"-".repeat(length_opening as usize)) {
             let preceding_chars_start = s[..closing].rfind("\n").map_or(0, |i| i + 1);
             if s[preceding_chars_start..closing].chars().all(is_whitespace) {
                 // candidate found
-                self.bump_bytes(closing);
+                self.bump_bytes(size + closing);
                 // in case like
                 // ---cargo
                 // --- blahblah
@@ -562,6 +563,7 @@ impl Cursor<'_> {
                 break;
             } else {
                 s = &s[closing + length_opening as usize..];
+                size += closing + length_opening as usize;
             }
         }
 
diff --git a/tests/ui/frontmatter/frontmatter-inner-hyphens-1.rs b/tests/ui/frontmatter/frontmatter-inner-hyphens-1.rs
new file mode 100644
index 00000000000..985b1cfe988
--- /dev/null
+++ b/tests/ui/frontmatter/frontmatter-inner-hyphens-1.rs
@@ -0,0 +1,10 @@
+---
+x ---🚧️
+---
+
+// Regression test for #141483
+//@check-pass
+
+#![feature(frontmatter)]
+
+fn main() {}
diff --git a/tests/ui/frontmatter/frontmatter-inner-hyphens-2.rs b/tests/ui/frontmatter/frontmatter-inner-hyphens-2.rs
new file mode 100644
index 00000000000..3d5fb453872
--- /dev/null
+++ b/tests/ui/frontmatter/frontmatter-inner-hyphens-2.rs
@@ -0,0 +1,11 @@
+---
+x ---y
+---
+
+// Test that hypens are allowed inside frontmatters if there is some
+// non-whitespace character preceding them.
+//@check-pass
+
+#![feature(frontmatter)]
+
+fn main() {}