-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
cfg_select!: parse unused branches
#149925
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: main
Are you sure you want to change the base?
cfg_select!: parse unused branches
#149925
Conversation
This comment has been minimized.
This comment has been minimized.
e00ad06 to
a118b81
Compare
| fn tts_to_mac_result<'cx, 'sess>( | ||
| ecx: &'cx mut ExtCtxt<'sess>, | ||
| site_span: Span, | ||
| tts: TokenStream, | ||
| span: Span, | ||
| ) -> Box<dyn MacResult + 'cx> { | ||
| match ExpandResult::from_tts(ecx, tts, site_span, span, Ident::with_dummy_span(sym::cfg_select)) | ||
| { | ||
| ExpandResult::Ready(x) => x, | ||
| _ => unreachable!("from_tts always returns Ready"), | ||
| } | ||
| } |
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 works around ParserAnyMacro not being exported from rustc_expand. Maybe it should be exported though?
This comment has been minimized.
This comment has been minimized.
0496cba to
e91cae2
Compare
| impl<'cx, 'sess> MacResult for CfgSelectResult<'cx, 'sess> { | ||
| forward_to_parser_any_macro!(make_expr, Box<Expr>); | ||
| forward_to_parser_any_macro!(make_stmts, SmallVec<[ast::Stmt; 1]>); | ||
| forward_to_parser_any_macro!(make_items, SmallVec<[Box<ast::Item>; 1]>); | ||
|
|
||
| branches.wildcard.map(|(_, tt, span)| (tt, span)) | ||
| forward_to_parser_any_macro!(make_impl_items, SmallVec<[Box<ast::AssocItem>; 1]>); | ||
| forward_to_parser_any_macro!(make_trait_impl_items, SmallVec<[Box<ast::AssocItem>; 1]>); | ||
| forward_to_parser_any_macro!(make_trait_items, SmallVec<[Box<ast::AssocItem>; 1]>); | ||
| forward_to_parser_any_macro!(make_foreign_items, SmallVec<[Box<ast::ForeignItem>; 1]>); | ||
| } |
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've started a discussion at #t-lang > where can `cfg_select!` go to decide what we want to support.
expr/stmt/item is the bare minimum I think, but we already had some use of (i believe) impl items in the stdlib test suite, so including the other item flavors seems natural. We can see about e.g. patterns or where-clauses etc.
|
Some changes occurred in compiler/rustc_attr_parsing |
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
|
r? jdonszelmann (Reviewed similar PRS recently, don't mind taking a look :) |
| where | ||
| F: Fn(&CfgEntry) -> bool, | ||
| { | ||
| for (index, (cfg, _, _)) in self.reachable.iter().enumerate() { |
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.
might be nicer to write as an iterator? and since it consumes maybe it can take self by value. Then it'd become something like self.reachable.into_iter().find(predicate).unwrap_or(self.wildcard) with maybe something added to just get the tts and span hehe
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.
That won't work, at least with the current setup. This function isn't just about finding the match, but also specifically about removing it from the CfgSelectBranches so that it does not get parsed twice (which would duplicate errors).
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
e91cae2 to
d5ca05a
Compare
|
@rustbot ready |
tracking issue: #115585
Emit parse errors that are found in the branches that are not selected, like e.g. how
#[cfg(false) { 1 ++ 2 }still emits a parse error even though the expression is never used by the program. Parsing the unused branches was requested by T-lang #149783 (comment).