diff options
| author | bors <bors@rust-lang.org> | 2019-06-08 23:17:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-06-08 23:17:06 +0000 |
| commit | 053666f9062d71091ea7970dcbad5963097191a3 (patch) | |
| tree | fa334bba0a1d0e0b7546d5e3a2c51242d0245e27 /src/libsyntax/ext | |
| parent | 991c719a1d0f95c37ed7ea56bdb38bcc2a6246b9 (diff) | |
| parent | e0c825cc72e3d7834e3493e12e673c25606c8293 (diff) | |
Auto merge of #61672 - Centril:rollup-jxo89ir, r=Centril
Rollup of 6 pull requests Successful merges: - #61646 (Remove useless allocations in macro_rules follow logic.) - #61658 (remove useless ident() functions in const tests) - #61660 (Minimize use of `#![feature(custom_attribute)]`) - #61666 (Add test for trait ICE) - #61669 ( syntax: Remove `Deref` impl from `Token`) - #61670 (Update RLS) Failed merges: r? @ghost
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 | 64 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/quoted.rs | 34 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/transcribe.rs | 21 |
4 files changed, 66 insertions, 81 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 4998129fdee..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 @@ -909,7 +909,7 @@ fn check_matcher_core(sess: &ParseSess, continue 'each_last; } IsInFollow::Yes => {} - IsInFollow::No(ref possible) => { + IsInFollow::No(possible) => { let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1 { @@ -933,7 +933,7 @@ fn check_matcher_core(sess: &ParseSess, format!("not allowed after `{}` fragments", frag_spec), ); let msg = "allowed there are: "; - match &possible[..] { + match possible { &[] => {} &[t] => { err.note(&format!( @@ -997,7 +997,7 @@ fn frag_can_be_followed_by_any(frag: &str) -> bool { enum IsInFollow { Yes, - No(Vec<&'static str>), + No(&'static [&'static str]), Invalid(String, &'static str), } @@ -1029,28 +1029,28 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { IsInFollow::Yes }, "stmt" | "expr" => { - let tokens = vec!["`=>`", "`,`", "`;`"]; + const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"]; match tok { TokenTree::Token(token) => match token.kind { FatArrow | Comma | Semi => IsInFollow::Yes, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), }, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), } }, "pat" => { - let tokens = vec!["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"]; + const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"]; match tok { TokenTree::Token(token) => match token.kind { FatArrow | Comma | Eq | BinOp(token::Or) => IsInFollow::Yes, Ident(name, false) if name == kw::If || name == kw::In => IsInFollow::Yes, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), }, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), } }, "path" | "ty" => { - let tokens = vec![ + const TOKENS: &[&str] = &[ "`{`", "`[`", "`=>`", "`,`", "`>`","`=`", "`:`", "`;`", "`|`", "`as`", "`where`", ]; @@ -1062,11 +1062,11 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { BinOp(token::Or) => IsInFollow::Yes, Ident(name, false) if name == kw::As || name == kw::Where => IsInFollow::Yes, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), }, TokenTree::MetaVarDecl(_, _, frag) if frag.name == sym::block => IsInFollow::Yes, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), } }, "ident" | "lifetime" => { @@ -1084,7 +1084,7 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { }, "vis" => { // Explicitly disallow `priv`, on the off chance it comes back. - let tokens = vec!["`,`", "an ident", "a type"]; + const TOKENS: &[&str] = &["`,`", "an ident", "a type"]; match tok { TokenTree::Token(token) => match token.kind { Comma => IsInFollow::Yes, @@ -1092,14 +1092,14 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { _ => if token.can_begin_type() { IsInFollow::Yes } else { - IsInFollow::No(tokens) + IsInFollow::No(TOKENS) } }, TokenTree::MetaVarDecl(_, _, frag) if frag.name == sym::ident || frag.name == sym::ty || frag.name == sym::path => IsInFollow::Yes, - _ => IsInFollow::No(tokens), + _ => IsInFollow::No(TOKENS), } }, "" => IsInFollow::Yes, // kw::Invalid 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()); } } |
