Skip to content
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

change guarded string reserved tokens to #", ##", ### #133924

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,16 @@ impl Cursor<'_> {
}

// Guarded string literal prefix: `#"` or `##`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will do

'#' if matches!(self.first(), '"' | '#') => {
'#' if matches!(
(self.first(), self.second()),
// #" ##" ###
('"', _) | ('#', '"') | ('#', '#')
Comment on lines +418 to +421
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miiiildly concerned about the implicit interaction between parser and lexer with these now, given the new reserved syntax, but that goes beyond this PR and is more about the feature as a whole, and I'm not going to block that.

) =>
{
self.bump();
if self.first() != '"' {
self.bump();
}
TokenKind::GuardedStrPrefix
}

Expand Down Expand Up @@ -804,8 +812,6 @@ impl Cursor<'_> {
/// guarded string is not found. It is the caller's
/// responsibility to do so.
pub fn guarded_double_quoted_string(&mut self) -> Option<GuardedStr> {
debug_assert!(self.prev() != '#');

Comment on lines -807 to -808
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be replaced with a check of str_before in maybe_report_guarded_str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this check should exist at all, and I'm surprised it hasn't blown up yet. It will blow up on #### if running in debug.

let mut n_start_hashes: u32 = 0;
while self.first() == '#' {
n_start_hashes += 1;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ parse_require_colon_after_labeled_expression = labeled expression must be follow
.suggestion = add `:` after the label

parse_reserved_multihash = reserved multi-hash token is forbidden
.note = sequences of two or more # are reserved for future use since Rust 2024
.note = sequences of three or more # are reserved for future use since Rust 2024
.suggestion_whitespace = consider inserting whitespace here

parse_reserved_string = invalid string literal
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,8 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
(true, span, unterminated)
}
None => {
// We should only get here in the `##+` case.
debug_assert_eq!(self.str_from_to(start, start + BytePos(2)), "##");
// We should only get here in the `###+` case.
debug_assert_eq!(self.str_from_to(start, start + BytePos(3)), "###");

(false, span, None)
}
Expand Down
32 changes: 25 additions & 7 deletions tests/ui/rust-2024/reserved-guarded-strings-lexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,51 @@
macro_rules! demo2 {
( $a:tt $b:tt ) => { println!("two tokens") };
}

macro_rules! demo3 {
( $a:tt $b:tt $c:tt ) => { println!("three tokens") };
}

macro_rules! demo4 {
( $a:tt $b:tt $c:tt $d:tt ) => { println!("four tokens") };
}

macro_rules! demo5 {
( $a:tt $b:tt $c:tt $d:tt $e:tt ) => { println!("five tokens") };
}

macro_rules! demo6 {
( $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt ) => { println!("six tokens") };
}
macro_rules! demo7 {
( $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt ) => { println!("seven tokens") };
}
macro_rules! demo9 {
( $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt ) => { println!("nine tokens") };
}


fn main() {
demo3!(## "foo");
demo4!(## "foo"#);

demo4!(### "foo");
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!(### "foo");
demo5!(#### "foo");
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!(## "foo"#);
demo5!(### "foo"#);
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo7!(### "foo"###);
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo9!(#### "foo"####);
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
Expand All @@ -58,9 +68,17 @@ fn main() {
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!("foo"###);
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo6!(#"foo"####);
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!("foo"###);
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo5!("foo"####);
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
Expand Down
Loading
Loading