Skip to content

Commit 20e53c9

Browse files
authored
Unrolled build for #146899
Rollup merge of #146899 - Teapot4195:issue-146847-fix, r=nnethercote Fix a crash/mislex when more than one frontmatter closing possibility is considered When the less fortunate recovery path for frontmatters are taken, if the lexer considers more than one possible frontmatter closing possibility, the current index is entirely mis-tracked and can result in bump_bytes landing in the middle of a multichar unicode character. This fixes it by tracking the actual base index and updating it as it considers additional closing possibilities. fixes #146847
2 parents 3e887f5 + 2d18c88 commit 20e53c9

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,16 @@ impl Cursor<'_> {
599599
if potential_closing.is_none() {
600600
// a less fortunate recovery if all else fails which finds any dashes preceded by whitespace
601601
// on a standalone line. Might be wrong.
602+
let mut base_index = 0;
602603
while let Some(closing) = rest.find("---") {
603604
let preceding_chars_start = rest[..closing].rfind("\n").map_or(0, |i| i + 1);
604605
if rest[preceding_chars_start..closing].chars().all(is_horizontal_whitespace) {
605606
// candidate found
606-
potential_closing = Some(closing);
607+
potential_closing = Some(closing + base_index);
607608
break;
608609
} else {
609610
rest = &rest[closing + 3..];
611+
base_index += closing + 3;
610612
}
611613
}
612614
}

tests/ui/frontmatter/unclosed-6.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
//~^ ERROR unclosed frontmatter
3+
🦀---
4+
---
5+
6+
// This test checks the location of the --- recovered by the parser is not
7+
// incorrectly tracked during the less fortunate recovery case and multiple
8+
// candidates are found, as seen with #146847
9+
10+
#![feature(frontmatter)]
11+
12+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: unclosed frontmatter
2+
--> $DIR/unclosed-6.rs:1:1
3+
|
4+
LL | / ---
5+
LL | |
6+
LL | | 🦀---
7+
LL | | ---
8+
... |
9+
LL | |
10+
| |_^
11+
|
12+
note: frontmatter opening here was not closed
13+
--> $DIR/unclosed-6.rs:1:1
14+
|
15+
LL | ---
16+
| ^^^
17+
18+
error: aborting due to 1 previous error
19+

0 commit comments

Comments
 (0)