Skip to content

Lexer: check in advance_token to avoid regard spare ## as GardedStrPrefix #141028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ pub fn is_ident(string: &str) -> bool {
impl Cursor<'_> {
/// Parses a token from the input string.
pub fn advance_token(&mut self) -> Token {
let pre_char = self.prev();
let first_char = match self.bump() {
Some(c) => c,
None => return Token::new(TokenKind::Eof, 0),
Expand Down Expand Up @@ -456,7 +457,7 @@ impl Cursor<'_> {
}

// Guarded string literal prefix: `#"` or `##`
'#' if matches!(self.first(), '"' | '#') => {
'#' if matches!(self.first(), '"' | '#') && pre_char != '#' => {
self.bump();
TokenKind::GuardedStrPrefix
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rust-2024/reserved-guarded-strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() {
demo1!(###"foo"###); //~ ERROR invalid string literal
demo2!(#"foo"###);
//~^ ERROR invalid string literal
//~| ERROR reserved multi-hash token is forbidden
//~| ERROR no rules expected `#`

// More than 255 hashes
demon!(####################################################################################################################################################################################################################################################################"foo");
Expand Down
27 changes: 15 additions & 12 deletions tests/ui/rust-2024/reserved-guarded-strings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,6 @@ help: consider inserting whitespace here
LL | demo2!(# "foo"###);
| +

error: reserved multi-hash token is forbidden
--> $DIR/reserved-guarded-strings.rs:66:19
|
LL | demo2!(#"foo"###);
| ^^
|
= note: sequences of two or more # are reserved for future use since Rust 2024
help: consider inserting whitespace here
|
LL | demo2!(#"foo"## #);
| +

error: invalid string literal
--> $DIR/reserved-guarded-strings.rs:71:12
|
Expand All @@ -250,5 +238,20 @@ help: consider inserting whitespace here
LL | demon!(# ###################################################################################################################################################################################################################################################################"foo");
| +

error: no rules expected `#`
--> $DIR/reserved-guarded-strings.rs:66:20
|
LL | macro_rules! demo2 {
| ------------------ when calling this macro
...
LL | demo2!(#"foo"###);
| ^ no rules expected this token in macro call
|
note: while trying to match meta-variable `$b:tt`
--> $DIR/reserved-guarded-strings.rs:9:13
|
LL | ( $a:tt $b:tt ) => { println!("two tokens") };
| ^^^^^

error: aborting due to 21 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//@ edition:2024

fn f0(){
r#"ok0!"#;
}

fn f1(){
r#"ok1!"##;
//~^ ERROR too many `#` when terminating raw string
}


fn f2(){
r#"ok2!"###;
//~^ ERROR too many `#` when terminating raw string
}

fn f3(){
#"ok3!"#;
//~^ ERROR invalid string literal
}


fn f4(){
#"ok4!"##;
//~^ ERROR invalid string literal
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
}

fn f5(){
#"ok5!"###;
//~^ ERROR invalid string literal
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
error: invalid string literal
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:19:5
|
LL | #"ok3!"#;
| ^^^^^^^^
|
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
help: consider inserting whitespace here
|
LL | # "ok3!"#;
| +

error: invalid string literal
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:25:5
|
LL | #"ok4!"##;
| ^^^^^^^^
|
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
help: consider inserting whitespace here
|
LL | # "ok4!"##;
| +

error: invalid string literal
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:5
|
LL | #"ok5!"###;
| ^^^^^^^^
|
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
help: consider inserting whitespace here
|
LL | # "ok5!"###;
| +

error: too many `#` when terminating raw string
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:8:14
|
LL | r#"ok1!"##;
| ---------^ help: remove the extra `#`
| |
| this raw string started with 1 `#`

error: too many `#` when terminating raw string
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:14:14
|
LL | r#"ok2!"###;
| ---------^^ help: remove the extra `#`s
| |
| this raw string started with 1 `#`

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:25:13
|
LL | #"ok4!"##;
| ^ expected one of `.`, `;`, `?`, `}`, or an operator

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:13
|
LL | #"ok5!"###;
| ^ expected one of `.`, `;`, `?`, `}`, or an operator

error: aborting due to 7 previous errors

Loading