diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index b2bd5e188efdc..ffcc7648e198b 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -371,6 +371,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 Some(first_char) = self.bump() else { return Token::new(TokenKind::Eof, 0); }; @@ -454,7 +455,7 @@ impl Cursor<'_> { } // Guarded string literal prefix: `#"` or `##` - '#' if matches!(self.first(), '"' | '#') => { + '#' if matches!(self.first(), '"' | '#') && pre_char != '#' => { self.bump(); TokenKind::GuardedStrPrefix } diff --git a/tests/ui/rust-2024/reserved-guarded-strings.rs b/tests/ui/rust-2024/reserved-guarded-strings.rs index ae4bd670fcad9..c086e2b9e8c15 100644 --- a/tests/ui/rust-2024/reserved-guarded-strings.rs +++ b/tests/ui/rust-2024/reserved-guarded-strings.rs @@ -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"); diff --git a/tests/ui/rust-2024/reserved-guarded-strings.stderr b/tests/ui/rust-2024/reserved-guarded-strings.stderr index fde3a719e6717..0b4710d416298 100644 --- a/tests/ui/rust-2024/reserved-guarded-strings.stderr +++ b/tests/ui/rust-2024/reserved-guarded-strings.stderr @@ -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 | @@ -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 diff --git a/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.rs b/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.rs new file mode 100644 index 0000000000000..7ef2639c03ad1 --- /dev/null +++ b/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.rs @@ -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() {} diff --git a/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.stderr b/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.stderr new file mode 100644 index 0000000000000..7f0604391f16c --- /dev/null +++ b/tests/ui/rust-2024/reverved-guarded-string-too-many-terminators-issue-140618.stderr @@ -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 +