Skip to content

Commit 606582d

Browse files
committed
Auto merge of #141028 - xizheyin:issue-140618, r=<try>
Lexer: check in `advance_token` to avoid regard spare `##` as `GardedStrPrefix` Fixes #140618 I performed additional checks to avoid treating the two `##` after `r#‘xxx’####` as `GardedStrPrefix`. `GardedStrPrefix` is introduced in <321a5db>. r? compiler
2 parents 44f415c + 3fe5df4 commit 606582d

File tree

5 files changed

+120
-14
lines changed

5 files changed

+120
-14
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ pub fn is_ident(string: &str) -> bool {
372372
impl Cursor<'_> {
373373
/// Parses a token from the input string.
374374
pub fn advance_token(&mut self) -> Token {
375+
let pre_char = self.prev();
375376
let first_char = match self.bump() {
376377
Some(c) => c,
377378
None => return Token::new(TokenKind::Eof, 0),
@@ -456,7 +457,7 @@ impl Cursor<'_> {
456457
}
457458

458459
// Guarded string literal prefix: `#"` or `##`
459-
'#' if matches!(self.first(), '"' | '#') => {
460+
'#' if matches!(self.first(), '"' | '#') && pre_char != '#' => {
460461
self.bump();
461462
TokenKind::GuardedStrPrefix
462463
}

tests/ui/rust-2024/reserved-guarded-strings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn main() {
6565
demo1!(###"foo"###); //~ ERROR invalid string literal
6666
demo2!(#"foo"###);
6767
//~^ ERROR invalid string literal
68-
//~| ERROR reserved multi-hash token is forbidden
68+
//~| ERROR no rules expected `#`
6969

7070
// More than 255 hashes
7171
demon!(####################################################################################################################################################################################################################################################################"foo");

tests/ui/rust-2024/reserved-guarded-strings.stderr

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,6 @@ help: consider inserting whitespace here
226226
LL | demo2!(# "foo"###);
227227
| +
228228

229-
error: reserved multi-hash token is forbidden
230-
--> $DIR/reserved-guarded-strings.rs:66:19
231-
|
232-
LL | demo2!(#"foo"###);
233-
| ^^
234-
|
235-
= note: sequences of two or more # are reserved for future use since Rust 2024
236-
help: consider inserting whitespace here
237-
|
238-
LL | demo2!(#"foo"## #);
239-
| +
240-
241229
error: invalid string literal
242230
--> $DIR/reserved-guarded-strings.rs:71:12
243231
|
@@ -250,5 +238,20 @@ help: consider inserting whitespace here
250238
LL | demon!(# ###################################################################################################################################################################################################################################################################"foo");
251239
| +
252240

241+
error: no rules expected `#`
242+
--> $DIR/reserved-guarded-strings.rs:66:20
243+
|
244+
LL | macro_rules! demo2 {
245+
| ------------------ when calling this macro
246+
...
247+
LL | demo2!(#"foo"###);
248+
| ^ no rules expected this token in macro call
249+
|
250+
note: while trying to match meta-variable `$b:tt`
251+
--> $DIR/reserved-guarded-strings.rs:9:13
252+
|
253+
LL | ( $a:tt $b:tt ) => { println!("two tokens") };
254+
| ^^^^^
255+
253256
error: aborting due to 21 previous errors
254257

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ edition:2024
2+
3+
fn f0(){
4+
r#"ok0!"#;
5+
}
6+
7+
fn f1(){
8+
r#"ok1!"##;
9+
//~^ ERROR too many `#` when terminating raw string
10+
}
11+
12+
13+
fn f2(){
14+
r#"ok2!"###;
15+
//~^ ERROR too many `#` when terminating raw string
16+
}
17+
18+
fn f3(){
19+
#"ok3!"#;
20+
//~^ ERROR invalid string literal
21+
}
22+
23+
24+
fn f4(){
25+
#"ok4!"##;
26+
//~^ ERROR invalid string literal
27+
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
28+
}
29+
30+
fn f5(){
31+
#"ok5!"###;
32+
//~^ ERROR invalid string literal
33+
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
34+
}
35+
36+
fn main() {}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error: invalid string literal
2+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:19:5
3+
|
4+
LL | #"ok3!"#;
5+
| ^^^^^^^^
6+
|
7+
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
8+
help: consider inserting whitespace here
9+
|
10+
LL | # "ok3!"#;
11+
| +
12+
13+
error: invalid string literal
14+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:25:5
15+
|
16+
LL | #"ok4!"##;
17+
| ^^^^^^^^
18+
|
19+
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
20+
help: consider inserting whitespace here
21+
|
22+
LL | # "ok4!"##;
23+
| +
24+
25+
error: invalid string literal
26+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:5
27+
|
28+
LL | #"ok5!"###;
29+
| ^^^^^^^^
30+
|
31+
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
32+
help: consider inserting whitespace here
33+
|
34+
LL | # "ok5!"###;
35+
| +
36+
37+
error: too many `#` when terminating raw string
38+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:8:14
39+
|
40+
LL | r#"ok1!"##;
41+
| ---------^ help: remove the extra `#`
42+
| |
43+
| this raw string started with 1 `#`
44+
45+
error: too many `#` when terminating raw string
46+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:14:14
47+
|
48+
LL | r#"ok2!"###;
49+
| ---------^^ help: remove the extra `#`s
50+
| |
51+
| this raw string started with 1 `#`
52+
53+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
54+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:25:13
55+
|
56+
LL | #"ok4!"##;
57+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
58+
59+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
60+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:13
61+
|
62+
LL | #"ok5!"###;
63+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
64+
65+
error: aborting due to 7 previous errors
66+

0 commit comments

Comments
 (0)