diff options
| author | bors <bors@rust-lang.org> | 2019-06-07 06:52:09 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-06-07 06:52:09 +0000 |
| commit | ca1bcfdde3f19afd68ef808cecf2ce56d08d5df4 (patch) | |
| tree | 07a0d2ef9340fa064341cc697a8ae58e3762373a /src/libsyntax/attr | |
| parent | c5295ac64a8f2c7aee9cdd13b8fe00b82aff8435 (diff) | |
| parent | 3a31f0634bb1669eae64e83f595942986f867125 (diff) | |
| download | rust-ca1bcfdde3f19afd68ef808cecf2ce56d08d5df4.tar.gz rust-ca1bcfdde3f19afd68ef808cecf2ce56d08d5df4.zip | |
Auto merge of #61541 - petrochenkov:tsp, r=oli-obk
syntax: Keep token span as a part of `Token` In the world with proc macros and edition hygiene `Token` without a span is not self-contained. In practice this means that tokens and spans are always stored and passed somewhere along with each other. This PR combines them into a single struct by doing the next renaming/replacement: - `Token` -> `TokenKind` - `TokenAndSpan` -> `Token` - `(Token, Span)` -> `Token` Some later commits (https://github.com/rust-lang/rust/commit/fb6e2fe8fd6caed247857758c6c3549fe2b59527 and https://github.com/rust-lang/rust/commit/1cdee86940db892cd17239c26add5364335e895a) remove duplicate spans in `token::Ident` and `token::Lifetime`. Those spans were supposed to be identical to token spans, but could easily go out of sync, as was noticed in https://github.com/rust-lang/rust/pull/60965#discussion_r285398523. The `(Token, Span)` -> `Token` change is a soft pre-requisite for this de-duplication since it allows to avoid some larger churn (passing spans to most of functions classifying identifiers).
Diffstat (limited to 'src/libsyntax/attr')
| -rw-r--r-- | src/libsyntax/attr/mod.rs | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 48948e4d0d7..edfe097c72f 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -20,7 +20,7 @@ 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}; -use crate::parse::token::{self, Token}; +use crate::parse::token::{self, Token, TokenKind}; use crate::ptr::P; use crate::symbol::{sym, Symbol}; use crate::ThinVec; @@ -465,10 +465,10 @@ impl MetaItem { let mod_sep_span = Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt()); - idents.push(TokenTree::Token(mod_sep_span, Token::ModSep).into()); + idents.push(TokenTree::token(token::ModSep, mod_sep_span).into()); } - idents.push(TokenTree::Token(segment.ident.span, - Token::from_ast_ident(segment.ident)).into()); + idents.push(TokenTree::token(TokenKind::from_ast_ident(segment.ident), + segment.ident.span).into()); last_pos = segment.ident.span.hi(); } self.node.tokens(self.span).append_to_tree_and_joint_vec(&mut idents); @@ -480,26 +480,28 @@ impl MetaItem { { // FIXME: Share code with `parse_path`. let path = match tokens.next() { - Some(TokenTree::Token(span, token @ Token::Ident(..))) | - Some(TokenTree::Token(span, token @ Token::ModSep)) => 'arm: { - let mut segments = if let Token::Ident(ident, _) = token { - if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() { + Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span })) | + Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: { + let mut segments = if let token::Ident(name, _) = kind { + if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) + = tokens.peek() { tokens.next(); - vec![PathSegment::from_ident(ident.with_span_pos(span))] + vec![PathSegment::from_ident(Ident::new(name, span))] } else { - break 'arm Path::from_ident(ident.with_span_pos(span)); + break 'arm Path::from_ident(Ident::new(name, span)); } } else { vec![PathSegment::path_root(span)] }; loop { - if let Some(TokenTree::Token(span, - Token::Ident(ident, _))) = tokens.next() { - segments.push(PathSegment::from_ident(ident.with_span_pos(span))); + if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) + = tokens.next() { + segments.push(PathSegment::from_ident(Ident::new(name, span))); } else { return None; } - if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() { + if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) + = tokens.peek() { tokens.next(); } else { break; @@ -508,7 +510,7 @@ impl MetaItem { let span = span.with_hi(segments.last().unwrap().ident.span.hi()); Path { span, segments } } - Some(TokenTree::Token(_, Token::Interpolated(nt))) => match *nt { + Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt { token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident), token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()), token::Nonterminal::NtPath(ref path) => path.clone(), @@ -533,7 +535,7 @@ impl MetaItemKind { match *self { MetaItemKind::Word => TokenStream::empty(), MetaItemKind::NameValue(ref lit) => { - let mut vec = vec![TokenTree::Token(span, Token::Eq).into()]; + let mut vec = vec![TokenTree::token(token::Eq, span).into()]; lit.tokens().append_to_tree_and_joint_vec(&mut vec); TokenStream::new(vec) } @@ -541,7 +543,7 @@ impl MetaItemKind { let mut tokens = Vec::new(); for (i, item) in list.iter().enumerate() { if i > 0 { - tokens.push(TokenTree::Token(span, Token::Comma).into()); + tokens.push(TokenTree::token(token::Comma, span).into()); } item.tokens().append_to_tree_and_joint_vec(&mut tokens); } @@ -558,10 +560,10 @@ impl MetaItemKind { where I: Iterator<Item = TokenTree>, { let delimited = match tokens.peek().cloned() { - Some(TokenTree::Token(_, token::Eq)) => { + Some(TokenTree::Token(token)) if token == token::Eq => { tokens.next(); - return if let Some(TokenTree::Token(span, token)) = tokens.next() { - Lit::from_token(&token, span).ok().map(MetaItemKind::NameValue) + return if let Some(TokenTree::Token(token)) = tokens.next() { + Lit::from_token(&token).ok().map(MetaItemKind::NameValue) } else { None }; @@ -579,7 +581,7 @@ impl MetaItemKind { let item = NestedMetaItem::from_tokens(&mut tokens)?; result.push(item); match tokens.next() { - None | Some(TokenTree::Token(_, Token::Comma)) => {} + None | Some(TokenTree::Token(Token { kind: token::Comma, .. })) => {} _ => return None, } } @@ -605,8 +607,8 @@ impl NestedMetaItem { fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem> where I: Iterator<Item = TokenTree>, { - if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() { - if let Ok(lit) = Lit::from_token(&token, span) { + if let Some(TokenTree::Token(token)) = tokens.peek() { + if let Ok(lit) = Lit::from_token(token) { tokens.next(); return Some(NestedMetaItem::Literal(lit)); } |
