-
Notifications
You must be signed in to change notification settings - Fork 13.5k
make cfg_select
a builtin macro
#143461
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
base: master
Are you sure you want to change the base?
make cfg_select
a builtin macro
#143461
Conversation
This comment has been minimized.
This comment has been minimized.
e3aeb08
to
1dc0034
Compare
#[instrument(skip(cx, tts))] | ||
pub fn expand_token_stream<'cx>( | ||
cx: &'cx mut ExtCtxt<'_>, | ||
sp: Span, | ||
arm_span: Span, | ||
node_id: NodeId, | ||
name: Ident, | ||
tts: TokenStream, | ||
) -> Box<dyn MacResult + 'cx> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to know if there is already a standard way to expand a token stream. I couldn't find one, so I did some refactoring here.
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. This PR modifies cc @jieyouxu |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior, per the tests and the impl, looks correct to me.
3d68353
to
3abcece
Compare
I moved some things around (analogously to Implementation-wise the one thing I'm not sure about is how to get |
cc @rust-lang/rustfmt @rust-lang/style |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the relevant file for rustfmt
: the parse_cfg_select
function returns a CfgSelectBranches
that keeps (as far as I can tell) all of the structure that is required to format cfg_select
.
A cfg_select
looks like this
fn print() {
println!(cfg_select! {
unix => { "unix" }
_ => { "not unix" }
});
}
where the right-hand side of the arrow is a TokenTree
. That token tree is expanded based on the context: if the macro is in expression position, it'll be parsed as an expression, similarly for statements, items and any other position where a macro can occur.
So the formatter heeds to handle this TokenTree
(really a TokenStream
after we strip of the outer braces) somehow. I didn't see immediately how to do that given the existing APIs, but I assume it's possible because macros-by-example would need the same thing.
☔ The latest upstream changes (presumably #143521) made this pull request unmergeable. Please resolve the merge conflicts. |
3abcece
to
53b3b88
Compare
tracking issue: #115585
This parses mostly the same as the
macro cfg_select
version, except:cfg_select {{ /* ... */ }}
is now rejected.cfg_select { _ => 1 }
now worksI've also added an error if none of the rules evaluate to true, and warnings for any arms that follow the
_
wildcard rule.cc @traviscross if I'm missing any feature that should/should not be included
r? @petrochenkov for the macro logic details