diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-06-09 00:20:38 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-09 00:20:38 +0200 |
| commit | 6b71fba9c1371a46e94917fabe042b6e3a14cf0a (patch) | |
| tree | 8ac3e87f2d3fec1a4cff83a44b5ed18148a62abf /src/libsyntax/ext | |
| parent | 18ca48d746c345f780914cc85d6049ff9a179635 (diff) | |
| parent | 9aaa7c770c976e35b1614f1f6bf120204c5727c8 (diff) | |
| download | rust-6b71fba9c1371a46e94917fabe042b6e3a14cf0a.tar.gz rust-6b71fba9c1371a46e94917fabe042b6e3a14cf0a.zip | |
Rollup merge of #61669 - petrochenkov:tokderef2, r=oli-obk
syntax: Remove `Deref` impl from `Token` Follow up to https://github.com/rust-lang/rust/pull/61541 r? @oli-obk
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/tt/macro_parser.rs | 28 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 34 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/quoted.rs | 34 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/transcribe.rs | 21 |
4 files changed, 51 insertions, 66 deletions
diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index f98e1433356..4758b6a50e5 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -78,7 +78,7 @@ use crate::ast::{Ident, Name}; use crate::ext::tt::quoted::{self, TokenTree}; use crate::parse::{Directory, ParseSess}; use crate::parse::parser::{Parser, PathStyle}; -use crate::parse::token::{self, DocComment, Nonterminal, Token, TokenKind}; +use crate::parse::token::{self, DocComment, Nonterminal, Token}; use crate::print::pprust; use crate::symbol::{kw, sym, Symbol}; use crate::tokenstream::{DelimSpan, TokenStream}; @@ -199,7 +199,7 @@ struct MatcherPos<'root, 'tt: 'root> { seq_op: Option<quoted::KleeneOp>, /// The separator if we are in a repetition. - sep: Option<TokenKind>, + sep: Option<Token>, /// The "parent" matcher position if we are in a repetition. That is, the matcher position just /// before we enter the sequence. @@ -417,24 +417,24 @@ fn nameize<I: Iterator<Item = NamedMatch>>( /// Generates an appropriate parsing failure message. For EOF, this is "unexpected end...". For /// other tokens, this is "unexpected token...". -pub fn parse_failure_msg(tok: TokenKind) -> String { - match tok { +pub fn parse_failure_msg(tok: &Token) -> String { + match tok.kind { token::Eof => "unexpected end of macro invocation".to_string(), _ => format!( "no rules expected the token `{}`", - pprust::token_to_string(&tok) + pprust::token_to_string(tok) ), } } /// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison) -fn token_name_eq(t1: &TokenKind, t2: &TokenKind) -> bool { - if let (Some((name1, is_raw1)), Some((name2, is_raw2))) = (t1.ident_name(), t2.ident_name()) { - name1 == name2 && is_raw1 == is_raw2 - } else if let (Some(name1), Some(name2)) = (t1.lifetime_name(), t2.lifetime_name()) { - name1 == name2 +fn token_name_eq(t1: &Token, t2: &Token) -> bool { + if let (Some((ident1, is_raw1)), Some((ident2, is_raw2))) = (t1.ident(), t2.ident()) { + ident1.name == ident2.name && is_raw1 == is_raw2 + } else if let (Some(ident1), Some(ident2)) = (t1.lifetime(), t2.lifetime()) { + ident1.name == ident2.name } else { - *t1 == *t2 + t1.kind == t2.kind } } @@ -712,7 +712,7 @@ pub fn parse( // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise, // either the parse is ambiguous (which should never happen) or there is a syntax error. - if token_name_eq(&parser.token, &token::Eof) { + if parser.token == token::Eof { if eof_items.len() == 1 { let matches = eof_items[0] .matches @@ -804,8 +804,8 @@ pub fn parse( /// The token is an identifier, but not `_`. /// We prohibit passing `_` to macros expecting `ident` for now. -fn get_macro_name(token: &TokenKind) -> Option<(Name, bool)> { - match *token { +fn get_macro_name(token: &Token) -> Option<(Name, bool)> { + match token.kind { token::Ident(name, is_raw) if name != kw::Underscore => Some((name, is_raw)), _ => None, } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index b9b93b954fe..6f82f509465 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -17,7 +17,7 @@ use crate::symbol::{Symbol, kw, sym}; use crate::tokenstream::{DelimSpan, TokenStream, TokenTree}; use errors::FatalError; -use syntax_pos::{Span, DUMMY_SP, symbol::Ident}; +use syntax_pos::{Span, symbol::Ident}; use log::debug; use rustc_data_structures::fx::{FxHashMap}; @@ -200,7 +200,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt<'_>, let (token, label) = best_failure.expect("ran no matchers"); let span = token.span.substitute_dummy(sp); - let mut err = cx.struct_span_err(span, &parse_failure_msg(token.kind)); + let mut err = cx.struct_span_err(span, &parse_failure_msg(&token)); err.span_label(span, label); if let Some(sp) = def_span { if cx.source_map().span_to_filename(sp).is_real() && !sp.is_dummy() { @@ -266,17 +266,19 @@ pub fn compile( let argument_gram = vec![ quoted::TokenTree::Sequence(DelimSpan::dummy(), Lrc::new(quoted::SequenceRepetition { tts: vec![ - quoted::TokenTree::MetaVarDecl(DUMMY_SP, lhs_nm, ast::Ident::from_str("tt")), - quoted::TokenTree::token(token::FatArrow, DUMMY_SP), - quoted::TokenTree::MetaVarDecl(DUMMY_SP, rhs_nm, ast::Ident::from_str("tt")), + quoted::TokenTree::MetaVarDecl(def.span, lhs_nm, ast::Ident::from_str("tt")), + quoted::TokenTree::token(token::FatArrow, def.span), + quoted::TokenTree::MetaVarDecl(def.span, rhs_nm, ast::Ident::from_str("tt")), ], - separator: Some(if body.legacy { token::Semi } else { token::Comma }), + separator: Some(Token::new( + if body.legacy { token::Semi } else { token::Comma }, def.span + )), op: quoted::KleeneOp::OneOrMore, num_captures: 2, })), // to phase into semicolon-termination instead of semicolon-separation quoted::TokenTree::Sequence(DelimSpan::dummy(), Lrc::new(quoted::SequenceRepetition { - tts: vec![quoted::TokenTree::token(token::Semi, DUMMY_SP)], + tts: vec![quoted::TokenTree::token(token::Semi, def.span)], separator: None, op: quoted::KleeneOp::ZeroOrMore, num_captures: 0 @@ -286,7 +288,7 @@ pub fn compile( let argument_map = match parse(sess, body.stream(), &argument_gram, None, true) { Success(m) => m, Failure(token, msg) => { - let s = parse_failure_msg(token.kind); + let s = parse_failure_msg(&token); let sp = token.span.substitute_dummy(def.span); let mut err = sess.span_diagnostic.struct_span_fatal(sp, &s); err.span_label(sp, msg); @@ -608,9 +610,8 @@ impl FirstSets { // If the sequence contents can be empty, then the first // token could be the separator token itself. - if let (Some(ref sep), true) = (seq_rep.separator.clone(), - subfirst.maybe_empty) { - first.add_one_maybe(TokenTree::token(sep.clone(), sp.entire())); + if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) { + first.add_one_maybe(TokenTree::Token(sep.clone())); } // Reverse scan: Sequence comes before `first`. @@ -658,9 +659,8 @@ impl FirstSets { // If the sequence contents can be empty, then the first // token could be the separator token itself. - if let (Some(ref sep), true) = (seq_rep.separator.clone(), - subfirst.maybe_empty) { - first.add_one_maybe(TokenTree::token(sep.clone(), sp.entire())); + if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) { + first.add_one_maybe(TokenTree::Token(sep.clone())); } assert!(first.maybe_empty); @@ -851,7 +851,7 @@ fn check_matcher_core(sess: &ParseSess, // against SUFFIX continue 'each_token; } - TokenTree::Sequence(sp, ref seq_rep) => { + TokenTree::Sequence(_, ref seq_rep) => { suffix_first = build_suffix_first(); // The trick here: when we check the interior, we want // to include the separator (if any) as a potential @@ -864,9 +864,9 @@ fn check_matcher_core(sess: &ParseSess, // work of cloning it? But then again, this way I may // get a "tighter" span? let mut new; - let my_suffix = if let Some(ref u) = seq_rep.separator { + let my_suffix = if let Some(sep) = &seq_rep.separator { new = suffix_first.clone(); - new.add_one_maybe(TokenTree::token(u.clone(), sp.entire())); + new.add_one_maybe(TokenTree::Token(sep.clone())); &new } else { &suffix_first diff --git a/src/libsyntax/ext/tt/quoted.rs b/src/libsyntax/ext/tt/quoted.rs index ec7d7f705d8..707fb65bcc5 100644 --- a/src/libsyntax/ext/tt/quoted.rs +++ b/src/libsyntax/ext/tt/quoted.rs @@ -23,16 +23,6 @@ pub struct Delimited { } impl Delimited { - /// Returns the opening delimiter (possibly `NoDelim`). - pub fn open_token(&self) -> TokenKind { - token::OpenDelim(self.delim) - } - - /// Returns the closing delimiter (possibly `NoDelim`). - pub fn close_token(&self) -> TokenKind { - token::CloseDelim(self.delim) - } - /// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter. pub fn open_tt(&self, span: Span) -> TokenTree { let open_span = if span.is_dummy() { @@ -40,7 +30,7 @@ impl Delimited { } else { span.with_lo(span.lo() + BytePos(self.delim.len() as u32)) }; - TokenTree::token(self.open_token(), open_span) + TokenTree::token(token::OpenDelim(self.delim), open_span) } /// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter. @@ -50,7 +40,7 @@ impl Delimited { } else { span.with_lo(span.hi() - BytePos(self.delim.len() as u32)) }; - TokenTree::token(self.close_token(), close_span) + TokenTree::token(token::CloseDelim(self.delim), close_span) } } @@ -59,7 +49,7 @@ pub struct SequenceRepetition { /// The sequence of token trees pub tts: Vec<TokenTree>, /// The optional separator - pub separator: Option<TokenKind>, + pub separator: Option<Token>, /// Whether the sequence can be repeated zero (*), or one or more times (+) pub op: KleeneOp, /// The number of `Match`s that appear in the sequence (and subsequences) @@ -282,7 +272,7 @@ where Some(tokenstream::TokenTree::Delimited(span, delim, tts)) => { // Must have `(` not `{` or `[` if delim != token::Paren { - let tok = pprust::token_to_string(&token::OpenDelim(delim)); + let tok = pprust::token_kind_to_string(&token::OpenDelim(delim)); let msg = format!("expected `(`, found `{}`", tok); sess.span_diagnostic.span_err(span.entire(), &msg); } @@ -371,8 +361,8 @@ where /// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return /// `None`. -fn kleene_op(token: &TokenKind) -> Option<KleeneOp> { - match *token { +fn kleene_op(token: &Token) -> Option<KleeneOp> { + match token.kind { token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore), token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore), token::Question => Some(KleeneOp::ZeroOrOne), @@ -424,7 +414,7 @@ fn parse_sep_and_kleene_op<I>( attrs: &[ast::Attribute], edition: Edition, macro_node_id: NodeId, -) -> (Option<TokenKind>, KleeneOp) +) -> (Option<Token>, KleeneOp) where I: Iterator<Item = tokenstream::TokenTree>, { @@ -449,7 +439,7 @@ fn parse_sep_and_kleene_op_2015<I>( _features: &Features, _attrs: &[ast::Attribute], macro_node_id: NodeId, -) -> (Option<TokenKind>, KleeneOp) +) -> (Option<Token>, KleeneOp) where I: Iterator<Item = tokenstream::TokenTree>, { @@ -502,7 +492,7 @@ where a hard error in an upcoming edition", ); - return (Some(token::Question), op); + return (Some(Token::new(token::Question, op1_span)), op); } // #2 is a random token (this is an error) :( @@ -541,7 +531,7 @@ where } // #2 is a KleeneOp :D - Ok(Ok((op, _))) => return (Some(token.kind), op), + Ok(Ok((op, _))) => return (Some(token), op), // #2 is a random token :( Ok(Err(token)) => token.span, @@ -567,7 +557,7 @@ fn parse_sep_and_kleene_op_2018<I>( sess: &ParseSess, _features: &Features, _attrs: &[ast::Attribute], -) -> (Option<TokenKind>, KleeneOp) +) -> (Option<Token>, KleeneOp) where I: Iterator<Item = tokenstream::TokenTree>, { @@ -596,7 +586,7 @@ where } // #2 is a KleeneOp :D - Ok(Ok((op, _))) => return (Some(token.kind), op), + Ok(Ok((op, _))) => return (Some(token), op), // #2 is a random token :( Ok(Err(token)) => token.span, diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 90a9cc8f34d..c51f4b20c31 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -4,11 +4,10 @@ use crate::ext::expand::Marker; use crate::ext::tt::macro_parser::{MatchedNonterminal, MatchedSeq, NamedMatch}; use crate::ext::tt::quoted; use crate::mut_visit::noop_visit_tt; -use crate::parse::token::{self, NtTT, TokenKind}; +use crate::parse::token::{self, NtTT, Token}; use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint}; use smallvec::{smallvec, SmallVec}; -use syntax_pos::DUMMY_SP; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::Lrc; @@ -18,7 +17,7 @@ use std::rc::Rc; /// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`). enum Frame { Delimited { forest: Lrc<quoted::Delimited>, idx: usize, span: DelimSpan }, - Sequence { forest: Lrc<quoted::SequenceRepetition>, idx: usize, sep: Option<TokenKind> }, + Sequence { forest: Lrc<quoted::SequenceRepetition>, idx: usize, sep: Option<Token> }, } impl Frame { @@ -109,17 +108,13 @@ pub fn transcribe( else { // Otherwise, if we have just reached the end of a sequence and we can keep repeating, // go back to the beginning of the sequence. - if let Frame::Sequence { ref mut idx, ref sep, .. } = *stack.last_mut().unwrap() { - let (ref mut repeat_idx, repeat_len) = *repeats.last_mut().unwrap(); + if let Frame::Sequence { idx, sep, .. } = stack.last_mut().unwrap() { + let (repeat_idx, repeat_len) = repeats.last_mut().unwrap(); *repeat_idx += 1; - if *repeat_idx < repeat_len { + if repeat_idx < repeat_len { *idx = 0; - if let Some(sep) = sep.clone() { - let prev_span = match result.last() { - Some((tt, _)) => tt.span(), - None => DUMMY_SP, - }; - result.push(TokenTree::token(sep, prev_span).into()); + if let Some(sep) = sep { + result.push(TokenTree::Token(sep.clone()).into()); } continue; } @@ -242,7 +237,7 @@ pub fn transcribe( Ident::new(ident.name, ident.span.apply_mark(cx.current_expansion.mark)); sp = sp.apply_mark(cx.current_expansion.mark); result.push(TokenTree::token(token::Dollar, sp).into()); - result.push(TokenTree::token(TokenKind::from_ast_ident(ident), sp).into()); + result.push(TokenTree::Token(Token::from_ast_ident(ident)).into()); } } |
