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

Simplify TokenTree and fix macro_rules! bugs #39419

Merged
merged 10 commits into from
Mar 1, 2017

Conversation

jseyfried
Copy link
Contributor

@jseyfried jseyfried commented Jan 31, 2017

This PR

r? @nrc

@jseyfried
Copy link
Contributor Author

jseyfried commented Jan 31, 2017

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

@jseyfried jseyfried 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
/// A kleene-style repetition sequence with a span
Sequence(Span, Rc<SequenceRepetition>),
/// Matches a nonterminal. This is only used in the left hand side of MBE macros.
Match(Span, ast::Ident /* name to bind */, ast::Ident /* kind of nonterminal */),
Copy link
Member

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)

Copy link
Contributor Author

@jseyfried jseyfried Jan 31, 2017

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 jseyfried force-pushed the simplify_tokentree branch 2 times, most recently from ca90c32 to 04b8382 Compare January 31, 2017 08:41
Copy link
Member

@nrc nrc left a comment

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.

idx: usize,
dotdotdoted: bool,
sep: Option<Token>,
enum Frame {
Copy link
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

Copy link
Contributor Author

@jseyfried jseyfried Feb 3, 2017

Choose a reason for hiding this comment

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

Done.

}
}
let mut stack = SmallVector::one(Frame::Delimited {
forest: Rc::new(tokenstream::Delimited { delim: token::NoDelim, tts: src }),
Copy link
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

Copy link
Contributor Author

@jseyfried jseyfried Feb 3, 2017

Choose a reason for hiding this comment

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

Done.

enum LockstepIterSize {
LisUnconstrained,
LisConstraint(usize, Ident),
LisContradiction(String),
Copy link
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

Copy link
Contributor Author

@jseyfried jseyfried Feb 3, 2017

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1,228 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
Copy link
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

Copy link
Contributor Author

@jseyfried jseyfried Feb 3, 2017

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
Copy link
Member

nrc commented Feb 7, 2017

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

Copy link
Member

@nrc nrc left a comment

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

@@ -76,6 +76,8 @@ pub enum TokenTree {
Delimited(Span, Rc<Delimited>),
/// A kleene-style repetition sequence with a span
Sequence(Span, Rc<SequenceRepetition>),
/// Matches a nonterminal. This is only used in the left hand side of MBE macros.
Match(Span, ast::Ident /* name to bind */, ast::Ident /* kind of nonterminal */),
Copy link
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.

Copy link
Contributor Author

@jseyfried jseyfried Feb 7, 2017

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
Member

Choose a reason for hiding this comment

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

MetaVarDecl SGTM

@bors
Copy link
Contributor

bors commented Feb 10, 2017

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

@bors
Copy link
Contributor

bors commented Feb 13, 2017

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

@jseyfried jseyfried force-pushed the simplify_tokentree branch 3 times, most recently from 67be452 to 88716e7 Compare February 16, 2017 12:38
@jseyfried
Copy link
Contributor Author

@eddyb would you mind Cratering this?

@bors
Copy link
Contributor

bors commented Feb 17, 2017

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

@eddyb
Copy link
Member

eddyb commented Feb 17, 2017

@jseyfried Can you ping me after a rebase?

@jseyfried
Copy link
Contributor Author

@eddyb rebased.

@eddyb
Copy link
Member

eddyb commented Feb 17, 2017

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

jseyfried commented Feb 18, 2017

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
Copy link
Member

eddyb commented Feb 18, 2017

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
Member

eddyb commented Feb 18, 2017

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
Contributor Author

@bors r=nrc

@bors
Copy link
Contributor

bors commented Feb 28, 2017

📌 Commit 839398a has been approved by nrc

@bors
Copy link
Contributor

bors commented Feb 28, 2017

⌛ Testing commit 839398a with merge d35bcfa...

@bors
Copy link
Contributor

bors commented Feb 28, 2017

💔 Test failed - status-appveyor

@alexcrichton
Copy link
Member

alexcrichton commented Feb 28, 2017 via email

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 1, 2017
Simplify `TokenTree` and fix `macro_rules!` bugs

This PR
 - fixes rust-lang#39390, fixes rust-lang#39403, and fixes rust-lang#39404 (each is a [breaking-change], see issues for examples),
 - fixes rust-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
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
Copy link
Contributor

bors commented Mar 1, 2017

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

bors commented Mar 1, 2017

☀️ 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:master Mar 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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
5 participants