Skip to content

Commit 03a5101

Browse files
mikeyhewricochet1k
authored andcommitted
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.
1 parent 4352c11 commit 03a5101

File tree

9 files changed

+89
-7
lines changed

9 files changed

+89
-7
lines changed

src/libsyntax/ext/quote.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ pub mod rt {
190190
}
191191
}
192192

193+
impl ToTokens for ast::Lifetime {
194+
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
195+
let lifetime_ident = ast::Ident::with_empty_ctxt(self.name);
196+
vec![TokenTree::Token(DUMMY_SP, token::Lifetime(lifetime_ident))]
197+
}
198+
}
199+
193200
macro_rules! impl_to_tokens_slice {
194201
($t: ty, $sep: expr) => {
195202
impl ToTokens for [$t] {

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
603603
"path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))),
604604
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
605605
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
606+
"lifetime" => token::NtLifetime(panictry!(p.parse_lifetime())),
606607
// this is not supposed to happen, since it has been checked
607608
// when compiling the macro.
608609
_ => p.span_bug(sp, "invalid fragment specifier")

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,11 @@ fn token_can_be_followed_by_any(tok: &quoted::TokenTree) -> bool {
768768
/// ANYTHING without fear of future compatibility hazards).
769769
fn frag_can_be_followed_by_any(frag: &str) -> bool {
770770
match frag {
771-
"item" | // always terminated by `}` or `;`
772-
"block" | // exactly one token tree
773-
"ident" | // exactly one token tree
774-
"meta" | // exactly one token tree
771+
"item" | // always terminated by `}` or `;`
772+
"block" | // exactly one token tree
773+
"ident" | // exactly one token tree
774+
"meta" | // exactly one token tree
775+
"lifetime" | // exactly one token tree
775776
"tt" => // exactly one token tree
776777
true,
777778

@@ -832,8 +833,8 @@ fn is_in_follow(tok: &quoted::TokenTree, frag: &str) -> Result<bool, (String, &'
832833
TokenTree::MetaVarDecl(_, _, frag) if frag.name == "block" => Ok(true),
833834
_ => Ok(false),
834835
},
835-
"ident" => {
836-
// being a single token, idents are harmless
836+
"ident" | "lifetime" => {
837+
// being a single token, idents and lifetimes are harmless
837838
Ok(true)
838839
},
839840
"meta" | "tt" => {
@@ -885,7 +886,7 @@ fn is_legal_fragment_specifier(sess: &ParseSess,
885886
frag_name: &str,
886887
frag_span: Span) -> bool {
887888
match frag_name {
888-
"item" | "block" | "stmt" | "expr" | "pat" |
889+
"item" | "block" | "stmt" | "expr" | "pat" | "lifetime" |
889890
"path" | "ty" | "ident" | "meta" | "tt" | "" => true,
890891
"vis" => {
891892
if !features.borrow().macro_vis_matcher

src/libsyntax/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
642642
token::NtWhereClause(fld.fold_where_clause(where_clause)),
643643
token::NtArg(arg) => token::NtArg(fld.fold_arg(arg)),
644644
token::NtVis(vis) => token::NtVis(fld.fold_vis(vis)),
645+
token::NtLifetime(lifetime) => token::NtLifetime(fld.fold_lifetime(lifetime)),
645646
}
646647
}
647648

src/libsyntax/parse/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ pub enum Nonterminal {
524524
NtGenerics(ast::Generics),
525525
NtWhereClause(ast::WhereClause),
526526
NtArg(ast::Arg),
527+
NtLifetime(ast::Lifetime),
527528
}
528529

529530
impl fmt::Debug for Nonterminal {
@@ -546,6 +547,7 @@ impl fmt::Debug for Nonterminal {
546547
NtWhereClause(..) => f.pad("NtWhereClause(..)"),
547548
NtArg(..) => f.pad("NtArg(..)"),
548549
NtVis(..) => f.pad("NtVis(..)"),
550+
NtLifetime(..) => f.pad("NtLifetime(..)"),
549551
}
550552
}
551553
}

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ pub fn token_to_string(tok: &Token) -> String {
279279
token::NtWhereClause(ref e) => where_clause_to_string(e),
280280
token::NtArg(ref e) => arg_to_string(e),
281281
token::NtVis(ref e) => vis_to_string(e),
282+
token::NtLifetime(ref e) => lifetime_to_string(e),
282283
}
283284
}
284285
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! foo {
12+
($l:lifetime, $l2:lifetime) => {
13+
fn f<$l: $l2, $l2>(arg: &$l str, arg2: &$l2 str) -> &$l str {
14+
arg
15+
}
16+
}
17+
}
18+
19+
pub fn main() {
20+
foo!('a, 'b);
21+
let x: &'static str = f("hi", "there");
22+
assert_eq!("hi", x);
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! foo {
12+
($l:lifetime) => {
13+
fn f(arg: &$l str) -> &$l str {
14+
arg
15+
}
16+
}
17+
}
18+
19+
pub fn main() {
20+
foo!('static);
21+
let x: &'static str = f("hi");
22+
assert_eq!("hi", x);
23+
}

src/test/run-pass/macro-lifetime.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! foo {
12+
($l:lifetime) => {
13+
fn f<$l>(arg: &$l str) -> &$l str {
14+
arg
15+
}
16+
}
17+
}
18+
19+
pub fn main() {
20+
foo!('a);
21+
let x: &'static str = f("hi");
22+
assert_eq!("hi", x);
23+
}

0 commit comments

Comments
 (0)