Skip to content

Commit 281cc7d

Browse files
committed
Rename ast::Lit as ast::MetaItemLit.
1 parent 3a4a095 commit 281cc7d

File tree

18 files changed

+102
-83
lines changed

18 files changed

+102
-83
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ pub enum NestedMetaItem {
489489
/// A literal.
490490
///
491491
/// E.g., `"foo"`, `64`, `true`.
492-
Literal(Lit),
492+
Literal(MetaItemLit),
493493
}
494494

495495
/// A spanned compile-time attribute item.
@@ -518,7 +518,7 @@ pub enum MetaItemKind {
518518
/// Name value meta item.
519519
///
520520
/// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
521-
NameValue(Lit),
521+
NameValue(MetaItemLit),
522522
}
523523

524524
/// A block (`{ .. }`).
@@ -1571,12 +1571,12 @@ pub enum AttrArgs {
15711571
}
15721572

15731573
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
1574-
// expansion is completed, all cases end up either as a literal, which is the
1575-
// form used after lowering to HIR, or as an error.
1574+
// expansion is completed, all cases end up either as a meta item literal,
1575+
// which is the form used after lowering to HIR, or as an error.
15761576
#[derive(Clone, Encodable, Decodable, Debug)]
15771577
pub enum AttrArgsEq {
15781578
Ast(P<Expr>),
1579-
Hir(Lit),
1579+
Hir(MetaItemLit),
15801580
}
15811581

