Skip to content

Commit

Permalink
Rollup merge of #104804 - nnethercote:MetaItemLit, r=petrochenkov
Browse files Browse the repository at this point in the history
Rename `ast::Lit` as `ast::MetaItemLit`.

And some other literal cleanups.

r? `@petrochenkov`
  • Loading branch information
matthiaskrgr authored Nov 28, 2022
2 parents 8a09420 + a60e337 commit 63ec33e
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 128 deletions.
24 changes: 12 additions & 12 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
//! - [`EnumDef`] and [`Variant`]: Enum declaration.
//! - [`Lit`] and [`LitKind`]: Literal expressions.
//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions.
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation.
//! - [`Attribute`]: Metadata associated with item.
//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
Expand Down Expand Up @@ -489,7 +489,7 @@ pub enum NestedMetaItem {
/// A literal.
///
/// E.g., `"foo"`, `64`, `true`.
Literal(Lit),
Lit(MetaItemLit),
}

/// A spanned compile-time attribute item.
Expand Down Expand Up @@ -518,7 +518,7 @@ pub enum MetaItemKind {
/// Name value meta item.
///
/// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
NameValue(Lit),
NameValue(MetaItemLit),
}

/// A block (`{ .. }`).
Expand Down Expand Up @@ -1599,12 +1599,12 @@ pub enum AttrArgs {
}

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

