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
This commit is contained in:
bors 2018-01-01 07:21:23 +00:00
commit 1bcc6dc7ea
15 changed files with 231 additions and 21 deletions

View file

@ -0,0 +1,14 @@
# `macro_lifetime_matcher`
The tracking issue for this feature is: [#46895]
With this feature gate enabled, the [list of fragment specifiers][frags] gains one more entry:
* `lifetime`: a lifetime. Examples: 'static, 'a.
A `lifetime` variable may be followed by anything.
[#46895]: https://github.com/rust-lang/rust/issues/46895
[frags]: ../book/first-edition/macros.html#syntactic-requirements
------------------------

View file

@ -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] {

View file

@ -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")

View file

@ -768,10 +768,11 @@ fn token_can_be_followed_by_any(tok: &quoted::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: &quoted::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",

View file

@ -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.";

View file

@ -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)),
}
}

View file

@ -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")
}
}

View file

@ -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,12 +314,24 @@ impl Token {
false
}
/// 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(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 {
match *self {
Lifetime(..) => true,
_ => false,
}
self.lifetime(syntax_pos::DUMMY_SP).is_some()
}
/// Returns `true` if the token is either the `mut` or `const` keyword.
@ -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(..)"),
}
}
}

View file

@ -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),
}
}
}

View file

@ -0,0 +1,25 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(macro_lifetime_matcher)]
macro_rules! foo {
($l:lifetime, $l2:lifetime) => {
fn f<$l: $l2, $l2>(arg: &$l str, arg2: &$l2 str) -> &$l str {
arg
}
}
}
pub fn main() {
foo!('a, 'b);
let x: &'static str = f("hi", "there");
assert_eq!("hi", x);
}

View file

@ -0,0 +1,44 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(unreachable_code)]
#![feature(macro_lifetime_matcher)]
macro_rules! x {
($a:lifetime) => {
$a: loop {
break $a;
panic!("failed");
}
}
}
macro_rules! br {
($a:lifetime) => {
break $a;
}
}
macro_rules! br2 {
($b:lifetime) => {
'b: loop {
break $b; // this $b should refer to the outer loop.
}
}
}
fn main() {
x!('a);
'c: loop {
br!('c);
panic!("failed");
}
'b: loop {
br2!('b);
panic!("failed");
}
}

View file

@ -0,0 +1,25 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(macro_lifetime_matcher)]
macro_rules! foo {
($l:lifetime) => {
fn f(arg: &$l str) -> &$l str {
arg
}
}
}
pub fn main() {
foo!('static);
let x: &'static str = f("hi");
assert_eq!("hi", x);
}

View file

@ -0,0 +1,25 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(macro_lifetime_matcher)]
macro_rules! foo {
($l:lifetime) => {
fn f<$l>(arg: &$l str) -> &$l str {
arg
}
}
}
pub fn main() {
foo!('a);
let x: &'static str = f("hi");
assert_eq!("hi", x);
}

View file

@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that the :lifetime macro fragment cannot be used when macro_lifetime_matcher
// feature gate is not used.
macro_rules! m { ($lt:lifetime) => {} }
//~^ ERROR :lifetime fragment specifier is experimental and subject to change
fn main() {
m!('a);
}

View file

@ -0,0 +1,10 @@
error: :lifetime fragment specifier is experimental and subject to change (see issue #46895)
--> $DIR/feature-gate-macro-lifetime-matcher.rs:14:19
|
14 | macro_rules! m { ($lt:lifetime) => {} }
| ^^^^^^^^^^^^
|
= help: add #![feature(macro_lifetime_matcher)] to the crate attributes to enable
error: aborting due to previous error