15821582
impl AttrArgs {
@@ -1698,14 +1698,13 @@ pub enum StrStyle {
16981698
Raw(u8),
16991699
}
17001700

1701-
/// An AST literal.
1701+
/// A literal in a meta item.
17021702
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1703-
pub struct Lit {
1703+
pub struct MetaItemLit {
17041704
/// The original literal token as written in source code.
17051705
pub token_lit: token::Lit,
17061706
/// The "semantic" representation of the literal lowered from the original tokens.
17071707
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
1708-
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
17091708
pub kind: LitKind,
17101709
pub span: Span,
17111710
}
@@ -1755,6 +1754,8 @@ pub enum LitFloatType {
17551754
Unsuffixed,
17561755
}
17571756

1757+
/// This type is used within both `ast::MetaItemLit` and `hir::Lit`.
1758+
///
17581759
/// Note that the entire literal (including the suffix) is considered when
17591760
/// deciding the `LitKind`. This means that float literals like `1f32` are
17601761
/// classified by this type as `Float`. This is different to `token::LitKind`
@@ -3068,9 +3069,9 @@ mod size_asserts {
30683069
static_assert_size!(Impl, 184);
30693070
static_assert_size!(Item, 184);
30703071
static_assert_size!(ItemKind, 112);
3071-
static_assert_size!(Lit, 48);
30723072
static_assert_size!(LitKind, 24);
30733073
static_assert_size!(Local, 72);
3074+
static_assert_size!(MetaItemLit, 48);
30743075
static_assert_size!(Param, 40);
30753076
static_assert_size!(Pat, 88);
30763077
static_assert_size!(Path, 24);

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::ast;
44
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
5-
use crate::ast::{DelimArgs, Lit, LitKind};
5+
use crate::ast::{DelimArgs, LitKind, MetaItemLit};
66
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
77
use crate::ast::{Path, PathSegment};
88
use crate::ptr::P;
@@ -50,8 +50,8 @@ impl NestedMetaItem {
5050
}
5151
}
5252

53-
/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
54-
pub fn literal(&self) -> Option<&Lit> {
53+
/// Returns the `MetaItemLit` if `self` is a `NestedMetaItem::Literal`s.
54+
pub fn literal(&self) -> Option<&MetaItemLit> {
5555
match self {
5656
NestedMetaItem::Literal(lit) => Some(lit),
5757
_ => None,
@@ -78,7 +78,7 @@ impl NestedMetaItem {
7878
}
7979

8080
/// Returns a name and single literal value tuple of the `MetaItem`.
81-
pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
81+
pub fn name_value_literal(&self) -> Option<(Symbol, &MetaItemLit)> {
8282
self.meta_item().and_then(|meta_item| {
8383
meta_item.meta_item_list().and_then(|meta_item_list| {
8484
if meta_item_list.len() == 1
@@ -177,7 +177,7 @@ impl MetaItem {
177177
// Example:
178178
// #[attribute(name = "value")]
179179
// ^^^^^^^^^^^^^^
180-
pub fn name_value_literal(&self) -> Option<&Lit> {
180+
pub fn name_value_literal(&self) -> Option<&MetaItemLit> {
181181
match &self.kind {
182182
MetaItemKind::NameValue(v) => Some(v),
183183
_ => None,
@@ -332,7 +332,7 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta
332332
}
333333

334334
pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
335-
let lit = Lit::from_lit_kind(lit_kind, lit_span);
335+
let lit = MetaItemLit::from_lit_kind(lit_kind, lit_span);
336336
let span = ident.span.to(lit_span);
337337
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
338338
}
@@ -602,7 +602,7 @@ impl MetaItemKind {
602602
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
603603
}
604604
Some(TokenTree::Token(token, _)) => {
605-
Lit::from_token(&token).map(MetaItemKind::NameValue)
605+
MetaItemLit::from_token(&token).map(MetaItemKind::NameValue)
606606
}
607607
_ => None,
608608
}
@@ -620,7 +620,7 @@ impl MetaItemKind {
620620
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
621621
ast::ExprKind::Lit(token_lit) => {
622622
// Turn failures to `None`, we'll get parse errors elsewhere.
623-
Lit::from_token_lit(token_lit, expr.span)
623+
MetaItemLit::from_token_lit(token_lit, expr.span)
624624
.ok()
625625
.map(|lit| MetaItemKind::NameValue(lit))
626626
}
@@ -672,7 +672,7 @@ impl NestedMetaItem {
672672
{
673673
match tokens.peek() {
674674
Some(TokenTree::Token(token, _))
675-
if let Some(lit) = Lit::from_token(token) =>
675+
if let Some(lit) = MetaItemLit::from_token(token) =>
676676
{
677677
tokens.next();
678678
return Some(NestedMetaItem::Literal(lit));

compiler/rustc_ast/src/util/literal.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Code related to parsing literals.
22
3-
use crate::ast::{self, Lit, LitKind};
3+
use crate::ast::{self, LitKind, MetaItemLit};
44
use crate::token::{self, Token};
55
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
66
use rustc_span::symbol::{kw, sym, Symbol};
@@ -195,26 +195,26 @@ impl LitKind {
195195
}
196196
}
197197

198-
impl Lit {
199-
/// Converts literal token into an AST literal.
200-
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<Lit, LitError> {
201-
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
198+
impl MetaItemLit {
199+
/// Converts token literal into a meta item literal.
200+
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<MetaItemLit, LitError> {
201+
Ok(MetaItemLit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
202202
}
203203

204-
/// Converts an arbitrary token into an AST literal.
205-
pub fn from_token(token: &Token) -> Option<Lit> {
204+
/// Converts an arbitrary token into meta item literal.
205+
pub fn from_token(token: &Token) -> Option<MetaItemLit> {
206206
token::Lit::from_token(token)
207-
.and_then(|token_lit| Lit::from_token_lit(token_lit, token.span).ok())
207+
.and_then(|token_lit| MetaItemLit::from_token_lit(token_lit, token.span).ok())
208208
}
209209

210-
/// Attempts to recover an AST literal from semantic literal.
210+
/// Attempts to create a meta item literal from a `LitKind`.
211211
/// This function is used when the original token doesn't exist (e.g. the literal is created
212212
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
213-
pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
214-
Lit { token_lit: kind.to_token_lit(), kind, span }
213+
pub fn from_lit_kind(kind: LitKind, span: Span) -> MetaItemLit {
214+
MetaItemLit { token_lit: kind.to_token_lit(), kind, span }
215215
}
216216

217-
/// Losslessly convert an AST literal into a token.
217+
/// Losslessly convert a meta item literal into a token.
218218
pub fn to_token(&self) -> Token {
219219
let kind = match self.token_lit.kind {
220220
token::Bool => token::Ident(self.token_lit.symbol, false),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,12 +941,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
941941
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
942942
// In valid code the value always ends up as a single literal. Otherwise, a dummy
943943
// literal suffices because the error is handled elsewhere.
944-
let lit = if let ExprKind::Lit(token_lit) = expr.kind
945-
&& let Ok(lit) = Lit::from_token_lit(token_lit, expr.span)
944+
let lit = if let ExprKind::Lit(token_lit) = expr.kind
945+
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
946946
{
947947
lit
948948
} else {
949-
Lit {
949+
MetaItemLit {
950950
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
951951
kind: LitKind::Err,
952952
span: DUMMY_SP,

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
371371
}
372372
}
373373

374-
fn print_literal(&mut self, lit: &ast::Lit) {
374+
fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
375375
self.print_token_literal(lit.token_lit, lit.span)
376376
}
377377

@@ -488,7 +488,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
488488
self.print_path(&item.path, false, 0);
489489
self.space();
490490
self.word_space("=");
491-
let token_str = self.literal_to_string(lit);
491+
let token_str = self.meta_item_lit_to_string(lit);
492492
self.word(token_str);
493493
}
494494
}
@@ -498,7 +498,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
498498
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
499499
match item {
500500
ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi),
501-
ast::NestedMetaItem::Literal(ref lit) => self.print_literal(lit),
501+
ast::NestedMetaItem::Literal(ref lit) => self.print_meta_item_lit(lit),
502502
}
503503
}
504504

@@ -510,7 +510,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
510510
self.print_path(&item.path, false, 0);
511511
self.space();
512512
self.word_space("=");
513-
self.print_literal(value);
513+
self.print_meta_item_lit(value);
514514
}
515515
ast::MetaItemKind::List(ref items) => {
516516
self.print_path(&item.path, false, 0);
@@ -825,8 +825,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
825825
Self::to_string(|s| s.print_expr(e))
826826
}
827827

828-
fn literal_to_string(&self, lit: &ast::Lit) -> String {
829-
Self::to_string(|s| s.print_literal(lit))
828+
fn meta_item_lit_to_string(&self, lit: &ast::MetaItemLit) -> String {
829+
Self::to_string(|s| s.print_meta_item_lit(lit))
830830
}
831831

832832
fn tt_to_string(&self, tt: &TokenTree) -> String {

compiler/rustc_attr/src/builtin.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Parsing and validation of builtin attributes
22
33
use rustc_ast as ast;
4-
use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId};
4+
use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId};
55
use rustc_ast_pretty::pprust;
66
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
77
use rustc_macros::HashStable_Generic;
@@ -658,11 +658,13 @@ pub fn eval_condition(
658658
ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => {
659659
try_gate_cfg(sym::version, cfg.span, sess, features);
660660
let (min_version, span) = match &mis[..] {
661-
[NestedMetaItem::Literal(Lit { kind: LitKind::Str(sym, ..), span, .. })] => {
662-
(sym, span)
663-
}
664661
[
665-
NestedMetaItem::Literal(Lit { span, .. })
662+
NestedMetaItem::Literal(MetaItemLit {
663+
kind: LitKind::Str(sym, ..), span, ..
664+
}),
665+
] => (sym, span),
666+
[
667+
NestedMetaItem::Literal(MetaItemLit { span, .. })
666668
| NestedMetaItem::MetaItem(MetaItem { span, .. }),
667669
] => {
668670
sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl MultiItemModifier for Expander {
5050
NestedMetaItem::MetaItem(meta) => Some(meta),
5151
NestedMetaItem::Literal(lit) => {
5252
// Reject `#[derive("Debug")]`.
53-
report_unexpected_literal(sess, &lit);
53+
report_unexpected_meta_item_lit(sess, &lit);
5454
None
5555
}
5656
})
@@ -127,7 +127,7 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
127127
bad_target
128128
}
129129

130-
fn report_unexpected_literal(sess: &Session, lit: &ast::Lit) {
130+
fn report_unexpected_meta_item_lit(sess: &Session, lit: &ast::MetaItemLit) {
131131
let help_msg = match lit.token_lit.kind {
132132
token::Str if rustc_lexer::is_ident(lit.token_lit.symbol.as_str()) => {
133133
format!("try using `#[derive({})]`", lit.token_lit.symbol)

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,7 @@ impl server::TokenStream for Rustc<'_, '_> {
527527
}
528528
ast::ExprKind::IncludedBytes(bytes) => {
529529
let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit();
530-
Ok(tokenstream::TokenStream::token_alone(
531-
token::TokenKind::Literal(lit),
532-
expr.span,
533-
))
530+
Ok(tokenstream::TokenStream::token_alone(token::TokenKind::Literal(lit), expr.span))
534531
}
535532
ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind {
536533
ast::ExprKind::Lit(token_lit) => match token_lit {

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
21462146
}
21472147

21482148
fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
2149-
use rustc_ast::{Lit, LitIntType, LitKind};
2149+
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
21502150
if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" {
21512151
feature_err(
21522152
&tcx.sess.parse_sess,
@@ -2169,7 +2169,9 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
21692169
}
21702170
_ => None,
21712171
};
2172-
if let Some(Lit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list {
2172+
if let Some(MetaItemLit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) =
2173+
sole_meta_list
2174+
{
21732175
// According to the table at https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header,
21742176
// the ordinal must fit into 16 bits. Similarly, the Ordinal field in COFFShortExport (defined
21752177
// in llvm/include/llvm/Object/COFFImportFile.h), which we use to communicate import information

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,9 @@ impl<'tcx> TyCtxt<'tcx> {
11871187
debug!("layout_scalar_valid_range: attr={:?}", attr);
11881188
if let Some(
11891189
&[
1190-
ast::NestedMetaItem::Literal(ast::Lit {
1191-
kind: ast::LitKind::Int(a, _), ..
1190+
ast::NestedMetaItem::Literal(ast::MetaItemLit {
1191+
kind: ast::LitKind::Int(a, _),
1192+
..
11921193
}),
11931194
],
11941195
) = attr.meta_item_list().as_deref()

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ impl<'a> Parser<'a> {
315315
Ok(attrs)
316316
}
317317

318-
pub(crate) fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> {
319-
let lit = self.parse_ast_lit()?;
318+
// Note: must be unsuffixed.
319+
pub(crate) fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'a, ast::MetaItemLit> {
320+
let lit = self.parse_meta_item_lit()?;
320321
debug!("checking if {:?} is unsuffixed", lit);
321322

322323
if !lit.kind.is_unsuffixed() {
@@ -391,7 +392,7 @@ impl<'a> Parser<'a> {
391392

392393
pub(crate) fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
393394
Ok(if self.eat(&token::Eq) {
394-
ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
395+
ast::MetaItemKind::NameValue(self.parse_unsuffixed_meta_item_lit()?)
395396
} else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
396397
// Matches `meta_seq = ( COMMASEP(meta_item_inner) )`.
397398
let (list, _) = self.parse_paren_comma_seq(|p| p.parse_meta_item_inner())?;
@@ -403,7 +404,7 @@ impl<'a> Parser<'a> {
403404

404405
/// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`.
405406
fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
406-
match self.parse_unsuffixed_lit() {
407+
match self.parse_unsuffixed_meta_item_lit() {
407408
Ok(lit) => return Ok(ast::NestedMetaItem::Literal(lit)),
408409
Err(err) => err.cancel(),
409410
}

0 commit comments

Comments
 (0)