Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the span from ast::Lit. #101516

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ pub enum NestedMetaItem {
/// A literal.
///
/// E.g., `"foo"`, `64`, `true`.
Literal(Lit),
Literal(Lit, Span),
}

/// A spanned compile-time attribute item.
Expand Down Expand Up @@ -550,7 +550,9 @@ pub enum MetaItemKind {
/// Name value meta item.
///
/// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
NameValue(Lit),
///
/// The span is that of the literal on the RHS.
NameValue(Lit, Span),
}

/// A block (`{ .. }`).
Expand Down Expand Up @@ -1605,7 +1607,7 @@ pub enum MacArgs {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum MacArgsEq {
Ast(P<Expr>),
Hir(Lit),
Hir(Lit, Span),
}

impl MacArgs {
Expand All @@ -1621,7 +1623,7 @@ impl MacArgs {
MacArgs::Empty => None,
MacArgs::Delimited(dspan, ..) => Some(dspan.entire()),
MacArgs::Eq(eq_span, MacArgsEq::Ast(expr)) => Some(eq_span.to(expr.span)),
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
unreachable!("in literal form when getting span: {:?}", lit);
}
}
Expand All @@ -1634,7 +1636,7 @@ impl MacArgs {
MacArgs::Empty => TokenStream::default(),
MacArgs::Delimited(.., tokens) => tokens.clone(),
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => TokenStream::from_ast(expr),
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
unreachable!("in literal form when getting inner tokens: {:?}", lit)
}
}
Expand Down Expand Up @@ -1663,9 +1665,10 @@ where
MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => {
unreachable!("hash_stable {:?}", expr);
}
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit)) => {
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit, lit_span)) => {
eq_span.hash_stable(ctx, hasher);
lit.hash_stable(ctx, hasher);
lit_span.hash_stable(ctx, hasher);
}
}
}
Expand Down Expand Up @@ -1717,6 +1720,8 @@ pub enum StrStyle {
}

