diff options
| author | bors <bors@rust-lang.org> | 2018-01-01 07:21:23 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-01-01 07:21:23 +0000 |
| commit | 1bcc6dc7ea430b7d403a57f60e48b3c3cdfcf562 (patch) | |
| tree | 2073115c9aa906116a5698340385e1aacfca21b4 /src/libsyntax | |
| parent | f3ca88cff7dc397f409f561321ee3c4021c05436 (diff) | |
| parent | 8b4bdc2f3f753e0d0b00ecc892a813e9786621e9 (diff) | |
| download | rust-1bcc6dc7ea430b7d403a57f60e48b3c3cdfcf562.tar.gz rust-1bcc6dc7ea430b7d403a57f60e48b3c3cdfcf562.zip | |
Auto merge of #46895 - ricochet1k:macro-lifetimes, r=jseyfried
Allow lifetimes in macros This is a resurrection of PR #41927 which was a resurrection of #33135, which is intended to fix #34303. In short, this allows macros_rules! to use :lifetime as a matcher to match 'lifetimes. Still to do: - [x] Feature gate
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/quote.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_parser.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 29 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 18 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 28 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 1 |
8 files changed, 69 insertions, 21 deletions
diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 6141c38ab14..59996d1e4a7 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -190,6 +190,12 @@ pub mod rt { } } + impl ToTokens for ast::Lifetime { + fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> { + vec![TokenTree::Token(DUMMY_SP, token::Lifetime(self.ident))] + } + } + macro_rules! impl_to_tokens_slice { ($t: ty, $sep: expr) => { impl ToTokens for [$t] { diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 2167b64e610..124477620c2 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -603,6 +603,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { "path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))), "meta" => token::NtMeta(panictry!(p.parse_meta_item())), "vis" => token::NtVis(panictry!(p.parse_visibility(true))), + "lifetime" => token::NtLifetime(p.expect_lifetime()), // this is not supposed to happen, since it has been checked // when compiling the macro. _ => p.span_bug(sp, "invalid fragment specifier") diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 5e58f003c2b..d86603e94e9 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -768,10 +768,11 @@ fn token_can_be_followed_by_any(tok: "ed::TokenTree) -> bool { /// ANYTHING without fear of future compatibility hazards). fn frag_can_be_followed_by_any(frag: &str) -> bool { match frag { - "item" | // always terminated by `}` or `;` - "block" | // exactly one token tree - "ident" | // exactly one token tree - "meta" | // exactly one token tree + "item" | // always terminated by `}` or `;` + "block" | // exactly one token tree + "ident" | // exactly one token tree + "meta" | // exactly one token tree + "lifetime" | // exactly one token tree "tt" => // exactly one token tree true, @@ -832,8 +833,8 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> Result<bool, (String, &' TokenTree::MetaVarDecl(_, _, frag) if frag.name == "block" => Ok(true), _ => Ok(false), }, - "ident" => { - // being a single token, idents are harmless + "ident" | "lifetime" => { + // being a single token, idents and lifetimes are harmless Ok(true) }, "meta" | "tt" => { @@ -887,9 +888,21 @@ fn is_legal_fragment_specifier(sess: &ParseSess, match frag_name { "item" | "block" | "stmt" | "expr" | "pat" | "path" | "ty" | "ident" | "meta" | "tt" | "" => true, + "lifetime" => { + if !features.borrow().macro_lifetime_matcher && + !attr::contains_name(attrs, "allow_internal_unstable") { + let explain = feature_gate::EXPLAIN_LIFETIME_MATCHER; + emit_feature_err(sess, + "macro_lifetime_matcher", + frag_span, + GateIssue::Language, + explain); + } + true + }, "vis" => { - if !features.borrow().macro_vis_matcher - && !attr::contains_name(attrs, "allow_internal_unstable") { + if !features.borrow().macro_vis_matcher && + !attr::contains_name(attrs, "allow_internal_unstable") { let explain = feature_gate::EXPLAIN_VIS_MATCHER; emit_feature_err(sess, "macro_vis_matcher", diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 7ae360678cd..c5de0da0979 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -447,6 +447,9 @@ declare_features! ( // Termination trait in main (RFC 1937) (active, termination_trait, "1.24.0", Some(43301)), + + // Allows use of the :lifetime macro fragment specifier + (active, macro_lifetime_matcher, "1.24.0", Some(46895)), ); declare_features! ( @@ -1226,6 +1229,9 @@ pub const EXPLAIN_DERIVE_UNDERSCORE: &'static str = pub const EXPLAIN_VIS_MATCHER: &'static str = ":vis fragment specifier is experimental and subject to change"; +pub const EXPLAIN_LIFETIME_MATCHER: &'static str = + ":lifetime fragment specifier is experimental and subject to change"; + pub const EXPLAIN_PLACEMENT_IN: &'static str = "placement-in expression syntax is experimental and subject to change."; diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 9916b74aeb3..6682a3439f1 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -642,6 +642,7 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T) token::NtWhereClause(fld.fold_where_clause(where_clause)), token::NtArg(arg) => token::NtArg(fld.fold_arg(arg)), token::NtVis(vis) => token::NtVis(fld.fold_vis(vis)), + token::NtLifetime(lifetime) => token::NtLifetime(fld.fold_lifetime(lifetime)), } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1a2146509e3..812e3c4967a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1295,6 +1295,10 @@ impl<'a> Parser<'a> { fn get_label(&mut self) -> ast::Ident { match self.token { token::Lifetime(ref ident) => *ident, + token::Interpolated(ref nt) => match nt.0 { + token::NtLifetime(lifetime) => lifetime.ident, + _ => self.bug("not a lifetime"), + }, _ => self.bug("not a lifetime"), } } @@ -2031,14 +2035,12 @@ impl<'a> Parser<'a> { } /// Parse single lifetime 'a or panic. - fn expect_lifetime(&mut self) -> Lifetime { - match self.token { - token::Lifetime(ident) => { - let ident_span = self.span; - self.bump(); - Lifetime { ident: ident, span: ident_span, id: ast::DUMMY_NODE_ID } - } - _ => self.span_bug(self.span, "not a lifetime") + pub fn expect_lifetime(&mut self) -> Lifetime { + if let Some(lifetime) = self.token.lifetime(self.span) { + self.bump(); + lifetime + } else { + self.span_bug(self.span, "not a lifetime") } } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 05368c52d2c..bd4f7f9853d 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -251,7 +251,7 @@ impl Token { Lt | BinOp(Shl) | // associated path ModSep => true, // global path Interpolated(ref nt) => match nt.0 { - NtIdent(..) | NtTy(..) | NtPath(..) => true, + NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true, _ => false, }, _ => false, @@ -314,14 +314,26 @@ impl Token { false } - /// Returns `true` if the token is a lifetime. - pub fn is_lifetime(&self) -> bool { + /// Returns a lifetime with the span and a dummy id if it is a lifetime, + /// or the original lifetime if it is an interpolated lifetime, ignoring + /// the span. + pub fn lifetime(&self, span: Span) -> Option<ast::Lifetime> { match *self { - Lifetime(..) => true, - _ => false, + Lifetime(ident) => + Some(ast::Lifetime { ident: ident, span: span, id: ast::DUMMY_NODE_ID }), + Interpolated(ref nt) => match nt.0 { + NtLifetime(lifetime) => Some(lifetime), + _ => None, + }, + _ => None, } } + /// Returns `true` if the token is a lifetime. + pub fn is_lifetime(&self) -> bool { + self.lifetime(syntax_pos::DUMMY_SP).is_some() + } + /// Returns `true` if the token is either the `mut` or `const` keyword. pub fn is_mutability(&self) -> bool { self.is_keyword(keywords::Mut) || @@ -486,6 +498,10 @@ impl Token { let token = Token::Ident(ident.node); tokens = Some(TokenTree::Token(ident.span, token).into()); } + Nonterminal::NtLifetime(lifetime) => { + let token = Token::Lifetime(lifetime.ident); + tokens = Some(TokenTree::Token(lifetime.span, token).into()); + } Nonterminal::NtTT(ref tt) => { tokens = Some(tt.clone().into()); } @@ -524,6 +540,7 @@ pub enum Nonterminal { NtGenerics(ast::Generics), NtWhereClause(ast::WhereClause), NtArg(ast::Arg), + NtLifetime(ast::Lifetime), } impl fmt::Debug for Nonterminal { @@ -546,6 +563,7 @@ impl fmt::Debug for Nonterminal { NtWhereClause(..) => f.pad("NtWhereClause(..)"), NtArg(..) => f.pad("NtArg(..)"), NtVis(..) => f.pad("NtVis(..)"), + NtLifetime(..) => f.pad("NtLifetime(..)"), } } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 8f619e8af76..dd343a2384b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -279,6 +279,7 @@ pub fn token_to_string(tok: &Token) -> String { token::NtWhereClause(ref e) => where_clause_to_string(e), token::NtArg(ref e) => arg_to_string(e), token::NtVis(ref e) => vis_to_string(e), + token::NtLifetime(ref e) => lifetime_to_string(e), } } } |
