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 in macros #46895

Merged
merged 9 commits into from
Jan 1, 2018
Merged

Allow lifetimes in macros #46895

merged 9 commits into from
Jan 1, 2018

Conversation

ricochet1k
Copy link
Contributor

@ricochet1k ricochet1k commented Dec 20, 2017

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:

  • Feature gate

@rust-highfive
Copy link
Collaborator

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.

@arielb1
Copy link
Contributor

arielb1 commented Dec 20, 2017

r? @jseyfried

@rust-highfive rust-highfive assigned jseyfried and unassigned arielb1 Dec 20, 2017
token::Interpolated(ref nt) => match nt.0 {
token::NtLifetime(lifetime) =>
lifetime,
//Lifetime { ident: lifetime.ident, span: lifetime.span, id: ast::DUMMY_NODE_ID },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 21, 2017
@ricochet1k ricochet1k force-pushed the macro-lifetimes branch 2 times, most recently from a83bb7b to 000e3f5 Compare December 21, 2017 16:30
@ricochet1k
Copy link
Contributor Author

I think this is ready for review now.

@bors
Copy link
Contributor

bors commented Dec 24, 2017

☔ The latest upstream changes (presumably #46888) made this pull request unmergeable. Please resolve the merge conflicts.

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 27, 2017
Copy link
Contributor

@jseyfried jseyfried left a 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

@@ -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") {
Copy link
Contributor

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 {
    // ...
}

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray whitespace change

token::Interpolated(ref nt) => match nt.0 {
token::NtLifetime(lifetime) =>
lifetime,
_ => self.span_bug(self.span, "not a lifetime")
Copy link
Contributor

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().

Copy link
Contributor Author

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.

Copy link
Contributor

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 }.

mikeyhew and others added 8 commits December 28, 2017 11:32
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`
@ricochet1k
Copy link
Contributor Author

@jseyfried Refactoring done.

@jseyfried
Copy link
Contributor

Nice! @bors r+

@bors
Copy link
Contributor

bors commented Jan 1, 2018

📌 Commit 8b4bdc2 has been approved by jseyfried

bors added a commit that referenced this pull request Jan 1, 2018
Allow lifetimes in macros

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:
- [x]  Feature gate
@bors
Copy link
Contributor

bors commented Jan 1, 2018

⌛ Testing commit 8b4bdc2 with merge 1bcc6dc...

@bors
Copy link
Contributor

bors commented Jan 1, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: jseyfried
Pushing 1bcc6dc to master...

@bors bors merged commit 8b4bdc2 into rust-lang:master Jan 1, 2018
@ivanovaleksey
Copy link

Hello, how could I know in which Rust version I could use this feature?

@ricochet1k
Copy link
Contributor Author

Nightlies from 1.23.0 and onward should include it.

@ricochet1k ricochet1k deleted the macro-lifetimes branch March 7, 2018 15:43
@durka
Copy link
Contributor

durka commented May 2, 2018

Tracking issues should point an open issue (that is closed when the feature is stabilized or deprecated), not to the implementing PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue for adding a lifetime specifier to macro_rules!
10 participants