Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lifetimes in macros #46895

Merged
merged 9 commits into from
Jan 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

------------------------
6 changes: 6 additions & 0 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 21 additions & 8 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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" => {
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! (
Expand Down Expand Up @@ -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.";

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand Down Expand Up @@ -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")
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -524,6 +540,7 @@ pub enum Nonterminal {
NtGenerics(ast::Generics),
NtWhereClause(ast::WhereClause),
NtArg(ast::Arg),
NtLifetime(ast::Lifetime),
}

impl fmt::Debug for Nonterminal {
Expand All @@ -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(..)"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/macro-lifetime-used-with-bound.rs
Original file line number Diff line number Diff line change
@@ -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);
}
44 changes: 44 additions & 0 deletions src/test/run-pass/macro-lifetime-used-with-labels.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
25 changes: 25 additions & 0 deletions src/test/run-pass/macro-lifetime-used-with-static.rs
Original file line number Diff line number Diff line change
@@ -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);
}
25 changes: 25 additions & 0 deletions src/test/run-pass/macro-lifetime.rs
Original file line number Diff line number Diff line change
@@ -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);
}
19 changes: 19 additions & 0 deletions src/test/ui/feature-gate-macro-lifetime-matcher.rs
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions src/test/ui/feature-gate-macro-lifetime-matcher.stderr
Original file line number Diff line number Diff line change
@@ -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