Skip to content

Commit e4a9150

Browse files
committed
Rename ast::Lit as ast::MetaItemLit.
1 parent aa10aad commit e4a9150

File tree

18 files changed

+103
-84
lines changed

18 files changed

+103
-84
lines changed

compiler/rustc_ast/src/ast.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
1414
//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
1515
//! - [`EnumDef`] and [`Variant`]: Enum declaration.
16-
//! - [`Lit`] and [`LitKind`]: Literal expressions.
16+
//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions.
1717
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation.
1818
//! - [`Attribute`]: Metadata associated with item.
1919
//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
@@ -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 (`{ .. }`).
@@ -1599,12 +1599,12 @@ pub enum AttrArgs {
15991599
}
16001600

16011601
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
1602-
// expansion is completed, all cases end up either as a literal, which is the
1603-
// form used after lowering to HIR, or as an error.
1602+
// expansion is completed, all cases end up either as a meta item literal,
1603+
// which is the form used after lowering to HIR, or as an error.
16041604
#[derive(Clone, Encodable, Decodable, Debug)]
16051605
pub enum AttrArgsEq {
16061606
Ast(P<Expr>),
1607-
Hir(Lit),
1607+
Hir(MetaItemLit),
16081608
}
16091609

16101610
impl AttrArgs {
@@ -1726,14 +1726,13 @@ pub enum StrStyle {
17261726
Raw(u8),
17271727
}
17281728

1729-
/// An AST literal.
1729+
/// A literal in a meta item.
17301730
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1731-
pub struct Lit {
1731+
pub struct MetaItemLit {
17321732
/// The original literal token as written in source code.
17331733
pub token_lit: token::Lit,
17341734
/// The "semantic" representation of the literal lowered from the original tokens.
17351735
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
1736-
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
17371736
pub kind: LitKind,
17381737
pub span: Span,
17391738
}
@@ -1783,6 +1782,8 @@ pub enum LitFloatType {
17831782
Unsuffixed,
17841783
}
17851784

1785+
/// This type is used within both `ast::MetaItemLit` and `hir::Lit`.
1786+
///
17861787
/// Note that the entire literal (including the suffix) is considered when
17871788
/// deciding the `LitKind`. This means that float literals like `1f32` are
17881789
/// classified by this type as `Float`. This is different to `token::LitKind`
@@ -3096,9 +3097,9 @@ mod size_asserts {
30963097
static_assert_size!(Impl, 184);
30973098
static_assert_size!(Item, 184);
30983099
static_assert_size!(ItemKind, 112);
3099-
static_assert_size!(Lit, 48);
31003100
static_assert_size!(LitKind, 24);
31013101
static_assert_size!(Local, 72);
3102+
static_assert_size!(MetaItemLit, 48);
31023103
static_assert_size!(Param, 40);
31033104
static_assert_size!(Pat, 88);
31043105
static_assert_size!(Path, 24);

compiler/rustc_ast/src/attr/mod.rs

+9-9
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
@@ -179,7 +179,7 @@ impl MetaItem {
179179
/// #[attribute(name = "value")]
180180
/// ^^^^^^^^^^^^^^
181181
/// ```
182-
pub fn name_value_literal(&self) -> Option<&Lit> {
182+
pub fn name_value_literal(&self) -> Option<&MetaItemLit> {
183183
match &self.kind {
184184
MetaItemKind::NameValue(v) => Some(v),
185185
_ => None,
@@ -334,7 +334,7 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta
334334
}
335335

336336
pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
337-
let lit = Lit::from_lit_kind(lit_kind, lit_span);
337+
let lit = MetaItemLit::from_lit_kind(lit_kind, lit_span);
338338
let span = ident.span.to(lit_span);
339339
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
340340
}
@@ -604,7 +604,7 @@ impl MetaItemKind {
604604
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
605605
}
606606
Some(TokenTree::Token(token, _)) => {
607-
Lit::from_token(&token).map(MetaItemKind::NameValue)
607+
MetaItemLit::from_token(&token).map(MetaItemKind::NameValue)
608608
}
609609
_ => None,
610610
}
@@ -622,7 +622,7 @@ impl MetaItemKind {
622622
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
623623
ast::ExprKind::Lit(token_lit) => {
624624
// Turn failures to `None`, we'll get parse errors elsewhere.
625-
Lit::from_token_lit(token_lit, expr.span)
625+
MetaItemLit::from_token_lit(token_lit, expr.span)
626626
.ok()
627627
.map(|lit| MetaItemKind::NameValue(lit))
628628
}
@@ -674,7 +674,7 @@ impl NestedMetaItem {
674674
{
675675
match tokens.peek() {
676676
Some(TokenTree::Token(token, _))
677-
if let Some(lit) = Lit::from_token(token) =>
677+
if let Some(lit) = MetaItemLit::from_token(token) =>
678678
{
679679
tokens.next();
680680
return Some(NestedMetaItem::Literal(lit));

compiler/rustc_ast/src/util/literal.rs

+12-12
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -948,12 +948,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
948948
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
949949
// In valid code the value always ends up as a single literal. Otherwise, a dummy
950950
// literal suffices because the error is handled elsewhere.
951-
let lit = if let ExprKind::Lit(token_lit) = expr.kind
952-
&& let Ok(lit) = Lit::from_token_lit(token_lit, expr.span)
951+
let lit = if let ExprKind::Lit(token_lit) = expr.kind
952+
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
953953
{
954954
lit
955955
} else {
956-
Lit {
956+
MetaItemLit {
957957
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
958958
kind: LitKind::Err,
959959
span: DUMMY_SP,

compiler/rustc_ast_pretty/src/pprust/state.rs

+6-6
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

+7-5
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

+2-2
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

+1-4
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
21452145
}
21462146

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

compiler/rustc_middle/src/ty/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,9 @@ impl<'tcx> TyCtxt<'tcx> {
11911191
debug!("layout_scalar_valid_range: attr={:?}", attr);
11921192
if let Some(
11931193
&[
1194-
ast::NestedMetaItem::Literal(ast::Lit {
1195-
kind: ast::LitKind::Int(a, _), ..
1194+
ast::NestedMetaItem::Literal(ast::MetaItemLit {
1195+
kind: ast::LitKind::Int(a, _),
1196+
..
11961197
}),
11971198
],
11981199
) = attr.meta_item_list().as_deref()

compiler/rustc_parse/src/parser/attr.rs

+5-4
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)