impl AttrArgs {
Expand Down Expand Up @@ -1726,19 +1726,18 @@ pub enum StrStyle {
Raw(u8),
}

/// An AST literal.
/// A literal in a meta item.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub struct Lit {
pub struct MetaItemLit {
/// The original literal token as written in source code.
pub token_lit: token::Lit,
/// The "semantic" representation of the literal lowered from the original tokens.
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
pub kind: LitKind,
pub span: Span,
}

/// Same as `Lit`, but restricted to string literals.
/// Similar to `MetaItemLit`, but restricted to string literals.
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
pub struct StrLit {
/// The original literal token as written in source code.
Expand All @@ -1747,7 +1746,6 @@ pub struct StrLit {
pub suffix: Option<Symbol>,
pub span: Span,
/// The unescaped "semantic" representation of the literal lowered from the original token.
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
pub symbol_unescaped: Symbol,
}

Expand Down Expand Up @@ -1783,6 +1781,8 @@ pub enum LitFloatType {
Unsuffixed,
}

/// This type is used within both `ast::MetaItemLit` and `hir::Lit`.
///
/// Note that the entire literal (including the suffix) is considered when
/// deciding the `LitKind`. This means that float literals like `1f32` are
/// classified by this type as `Float`. This is different to `token::LitKind`
Expand Down Expand Up @@ -3096,9 +3096,9 @@ mod size_asserts {
static_assert_size!(Impl, 184);
static_assert_size!(Item, 184);
static_assert_size!(ItemKind, 112);
static_assert_size!(Lit, 48);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 72);
static_assert_size!(MetaItemLit, 48);
static_assert_size!(Param, 40);
static_assert_size!(Pat, 88);
static_assert_size!(Path, 24);
Expand Down
28 changes: 14 additions & 14 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::ast;
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
use crate::ast::{DelimArgs, Lit, LitKind};
use crate::ast::{DelimArgs, LitKind, MetaItemLit};
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
use crate::ast::{Path, PathSegment};
use crate::ptr::P;
Expand Down Expand Up @@ -50,10 +50,10 @@ impl NestedMetaItem {
}
}

/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
pub fn literal(&self) -> Option<&Lit> {
/// Returns the `MetaItemLit` if `self` is a `NestedMetaItem::Literal`s.
pub fn lit(&self) -> Option<&MetaItemLit> {
match self {
NestedMetaItem::Literal(lit) => Some(lit),
NestedMetaItem::Lit(lit) => Some(lit),
_ => None,
}
}
Expand All @@ -78,12 +78,12 @@ impl NestedMetaItem {
}

/// Returns a name and single literal value tuple of the `MetaItem`.
pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
pub fn name_value_literal(&self) -> Option<(Symbol, &MetaItemLit)> {
self.meta_item().and_then(|meta_item| {
meta_item.meta_item_list().and_then(|meta_item_list| {
if meta_item_list.len() == 1
&& let Some(ident) = meta_item.ident()
&& let Some(lit) = meta_item_list[0].literal()
&& let Some(lit) = meta_item_list[0].lit()
{
return Some((ident.name, lit));
}
Expand Down Expand Up @@ -179,7 +179,7 @@ impl MetaItem {
/// #[attribute(name = "value")]
/// ^^^^^^^^^^^^^^
/// ```
pub fn name_value_literal(&self) -> Option<&Lit> {
pub fn name_value_literal(&self) -> Option<&MetaItemLit> {
match &self.kind {
MetaItemKind::NameValue(v) => Some(v),
_ => None,
Expand Down Expand Up @@ -334,7 +334,7 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta
}

pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
let lit = Lit::from_lit_kind(lit_kind, lit_span);
let lit = MetaItemLit::from_lit_kind(lit_kind, lit_span);
let span = ident.span.to(lit_span);
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
}
Expand Down Expand Up @@ -604,7 +604,7 @@ impl MetaItemKind {
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
}
Some(TokenTree::Token(token, _)) => {
Lit::from_token(&token).map(MetaItemKind::NameValue)
MetaItemLit::from_token(&token).map(MetaItemKind::NameValue)
}
_ => None,
}
Expand All @@ -622,7 +622,7 @@ impl MetaItemKind {
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
ast::ExprKind::Lit(token_lit) => {
// Turn failures to `None`, we'll get parse errors elsewhere.
Lit::from_token_lit(token_lit, expr.span)
MetaItemLit::from_token_lit(token_lit, expr.span)
.ok()
.map(|lit| MetaItemKind::NameValue(lit))
}
Expand Down Expand Up @@ -655,14 +655,14 @@ impl NestedMetaItem {
pub fn span(&self) -> Span {
match self {
NestedMetaItem::MetaItem(item) => item.span,
NestedMetaItem::Literal(lit) => lit.span,
NestedMetaItem::Lit(lit) => lit.span,
}
}

fn token_trees(&self) -> Vec<TokenTree> {
match self {
NestedMetaItem::MetaItem(item) => item.token_trees(),
NestedMetaItem::Literal(lit) => {
NestedMetaItem::Lit(lit) => {
vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
}
}
Expand All @@ -674,10 +674,10 @@ impl NestedMetaItem {
{
match tokens.peek() {
Some(TokenTree::Token(token, _))
if let Some(lit) = Lit::from_token(token) =>
if let Some(lit) = MetaItemLit::from_token(token) =>
{
tokens.next();
return Some(NestedMetaItem::Literal(lit));
return Some(NestedMetaItem::Lit(lit));
}
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
let inner_tokens = inner_tokens.clone();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T
pub fn noop_visit_meta_list_item<T: MutVisitor>(li: &mut NestedMetaItem, vis: &mut T) {
match li {
NestedMetaItem::MetaItem(mi) => vis.visit_meta_item(mi),
NestedMetaItem::Literal(_lit) => {}
NestedMetaItem::Lit(_lit) => {}
}
}

Expand Down
32 changes: 12 additions & 20 deletions compiler/rustc_ast/src/util/literal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Code related to parsing literals.
use crate::ast::{self, Lit, LitKind};
use crate::ast::{self, LitKind, MetaItemLit};
use crate::token::{self, Token};
use rustc_data_structures::sync::Lrc;
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
Expand Down Expand Up @@ -196,33 +195,26 @@ impl LitKind {
}
}

impl Lit {
/// Converts literal token into an AST literal.
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<Lit, LitError> {
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
impl MetaItemLit {
/// Converts 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 })
}

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

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

/// Recovers an AST literal from a string of bytes produced by `include_bytes!`.
/// This requires ASCII-escaping the string, which can result in poor performance
/// for very large strings of bytes.
pub fn from_included_bytes(bytes: &Lrc<[u8]>, span: Span) -> Lit {
Self::from_lit_kind(LitKind::ByteStr(bytes.clone()), span)
}

/// Losslessly convert an AST literal into a token.
/// Losslessly convert a meta item literal into a token.
pub fn to_token(&self) -> Token {
let kind = match self.token_lit.kind {
token::Bool => token::Ident(self.token_lit.symbol, false),
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
// In valid code the value always ends up as a single literal. Otherwise, a dummy
// literal suffices because the error is handled elsewhere.
let lit = if let ExprKind::Lit(token_lit) = expr.kind {
match Lit::from_token_lit(token_lit, expr.span) {
Ok(lit) => lit,
Err(_err) => Lit {
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
kind: LitKind::Err,
span: DUMMY_SP,
},
}
let lit = if let ExprKind::Lit(token_lit) = expr.kind
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
{
lit
} else {
Lit {
MetaItemLit {
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
kind: LitKind::Err,
span: DUMMY_SP,
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

fn print_literal(&mut self, lit: &ast::Lit) {
fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
self.print_token_literal(lit.token_lit, lit.span)
}

Expand Down Expand Up @@ -488,7 +488,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.print_path(&item.path, false, 0);
self.space();
self.word_space("=");
let token_str = self.literal_to_string(lit);
let token_str = self.meta_item_lit_to_string(lit);
self.word(token_str);
}
}
Expand All @@ -498,7 +498,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
match item {
ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi),
ast::NestedMetaItem::Literal(ref lit) => self.print_literal(lit),
ast::NestedMetaItem::Lit(ref lit) => self.print_meta_item_lit(lit),
}
}

Expand All @@ -510,7 +510,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.print_path(&item.path, false, 0);
self.space();
self.word_space("=");
self.print_literal(value);
self.print_meta_item_lit(value);
}
ast::MetaItemKind::List(ref items) => {
self.print_path(&item.path, false, 0);
Expand Down Expand Up @@ -825,8 +825,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
Self::to_string(|s| s.print_expr(e))
}

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

fn tt_to_string(&self, tt: &TokenTree) -> String {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ impl<'a> State<'a> {
self.print_token_literal(token_lit, expr.span);
}
ast::ExprKind::IncludedBytes(ref bytes) => {
let lit = ast::Lit::from_included_bytes(bytes, expr.span);
self.print_literal(&lit)
let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit();
self.print_token_literal(lit, expr.span)
}
ast::ExprKind::Cast(ref expr, ref ty) => {
let prec = AssocOp::As.precedence() as i8;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Parsing and validation of builtin attributes
use rustc_ast as ast;
use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId};
use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId};
use rustc_ast_pretty::pprust;
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -486,7 +486,7 @@ where
continue 'outer;
}
},
NestedMetaItem::Literal(lit) => {
NestedMetaItem::Lit(lit) => {
handle_errors(
&sess.parse_sess,
lit.span,
Expand Down Expand Up @@ -658,11 +658,11 @@ pub fn eval_condition(
ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => {
try_gate_cfg(sym::version, cfg.span, sess, features);
let (min_version, span) = match &mis[..] {
[NestedMetaItem::Literal(Lit { kind: LitKind::Str(sym, ..), span, .. })] => {
[NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Str(sym, ..), span, .. })] => {
(sym, span)
}
[
NestedMetaItem::Literal(Lit { span, .. })
NestedMetaItem::Lit(MetaItemLit { span, .. })
| NestedMetaItem::MetaItem(MetaItem { span, .. }),
] => {
sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });
Expand Down Expand Up @@ -899,7 +899,7 @@ where
continue 'outer;
}
},
NestedMetaItem::Literal(lit) => {
NestedMetaItem::Lit(lit) => {
handle_errors(
&sess.parse_sess,
lit.span,
Expand Down
Loading

0 comments on commit 63ec33e

Please sign in to comment.