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 to be passed to macros #33135

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ pub mod rt {
}
}

impl ToTokens for ast::Lifetime {
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
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] {
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,13 @@ pub fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
token::NtPath(Box::new(panictry!(p.parse_path(LifetimeAndTypesWithoutColons))))
},
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
"lifetime" => token::NtLifetime(panictry!(p.parse_lifetime())),
_ => {
p.span_fatal_help(sp,
&format!("invalid fragment specifier `{}`", name),
"valid fragment specifiers are `ident`, `block`, \
`stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
and `item`").emit();
`stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt`, \
`lifetime`, and `item`").emit();
panic!(FatalError);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ fn frag_can_be_followed_by_any(frag: &str) -> bool {
"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 All @@ -963,6 +964,7 @@ fn can_be_followed_by_any(frag: &str) -> bool {
"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 @@ -1020,8 +1022,8 @@ fn is_in_follow(_: &ExtCtxt, tok: &Token, frag: &str) -> Result<bool, String> {
_ => 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 All @@ -1048,7 +1050,7 @@ fn has_legal_fragment_specifier(tok: &Token) -> Result<(), String> {
fn is_legal_fragment_specifier(frag: &str) -> bool {
match frag {
"item" | "block" | "stmt" | "expr" | "pat" |
"path" | "ty" | "ident" | "meta" | "tt" => true,
"path" | "ty" | "ident" | "meta" | "tt" | "lifetime" => true,
_ => false,
}
}
9 changes: 8 additions & 1 deletion src/libsyntax/ext/tt/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use codemap::{Span, DUMMY_SP};
use errors::Handler;
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use parse::token::{DocComment, MatchNt, SubstNt};
use parse::token::{Token, NtIdent, SpecialMacroVar};
use parse::token::{Token, NtIdent, NtLifetime, SpecialMacroVar};
use parse::token;
use parse::lexer::TokenAndSpan;

Expand Down Expand Up @@ -297,6 +297,13 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
r.cur_tok = token::Ident(sn.node, b);
return ret_val;
}
// interpolate lifetimes as well for the same reasons as
// idents
MatchedNonterminal(NtLifetime(ref lt)) => {
r.cur_span = lt.span;
r.cur_tok = token::Lifetime(ast::Ident::with_empty_ctxt(lt.name));
return ret_val;
}
MatchedNonterminal(ref other_whole_nt) => {
// FIXME(pcwalton): Bad copy.
r.cur_span = sp;
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
token::NtWhereClause(where_clause) =>
token::NtWhereClause(fld.fold_where_clause(where_clause)),
token::NtArg(arg) => token::NtArg(fld.fold_arg(arg)),
token::NtLifetime(lifetime) => token::NtLifetime(fld.fold_lifetime(lifetime)),
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,9 @@ impl<'a> Parser<'a> {
name: i.name
});
}
token::Interpolated(token::NtLifetime(..)) => {
self.bug("lifetime interpolation not converted to real token");
}
_ => {
return Err(self.fatal("expected a lifetime name"));
}
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ pub enum Nonterminal {
NtGenerics(ast::Generics),
NtWhereClause(ast::WhereClause),
NtArg(ast::Arg),
NtLifetime(ast::Lifetime),
}

impl fmt::Debug for Nonterminal {
Expand All @@ -418,6 +419,7 @@ impl fmt::Debug for Nonterminal {
NtGenerics(..) => f.pad("NtGenerics(..)"),
NtWhereClause(..) => f.pad("NtWhereClause(..)"),
NtArg(..) => f.pad("NtArg(..)"),
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 @@ -302,6 +302,7 @@ pub fn token_to_string(tok: &Token) -> String {
token::NtGenerics(ref e) => generics_to_string(&e),
token::NtWhereClause(ref e) => where_clause_to_string(&e),
token::NtArg(ref e) => arg_to_string(&e),
token::NtLifetime(ref e) => lifetime_to_string(&e),
}
}
}
Expand Down
23 changes: 23 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,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 <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.

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);
}
23 changes: 23 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,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 <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.

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);
}
23 changes: 23 additions & 0 deletions src/test/run-pass/macro-lifetime.rs
Original file line number Diff line number Diff line change
@@ -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 <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.

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);
}