Skip to content

Commit 3fe5df4

Browse files
committed
Lexer: check in advance_token to avoid regard ## as GardedStrPrefix
Signed-off-by: xizheyin <[email protected]>
1 parent c492384 commit 3fe5df4

5 files changed

+32
-52
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

tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ fn f1(){
1212

1313
fn f2(){
1414
r#"ok2!"###;
15-
//~^ ERROR reserved multi-hash token is forbidden
16-
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
15+
//~^ ERROR too many `#` when terminating raw string
1716
}
1817

1918
fn f3(){
@@ -31,8 +30,7 @@ fn f4(){
3130
fn f5(){
3231
#"ok5!"###;
3332
//~^ ERROR invalid string literal
34-
//~| ERROR reserved multi-hash token is forbidden
35-
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
33+
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
3634
}
3735

3836
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
error: reserved multi-hash token is forbidden
2-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:14: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-
131
error: invalid string literal
14-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:20:5
2+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:19:5
153
|
164
LL | #"ok3!"#;
175
| ^^^^^^^^
@@ -23,7 +11,7 @@ LL | # "ok3!"#;
2311
| +
2412

2513
error: invalid string literal
26-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:26:5
14+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:25:5
2715
|
2816
LL | #"ok4!"##;
2917
| ^^^^^^^^
@@ -35,7 +23,7 @@ LL | # "ok4!"##;
3523
| +
3624

3725
error: invalid string literal
38-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:32:5
26+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:5
3927
|
4028
LL | #"ok5!"###;
4129
| ^^^^^^^^
@@ -46,18 +34,6 @@ help: consider inserting whitespace here
4634
LL | # "ok5!"###;
4735
| +
4836

49-
error: reserved multi-hash token is forbidden
50-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:32:13
51-
|
52-
LL | #"ok5!"###;
53-
| ^^
54-
|
55-
= note: sequences of two or more # are reserved for future use since Rust 2024
56-
help: consider inserting whitespace here
57-
|
58-
LL | #"ok5!"## #;
59-
| +
60-
6137
error: too many `#` when terminating raw string
6238
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:8:14
6339
|
@@ -66,23 +42,25 @@ LL | r#"ok1!"##;
6642
| |
6743
| this raw string started with 1 `#`
6844

69-
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
45+
error: too many `#` when terminating raw string
7046
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:14:14
7147
|
7248
LL | r#"ok2!"###;
73-
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator
49+
| ---------^^ help: remove the extra `#`s
50+
| |
51+
| this raw string started with 1 `#`
7452

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

81-
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
82-
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:32:13
59+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
60+
--> $DIR/reverved-guarded-string-too-many-terminators-issue-140618.rs:31:13
8361
|
8462
LL | #"ok5!"###;
85-
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator
63+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
8664

87-
error: aborting due to 9 previous errors
65+
error: aborting due to 7 previous errors
8866

0 commit comments

Comments
 (0)