Skip to content

Commit 2fd364a

Browse files
committed
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.
1 parent a7f35c4 commit 2fd364a

File tree

9 files changed

+56
-23
lines changed

9 files changed

+56
-23
lines changed

compiler/rustc_ast/src/ast.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1733,8 +1733,10 @@ pub enum StrStyle {
17331733
/// A literal in a meta item.
17341734
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
17351735
pub struct MetaItemLit {
1736-
/// The original literal token as written in source code.
1737-
pub token_lit: token::Lit,
1736+
/// The original literal as written in the source code.
1737+
pub symbol: Symbol,
1738+
/// The original suffix as written in the source code.
1739+
pub suffix: Option<Symbol>,
17381740
/// The "semantic" representation of the literal lowered from the original tokens.
17391741
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
17401742
pub kind: LitKind,
@@ -3103,7 +3105,7 @@ mod size_asserts {
31033105
static_assert_size!(ItemKind, 112);
31043106
static_assert_size!(LitKind, 24);
31053107
static_assert_size!(Local, 72);
3106-
static_assert_size!(MetaItemLit, 48);
3108+
static_assert_size!(MetaItemLit, 40);
31073109
static_assert_size!(Param, 40);
31083110
static_assert_size!(Pat, 88);
31093111
static_assert_size!(Path, 24);

compiler/rustc_ast/src/attr/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta
328328
}
329329

330330
pub fn mk_name_value_item(ident: Ident, kind: LitKind, lit_span: Span) -> MetaItem {
331-
let lit = MetaItemLit { token_lit: kind.synthesize_token_lit(), kind, span: lit_span };
331+
let token_lit = kind.synthesize_token_lit();
332+
let lit =
333+
MetaItemLit { symbol: token_lit.symbol, suffix: token_lit.suffix, kind, span: lit_span };
332334
let span = ident.span.to(lit_span);
333335
MetaItem { path: Path::from_ident(ident), kind: MetaItemKind::NameValue(lit), span }
334336
}

compiler/rustc_ast/src/util/literal.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,32 @@ impl LitKind {
202202
}
203203

204204
impl MetaItemLit {
205-
/// Converts token literal into a meta item literal.
205+
/// Converts a token literal into a meta item literal.
206206
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<MetaItemLit, LitError> {
207-
Ok(MetaItemLit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
207+
Ok(MetaItemLit {
208+
symbol: token_lit.symbol,
209+
suffix: token_lit.suffix,
210+
kind: LitKind::from_token_lit(token_lit)?,
211+
span,
212+
})
213+
}
214+
215+
/// Cheaply converts a meta item literal into a token literal.
216+
pub fn as_token_lit(&self) -> token::Lit {
217+
let kind = match self.kind {
218+
LitKind::Bool(_) => token::Bool,
219+
LitKind::Str(_, ast::StrStyle::Cooked) => token::Str,
220+
LitKind::Str(_, ast::StrStyle::Raw(n)) => token::StrRaw(n),
221+
LitKind::ByteStr(_, ast::StrStyle::Cooked) => token::ByteStr,
222+
LitKind::ByteStr(_, ast::StrStyle::Raw(n)) => token::ByteStrRaw(n),
223+
LitKind::Byte(_) => token::Byte,
224+
LitKind::Char(_) => token::Char,
225+
LitKind::Int(..) => token::Integer,
226+
LitKind::Float(..) => token::Float,
227+
LitKind::Err => token::Err,
228+
};
229+
230+
token::Lit::new(kind, self.symbol, self.suffix)
208231
}
209232

210233
/// Converts an arbitrary token into meta item literal.

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
954954
lit
955955
} else {
956956
MetaItemLit {
957-
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
957+
symbol: kw::Empty,
958+
suffix: None,
958959
kind: LitKind::Err,
959960
span: DUMMY_SP,
960961
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
376376
}
377377

378378
fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
379-
self.print_token_literal(lit.token_lit, lit.span)
379+
self.print_token_literal(lit.as_token_lit(), lit.span)
380380
}
381381

382382
fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {

compiler/rustc_builtin_macros/src/derive.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cfg_eval::cfg_eval;
22

33
use rustc_ast as ast;
4-
use rustc_ast::{token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
4+
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
55
use rustc_errors::{struct_span_err, Applicability};
66
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
77
use rustc_feature::AttributeTemplate;
@@ -130,9 +130,11 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
130130
}
131131

132132
fn report_unexpected_meta_item_lit(sess: &Session, lit: &ast::MetaItemLit) {
133-
let help_msg = match lit.token_lit.kind {
134-
token::Str if rustc_lexer::is_ident(lit.token_lit.symbol.as_str()) => {
135-
format!("try using `#[derive({})]`", lit.token_lit.symbol)
133+
let help_msg = match lit.kind {
134+
ast::LitKind::Str(_, ast::StrStyle::Cooked)
135+
if rustc_lexer::is_ident(lit.symbol.as_str()) =>
136+
{
137+
format!("try using `#[derive({})]`", lit.symbol)
136138
}
137139
_ => "for example, write `#[derive(Debug)]` for `Debug`".to_string(),
138140
};

compiler/rustc_parse/src/parser/expr.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ impl<'a> Parser<'a> {
15511551
})
15521552
});
15531553
consume_colon = false;
1554-
Ok(self.mk_expr(lo, ExprKind::Lit(lit.token_lit)))
1554+
Ok(self.mk_expr(lo, ExprKind::Lit(lit.as_token_lit())))
15551555
} else if !ate_colon
15561556
&& (self.check_noexpect(&TokenKind::Comma) || self.check_noexpect(&TokenKind::Gt))
15571557
{
@@ -1654,7 +1654,8 @@ impl<'a> Parser<'a> {
16541654
}
16551655
let name = lifetime.without_first_quote().name;
16561656
ast::MetaItemLit {
1657-
token_lit: token::Lit::new(token::LitKind::Char, name, None),
1657+
symbol: name,
1658+
suffix: None,
16581659
kind: ast::LitKind::Char(name.as_str().chars().next().unwrap_or('_')),
16591660
span: lifetime.span,
16601661
}
@@ -1773,8 +1774,8 @@ impl<'a> Parser<'a> {
17731774
Some(lit) => match lit.kind {
17741775
ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
17751776
style,
1776-
symbol: lit.token_lit.symbol,
1777-
suffix: lit.token_lit.suffix,
1777+
symbol: lit.symbol,
1778+
suffix: lit.suffix,
17781779
span: lit.span,
17791780
symbol_unescaped,
17801781
}),
@@ -1817,7 +1818,7 @@ impl<'a> Parser<'a> {
18171818
pub(super) fn parse_token_lit(&mut self) -> PResult<'a, (token::Lit, Span)> {
18181819
self.parse_opt_token_lit()
18191820
.ok_or(())
1820-
.or_else(|()| self.handle_missing_lit().map(|lit| (lit.token_lit, lit.span)))
1821+
.or_else(|()| self.handle_missing_lit().map(|lit| (lit.as_token_lit(), lit.span)))
18211822
}
18221823

18231824
pub(super) fn parse_meta_item_lit(&mut self) -> PResult<'a, MetaItemLit> {

compiler/rustc_parse/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl<'a> Parser<'a> {
420420
err.span_label(self_.token.span, format!("expected {}", expected));
421421
err
422422
});
423-
PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit.token_lit)))
423+
PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit.as_token_lit())))
424424
} else {
425425
// Try to parse everything else as literal with optional minus
426426
match self.parse_literal_maybe_minus() {

src/tools/rustfmt/src/attr.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ impl Rewrite for ast::NestedMetaItem {
260260
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
261261
match self {
262262
ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
263-
ast::NestedMetaItem::Lit(ref l) => rewrite_literal(context, l.token_lit, l.span, shape),
263+
ast::NestedMetaItem::Lit(ref l) => {
264+
rewrite_literal(context, l.as_token_lit(), l.span, shape)
265+
}
264266
}
265267
}
266268
}
@@ -308,18 +310,18 @@ impl Rewrite for ast::MetaItem {
308310
}),
309311
)?
310312
}
311-
ast::MetaItemKind::NameValue(ref literal) => {
313+
ast::MetaItemKind::NameValue(ref lit) => {
312314
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
313315
// 3 = ` = `
314316
let lit_shape = shape.shrink_left(path.len() + 3)?;
315-
// `rewrite_literal` returns `None` when `literal` exceeds max
317+
// `rewrite_literal` returns `None` when `lit` exceeds max
316318
// width. Since a literal is basically unformattable unless it
317319
// is a string literal (and only if `format_strings` is set),
318320
// we might be better off ignoring the fact that the attribute
319321
// is longer than the max width and continue on formatting.
320322
// See #2479 for example.
321-
let value = rewrite_literal(context, literal.token_lit, literal.span, lit_shape)
322-
.unwrap_or_else(|| context.snippet(literal.span).to_owned());
323+
let value = rewrite_literal(context, lit.as_token_lit(), lit.span, lit_shape)
324+
.unwrap_or_else(|| context.snippet(lit.span).to_owned());
323325
format!("{} = {}", path, value)
324326
}
325327
})

0 commit comments

Comments
 (0)