Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,28 +550,20 @@ impl Cursor<'_> {
self.eat_while(|ch| ch != '\n' && is_whitespace(ch));
let invalid_infostring = self.first() != '\n';

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(size + closing);
// in case like
// ---cargo
// --- blahblah
// or
// ---cargo
// ----
// combine those stuff into this frontmatter token such that it gets detected later.
self.eat_until(b'\n');
found = true;
break;
} else {
s = &s[closing + length_opening as usize..];
size += closing + length_opening as usize;
}
let nl_fence_pattern = format!("\n{:-<1$}", "", length_opening as usize);
if let Some(closing) = self.as_str().find(&nl_fence_pattern) {
// candidate found
self.bump_bytes(closing + nl_fence_pattern.len());
// in case like
// ---cargo
// --- blahblah
// or
// ---cargo
// ----
// combine those stuff into this frontmatter token such that it gets detected later.
self.eat_until(b'\n');
found = true;
}

if !found {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/frontmatter/frontmatter-whitespace-1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
//~^ ERROR: invalid preceding whitespace for frontmatter opening
//~^^ ERROR: unclosed frontmatter
---
//~^ ERROR: invalid preceding whitespace for frontmatter close

#![feature(frontmatter)]

Expand Down
18 changes: 11 additions & 7 deletions tests/ui/frontmatter/frontmatter-whitespace-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ note: frontmatter opening should not be preceded by whitespace
LL | ---
| ^^

error: invalid preceding whitespace for frontmatter close
--> $DIR/frontmatter-whitespace-1.rs:3:1
error: unclosed frontmatter
--> $DIR/frontmatter-whitespace-1.rs:1:3
|
LL | ---
| ^^^^^
LL | / ---
LL | |
LL | |
LL | | ---
LL | |
| |_^
|
note: frontmatter close should not be preceded by whitespace
--> $DIR/frontmatter-whitespace-1.rs:3:1
note: frontmatter opening here was not closed
--> $DIR/frontmatter-whitespace-1.rs:1:3
|
LL | ---
| ^^
| ^^^

error: aborting due to 2 previous errors

5 changes: 2 additions & 3 deletions tests/ui/frontmatter/frontmatter-whitespace-2.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
---cargo
//~^ ERROR: unclosed frontmatter

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

#![feature(frontmatter)]

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

// this test is for the weird case that valid Rust code can have three dashes
// within them and get treated as a frontmatter close.
38 changes: 21 additions & 17 deletions tests/ui/frontmatter/frontmatter-whitespace-2.stderr
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
error: invalid preceding whitespace for frontmatter close
--> $DIR/frontmatter-whitespace-2.rs:8:1
error: unclosed frontmatter
--> $DIR/frontmatter-whitespace-2.rs:1:1
|
LL | ---x
| ^^^^^^^^
LL | / ---cargo
... |
LL | |
| |_^
|
note: frontmatter close should not be preceded by whitespace
--> $DIR/frontmatter-whitespace-2.rs:8:1
note: frontmatter opening here was not closed
--> $DIR/frontmatter-whitespace-2.rs:1:1
|
LL | ---x
| ^^^^
LL | ---cargo
| ^^^

error: extra characters after frontmatter close are not allowed
--> $DIR/frontmatter-whitespace-2.rs:8:1
warning: use of a double negation
--> $DIR/frontmatter-whitespace-2.rs:9:6
|
LL | ---x
| ^^^^^^^^

error: unexpected closing delimiter: `}`
--> $DIR/frontmatter-whitespace-2.rs:11:1
| ^^^
|
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
= note: use `-= 1` if you meant to decrement the value
= note: `#[warn(double_negations)]` on by default
help: add parentheses for clarity
|
LL | }
| ^ unexpected closing delimiter
LL | --(-x)
| + +

error: aborting due to 3 previous errors
error: aborting due to 1 previous error; 1 warning emitted

6 changes: 3 additions & 3 deletions tests/ui/frontmatter/multifrontmatter-2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
---
//~^ ERROR: invalid preceding whitespace for frontmatter close

---
//~^ ERROR: expected item, found `-`
// FIXME(frontmatter): make this diagnostic better
---

// hyphens only need to be escaped when at the start of a line
//@ check-pass

#![feature(frontmatter)]

fn main() {}
22 changes: 0 additions & 22 deletions tests/ui/frontmatter/multifrontmatter-2.stderr

This file was deleted.

Loading