Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Turn ast::Lit into a struct
  • Loading branch information
petrochenkov committed May 11, 2019
1 parent b8e0d0a commit 28b125b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/librustc/ich/impls_syntax.rs
Expand Up @@ -162,7 +162,11 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
Unsuffixed
});

impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
impl_stable_hash_for!(struct ::syntax::ast::Lit {
node,
span
});

impl_stable_hash_for!(enum ::syntax::ast::LitKind {
Str(value, style),
Err(value),
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/ast.rs
Expand Up @@ -1351,7 +1351,11 @@ pub enum StrStyle {
}

/// A literal.
pub type Lit = Spanned<LitKind>;
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)]
pub struct Lit {
pub node: LitKind,
pub span: Span,
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq)]
pub enum LitIntType {
Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/attr/mod.rs
Expand Up @@ -16,7 +16,7 @@ use crate::ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
use crate::ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
use crate::mut_visit::visit_clobber;
use crate::source_map::{BytePos, Spanned, respan, dummy_spanned};
use crate::source_map::{BytePos, Spanned, dummy_spanned};
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use crate::parse::parser::Parser;
use crate::parse::{self, ParseSess, PResult};
Expand Down Expand Up @@ -350,11 +350,11 @@ impl Attribute {
/* Constructors */

pub fn mk_name_value_item_str(ident: Ident, value: Spanned<Symbol>) -> MetaItem {
let value = respan(value.span, LitKind::Str(value.node, ast::StrStyle::Cooked));
let value = Lit { node: LitKind::Str(value.node, ast::StrStyle::Cooked), span: value.span };
mk_name_value_item(ident.span.to(value.span), ident, value)
}

pub fn mk_name_value_item(span: Span, ident: Ident, value: ast::Lit) -> MetaItem {
pub fn mk_name_value_item(span: Span, ident: Ident, value: Lit) -> MetaItem {
MetaItem { path: Path::from_ident(ident), span, node: MetaItemKind::NameValue(value) }
}

Expand Down Expand Up @@ -417,7 +417,7 @@ pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute

pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
let style = doc_comment_style(&text.as_str());
let lit = respan(span, LitKind::Str(text, ast::StrStyle::Cooked));
let lit = Lit { node: LitKind::Str(text, ast::StrStyle::Cooked), span };
Attribute {
id,
style,
Expand Down Expand Up @@ -562,7 +562,7 @@ impl MetaItemKind {
tokens.next();
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
LitKind::from_token(token)
.map(|lit| MetaItemKind::NameValue(Spanned { node: lit, span: span }))
.map(|node| MetaItemKind::NameValue(Lit { node, span }))
} else {
None
};
Expand Down Expand Up @@ -609,7 +609,7 @@ impl NestedMetaItem {
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
if let Some(node) = LitKind::from_token(token) {
tokens.next();
return Some(NestedMetaItem::Literal(respan(span, node)));
return Some(NestedMetaItem::Literal(Lit { node, span }));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/build.rs
Expand Up @@ -697,8 +697,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
self.expr_struct(span, self.path_ident(span, id), fields)
}

fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Lit(respan(sp, lit)))
fn expr_lit(&self, span: Span, node: ast::LitKind) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Lit(ast::Lit { node, span }))
}
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
self.expr_lit(span, ast::LitKind::Int(i as u128,
Expand Down Expand Up @@ -1164,10 +1164,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
}

fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
fn meta_name_value(&self, span: Span, name: ast::Name, node: ast::LitKind)
-> ast::MetaItem {
attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
respan(sp, value))
attr::mk_name_value_item(span, Ident::with_empty_ctxt(name).with_span_pos(span),
ast::Lit { node, span })
}

fn item_use(&self, sp: Span,
Expand Down
7 changes: 3 additions & 4 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -2140,15 +2140,14 @@ impl<'a> Parser<'a> {
/// Matches `lit = true | false | token_lit`.
crate fn parse_lit(&mut self) -> PResult<'a, Lit> {
let lo = self.span;
let lit = if self.eat_keyword(keywords::True) {
let node = if self.eat_keyword(keywords::True) {
LitKind::Bool(true)
} else if self.eat_keyword(keywords::False) {
LitKind::Bool(false)
} else {
let lit = self.parse_lit_token()?;
lit
self.parse_lit_token()?
};
Ok(source_map::Spanned { node: lit, span: lo.to(self.prev_span) })
Ok(Lit { node, span: lo.to(self.prev_span) })
}

/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
Expand Down

0 comments on commit 28b125b

Please sign in to comment.