-
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
proc_macro: Add an expand_expr method to TokenStream #87264
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
I've left this as a draft PR for now as I'm unsure whether a feature like this would be desired, and/or whether an RFC would be required before landing. |
This comment has been minimized.
This comment has been minimized.
There were some RFCs about eager expansion (of which |
Ah, that's good to know. Thanks for the links (I should've probably looked them up earlier). I take it that that means a change like this would require a corresponding RFC :-) |
I'm mostly ok with merging something like this in general. Why stabilizing this may be simpler than eager expansion in general:
|
@mystor |
We discussed this in the library-api meeting just now. We do like to see macro-expanding functionality on TokenStream. We do see the concerns with a general .expand() as already mentioned above, but would like to explore solutions that allow expanding to more than just literals in the near future. So we'd like to propose:
This way we sidestep the potential issues for now, but leave space for expanding the use cases of this function in the near future to include more expansions than only literals. @mystor I'd like to hear your thoughts on this. |
One other thought from that discussion (not something that needs to be done in this PR, just a possibility for solving the more general case in the future): Could we provide a way to expand all macros recursively, and then if any macro expands to code that uses any unstable features internally, we expand that particular macro to an |
I'm not sure a general If we do generalize to
Having some way to represent an opaque sequence of tokens in the The approach which seems most likely to me to not break existing macros would be to re-wrap the expanded AST as an unexpanded macro (e.g. Either way, this is probably something which should be handled in a separate issue or RFC. |
This comment has been minimized.
This comment has been minimized.
@mystor Thanks for clarifying the issue. We discussed this in today's @rust-lang/libs-api meeting, and we understand and agree that a fully general |
Maybe open an issue for that? |
I'm not sure if it's a serious issue in general, because normally this would work fine. The replacement of paths to rust/src/tools/compiletest/src/runtest.rs Lines 3515 to 3518 in 220ed09
I think my particular case here of printing a string literal token and hoping that the path inside of it is normalized just isn't handled by |
This comment has been minimized.
This comment has been minimized.
This feature is aimed at giving proc macros access to powers similar to those used by builtin macros such as `format_args!` or `concat!`. These macros are able to accept macros in place of string literal parameters, such as the format string, as they perform recursive macro expansion while being expanded. This can be especially useful in many cases thanks to helper macros like `concat!`, `stringify!` and `include_str!` which are often used to construct string literals at compile-time in user code. For now, this method only allows expanding macros which produce literals, although more expresisons will be supported before the method is stabilized.
756ae97
to
3e4d3d2
Compare
@bors r+ |
📌 Commit 3e4d3d2 has been approved by |
☀️ Test successful - checks-actions |
Finished benchmarking commit (3e018ce): comparison url. Summary: This change led to very large relevant regressions 😿 in compiler performance.
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression |
Huh, I'm quite surprised this caused a perf regression... Only reason I can think of off the top of my head is because accessing rust/compiler/rustc_expand/src/proc_macro_server.rs Lines 380 to 382 in 1b12d01
Should be fairly straightforward to reduce the interactions back down I suppose. We could add a field:
and fetch it during the constructor before storing the I can't think of what else could be causing the regression of the top of my head, as the vast majority of the changes here should not be in any critical path. |
This feature is aimed at giving proc macros access to powers similar to those used by builtin macros such as
format_args!
orconcat!
. These macros are able to accept macros in place of string literal parameters, such as the format string, as they perform recursive macro expansion while being expanded.This can be especially useful in many cases thanks to helper macros like
concat!
,stringify!
andinclude_str!
which are often used to construct string literals at compile-time in user code.For now, this method only allows expanding macros which produce literals, although more expressions will be supported before the method is stabilized.
In earlier versions of this PR, this method exclusively returned
Literal
, and spans on returned literals were stripped of expansion context before being returned to be as conservative as possible about permission leakage. The method's naming has been generalized to eventually support arbitrary expressions, and the context stripping has been removed (#87264 (comment)), which should allow for more general APIs like "format_args_implicits" (#67984) to be supported as well.API Surface