diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-10-10 13:40:56 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-11-16 09:41:28 +1100 |
| commit | 358a603f110b0489d22f3929d3f232e684fd9ffb (patch) | |
| tree | e409967cbf3698809be84c84dbf25285dc154bdb /compiler/rustc_ast/src/util/literal.rs | |
| parent | 01760265cbdc985af50e7046e7635d124b24e335 (diff) | |
| download | rust-358a603f110b0489d22f3929d3f232e684fd9ffb.tar.gz rust-358a603f110b0489d22f3929d3f232e684fd9ffb.zip | |
Use `token::Lit` in `ast::ExprKind::Lit`.
Instead of `ast::Lit`. Literal lowering now happens at two different times. Expression literals are lowered when HIR is crated. Attribute literals are lowered during parsing. This commit changes the language very slightly. Some programs that used to not compile now will compile. This is because some invalid literals that are removed by `cfg` or attribute macros will no longer trigger errors. See this comment for more details: https://github.com/rust-lang/rust/pull/102944#issuecomment-1277476773
Diffstat (limited to 'compiler/rustc_ast/src/util/literal.rs')
| -rw-r--r-- | compiler/rustc_ast/src/util/literal.rs | 27 |
1 files changed, 5 insertions, 22 deletions
diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs index e267f8cd100..db2ac9626af 100644 --- a/compiler/rustc_ast/src/util/literal.rs +++ b/compiler/rustc_ast/src/util/literal.rs @@ -8,8 +8,8 @@ use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; use std::ascii; +#[derive(Debug)] pub enum LitError { - NotLiteral, LexerError, InvalidSuffix, InvalidIntSuffix, @@ -202,27 +202,10 @@ impl Lit { Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span }) } - /// Converts arbitrary token into an AST literal. - /// - /// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation. - pub fn from_token(token: &Token) -> Result<Lit, LitError> { - let lit = match token.uninterpolate().kind { - token::Ident(name, false) if name.is_bool_lit() => { - token::Lit::new(token::Bool, name, None) - } - token::Literal(lit) => lit, - token::Interpolated(ref nt) => { - if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt - && let ast::ExprKind::Lit(lit) = &expr.kind - { - return Ok(lit.clone()); - } - return Err(LitError::NotLiteral); - } - _ => return Err(LitError::NotLiteral), - }; - - Lit::from_token_lit(lit, token.span) + /// Converts an arbitrary token into an AST literal. + pub fn from_token(token: &Token) -> Option<Lit> { + token::Lit::from_token(token) + .and_then(|token_lit| Lit::from_token_lit(token_lit, token.span).ok()) } /// Attempts to recover an AST literal from semantic literal. |
