diff --git a/src/doc/unstable-book/src/language-features/macro-lifetime-matcher.md b/src/doc/unstable-book/src/language-features/macro-lifetime-matcher.md new file mode 100644 index 0000000000000..5b585d7f041d2 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/macro-lifetime-matcher.md @@ -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 + +------------------------ diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 6141c38ab145d..59996d1e4a7b1 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 { + 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 2167b64e6103d..124477620c27f 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 5e58f003c2be7..d86603e94e9df 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 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 7ae360678cd50..c5de0da0979a1 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 9916b74aeb315..6682a3439f1b9 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -642,6 +642,7 @@ pub fn noop_fold_interpolated(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 a4aad81f5f5af..eae558f002a4c 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 05368c52d2c32..bd4f7f9853d76 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 { 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 8f619e8af765a..dd343a2384b1f 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), } } } diff --git a/src/test/run-pass/macro-lifetime-used-with-bound.rs b/src/test/run-pass/macro-lifetime-used-with-bound.rs new file mode 100644 index 0000000000000..b0c9280b6ce44 --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-bound.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/run-pass/macro-lifetime-used-with-labels.rs b/src/test/run-pass/macro-lifetime-used-with-labels.rs new file mode 100644 index 0000000000000..8a2d76e17df68 --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-labels.rs @@ -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 or the MIT license +// , 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"); + } +} diff --git a/src/test/run-pass/macro-lifetime-used-with-static.rs b/src/test/run-pass/macro-lifetime-used-with-static.rs new file mode 100644 index 0000000000000..468ee2e943657 --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-static.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/run-pass/macro-lifetime.rs b/src/test/run-pass/macro-lifetime.rs new file mode 100644 index 0000000000000..db521ca7f103f --- /dev/null +++ b/src/test/run-pass/macro-lifetime.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/ui/feature-gate-macro-lifetime-matcher.rs b/src/test/ui/feature-gate-macro-lifetime-matcher.rs new file mode 100644 index 0000000000000..0d107d283cdff --- /dev/null +++ b/src/test/ui/feature-gate-macro-lifetime-matcher.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/ui/feature-gate-macro-lifetime-matcher.stderr b/src/test/ui/feature-gate-macro-lifetime-matcher.stderr new file mode 100644 index 0000000000000..e78f7684cf2be --- /dev/null +++ b/src/test/ui/feature-gate-macro-lifetime-matcher.stderr @@ -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 +