From 03a51019a40c1c5e2ab1635f993fc71517d1edc2 Mon Sep 17 00:00:00 2001 From: Michael Hewson Date: Wed, 10 May 2017 00:30:47 +0000 Subject: [PATCH 1/9] Resurrecting #33135 Started rebasing @sgrif's PR #33135 off of current master. (Well, actually merging it into a new branch based off current master.) The following files still need to be fixed or at least reviewed: - `src/libsyntax/ext/tt/macro_parser.rs`: calls `Parser::parse_lifetime`, which doesn't exist anymore - `src/libsyntax/parse/parser.rs`: @sgrif added an error message to `Parser::parse_lifetime`. Code has since been refactored, so I just took it out for now. - `src/libsyntax/ext/tt/transcribe.rs`: This code has been refactored bigtime. Not sure whether @sgrif's changes here are still necessary. Took it out for this commit. --- src/libsyntax/ext/quote.rs | 7 ++++++ src/libsyntax/ext/tt/macro_parser.rs | 1 + src/libsyntax/ext/tt/macro_rules.rs | 15 ++++++------ src/libsyntax/fold.rs | 1 + src/libsyntax/parse/token.rs | 2 ++ src/libsyntax/print/pprust.rs | 1 + .../macro-lifetime-used-with-bound.rs | 23 +++++++++++++++++++ .../macro-lifetime-used-with-static.rs | 23 +++++++++++++++++++ src/test/run-pass/macro-lifetime.rs | 23 +++++++++++++++++++ 9 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/test/run-pass/macro-lifetime-used-with-bound.rs create mode 100644 src/test/run-pass/macro-lifetime-used-with-static.rs create mode 100644 src/test/run-pass/macro-lifetime.rs diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 6141c38ab145d..a0d72b786bf8f 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -190,6 +190,13 @@ pub mod rt { } } + impl ToTokens for ast::Lifetime { + fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { + let lifetime_ident = ast::Ident::with_empty_ctxt(self.name); + vec![TokenTree::Token(DUMMY_SP, token::Lifetime(lifetime_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..91074ec473445 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(panictry!(p.parse_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..8f2e571c21340 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" => { @@ -885,7 +886,7 @@ fn is_legal_fragment_specifier(sess: &ParseSess, frag_name: &str, frag_span: Span) -> bool { match frag_name { - "item" | "block" | "stmt" | "expr" | "pat" | + "item" | "block" | "stmt" | "expr" | "pat" | "lifetime" | "path" | "ty" | "ident" | "meta" | "tt" | "" => true, "vis" => { if !features.borrow().macro_vis_matcher 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/token.rs b/src/libsyntax/parse/token.rs index 05368c52d2c32..a5766f5601a99 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -524,6 +524,7 @@ pub enum Nonterminal { NtGenerics(ast::Generics), NtWhereClause(ast::WhereClause), NtArg(ast::Arg), + NtLifetime(ast::Lifetime), } impl fmt::Debug for Nonterminal { @@ -546,6 +547,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..b9e1fde6b1f3e --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-bound.rs @@ -0,0 +1,23 @@ +// 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. + +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-static.rs b/src/test/run-pass/macro-lifetime-used-with-static.rs new file mode 100644 index 0000000000000..5c1f8683e00f6 --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-static.rs @@ -0,0 +1,23 @@ +// 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. + +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..ff5798ff78d62 --- /dev/null +++ b/src/test/run-pass/macro-lifetime.rs @@ -0,0 +1,23 @@ +// 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. + +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); +} From e12b87096a9c1856db9874c3af0bc64423b5477a Mon Sep 17 00:00:00 2001 From: Michael Hewson Date: Thu, 11 May 2017 22:59:48 +0000 Subject: [PATCH 2/9] replace parse_lifetime with expect_lifetime made `parser::Parser::expect_lifetime` public, so it can be called from `macro_parser::parse_nt` --- src/libsyntax/ext/tt/macro_parser.rs | 2 +- src/libsyntax/parse/parser.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 91074ec473445..124477620c27f 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -603,7 +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(panictry!(p.parse_lifetime())), + "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/parse/parser.rs b/src/libsyntax/parse/parser.rs index a4aad81f5f5af..29f0e2a0fee4e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2031,7 +2031,7 @@ impl<'a> Parser<'a> { } /// Parse single lifetime 'a or panic. - fn expect_lifetime(&mut self) -> Lifetime { + pub fn expect_lifetime(&mut self) -> Lifetime { match self.token { token::Lifetime(ident) => { let ident_span = self.span; From 0e53360af69bf9e71ef39e570655d73fc44339b9 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Wed, 20 Dec 2017 15:37:22 -0500 Subject: [PATCH 3/9] Fix build and add a macro lifetime labels test --- src/libsyntax/ext/quote.rs | 3 +- .../macro-lifetime-used-with-labels.rs | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/test/run-pass/macro-lifetime-used-with-labels.rs diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index a0d72b786bf8f..59996d1e4a7b1 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -192,8 +192,7 @@ pub mod rt { impl ToTokens for ast::Lifetime { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { - let lifetime_ident = ast::Ident::with_empty_ctxt(self.name); - vec![TokenTree::Token(DUMMY_SP, token::Lifetime(lifetime_ident))] + vec![TokenTree::Token(DUMMY_SP, token::Lifetime(self.ident))] } } 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..d003d7dcfb620 --- /dev/null +++ b/src/test/run-pass/macro-lifetime-used-with-labels.rs @@ -0,0 +1,43 @@ +// 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)] + +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"); + } +} From ce76b1a1f396dd27e8c73bd59333aef42308920b Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Wed, 20 Dec 2017 17:22:37 -0500 Subject: [PATCH 4/9] Fix tests --- src/libsyntax/parse/parser.rs | 24 +++++++++++++++++------- src/libsyntax/parse/token.rs | 12 ++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 29f0e2a0fee4e..8aa03e007e45e 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"), } } @@ -2032,14 +2036,20 @@ impl<'a> Parser<'a> { /// Parse single lifetime 'a or panic. pub 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 } + let lifetime = match self.token { + token::Lifetime(ident) => + Lifetime { ident: ident, span: self.span, id: ast::DUMMY_NODE_ID }, + token::Interpolated(ref nt) => match nt.0 { + token::NtLifetime(lifetime) => + lifetime, + //Lifetime { ident: lifetime.ident, span: lifetime.span, id: ast::DUMMY_NODE_ID }, + _ => self.span_bug(self.span, &format!("not a lifetime: {:?}", self.token)) } - _ => self.span_bug(self.span, "not a lifetime") - } + _ => self.span_bug(self.span, &format!("not a lifetime: {:?}", self.token)) + }; + + self.bump(); + lifetime } /// Parse mutability (`mut` or nothing). diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index a5766f5601a99..2d7ab938f7bb8 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, @@ -318,7 +318,11 @@ impl Token { pub fn is_lifetime(&self) -> bool { match *self { Lifetime(..) => true, - _ => false, + Interpolated(ref nt) => match nt.0 { + NtLifetime(..) => true, + _ => false, + }, + _ => false, } } @@ -486,6 +490,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()); } From e838cfce03caf9394fb71c909ab9e1afa8bae282 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 21 Dec 2017 08:45:44 -0500 Subject: [PATCH 5/9] Cleanup --- src/libsyntax/parse/parser.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8aa03e007e45e..7251af77dcc7e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2040,12 +2040,11 @@ impl<'a> Parser<'a> { token::Lifetime(ident) => Lifetime { ident: ident, span: self.span, id: ast::DUMMY_NODE_ID }, token::Interpolated(ref nt) => match nt.0 { - token::NtLifetime(lifetime) => + token::NtLifetime(lifetime) => lifetime, - //Lifetime { ident: lifetime.ident, span: lifetime.span, id: ast::DUMMY_NODE_ID }, - _ => self.span_bug(self.span, &format!("not a lifetime: {:?}", self.token)) + _ => self.span_bug(self.span, "not a lifetime") } - _ => self.span_bug(self.span, &format!("not a lifetime: {:?}", self.token)) + _ => self.span_bug(self.span, "not a lifetime") }; self.bump(); From b2844190640fc9a69d087e30e3c3cf37cbd40142 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 21 Dec 2017 10:44:44 -0500 Subject: [PATCH 6/9] Add feature gate macro_lifetime_matcher --- src/libsyntax/ext/tt/macro_rules.rs | 14 +++++++++++++- src/libsyntax/feature_gate.rs | 8 +++++++- .../macro-lifetime-used-with-bound.rs | 2 ++ .../macro-lifetime-used-with-labels.rs | 1 + .../macro-lifetime-used-with-static.rs | 2 ++ src/test/run-pass/macro-lifetime.rs | 2 ++ .../ui/feature-gate-macro-lifetime-matcher.rs | 19 +++++++++++++++++++ ...feature-gate-macro-lifetime-matcher.stderr | 10 ++++++++++ 8 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/feature-gate-macro-lifetime-matcher.rs create mode 100644 src/test/ui/feature-gate-macro-lifetime-matcher.stderr diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 8f2e571c21340..5412decd3b744 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -886,8 +886,20 @@ fn is_legal_fragment_specifier(sess: &ParseSess, frag_name: &str, frag_span: Span) -> bool { match frag_name { - "item" | "block" | "stmt" | "expr" | "pat" | "lifetime" | + "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") { diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 7ae360678cd50..471251b063009 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! ( @@ -520,7 +523,7 @@ declare_features! ( (accepted, loop_break_value, "1.19.0", Some(37339)), // Permits numeric fields in struct expressions and patterns. (accepted, relaxed_adts, "1.19.0", Some(35626)), - // Coerces non capturing closures to function pointers + // Coerces non capturing closures to function pointers (accepted, closure_to_fn_coercion, "1.19.0", Some(39817)), // Allows attributes on struct literal fields. (accepted, struct_field_attributes, "1.20.0", Some(38814)), @@ -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/test/run-pass/macro-lifetime-used-with-bound.rs b/src/test/run-pass/macro-lifetime-used-with-bound.rs index b9e1fde6b1f3e..b0c9280b6ce44 100644 --- a/src/test/run-pass/macro-lifetime-used-with-bound.rs +++ b/src/test/run-pass/macro-lifetime-used-with-bound.rs @@ -8,6 +8,8 @@ // 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 { diff --git a/src/test/run-pass/macro-lifetime-used-with-labels.rs b/src/test/run-pass/macro-lifetime-used-with-labels.rs index d003d7dcfb620..8a2d76e17df68 100644 --- a/src/test/run-pass/macro-lifetime-used-with-labels.rs +++ b/src/test/run-pass/macro-lifetime-used-with-labels.rs @@ -9,6 +9,7 @@ // except according to those terms. #![allow(unreachable_code)] +#![feature(macro_lifetime_matcher)] macro_rules! x { ($a:lifetime) => { diff --git a/src/test/run-pass/macro-lifetime-used-with-static.rs b/src/test/run-pass/macro-lifetime-used-with-static.rs index 5c1f8683e00f6..468ee2e943657 100644 --- a/src/test/run-pass/macro-lifetime-used-with-static.rs +++ b/src/test/run-pass/macro-lifetime-used-with-static.rs @@ -8,6 +8,8 @@ // 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 { diff --git a/src/test/run-pass/macro-lifetime.rs b/src/test/run-pass/macro-lifetime.rs index ff5798ff78d62..db521ca7f103f 100644 --- a/src/test/run-pass/macro-lifetime.rs +++ b/src/test/run-pass/macro-lifetime.rs @@ -8,6 +8,8 @@ // 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 { 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 + From 0f9c9b66bb43fee06ce01763cd9f3040a5a59981 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 21 Dec 2017 15:35:26 -0500 Subject: [PATCH 7/9] Add an entry in the unstable book --- .../language-features/macro-lifetime-matcher.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/doc/unstable-book/src/language-features/macro-lifetime-matcher.md 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 + +------------------------ From f55242583cddf969c863ba8948682beb7d5bb99e Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 28 Dec 2017 11:52:50 -0500 Subject: [PATCH 8/9] Cleanup --- src/libsyntax/ext/tt/macro_rules.rs | 8 ++++---- src/libsyntax/feature_gate.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 5412decd3b744..d86603e94e9df 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -889,8 +889,8 @@ fn is_legal_fragment_specifier(sess: &ParseSess, "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") { + 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", @@ -901,8 +901,8 @@ fn is_legal_fragment_specifier(sess: &ParseSess, 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 471251b063009..c5de0da0979a1 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -523,7 +523,7 @@ declare_features! ( (accepted, loop_break_value, "1.19.0", Some(37339)), // Permits numeric fields in struct expressions and patterns. (accepted, relaxed_adts, "1.19.0", Some(35626)), - // Coerces non capturing closures to function pointers + // Coerces non capturing closures to function pointers (accepted, closure_to_fn_coercion, "1.19.0", Some(39817)), // Allows attributes on struct literal fields. (accepted, struct_field_attributes, "1.20.0", Some(38814)), From 8b4bdc2f3f753e0d0b00ecc892a813e9786621e9 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Sat, 30 Dec 2017 21:47:45 -0500 Subject: [PATCH 9/9] refactor lifetime out of is_lifetime --- src/libsyntax/parse/parser.rs | 19 ++++++------------- src/libsyntax/parse/token.rs | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7251af77dcc7e..eae558f002a4c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2036,19 +2036,12 @@ impl<'a> Parser<'a> { /// Parse single lifetime 'a or panic. pub fn expect_lifetime(&mut self) -> Lifetime { - let lifetime = match self.token { - token::Lifetime(ident) => - Lifetime { ident: ident, span: self.span, id: ast::DUMMY_NODE_ID }, - token::Interpolated(ref nt) => match nt.0 { - token::NtLifetime(lifetime) => - lifetime, - _ => self.span_bug(self.span, "not a lifetime") - } - _ => self.span_bug(self.span, "not a lifetime") - }; - - self.bump(); - lifetime + if let Some(lifetime) = self.token.lifetime(self.span) { + self.bump(); + lifetime + } else { + self.span_bug(self.span, "not a lifetime") + } } /// Parse mutability (`mut` or nothing). diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 2d7ab938f7bb8..bd4f7f9853d76 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -314,18 +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, + Lifetime(ident) => + Some(ast::Lifetime { ident: ident, span: span, id: ast::DUMMY_NODE_ID }), Interpolated(ref nt) => match nt.0 { - NtLifetime(..) => true, - _ => false, + NtLifetime(lifetime) => Some(lifetime), + _ => None, }, - _ => false, + _ => 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) ||