Skip to content

Simplify TokenTree and fix macro_rules! bugs - #39419

Merged
bors merged 10 commits into
rust-lang:masterfrom
jseyfried:simplify_tokentree
Mar 1, 2017
Merged

Simplify TokenTree and fix macro_rules! bugs#39419
bors merged 10 commits into
rust-lang:masterfrom
jseyfried:simplify_tokentree

Conversation

@jseyfried

@jseyfriedjseyfried commented Jan 31, 2017

Copy link
Copy Markdown
Contributor

This PR

r? @nrc

@jseyfried

jseyfried commented Jan 31, 2017

Copy link
Copy Markdown
ContributorAuthor

This is groundwork for procedural and declarative macros 2.0.
cc #38356#39412

@jseyfriedjseyfried mentioned this pull request Jan 31, 2017
@jseyfriedjseyfried added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jan 31, 2017
Comment threadsrc/libsyntax/ext/tt/quoted.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't this be done on the fly? (i.e in parsing not lexing)

@jseyfriedjseyfriedJan 31, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Neither parsing nor lexing knows about sequences and matchers at all. We lex directly into a Vec<TokenTree> and consume the token trees in the parser directly (e.g. parsing a token tree is constant time). Sequences and matchers are parsed by ext::tt::quoted::parse.

@jseyfried
jseyfriedforce-pushed the simplify_tokentree branch 2 times, most recently from ca90c32 to 04b8382CompareJanuary 31, 2017 08:41

@nrcnrc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I only got halfway through reviewing this PR before the (long) weekend, and then my browser crashed and lost my comments. There wasn't anything major. Anyway, here is a single comment and I'll finish the review on Tuesday. Sorry for the delay.

Comment threadsrc/libsyntax/ext/tt/transcribe.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This would benefit from a comment

@jseyfriedjseyfriedFeb 3, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Done.

Comment threadsrc/libsyntax/ext/tt/transcribe.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would factor this out into a ctor function

@jseyfriedjseyfriedFeb 3, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Done.

Comment threadsrc/libsyntax/ext/tt/transcribe.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I'd kill the Lis prefix

@jseyfriedjseyfriedFeb 3, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Done.

Comment threadsrc/libsyntax/ext/tt/quoted.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why make this module here? I'd have thought TTs were much more general than just quasi quoting

@jseyfriedjseyfriedFeb 3, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

We still have tokenstream::{TokenStream, TokenTree, ...}.

quoted::TokenTree is more general than tokenstream::TokenTree since it also supports "$-quoted tokens" from the macro_rules! syntax -- $e, $e:expr, and $(...)*. In other words, quoted::TokenTree is the parse tree for macro_rules! syntax.

@nrc

nrc commented Feb 7, 2017

Copy link
Copy Markdown
Member

Oh, GH didn't lose my earlier comments. Good.

nrc
nrc approved these changes Feb 7, 2017

@nrcnrc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just one more comment, and that can be done later

Comment threadsrc/libsyntax/ext/tt/quoted.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think Match is a pretty bad name - it is not actually a match of a pattern to text (or anything to do with a match expression). Maybe PatternVariable or MacroVariable is better? Doesn't have to be done in this PR though.

@jseyfriedjseyfriedFeb 7, 2017

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

With a bit more refactoring I could also move Nonterminal::SubstNt here -- I think MacroVariable would be a better fit for that. MacroVariableDeclaration is too long though (imo); how about MetaVarDecl?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MetaVarDecl SGTM

@bors

bors commented Feb 10, 2017

Copy link
Copy Markdown
Collaborator

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

@bors

bors commented Feb 13, 2017

Copy link
Copy Markdown
Collaborator

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

@jseyfried
jseyfriedforce-pushed the simplify_tokentree branch 3 times, most recently from 67be452 to 88716e7CompareFebruary 16, 2017 12:38
@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@eddyb would you mind Cratering this?

@bors

bors commented Feb 17, 2017

Copy link
Copy Markdown
Collaborator

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

@eddyb

Copy link
Copy Markdown
Contributor

@jseyfried Can you ping me after a rebase?

@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@eddyb rebased.

@eddyb

eddyb commented Feb 17, 2017

Copy link
Copy Markdown
Contributor

The crater report shows 4 main regressions:

These all look like macro-defining macros, and they seem like they should work, but something is perhaps doing too-eager parsing? I've never liked $foo:bar (or even just $foo) being a single token, it shouldn't.

@jseyfried

jseyfried commented Feb 18, 2017

Copy link
Copy Markdown
ContributorAuthor

It looks like all these errors are due to fixing #39404. That is, they are legitimate errors in the macro definition that didn't show up before because the erroneous pattern never ended up getting used to match anything. I'll start a warning cycle.

I've never liked $foo:bar (or even just $foo) being a single token, it shouldn't.

