-
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
macros: Remove matching on "complex" nonterminals requiring AST comparisons #49326
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
cc #49154 (comment) |
There's one pattern I know of that this could break, though it's fine if macro_rules! eq {
($a:tt $b:tt) => {
macro_rules! __eq {
($a $a) => { /* equal */ };
($a $b) => { /* not equal */ }
}
__eq!($a $b);
}
} Arcane, yes, but useful. This trick is the basis of my crate |
@durka notably, iirc, this trick no longer works if you call that macro more than once: last time I tried it, I got a "macro shadows another macro" error :( |
@sdleffler |
We'll want crater artifacts eventually presumably. @bors try |
⌛ Trying commit 8eb9aa4acebd6a6c62328e728e1525e89f279196 with merge 0b4b23eb7eedcac71ed7287e39e46f44946babf1... |
We should probably compare the underlying tokens the fragment was parsed from, eventually. Another weird thing happening with nonterminals is we expand macros in them (#47358 (comment)). |
☀️ Test successful - status-travis |
Crater run started. |
(apologies for the delay, I fell ill last week) Hi @petrochenkov (crater requester), @eddyb (PR reviewer)! Crater results are at: http://cargobomb-reports.s3.amazonaws.com/pr-49326/index.html. 'Blacklisted' crates (spurious failures etc) can be found here. If you see any spurious failures not on the list, please make a PR against that file. (interested observers: Crater is a tool for testing the impact of changes on the crates.io ecosystem. You can find out more at the repo if you're curious) |
All the regressions are spurious. |
Discussed in the @rust-lang/lang meeting and we agreed to go forward with this change. It seems like this was never intentional and we can't really be exposing the details of how |
@bors r+ |
📌 Commit 8eb9aa4 has been approved by |
⌛ Testing commit 8eb9aa4acebd6a6c62328e728e1525e89f279196 with merge e6410f50dc146fecc530e42971e4012ad15247f7... |
💔 Test failed - status-appveyor |
Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Needs rebase. |
@bors r=eddyb |
📌 Commit 5bbfd67 has been approved by |
@bors r=eddyb |
📌 Commit 7e1f73b has been approved by |
Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
macros: Remove matching on "complex" nonterminals requiring AST comparisons So, you can actually use nonterminals from outer macros in left hand side of nested macros and invocations of nested macros will try to match passed arguments to them. ```rust macro outer($nt_item: item) { macro inner($nt_item) { struct S; } inner!($nt_item); // OK, `$nt_item` matches `$nt_item` } ``` Why this is bad: - We can't do this matching correctly. When two nonterminals are compared, the original tokens are lost and we have to compare AST fragments instead. Right now the comparison is done by `PartialEq` impls derived on AST structures. - On one hand, AST loses information compared to original tokens (e.g. trailing separators and other simplifications done during parsing to AST), so we can produce matches that are not actually correct. - On another hand derived `PartialEq` impls for AST structures don't make much sense in general and compare various auxiliary garbage like spans. For the argument nonterminal to match we should use literally the same token (possibly cloned) as was used in the macro LHS (as in the example above). So we can reject matches that are actually correct. - Support for nonterminal matching is the only thing that forces us to derive `PartialEq` for all (!) AST structures. As I mentioned these impls are also mostly nonsensical. This PR removes support for matching on all nonterminals except for "simple" ones like `ident`, `lifetime` and `tt` for which we have original tokens that can be compared. After this is done I'll submit another PR removing huge number of `PartialEq` impls from AST and HIR structures. This is an arcane feature and I don't personally know why would anyone use it, but the change should ideally go through crater. We'll be able to support this feature again in the future when all nonterminals have original token streams attached to them in addition to (or instead of) AST fragments.
☀️ Test successful - status-appveyor, status-travis |
Remove most of `PartialEq` and `Hash` impls from AST and HIR structures Continuation of #49326, prerequisite for removing `PartialEq` for `Ident`.
So, you can actually use nonterminals from outer macros in left hand side of nested macros and invocations of nested macros will try to match passed arguments to them.
Why this is bad:
PartialEq
impls derived on AST structures.PartialEq
impls for AST structures don't make much sense in general and compare various auxiliary garbage like spans. For the argument nonterminal to match we should use literally the same token (possibly cloned) as was used in the macro LHS (as in the example above). So we can reject matches that are actually correct.PartialEq
for all (!) AST structures. As I mentioned these impls are also mostly nonsensical.This PR removes support for matching on all nonterminals except for "simple" ones like
ident
,lifetime
andtt
for which we have original tokens that can be compared.After this is done I'll submit another PR removing huge number of
PartialEq
impls from AST and HIR structures.This is an arcane feature and I don't personally know why would anyone use it, but the change should ideally go through crater.
We'll be able to support this feature again in the future when all nonterminals have original token streams attached to them in addition to (or instead of) AST fragments.