summary refs log tree commit diff
path: root/compiler/rustc_ast/src/util
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-11-29 13:36:00 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2022-12-02 13:49:19 +1100
commit2fd364acff5f962b0ce4f4dffb5ae085d5f2b67a (patch)
tree041c2e45d4449c44f874c737a3dcdbcaa8895c9f /compiler/rustc_ast/src/util
parenta7f35c42d474f893c56b6e0f7df3f8bb965f2650 (diff)
downloadrust-2fd364acff5f962b0ce4f4dffb5ae085d5f2b67a.tar.gz
rust-2fd364acff5f962b0ce4f4dffb5ae085d5f2b67a.zip
Remove `token::Lit` from `ast::MetaItemLit`.
`token::Lit` contains a `kind` field that indicates what kind of literal
it is. `ast::MetaItemLit` currently wraps a `token::Lit` but also has
its own `kind` field. This means that `ast::MetaItemLit` encodes the
literal kind in two different ways.

This commit changes `ast::MetaItemLit` so it no longer wraps
`token::Lit`. It now contains the `symbol` and `suffix` fields from
`token::Lit`, but not the `kind` field, eliminating the redundancy.
Diffstat (limited to 'compiler/rustc_ast/src/util')
-rw-r--r--compiler/rustc_ast/src/util/literal.rs27
1 files changed, 25 insertions, 2 deletions
diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs
index 9f6fdf44ac0..a0a925d4700 100644
--- a/compiler/rustc_ast/src/util/literal.rs
+++ b/compiler/rustc_ast/src/util/literal.rs
@@ -202,9 +202,32 @@ impl LitKind {
 }
 
 impl MetaItemLit {
-    /// Converts token literal into a meta item literal.
+    /// Converts a token literal into a meta item literal.
     pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<MetaItemLit, LitError> {
-        Ok(MetaItemLit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
+        Ok(MetaItemLit {
+            symbol: token_lit.symbol,
+            suffix: token_lit.suffix,
+            kind: LitKind::from_token_lit(token_lit)?,
+            span,
+        })
+    }
+
+    /// Cheaply converts a meta item literal into a token literal.
+    pub fn as_token_lit(&self) -> token::Lit {
+        let kind = match self.kind {
+            LitKind::Bool(_) => token::Bool,
+            LitKind::Str(_, ast::StrStyle::Cooked) => token::Str,
+            LitKind::Str(_, ast::StrStyle::Raw(n)) => token::StrRaw(n),
+            LitKind::ByteStr(_, ast::StrStyle::Cooked) => token::ByteStr,
+            LitKind::ByteStr(_, ast::StrStyle::Raw(n)) => token::ByteStrRaw(n),
+            LitKind::Byte(_) => token::Byte,
+            LitKind::Char(_) => token::Char,
+            LitKind::Int(..) => token::Integer,
+            LitKind::Float(..) => token::Float,
+            LitKind::Err => token::Err,
+        };
+
+        token::Lit::new(kind, self.symbol, self.suffix)
     }
 
     /// Converts an arbitrary token into meta item literal.