From 28b125b83d9db4094a08b512a956c187bd29a51f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 9 May 2019 02:00:29 +0300 Subject: [PATCH] Turn `ast::Lit` into a struct --- src/librustc/ich/impls_syntax.rs | 6 +++++- src/libsyntax/ast.rs | 6 +++++- src/libsyntax/attr/mod.rs | 12 ++++++------ src/libsyntax/ext/build.rs | 10 +++++----- src/libsyntax/parse/parser.rs | 7 +++---- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 35df43ef25e..88a2c295a6e 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -162,7 +162,11 @@ fn hash_stable(&self, 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), diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index af2302d24f5..783792cf197 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1351,7 +1351,11 @@ pub enum StrStyle { } /// A literal. -pub type Lit = Spanned; +#[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 { diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index e00f91e3952..e331a263354 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -16,7 +16,7 @@ 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}; @@ -350,11 +350,11 @@ pub fn with_desugared_doc(&self, f: F) -> T where /* Constructors */ pub fn mk_name_value_item_str(ident: Ident, value: Spanned) -> 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) } } @@ -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, @@ -562,7 +562,7 @@ fn from_tokens(tokens: &mut iter::Peekable) -> Option 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 }; @@ -609,7 +609,7 @@ fn from_tokens(tokens: &mut iter::Peekable) -> Option 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 })); } } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 40dd187ed28..0fe85361b54 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -697,8 +697,8 @@ fn expr_struct_ident(&self, span: Span, self.expr_struct(span, self.path_ident(span, id), fields) } - fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P { - self.expr(sp, ast::ExprKind::Lit(respan(sp, lit))) + fn expr_lit(&self, span: Span, node: ast::LitKind) -> P { + self.expr(span, ast::ExprKind::Lit(ast::Lit { node, span })) } fn expr_usize(&self, span: Span, i: usize) -> P { self.expr_lit(span, ast::LitKind::Int(i as u128, @@ -1164,10 +1164,10 @@ fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec) 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, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d97d1e2f0f4..2b30d2db95e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2140,15 +2140,14 @@ fn parse_lit_token(&mut self) -> PResult<'a, LitKind> { /// 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`). -- 2.44.0