-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add a vis
matcher to macro_rules!
.
#1575
Conversation
Please. Please please please please please please please please please. Please. That is all. |
This is sorely needed and the design presented here looks simple yet comprehensive. It would be great if we could time this with the implementation of RFC 1422 so that macros can be updated. Another possible name for the matcher is |
Another alternative is to make visibility always representable as With RFC 1422 "non- Also, this parsing problem may be relevant to this RFC. |
I don't quite understand what you mean by this. Do you mean to transform something like
Well, building in a matcher to the macro system will make macros more resilient to whatever solution is devised to fix that problem! |
cc @pnkfelix |
I'm not sure that this is entirely relevant. This would imply either that we require users invoking macros to always write visibility annotations in some "maximally specific" form, or that I agree that having a uniform syntax for visibility annotations would be useful for expansions and other forms of code generation, but it doesn't help us with parsing what's already valid in the language. At least, I don't think so, based on what you've said. I could be misunderstanding things. |
Non-user facing macros can be invoked with "maximally specific" visibilities, user-facing macros can use a thin shim to transform |
@petrochenkov That's not really all that different from the current situation. Currently, if you need to use the visibility, you have to parse it, then pack it inside a Plus, you'd still require a new, third rule for restricted visibility, and it doesn't improve the situation with struct fields (though I admit that one's still half-broken even with this proposal). |
Yeah, what @petrochenkov is proposing is the "Do nothing" alternative to this RFC. |
I threw together an implementation, linked in the top comment. As a result of this, the follow set was modified to include comma and exclude |
Also added some additional cases to the test.
I added |
I'm in favor of this RFC, but there is one complexity that I don't see discussed in the RFC itself -- visibilities now include macro_rules! foo {
(struct $name:id ( $v:vis $t:ty )) => { ... }
} when applied to |
The addition of RFC 1422 is one reason that this matcher would be so useful, and so difficult for macros to work around today. The problem with parsing tuple structs was already discovered and fixed in the normal parser, I thought. |
@durka I agree this RFC would be useful for dealing with visibilities. Moreover, rust-lang/rust#33161 (not yet landed) does address the issue of tuple struct parsing. But that fix will not help macros, since it relies on (effectively) backtracking in the parser based on what follows the visibility (though it's implemented as reinterpreting a type as a path, the principle is the same). My understanding of how macros work is that backtracking is limiting to backtracking between arms today, so you'd have to have something like this to model tuple structs (which I guess is tolerable, actually): macro_rules! foo {
(struct $name:id ( pub $t:ty )) => { ... }
(struct $name:id ( $v:vis $t:ty )) => { ... }
} Regardless, I'd like to see some discussion of this general topic in the RFC. |
@nikomatsakis Urgh, yuck. My initial thought was: "Oh, easy; you can't have restricted paths on tuple struct fields, so we'll just add a If we could destructure tokens, the easy fix would be to add a I wondered if maybe we could just grab the following At present, I don't have any idea what to do about this. I'll mull on it. |
I see now that the fix for rust-lang/rust#33161 was applied outside the |
I'm in favour of the idea, but I worry about the fact that the semantic meaning of the visibility modifier does not correspond exactly with the syntax. For example, matching the visibility of a private field in a struct would give an empty string, but applying that to a method in a trait means 'public' (obvs, this is more of a problem in reverse). I'd also like to see a solution to the pub(restricted) issue before accepting this RFC. And though I'm in favour of adding a visibility matcher, I'm not in favour of adding two of them since that seems like too much complexity. I wonder if this ends up being something that can only be handled by procedural macros - the complexity here suggests to me that it perhaps can't be handled by syntactic macros. |
Well it can be handled by syntactic macros, it's just a huge pain: https://is.gd/F4WRHr |
I am definitely sympathetic to the aims, but I think the @durka points out that we could use some kind of speculative forward parsing. I think the idea would be that we push a "checkpoint" and try to interpret
Should (I am vaguely concerned that this will have new forwards compatibility concerns as well, seems like everything does, though that's just FUD on my part until I come up with sometime more concrete.) |
I just wish we had considered this while deciding on the syntax for On Mon, Aug 8, 2016 at 2:14 PM, Niko Matsakis notifications@github.com
|
I hadn't considered revising As an aside, I'd intended to see if I could find a (more drastic) implementation solution for the POC, but I just cannot get |
@DanielKeep |
|
Greedily? |
I guess On Wed, Aug 10, 2016 at 5:30 PM, Vadim Petrochenkov <
|
or |
|
@petrochenkov Well, |
Let's take the discussion about alternate syntaxes to the tracking issue for |
Hear ye, hear ye! This RFC is now entering final comment period with an inclination to close. The general feeling is that the we should revisit after we decide whether or not to adopt a distinct syntax for Summary of the conversation:
@rust-lang/lang members, please check off your name to signal agreement. Leave a comment with concerns or objections. Others, please leave comments. Thanks!
|
Based on the reasoning in the previous comment, we've decided to close this RFC. As stated, if we adopted a distinct syntax for |
|
:vis matcher for macro_rules Resurrection of @DanielKeep's implementation posted with [RFC 1575](rust-lang/rfcs#1575). @jseyfried was of the opinion that this doesn't need an RFC. Needed before merge: - [x] sign-off from @DanielKeep since I stole his code - [x] feature gate - [x] docs
Add a
vis
matcher tomacro_rules!
that matches valid visibility annotations.(Rendered).
(Implementation).