Agreed, that's one of the reasons for this PR. After this PR, $foo:bar is never a single token (it used to be Token::Interpolated(Nonterminal::MatchNt(..)) sometimes; this PR removes Nonterminal::MatchNt).
Also, $foo is never a single token in a tokenstream::TokenTree after this PR, except on certain error paths (planning on removing Nonterminal::SubstNt later after refactoring the relevant errors).

@eddyb

eddyb commented Feb 18, 2017

Copy link
Copy Markdown
Contributor

Is it really #39404? It looked like the lone $foo was supposed to be interpolated from an outer macro, or am I imagining things? The fallout looks pretty bad if it's their bug, multiple patch releases may be needed.

EDIT: No, you're right 😞

@eddyb

Copy link
Copy Markdown
Contributor

Okay, this might not be that bad. clap is affected in 1.* and 2.* and since these are post-0.*, one release for each (of 1.* and 2.*) should be enough to fix most (ideally, all) of the regressions.
I've opened clap-rs/clap#857 and we can do another crater run once that's released.

@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@bors r=nrc

@bors

bors commented Feb 28, 2017

Copy link
Copy Markdown
Collaborator

📌 Commit 839398a has been approved by nrc

@bors

bors commented Feb 28, 2017

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 839398a with merge d35bcfa...

@bors

bors commented Feb 28, 2017

Copy link
Copy Markdown
Collaborator

💔 Test failed - status-appveyor

@alexcrichton

alexcrichton commented Feb 28, 2017 via email

Copy link
Copy Markdown
Member

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 1, 2017
Simplify `TokenTree` and fix `macro_rules!` bugs
This PR
- fixesrust-lang#39390, fixesrust-lang#39403, and fixesrust-lang#39404 (each is a [breaking-change], see issues for examples),
- fixesrust-lang#39889,
- simplifies and optimizes macro invocation parsing,
- cleans up `ext::tt::transcribe`,
- removes `tokenstream::TokenTree::Sequence` and `Token::MatchNt`,
- instead, adds a new type `ext::tt::quoted::TokenTree` for use by `macro_rules!` (`ext::tt`)
- removes `parser.quote_depth` and `parser.parsing_token_tree`, and
- removes `quote_matcher!`.
- Instead, use `quote_tokens!` and `ext::tt::quoted::parse` the result with `expect_matchers=true`.
- I found no outside uses of `quote_matcher!` when searching Rust code on Github.
r? @nrc
@frewsxcvfrewsxcv mentioned this pull request Mar 1, 2017
bors added a commit that referenced this pull request Mar 1, 2017
Rollup of 6 pull requests
- Successful merges: #39419, #39936, #39944, #39960, #40028, #40128
- Failed merges:
@bors

bors commented Mar 1, 2017

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 839398a with merge 7ce1fbe...

bors added a commit that referenced this pull request Mar 1, 2017
Simplify `TokenTree` and fix `macro_rules!` bugs
This PR
- fixes#39390, fixes#39403, and fixes#39404 (each is a [breaking-change], see issues for examples),
- fixes#39889,
- simplifies and optimizes macro invocation parsing,
- cleans up `ext::tt::transcribe`,
- removes `tokenstream::TokenTree::Sequence` and `Token::MatchNt`,
- instead, adds a new type `ext::tt::quoted::TokenTree` for use by `macro_rules!` (`ext::tt`)
- removes `parser.quote_depth` and `parser.parsing_token_tree`, and
- removes `quote_matcher!`.
- Instead, use `quote_tokens!` and `ext::tt::quoted::parse` the result with `expect_matchers=true`.
- I found no outside uses of `quote_matcher!` when searching Rust code on Github.
r? @nrc
@bors

bors commented Mar 1, 2017

Copy link
Copy Markdown
Collaborator

☀️ Test successful - status-appveyor, status-travis
Approved by: nrc
Pushing 7ce1fbe to master...

bors added a commit that referenced this pull request Mar 1, 2017
Rollup of 6 pull requests
- Successful merges: #39419, #39936, #39944, #39960, #40028, #40128
- Failed merges:
@bors
bors merged commit 839398a into rust-lang:masterMar 1, 2017
@jseyfried
jseyfried deleted the simplify_tokentree branch March 9, 2017 06:28
harpocrates added a commit to harpocrates/language-rust that referenced this pull request May 13, 2017
Updated AST as per <rust-lang/rust#39419>,
thereby closing #19.
Since those changes involved removing MathcNt and SubstNt, I've
removed the unsafe parts of Quote that relied on that. Quote is now a
safe module (where '$x' and '$x:ty' don't work), enabled by default.
Difference tests now work on 'ast.rs' as well as 'parser.rs'. Additional
work was done on cleaning up tests around macros and tokens. Closes#16.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)S-waiting-on-craterStatus: Waiting on a crater run to be completed.

Projects

None yet

5 participants

@jseyfried@nrc@bors@eddyb@alexcrichton