Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHygiene opt-out for idents in expansion of declarative macros #47992
Conversation
rust-highfive
assigned
petrochenkov
Feb 4, 2018
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (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. |
alexreg
changed the title
Hygiene opt-out for idents in expansion of declarative macros
WIP: Hygiene opt-out for idents in expansion of declarative macros
Feb 4, 2018
pietroalbini
added
the
S-waiting-on-review
label
Feb 4, 2018
This comment has been minimized.
This comment has been minimized.
|
Could you add all the examples from the PR descriptions as tests? |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Yep, sounds fair. Also, thoughts on my above comment about use of the syntax in legacy |
This comment has been minimized.
This comment has been minimized.
|
Oh, and I think the syntax |
This comment has been minimized.
This comment has been minimized.
|
A further question:
|
This comment has been minimized.
This comment has been minimized.
|
Okay, my present thoughts are that 3 and 4 should indeed fail. To make them work would mean having the macro parser understand items, which would mean binding it more tightly to the normal parser, and really make a mess of things. |
This comment has been minimized.
This comment has been minimized.
|
@alexreg I'm confused as to what it would mean for them to work, even if it would involve reworking the parser. If I understand correctly, in examples 3 and 4 // `main` scope
// vvvvvvv
pub mod foo_mod {
// `foo!` scope
// vvv
pub const BAR: u32 = 123;
}In both cases |
alexreg
force-pushed the
alexreg:hygiene-optout
branch
from
ad66b24
to
c64d318
Feb 6, 2018
This comment has been minimized.
This comment has been minimized.
|
@pierzchalski Well, the alternative semantics would be i.e. We have
(The same end result in terms of hygiene as for |
alexreg
force-pushed the
alexreg:hygiene-optout
branch
from
c64d318
to
e7495a9
Feb 6, 2018
This comment has been minimized.
This comment has been minimized.
|
@alexreg Ah, I see! Yeah, making scopes semantics-aware sounds like a lot of work. It also means you'd need a way to 'un-lift' any identifiers you expected to use privately: // `main` scope
// vvvvvvv
pub mod foo_mod {
// `main` scope
// vvv
pub fn bar() {
// I want this to be in `foo_mod!` scope
baz();
}
// Perhaps I want this private to `foo_mod!` but
// `pub` because it's used by... other modules generated by `foo_mod!`,
// or something else silly.
pub fn baz() { ... }
} |
This comment has been minimized.
This comment has been minimized.
If it can be easily prohibited, then let's prohibit it for a start, otherwise any use of the |
This comment has been minimized.
This comment has been minimized.
Why? |
This comment has been minimized.
This comment has been minimized.
Yes, ideally we should. |
This comment has been minimized.
This comment has been minimized.
What is "3 and 4"? |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov The 3rd and 4th code snippets in the starting post (I was also briefly lost on that). |
This comment has been minimized.
This comment has been minimized.
They should fail then, |
This comment has been minimized.
This comment has been minimized.
cc https://internals.rust-lang.org/t/pre-rfc-splitting-liftime-into-two-tokens/6716 |
This comment has been minimized.
This comment has been minimized.
|
@pierzchalski Exactly. The difficulties outweigh the small benefits and added complexity I think. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Sounds good to me. If we can solve the spaces problem mentioned there, it will definitely simplify a lot of code. For now I may hack around it. |
This comment has been minimized.
This comment has been minimized.
Sure. I'll have a go at this – I don't think it's too hard. |
This comment has been minimized.
This comment has been minimized.
I think of it in terms of composability. For a metavar |
This comment has been minimized.
This comment has been minimized.
|
Okay, tests added (both run-pass & compile-fail) – everything passing locally. r? @jseyfried CC @nikomatsakis too, in case @jseyfried is too busy still (I've noticed a bit of activity from him though). |
rust-highfive
assigned
jseyfried
and unassigned
petrochenkov
Feb 7, 2018
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The job 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 |
alexreg
force-pushed the
alexreg:hygiene-optout
branch
from
c026bd7
to
f78d4f5
May 9, 2018
This comment has been minimized.
This comment has been minimized.
|
The job 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 |
alexreg
force-pushed the
alexreg:hygiene-optout
branch
from
f78d4f5
to
c7f158b
May 10, 2018
This comment has been minimized.
This comment has been minimized.
|
How are we looking now, @petrochenkov / @jseyfried? :-) |
alexreg
changed the title
WIP: Hygiene opt-out for idents in expansion of declarative macros
Hygiene opt-out for idents in expansion of declarative macros
May 10, 2018
This comment has been minimized.
This comment has been minimized.
|
We discussed this in the language team meeting, and we agree that this needs an actual RFC to specify this for review. This is not the kind of thing we should just do without an RFC. |
This comment has been minimized.
This comment has been minimized.
|
@joshtriplett Okay. What I'll do then is get @petrochenkov & @jseyfried's feedback now, and then start writing up an RFC including what's been done (and learnt) in this PR, plus future plans. Maybe we can leave this open in the meanwhile, then once that's accepted, we can hopefully get this merged with few changes? |
This comment has been minimized.
This comment has been minimized.
|
@alexreg I'd prefer for this PR to be closed in the meantime, otherwise triage would have to check this PR every week. Then when you're ready you can open it again. |
This comment has been minimized.
This comment has been minimized.
|
@pietroalbini As long as it can be reopened easily, sure. Hopefully @petrochenkov & @jseyfried will see my above comments anyway, and leave some feedback before I get to writing the RFC. |
petrochenkov
added
the
S-waiting-on-review
label
May 10, 2018
petrochenkov
reviewed
May 13, 2018
| pub struct Escaper(pub SyntaxContext); | ||
|
|
||
| impl Folder for Escaper { | ||
| fn fold_ident(&mut self, mut ident: Ident) -> Ident { |
This comment has been minimized.
This comment has been minimized.
petrochenkov
May 13, 2018
Contributor
Overriding fold_ident is not necessary, overridden new_span applies to identifiers as well.
This comment has been minimized.
This comment has been minimized.
alexreg
May 13, 2018
Author
Contributor
Ah, I see. Why is it necessary in the Folder implementation for Marker though?
This comment has been minimized.
This comment has been minimized.
petrochenkov
May 13, 2018
Contributor
It isn't necessary there either, it's just something I forgot to remove in #49154.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'm not even sure there's a need in a separate feature gate beyond |
This comment has been minimized.
This comment has been minimized.
Feedback: the implementation looks good to me, but if lang team thinks it needs an RFC, then it needs an RFC. |
This comment has been minimized.
This comment has been minimized.
|
Ok, closing this until an RFC is approved. @alexreg when you're ready to work on this again just click the reopen button |
pietroalbini
closed this
May 13, 2018
This comment has been minimized.
This comment has been minimized.
|
@pietroalbini |
This comment has been minimized.
This comment has been minimized.
|
@pietroalbini It turns out I don't have the rights to reopen my PRs (as I thought), but I'll ping you or someone else when it's time, sure! |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov There's no easy way to get whether we're in legacy mode from within the |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Also, should the errors be emitted when doing the actual parsing of the macro or the expansion? |
This comment has been minimized.
This comment has been minimized.
Don't know, looks like no.
Ideally, if some error will be reported at any expansion, then it should be reported at macro definition. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Makes sense, thanks. I think we can bail within the |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Incidentally, do you want to disallow I'm not sure what the error message should be when an interpolated token follows |
This comment has been minimized.
This comment has been minimized.
|
Something like "Hygiene opt-out is not supported for macro parameters" or "... on the left side of the macro". |
This comment has been minimized.
This comment has been minimized.
|
Hmm, what are interpolated tokens? Something to do with proc macros I seem to recall, but I don’t really know. The term might confuse users, no? |
This comment has been minimized.
This comment has been minimized.
The problem with this is, we're allowing |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Pushed new commits anyway, in case you want to have a look. May make another, depending on your answers to the above two queries, but otherwise I'll focus on the RFC now. |
alexreg commentedFeb 4, 2018
•
edited
This PR adds basic support for hygiene opt-out for idents during macro expansion. This has been previously discussed in #40847 and #39412.
The syntax involves prefixing idents with the
#(i.e. pound/hash) character to indicate that the syntax context of the ident should be that of the call site rather than the definition site.The following now compiles and runs as expected:
as does:
but the following does not:
nor does:
Questions:
macro_rulesmacros, of course, but should we explicitly disallow the#syntax in such places? (Currently that is not done.)CC @jseyfried