/// An AST literal.
///
/// Consult the enclosing type for the span (e.g. `ExprKind::Lit`).
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub struct Lit {
/// The original literal token as written in source code.
Expand All @@ -1725,7 +1730,6 @@ pub struct Lit {
/// 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.
Expand All @@ -1749,7 +1753,6 @@ impl StrLit {
};
Lit {
token_lit: token::Lit::new(token_kind, self.symbol, self.suffix),
span: self.span,
kind: LitKind::Str(self.symbol_unescaped, self.style),
}
}
Expand Down Expand Up @@ -3084,7 +3087,7 @@ mod size_asserts {
static_assert_size!(Impl, 200);
static_assert_size!(Item, 184);
static_assert_size!(ItemKind, 112);
static_assert_size!(Lit, 48);
static_assert_size!(Lit, 40);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 72);
static_assert_size!(Param, 40);
Expand Down
69 changes: 37 additions & 32 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl NestedMetaItem {
/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
pub fn literal(&self) -> Option<&Lit> {
match *self {
NestedMetaItem::Literal(ref lit) => Some(lit),
NestedMetaItem::Literal(ref lit, _) => Some(lit),
_ => None,
}
}
Expand Down Expand Up @@ -174,10 +174,24 @@ impl MetaItem {

// Example:
// #[attribute(name = "value")]
// ^^^^^^^^^^^^^^
// ^^^^^^^
pub fn name_value_literal(&self) -> Option<&Lit> {
match &self.kind {
MetaItemKind::NameValue(v) => Some(v),
MetaItemKind::NameValue(v, _) => Some(v),
_ => None,
}
}

pub fn name_value_literal_and_span(&self) -> Option<(&Lit, Span)> {
match &self.kind {
MetaItemKind::NameValue(v, v_span) => Some((v, *v_span)),
_ => None,
}
}

pub fn name_value_literal_span(&self) -> Option<Span> {
match self.kind {
MetaItemKind::NameValue(_, v_span) => Some(v_span),
_ => None,
}
}
Expand All @@ -200,17 +214,6 @@ impl MetaItem {
pub fn has_name(&self, name: Symbol) -> bool {
self.path == name
}

/// This is used in case you want the value span instead of the whole attribute. Example:
///
/// ```text
/// #[doc(alias = "foo")]
/// ```
///
/// In here, it'll return a span for `"foo"`.
pub fn name_value_literal_span(&self) -> Option<Span> {
Some(self.name_value_literal()?.span)
}
}

impl AttrItem {
Expand Down Expand Up @@ -320,9 +323,9 @@ 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 = Lit::from_lit_kind(lit_kind);
let span = ident.span.to(lit_span);
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit, lit_span) }
}

pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
Expand Down Expand Up @@ -458,7 +461,7 @@ impl MetaItem {
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
let kind = MetaItemKind::from_tokens(tokens)?;
let hi = match kind {
MetaItemKind::NameValue(ref lit) => lit.span.hi(),
MetaItemKind::NameValue(_, lit_span) => lit_span.hi(),
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
_ => path.span.hi(),
};
Expand All @@ -470,7 +473,7 @@ impl MetaItem {
impl MetaItemKind {
pub fn value_str(&self) -> Option<Symbol> {
match self {
MetaItemKind::NameValue(ref v) => match v.kind {
MetaItemKind::NameValue(ref v, _) => match v.kind {
LitKind::Str(ref s, _) => Some(*s),
_ => None,
},
Expand All @@ -481,11 +484,11 @@ impl MetaItemKind {
pub fn mac_args(&self, span: Span) -> MacArgs {
match self {
MetaItemKind::Word => MacArgs::Empty,
MetaItemKind::NameValue(lit) => {
MetaItemKind::NameValue(lit, lit_span) => {
let expr = P(ast::Expr {
id: ast::DUMMY_NODE_ID,
kind: ast::ExprKind::Lit(lit.clone()),
span: lit.span,
span: *lit_span,
attrs: ast::AttrVec::new(),
tokens: None,
});
Expand All @@ -511,10 +514,10 @@ impl MetaItemKind {
fn token_trees(&self, span: Span) -> Vec<TokenTree> {
match *self {
MetaItemKind::Word => vec![],
MetaItemKind::NameValue(ref lit) => {
MetaItemKind::NameValue(ref lit, lit_span) => {
vec![
TokenTree::token_alone(token::Eq, span),
TokenTree::Token(lit.to_token(), Spacing::Alone),
TokenTree::Token(lit.to_token(lit_span), Spacing::Alone),
]
}
MetaItemKind::List(ref list) => {
Expand Down Expand Up @@ -555,9 +558,9 @@ impl MetaItemKind {
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
}
Some(TokenTree::Token(token, _)) => {
Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
}
Some(TokenTree::Token(token, _)) => Lit::from_token(&token)
.ok()
.map(|(lit, lit_span)| MetaItemKind::NameValue(lit, lit_span)),
_ => None,
}
}
Expand All @@ -570,10 +573,12 @@ impl MetaItemKind {
}
MacArgs::Delimited(..) => None,
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => match &expr.kind {
ast::ExprKind::Lit(lit) => Some(MetaItemKind::NameValue(lit.clone())),
ast::ExprKind::Lit(lit) => Some(MetaItemKind::NameValue(lit.clone(), expr.span)),
_ => None,
},
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
MacArgs::Eq(_, MacArgsEq::Hir(lit, span)) => {
Some(MetaItemKind::NameValue(lit.clone(), *span))
}
}
}

Expand All @@ -600,15 +605,15 @@ impl NestedMetaItem {
pub fn span(&self) -> Span {
match *self {
NestedMetaItem::MetaItem(ref item) => item.span,
NestedMetaItem::Literal(ref lit) => lit.span,
NestedMetaItem::Literal(_, lit_span) => lit_span,
}
}

fn token_trees(&self) -> Vec<TokenTree> {
match *self {
NestedMetaItem::MetaItem(ref item) => item.token_trees(),
NestedMetaItem::Literal(ref lit) => {
vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
NestedMetaItem::Literal(ref lit, lit_span) => {
vec![TokenTree::Token(lit.to_token(lit_span), Spacing::Alone)]
}
}
}
Expand All @@ -619,10 +624,10 @@ impl NestedMetaItem {
{
match tokens.peek() {
Some(TokenTree::Token(token, _))
if let Ok(lit) = Lit::from_token(token) =>
if let Ok((lit, lit_span)) = Lit::from_token(token) =>
{
tokens.next();
return Some(NestedMetaItem::Literal(lit));
return Some(NestedMetaItem::Literal(lit, lit_span));
}
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
let inner_tokens = inner_tokens.clone();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub fn visit_mac_args<T: MutVisitor>(args: &mut MacArgs, vis: &mut T) {
vis.visit_span(eq_span);
vis.visit_expr(expr);
}
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
}
}
Expand Down Expand Up @@ -617,7 +617,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::Literal(_lit, _lit_span) => {}
}
}

Expand All @@ -626,7 +626,7 @@ pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) {
match kind {
MetaItemKind::Word => {}
MetaItemKind::List(mis) => visit_vec(mis, |mi| vis.visit_meta_list_item(mi)),
MetaItemKind::NameValue(_s) => {}
MetaItemKind::NameValue(_s, _) => {}
}
vis.visit_span(span);
}
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_ast/src/util/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ 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 })
pub fn from_token_lit(token_lit: token::Lit) -> Result<Lit, LitError> {
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)? })
}

/// Converts arbitrary token into an AST literal.
///
/// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
pub fn from_token(token: &Token) -> Result<Lit, LitError> {
pub fn from_token(token: &Token) -> Result<(Lit, Span), LitError> {
let lit = match token.uninterpolate().kind {
token::Ident(name, false) if name.is_bool_lit() => {
token::Lit::new(token::Bool, name, None)
Expand All @@ -226,30 +226,30 @@ impl Lit {
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt
&& let ast::ExprKind::Lit(lit) = &expr.kind
{
return Ok(lit.clone());
return Ok((lit.clone(), expr.span));
}
return Err(LitError::NotLiteral);
}
_ => return Err(LitError::NotLiteral),
};

Lit::from_token_lit(lit, token.span)
Ok((Lit::from_token_lit(lit)?, token.span))
}

/// Attempts to recover an AST literal from semantic literal.
/// 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) -> Lit {
Lit { token_lit: kind.to_token_lit(), kind }
}

/// Losslessly convert an AST literal into a token.
pub fn to_token(&self) -> Token {
pub fn to_token(&self, span: Span) -> Token {
let kind = match self.token_lit.kind {
token::Bool => token::Ident(self.token_lit.symbol, false),
_ => token::Literal(self.token_lit),
};
Token::new(kind, self.span)
Token::new(kind, span)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
MacArgs::Empty => {}
MacArgs::Delimited(_dspan, _delim, _tokens) => {}
MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => visitor.visit_expr(expr),
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
unreachable!("in literal form when walking mac args eq: {:?}", lit)
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::Unary(op, ohs)
}
ExprKind::Lit(ref l) => {
hir::ExprKind::Lit(respan(self.lower_span(l.span), l.kind.clone()))
hir::ExprKind::Lit(respan(self.lower_span(e.span), l.kind.clone()))
}
ExprKind::Cast(ref expr, ref ty) => {
let expr = self.lower_expr(expr);
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
Lit {
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
kind: LitKind::Err,
span: DUMMY_SP,
}
};
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit))
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit, expr.span))
}
MacArgs::Eq(_, MacArgsEq::Hir(ref lit)) => {
MacArgs::Eq(_, MacArgsEq::Hir(ref lit, _)) => {
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
}
}
Expand Down
Loading