Skip to content

Commit 1ab58fc

Browse files
committed
Lexer: check ### to avoid regard ## as GardedStrPrefix
Signed-off-by: xizheyin <[email protected]>
1 parent d6d8e16 commit 1ab58fc

5 files changed

+48
-65
lines changed

compiler/rustc_lexer/src/lib.rs

+2-1
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

+1-1
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

+15-12
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

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
//@ edition:2024
22
fn main() {
33
r#"ok0!"#;
4-
r#"ok1!"##;
4+
r#"ok1!"##;
55
//~^ ERROR too many `#` when terminating raw string
66
r#"ok2!"###;
7-
//~^ ERROR reserved multi-hash token is forbidden
8-
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
9-
r#"ok3!"####;
10-
//~^ ERROR reserved multi-hash token is forbidden
7+
//~^ ERROR too many `#` when terminating raw string
8+
r#"ok3!"####;
9+
//~^ ERROR too many `#` when terminating raw string
1110
#"ok4!"#;
1211
//~^ ERROR invalid string literal
13-
#"ok5!"##;
12+
#"ok5!"##;
1413
//~^ ERROR invalid string literal
14+
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
1515
#"ok6!"###;
1616
//~^ ERROR invalid string literal
17-
//~| ERROR reserved multi-hash token is forbidden
1817
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
1-
error: reserved multi-hash token is forbidden
2-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:6:14
3-
|
4-
LL | r#"ok2!"###;
5-
| ^^
6-
|
7-
= note: sequences of two or more # are reserved for future use since Rust 2024
8-
help: consider inserting whitespace here
9-
|
10-
LL | r#"ok2!"## #;
11-
| +
12-
13-
error: reserved multi-hash token is forbidden
14-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:9:14
15-
|
16-
LL | r#"ok3!"####;
17-
| ^^
18-
|
19-
= note: sequences of two or more # are reserved for future use since Rust 2024
20-
help: consider inserting whitespace here
21-
|
22-
LL | r#"ok3!"## ##;
23-
| +
24-
251
error: invalid string literal
26-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:11:5
2+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:10:5
273
|
284
LL | #"ok4!"#;
295
| ^^^^^^^^
@@ -35,15 +11,15 @@ LL | # "ok4!"#;
3511
| +
3612

3713
error: invalid string literal
38-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:13:5
14+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:12:5
3915
|
40-
LL | #"ok5!"##;
16+
LL | #"ok5!"##;
4117
| ^^^^^^^^
4218
|
4319
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
4420
help: consider inserting whitespace here
4521
|
46-
LL | # "ok5!"##;
22+
LL | # "ok5!"##;
4723
| +
4824

4925
error: invalid string literal
@@ -58,31 +34,35 @@ help: consider inserting whitespace here
5834
LL | # "ok6!"###;
5935
| +
6036

61-
error: reserved multi-hash token is forbidden
62-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:15:13
63-
|
64-
LL | #"ok6!"###;
65-
| ^^
66-
|
67-
= note: sequences of two or more # are reserved for future use since Rust 2024
68-
help: consider inserting whitespace here
69-
|
70-
LL | #"ok6!"## #;
71-
| +
72-
7337
error: too many `#` when terminating raw string
7438
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:4:14
7539
|
76-
LL | r#"ok1!"##;
40+
LL | r#"ok1!"##;
7741
| ---------^ help: remove the extra `#`
7842
| |
7943
| this raw string started with 1 `#`
8044

81-
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
45+
error: too many `#` when terminating raw string
8246
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:6:14
8347
|
8448
LL | r#"ok2!"###;
85-
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator
49+
| ---------^^ help: remove the extra `#`s
50+
| |
51+
| this raw string started with 1 `#`
52+
53+
error: too many `#` when terminating raw string
54+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:8:14
55+
|
56+
LL | r#"ok3!"####;
57+
| ---------^^^ help: remove the extra `#`s
58+
| |
59+
| this raw string started with 1 `#`
60+
61+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
62+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:12:13
63+
|
64+
LL | #"ok5!"##;
65+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
8666

87-
error: aborting due to 8 previous errors
67+
error: aborting due to 7 previous errors
8868

0 commit comments

Comments
 (0)