From 7637ef87a0a18489466b2c5c710c877eeb0d9b7a Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Fri, 27 Sep 2024 17:07:55 -0600 Subject: [PATCH] move back to TokenKind --- compiler/rustc_lexer/src/lib.rs | 14 +++++++------- compiler/rustc_parse/src/lexer/mod.rs | 6 +----- src/librustdoc/html/highlight.rs | 2 +- .../rust-analyzer/crates/parser/src/lexed_str.rs | 5 ++++- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index d25b0c6544ef4..b0ab50dd77388 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -104,6 +104,12 @@ pub enum TokenKind { /// for emoji identifier recovery, as those are not meant to be ever accepted. InvalidPrefix, + /// Guarded string literal prefix: `#"` or `##`. + /// + /// Used for reserving "guarded strings" (RFC 3598) in edition 2024. + /// Split into the component tokens on older editions. + GuardedStrPrefix, + /// Examples: `12u8`, `1.0e-40`, `b"123"`. Note that `_` is an invalid /// suffix, but may be present here on string and float literals. Users of /// this type will need to check for and reject that case. @@ -205,11 +211,6 @@ pub enum LiteralKind { ByteStr { terminated: bool }, /// `c"abc"`, `c"abc` CStr { terminated: bool }, - /// Guarded string literal prefix: `#"` or `##`. - /// - /// Used for reserving "guarded strings" (RFC 3598) in edition 2024. - /// Split into the component tokens on older editions. - GuardedStrPrefix, /// `r"abc"`, `r#"abc"#`, `r####"ab"###"c"####`, `r#"a`. `None` indicates /// an invalid literal. RawStr { n_hashes: Option }, @@ -422,8 +423,7 @@ impl Cursor<'_> { // Guarded string literal prefix: `#"` or `##` '#' if matches!(self.first(), '"' | '#') => { self.bump(); - let suffix_start = self.pos_within_token(); - TokenKind::Literal { kind: GuardedStrPrefix, suffix_start } + TokenKind::GuardedStrPrefix } // One-symbol tokens. diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 53dc3c4c2e3aa..d627ef3d2cbe3 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -252,10 +252,7 @@ impl<'psess, 'src> StringReader<'psess, 'src> { let prefix_span = self.mk_sp(start, lit_start); return (Token::new(self.ident(start), prefix_span), preceded_by_whitespace); } - rustc_lexer::TokenKind::Literal { - kind: rustc_lexer::LiteralKind::GuardedStrPrefix, - .. - } => self.maybe_report_guarded_str(start, str_before), + rustc_lexer::TokenKind::GuardedStrPrefix => self.maybe_report_guarded_str(start, str_before), rustc_lexer::TokenKind::Literal { kind, suffix_start } => { let suffix_start = start + BytePos(suffix_start); let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind); @@ -607,7 +604,6 @@ impl<'psess, 'src> StringReader<'psess, 'src> { } (kind, self.symbol_from_to(start, end)) } - rustc_lexer::LiteralKind::GuardedStrPrefix => unreachable!(), } } diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 5d2b434984995..a49b6aab6b724 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -844,8 +844,8 @@ impl<'src> Classifier<'src> { | LiteralKind::RawCStr { .. } => Class::String, // Number literals. LiteralKind::Float { .. } | LiteralKind::Int { .. } => Class::Number, - LiteralKind::GuardedStrPrefix => return no_highlight(sink), }, + TokenKind::GuardedStrPrefix => return no_highlight(sink), TokenKind::Ident | TokenKind::RawIdent if lookahead == Some(TokenKind::Bang) => { self.in_macro = true; sink(Highlight::EnterSpan { class: Class::Macro(self.new_span(before, text)) }); diff --git a/src/tools/rust-analyzer/crates/parser/src/lexed_str.rs b/src/tools/rust-analyzer/crates/parser/src/lexed_str.rs index 928477169fba0..7ea23b4f752c1 100644 --- a/src/tools/rust-analyzer/crates/parser/src/lexed_str.rs +++ b/src/tools/rust-analyzer/crates/parser/src/lexed_str.rs @@ -188,7 +188,10 @@ impl<'a> Converter<'a> { rustc_lexer::TokenKind::RawIdent => IDENT, - rustc_lexer::TokenKind::Literal { kind: GuardedStrPrefix, .. } => ERROR, + rustc_lexer::TokenKind::GuardedStrPrefix => { + err = "Invalid string literal (reserved syntax)"; + ERROR + }, rustc_lexer::TokenKind::Literal { kind, .. } => { self.extend_literal(token_text.len(), kind);