-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Allow lifetimes in macros #46895
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
r? @jseyfried |
src/libsyntax/parse/parser.rs
Outdated
token::Interpolated(ref nt) => match nt.0 { | ||
token::NtLifetime(lifetime) => | ||
lifetime, | ||
//Lifetime { ident: lifetime.ident, span: lifetime.span, id: ast::DUMMY_NODE_ID }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover?
a83bb7b
to
000e3f5
Compare
I think this is ready for review now. |
☔ The latest upstream changes (presumably #46888) made this pull request unmergeable. Please resolve the merge conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! r=me
src/libsyntax/ext/tt/macro_rules.rs
Outdated
@@ -887,6 +888,18 @@ 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") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting nit -- this is more idiomatic:
if condition1 &&
condition2 {
// ...
}
src/libsyntax/feature_gate.rs
Outdated
@@ -514,7 +517,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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stray whitespace change
src/libsyntax/parse/parser.rs
Outdated
token::Interpolated(ref nt) => match nt.0 { | ||
token::NtLifetime(lifetime) => | ||
lifetime, | ||
_ => self.span_bug(self.span, "not a lifetime") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional improvement -- to avoid duplicating these matches, I'd refactor out token.lifetime()
such that token.is_lifetime()
is token.lifetime().is_some()
; see token.ident()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds nice but it isn't that straightforward, since token::Lifetime has an ident and token::NtLifetime has a lifetime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I think it would work if token.lifetime()
took an argument token.lifetime(span)
so that the token::Lifetime
case would return Lifetime { ident, span, id: ast::DUMMY_NODE_ID }
.
Started rebasing @sgrif's PR rust-lang#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.
made `parser::Parser::expect_lifetime` public, so it can be called from `macro_parser::parse_nt`
c5d7264
to
f552425
Compare
@jseyfried Refactoring done. |
Nice! @bors r+ |
📌 Commit 8b4bdc2 has been approved by |
☀️ Test successful - status-appveyor, status-travis |
Hello, how could I know in which Rust version I could use this feature? |
Nightlies from 1.23.0 and onward should include it. |
Tracking issues should point an open issue (that is closed when the feature is stabilized or deprecated), not to the implementing PR. |
This is a resurrection of PR #41927 which was a resurrection of #33135, which is intended to fix #34303.
In short, this allows macros_rules! to use :lifetime as a matcher to match 'lifetimes.
Still to do: