Skip to content

Commit bebd57a

Browse files
committedNov 16, 2022
Auto merge of #102944 - nnethercote:ast-Lit-third-time-lucky, r=petrochenkov
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. r? `@petrochenkov`
2 parents e9493d6 + 358a603 commit bebd57a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+786
-507
lines changed
 

‎compiler/rustc_ast/src/ast.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ pub enum ExprKind {
13321332
/// A unary operation (e.g., `!x`, `*x`).
13331333
Unary(UnOp, P<Expr>),
13341334
/// A literal (e.g., `1`, `"foo"`).
1335-
Lit(Lit),
1335+
Lit(token::Lit),
13361336
/// A cast (e.g., `foo as f64`).
13371337
Cast(P<Expr>, P<Ty>),
13381338
/// A type ascription (e.g., `42: usize`).
@@ -1698,16 +1698,12 @@ pub struct StrLit {
16981698
}
16991699

17001700
impl StrLit {
1701-
pub fn as_lit(&self) -> Lit {
1701+
pub fn as_token_lit(&self) -> token::Lit {
17021702
let token_kind = match self.style {
17031703
StrStyle::Cooked => token::Str,
17041704
StrStyle::Raw(n) => token::StrRaw(n),
17051705
};
1706-
Lit {
1707-
token_lit: token::Lit::new(token_kind, self.symbol, self.suffix),
1708-
span: self.span,
1709-
kind: LitKind::Str(self.symbol_unescaped, self.style),
1710-
}
1706+
token::Lit::new(token_kind, self.symbol, self.suffix)
17111707
}
17121708
}
17131709

@@ -1733,9 +1729,10 @@ pub enum LitFloatType {
17331729
Unsuffixed,
17341730
}
17351731

1736-
/// Literal kind.
1737-
///
1738-
/// E.g., `"foo"`, `42`, `12.34`, or `bool`.
1732+
/// Note that the entire literal (including the suffix) is considered when
1733+
/// deciding the `LitKind`. This means that float literals like `1f32` are
1734+
/// classified by this type as `Float`. This is different to `token::LitKind`
1735+
/// which does *not* consider the suffix.
17391736
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
17401737
pub enum LitKind {
17411738
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
@@ -1749,10 +1746,11 @@ pub enum LitKind {
17491746
Char(char),
17501747
/// An integer literal (`1`).
17511748
Int(u128, LitIntType),
1752-
/// A float literal (`1f64` or `1E10f64`). Stored as a symbol rather than
1753-
/// `f64` so that `LitKind` can impl `Eq` and `Hash`.
1749+
/// A float literal (`1.0`, `1f64` or `1E10f64`). The pre-suffix part is
1750+
/// stored as a symbol rather than `f64` so that `LitKind` can impl `Eq`
1751+
/// and `Hash`.
17541752
Float(Symbol, LitFloatType),
1755-
/// A boolean literal.
1753+
/// A boolean literal (`true`, `false`).
17561754
Bool(bool),
17571755
/// Placeholder for a literal that wasn't well-formed in some way.
17581756
Err,

‎compiler/rustc_ast/src/attr/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl MetaItemKind {
533533
MetaItemKind::NameValue(lit) => {
534534
let expr = P(ast::Expr {
535535
id: ast::DUMMY_NODE_ID,
536-
kind: ast::ExprKind::Lit(lit.clone()),
536+
kind: ast::ExprKind::Lit(lit.token_lit.clone()),
537537
span: lit.span,
538538
attrs: ast::AttrVec::new(),
539539
tokens: None,
@@ -605,7 +605,7 @@ impl MetaItemKind {
605605
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
606606
}
607607
Some(TokenTree::Token(token, _)) => {
608-
Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
608+
Lit::from_token(&token).map(MetaItemKind::NameValue)
609609
}
610610
_ => None,
611611
}
@@ -618,8 +618,10 @@ impl MetaItemKind {
618618
MetaItemKind::list_from_tokens(tokens.clone())
619619
}
620620
MacArgs::Delimited(..) => None,
621-
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => match &expr.kind {
622-
ast::ExprKind::Lit(lit) => Some(MetaItemKind::NameValue(lit.clone())),
621+
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => match expr.kind {
622+
ast::ExprKind::Lit(token_lit) => Some(MetaItemKind::NameValue(
623+
Lit::from_token_lit(token_lit, expr.span).expect("token_lit in from_mac_args"),
624+
)),
623625
_ => None,
624626
},
625627
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
@@ -668,7 +670,7 @@ impl NestedMetaItem {
668670
{
669671
match tokens.peek() {
670672
Some(TokenTree::Token(token, _))
671-
if let Ok(lit) = Lit::from_token(token) =>
673+
if let Some(lit) = Lit::from_token(token) =>
672674
{
673675
tokens.next();
674676
return Some(NestedMetaItem::Literal(lit));

0 commit comments

Comments
 (0)