Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0972fd9

Browse files
Rollup merge of #145754 - epage:escape, r=SparrowLii
fix(lexer): Don't require frontmatters to be escaped with indented fences The RFC only limits hyphens at the beginning of lines and not if they are indented or embedded in other content. Sticking to that approach was confirmed by the T-lang liason at #141367 (comment) There is a regression in error message quality which I'm leaving for someone if they feel this needs improving. Tracking issue: #136889 Fixes #141367
2 parents 88c254f + 142e25e commit 0972fd9

File tree

7 files changed

+51
-74
lines changed

7 files changed

+51
-74
lines changed

‎compiler/rustc_lexer/src/lib.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -550,28 +550,20 @@ impl Cursor<'_> {
550550
self.eat_while(|ch| ch != '\n' && is_whitespace(ch));
551551
let invalid_infostring = self.first() != '\n';
552552

553-
let mut s = self.as_str();
554553
let mut found = false;
555-
let mut size = 0;
556-
while let Some(closing) = s.find(&"-".repeat(length_opening as usize)) {
557-
let preceding_chars_start = s[..closing].rfind("\n").map_or(0, |i| i + 1);
558-
if s[preceding_chars_start..closing].chars().all(is_whitespace) {
559-
// candidate found
560-
self.bump_bytes(size + closing);
561-
// in case like
562-
// ---cargo
563-
// --- blahblah
564-
// or
565-
// ---cargo
566-
// ----
567-
// combine those stuff into this frontmatter token such that it gets detected later.
568-
self.eat_until(b'\n');
569-
found = true;
570-
break;
571-
} else {
572-
s = &s[closing + length_opening as usize..];
573-
size += closing + length_opening as usize;
574-
}
554+
let nl_fence_pattern = format!("\n{:-<1$}", "", length_opening as usize);
555+
if let Some(closing) = self.as_str().find(&nl_fence_pattern) {
556+
// candidate found
557+
self.bump_bytes(closing + nl_fence_pattern.len());
558+
// in case like
559+
// ---cargo
560+
// --- blahblah
561+
// or
562+
// ---cargo
563+
// ----
564+
// combine those stuff into this frontmatter token such that it gets detected later.
565+
self.eat_until(b'\n');
566+
found = true;
575567
}
576568

577569
if !found {

‎tests/ui/frontmatter/frontmatter-whitespace-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
//~^ ERROR: invalid preceding whitespace for frontmatter opening
3+
//~^^ ERROR: unclosed frontmatter
34
---
4-
//~^ ERROR: invalid preceding whitespace for frontmatter close
55

66
#![feature(frontmatter)]
77

‎tests/ui/frontmatter/frontmatter-whitespace-1.stderr

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ note: frontmatter opening should not be preceded by whitespace
1010
LL | ---
1111
| ^^
1212

13-
error: invalid preceding whitespace for frontmatter close
14-
--> $DIR/frontmatter-whitespace-1.rs:3:1
13+
error: unclosed frontmatter
14+
--> $DIR/frontmatter-whitespace-1.rs:1:3
1515
|
16-
LL | ---
17-
| ^^^^^
16+
LL | / ---
17+
LL | |
18+
LL | |
19+
LL | | ---
20+
LL | |
21+
| |_^
1822
|
19-
note: frontmatter close should not be preceded by whitespace
20-
--> $DIR/frontmatter-whitespace-1.rs:3:1
23+
note: frontmatter opening here was not closed
24+
--> $DIR/frontmatter-whitespace-1.rs:1:3
2125
|
2226
LL | ---
23-
| ^^
27+
| ^^^
2428

2529
error: aborting due to 2 previous errors
2630

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
---cargo
2+
//~^ ERROR: unclosed frontmatter
23

34
//@ compile-flags: --crate-type lib
45

56
#![feature(frontmatter)]
67

78
fn foo(x: i32) -> i32 {
89
---x
9-
//~^ ERROR: invalid preceding whitespace for frontmatter close
10-
//~| ERROR: extra characters after frontmatter close are not allowed
10+
//~^ WARNING: use of a double negation [double_negations]
1111
}
12-
//~^ ERROR: unexpected closing delimiter: `}`
1312

1413
// this test is for the weird case that valid Rust code can have three dashes
1514
// within them and get treated as a frontmatter close.
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
error: invalid preceding whitespace for frontmatter close
2-
--> $DIR/frontmatter-whitespace-2.rs:8:1
1+
error: unclosed frontmatter
2+
--> $DIR/frontmatter-whitespace-2.rs:1:1
33
|
4-
LL | ---x
5-
| ^^^^^^^^
4+
LL | / ---cargo
5+
... |
6+
LL | |
7+
| |_^
68
|
7-
note: frontmatter close should not be preceded by whitespace
8-
--> $DIR/frontmatter-whitespace-2.rs:8:1
9+
note: frontmatter opening here was not closed
10+
--> $DIR/frontmatter-whitespace-2.rs:1:1
911
|
10-
LL | ---x
11-
| ^^^^
12+
LL | ---cargo
13+
| ^^^
1214

13-
error: extra characters after frontmatter close are not allowed
14-
--> $DIR/frontmatter-whitespace-2.rs:8:1
15+
warning: use of a double negation
16+
--> $DIR/frontmatter-whitespace-2.rs:9:6
1517
|
1618
LL | ---x
17-
| ^^^^^^^^
18-
19-
error: unexpected closing delimiter: `}`
20-
--> $DIR/frontmatter-whitespace-2.rs:11:1
19+
| ^^^
20+
|
21+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
22+
= note: use `-= 1` if you meant to decrement the value
23+
= note: `#[warn(double_negations)]` on by default
24+
help: add parentheses for clarity
2125
|
22-
LL | }
23-
| ^ unexpected closing delimiter
26+
LL | --(-x)
27+
| + +
2428

25-
error: aborting due to 3 previous errors
29+
error: aborting due to 1 previous error; 1 warning emitted
2630

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
---
3-
//~^ ERROR: invalid preceding whitespace for frontmatter close
43

54
---
6-
//~^ ERROR: expected item, found `-`
7-
// FIXME(frontmatter): make this diagnostic better
85
---
96

7+
// hyphens only need to be escaped when at the start of a line
8+
//@ check-pass
9+
1010
#![feature(frontmatter)]
1111

1212
fn main() {}

‎tests/ui/frontmatter/multifrontmatter-2.stderr

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /