Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ builtin_macros_cfg_accessible_literal_path = `cfg_accessible` path cannot be a l
builtin_macros_cfg_accessible_multiple_paths = multiple `cfg_accessible` paths are specified
builtin_macros_cfg_accessible_unspecified_path = `cfg_accessible` path is not specified
builtin_macros_cfg_select_no_matches = none of the rules in this `cfg_select` evaluated to true
builtin_macros_cfg_select_unreachable = unreachable rule
.label = always matches
.label2 = this rules is never reached
builtin_macros_coerce_pointee_requires_maybe_sized = `derive(CoercePointee)` requires `{$name}` to be marked `?Sized`
builtin_macros_coerce_pointee_requires_one_field = `CoercePointee` can only be derived on `struct`s with at least one field
Expand Down
64 changes: 64 additions & 0 deletions compiler/rustc_builtin_macros/src/cfg_select.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use rustc_ast::tokenstream::TokenStream;
use rustc_attr_parsing as attr;
use rustc_expand::base::{ExtCtxt, MacroExpanderResult, *};
use rustc_parse::parser::cfg_select::{CfgSelectBranches, CfgSelectRule, parse_cfg_select};
use rustc_span::{Ident, Span, sym};

use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable};

/// Selects the first arm whose rule evaluates to true.
fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> {
for (cfg, tt, arm_span) in branches.reachable {
if attr::cfg_matches(
&cfg,
&ecx.sess,
ecx.current_expansion.lint_node_id,
Some(ecx.ecfg.features),
) {
return Some((tt, arm_span));
}
}

branches.wildcard.map(|(_, tt, span)| (tt, span))
}

pub(super) fn expand_cfg_select<'cx>(
ecx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> MacroExpanderResult<'cx> {
ExpandResult::Ready(match parse_cfg_select(&mut ecx.new_parser_from_tts(tts)) {
Ok(branches) => {
if let Some((underscore, _, _)) = branches.wildcard {
// Warn for every unreachable rule.
for (rule, _, _) in &branches.unreachable {
let span = match rule {
CfgSelectRule::Wildcard(underscore) => underscore.span,
CfgSelectRule::Cfg(cfg) => cfg.span(),
};
let err = CfgSelectUnreachable { span, wildcard_span: underscore.span };
ecx.dcx().emit_warn(err);
}
}

if let Some((tts, arm_span)) = select_arm(ecx, branches) {
rustc_expand::expand_token_stream(
ecx,
sp,
arm_span,
ecx.current_expansion.lint_node_id,
Ident::with_dummy_span(sym::cfg_select),
tts,
)
} else {
// Emit a compiler error when none of the rules matched.
let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp });
DummyResult::any(sp, guar)
}
}
Err(err) => {
let guar = err.emit();
DummyResult::any(sp, guar)
}
})
}
18 changes: 18 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,21 @@ pub(crate) struct AsmExpectedOther {
pub(crate) span: Span,
pub(crate) is_inline_asm: bool,
}

#[derive(Diagnostic)]
#[diag(builtin_macros_cfg_select_no_matches)]
pub(crate) struct CfgSelectNoMatches {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(builtin_macros_cfg_select_unreachable)]
pub(crate) struct CfgSelectUnreachable {
#[primary_span]
#[label(builtin_macros_label2)]
pub span: Span,

#[label]
pub wildcard_span: Span,
}
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod autodiff;
mod cfg;
mod cfg_accessible;
mod cfg_eval;
mod cfg_select;
mod compile_error;
mod concat;
mod concat_bytes;
Expand Down Expand Up @@ -79,6 +80,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
asm: asm::expand_asm,
assert: assert::expand_assert,
cfg: cfg::expand_cfg,
cfg_select: cfg_select::expand_cfg_select,
column: source_util::expand_column,
compile_error: compile_error::expand_compile_error,
concat: concat::expand_concat,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod placeholders;
mod proc_macro_server;
mod stats;

pub use mbe::macro_rules::compile_declarative_macro;
pub use mbe::macro_rules::{compile_declarative_macro, expand_token_stream};
pub mod base;
pub mod config;
pub mod expand;
Expand Down
73 changes: 46 additions & 27 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ impl<'matcher> Tracker<'matcher> for NoopTracker {
}
}

#[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> {
Comment on lines +197 to +205
Copy link
Contributor Author

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.

Box::new(ParserAnyMacro {
parser: Parser::new(&cx.sess.psess, tts, None),

// Pass along the original expansion site and the name of the macro
// so we can print a useful error message if the parse of the expanded
// macro leaves unparsed tokens.
site_span: sp,
macro_ident: name,
lint_node_id: cx.current_expansion.lint_node_id,
is_trailing_mac: cx.current_expansion.is_trailing_mac,
arm_span,
is_local: is_defined_in_current_crate(node_id),
})
}

/// Expands the rules based macro defined by `rules` for a given input `arg`.
#[instrument(skip(cx, transparency, arg, rules))]
fn expand_macro<'cx>(
Expand All @@ -207,9 +231,6 @@ fn expand_macro<'cx>(
rules: &[MacroRule],
) -> Box<dyn MacResult + 'cx> {
let psess = &cx.sess.psess;
// Macros defined in the current crate have a real node id,
// whereas macros from an external crate have a dummy id.
let is_local = node_id != DUMMY_NODE_ID;

if cx.trace_macros() {
let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
Expand All @@ -220,7 +241,7 @@ fn expand_macro<'cx>(
let try_success_result = try_match_macro(psess, name, &arg, rules, &mut NoopTracker);

match try_success_result {
Ok((i, rule, named_matches)) => {
Ok((rule_index, rule, named_matches)) => {
let mbe::TokenTree::Delimited(rhs_span, _, ref rhs) = rule.rhs else {
cx.dcx().span_bug(sp, "malformed macro rhs");
};
Expand All @@ -241,27 +262,13 @@ fn expand_macro<'cx>(
trace_macros_note(&mut cx.expansions, sp, msg);
}

let p = Parser::new(psess, tts, None);

if is_local {
cx.resolver.record_macro_rule_usage(node_id, i);
if is_defined_in_current_crate(node_id) {
cx.resolver.record_macro_rule_usage(node_id, rule_index);
}

// Let the context choose how to interpret the result.
// Weird, but useful for X-macros.
Box::new(ParserAnyMacro {
parser: p,

// Pass along the original expansion site and the name of the macro
// so we can print a useful error message if the parse of the expanded
// macro leaves unparsed tokens.
site_span: sp,
macro_ident: name,
lint_node_id: cx.current_expansion.lint_node_id,
is_trailing_mac: cx.current_expansion.is_trailing_mac,
arm_span,
is_local,
})
expand_token_stream(cx, sp, arm_span, node_id, name, tts)
}
Err(CanRetry::No(guar)) => {
debug!("Will not retry matching as an error was emitted already");
Expand Down Expand Up @@ -373,10 +380,18 @@ pub fn compile_declarative_macro(
node_id: NodeId,
edition: Edition,
) -> (SyntaxExtension, usize) {
let is_local = node_id != DUMMY_NODE_ID;
let mk_syn_ext = |expander| {
let kind = SyntaxExtensionKind::LegacyBang(expander);
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
SyntaxExtension::new(
sess,
kind,
span,
Vec::new(),
edition,
ident.name,
attrs,
is_defined_in_current_crate(node_id),
)
};
let dummy_syn_ext = |guar| (mk_syn_ext(Arc::new(DummyExpander(guar))), 0);

Expand Down Expand Up @@ -439,7 +454,7 @@ pub fn compile_declarative_macro(
}

// Return the number of rules for unused rule linting, if this is a local macro.
let nrules = if is_local { rules.len() } else { 0 };
let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };

let expander =
Arc::new(MacroRulesMacroExpander { name: ident, span, node_id, transparency, rules });
Expand Down Expand Up @@ -1034,9 +1049,7 @@ fn check_matcher_core<'tt>(
// definition of this macro_rules, not while (re)parsing
// the macro when compiling another crate that is using the
// macro. (See #86567.)
// Macros defined in the current crate have a real node id,
// whereas macros from an external crate have a dummy id.
if node_id != DUMMY_NODE_ID
if is_defined_in_current_crate(node_id)
&& matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
&& matches!(
next_token,
Expand Down Expand Up @@ -1296,6 +1309,12 @@ fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
}
}

fn is_defined_in_current_crate(node_id: NodeId) -> bool {
// Macros defined in the current crate have a real node id,
// whereas macros from an external crate have a dummy id.
node_id != DUMMY_NODE_ID
}

pub(super) fn parser_from_cx(
psess: &ParseSess,
mut tts: TokenStream,
Expand Down
73 changes: 73 additions & 0 deletions compiler/rustc_parse/src/parser/cfg_select.rs
Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[derive(Default)]
pub struct CfgSelectBranches {
    pub reachable: Vec<(MetaItemInner, TokenStream, Span)>,
    pub wildcard: Option<(Token, TokenStream, Span)>,
    pub unreachable: Vec<(CfgSelectRule, TokenStream, Span)>,
}

Question, what's the difference between reachable, wildcard, and unreachable?

Also, will the TokenTree on the right-hand side of the arrow always be valid rust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, what's the difference between reachable, wildcard, and unreachable?

For formatting there is really no difference. The goal of this structure is to be able to emit warnings for unreachable branches.

The cfg rules on the left-hand side are evaluated from top to bottom, and the first one that evaluates to true is picked. The wildcard _ always evaluates to true, so any branches that follow it are unreachable.

Also, will the TokenTree on the right-hand side of the arrow always be valid rust?

I don't think so. It is like the right-hand side of a macro_rules! macro rule.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide examples of more complicated RHS that aren't valid rust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this

error: macro expansion ignores `+` and any tokens following
  --> /home/folkertdev/rust/rust/tests/ui/macros/cfg_select.rs:30:12
   |
LL | / cfg_select! {
LL | |     _ => { + + + }
   | |            ^
LL | | }
   | |_- caused by the macro expansion here
   |
   = note: the usage of `cfg_select!` is likely invalid in item context

the rhs is a valid token tree, but when expanded it's not valid rust code.

Copy link
Contributor

@ytmimi ytmimi Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt would definitely fail to handle that. Is there a more realistic example you can think of?

In the example above things fail to compile, but is there a case where the RHS is composed of valid tokens that don't parse as valid rust, but still get expanded to valid rust?

Technically, rustfmt doesn't care what the tokens get expanded to as long as the tokens themselves can be parsed as valid rust, otherwise there's no hope to format them. Would you say that the typical case is going to be a RHS that parses as valid rust?

Copy link
Contributor Author

@folkertdev folkertdev Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in valid programs the rhs will generally be valid rust. The only tricky thing is that it's unclear what kind (could be a sequence of items, or an expression, or a statement).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I think it should be enough to call format_snippet. That's how we currently format branches in macro_rules macros.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use rustc_ast::token::Token;
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::{MetaItemInner, token};
use rustc_errors::PResult;
use rustc_span::{Span, kw};

use crate::exp;
use crate::parser::Parser;

pub enum CfgSelectRule {
Cfg(MetaItemInner),
Wildcard(Token),
}

#[derive(Default)]
pub struct CfgSelectBranches {
/// All the conditional branches.
pub reachable: Vec<(MetaItemInner, TokenStream, Span)>,
/// The first wildcard `_ => { ... }` branch.
pub wildcard: Option<(Token, TokenStream, Span)>,
/// All branches after the first wildcard, including further wildcards.
pub unreachable: Vec<(CfgSelectRule, TokenStream, Span)>,
}

/// Parses a `TokenTree` that must be of the form `{ /* ... */ }`, and returns a `TokenStream` where
/// the surrounding braces are stripped.
fn parse_token_tree<'a>(p: &mut Parser<'a>) -> PResult<'a, TokenStream> {
// Generate an error if the `=>` is not followed by `{`.
if p.token != token::OpenBrace {
p.expect(exp!(OpenBrace))?;
}

// Strip the outer '{' and '}'.
match p.parse_token_tree() {
TokenTree::Token(..) => unreachable!("because of the expect above"),
TokenTree::Delimited(.., tts) => Ok(tts),
}
}

pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches> {
let mut branches = CfgSelectBranches::default();

while p.token != token::Eof {
if p.token.is_keyword(kw::Underscore) {
let underscore = p.token;
p.bump();
p.expect(exp!(FatArrow))?;

let tts = parse_token_tree(p)?;
let span = underscore.span.to(p.token.span);

match branches.wildcard {
None => branches.wildcard = Some((underscore, tts, span)),
Some(_) => {
branches.unreachable.push((CfgSelectRule::Wildcard(underscore), tts, span))
}
}
} else {
let meta_item = p.parse_meta_item_inner()?;
p.expect(exp!(FatArrow))?;

let tts = parse_token_tree(p)?;
let span = meta_item.span().to(p.token.span);

match branches.wildcard {
None => branches.reachable.push((meta_item, tts, span)),
Some(_) => branches.unreachable.push((CfgSelectRule::Cfg(meta_item), tts, span)),
}
}
}

Ok(branches)
}
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod asm;
pub mod attr;
mod attr_wrapper;
pub mod cfg_select;
mod diagnostics;
mod expr;
mod generics;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ symbols! {
cfg_relocation_model,
cfg_sanitize,
cfg_sanitizer_cfi,
cfg_select,
cfg_target_abi,
cfg_target_compact,
cfg_target_feature,
Expand Down
25 changes: 5 additions & 20 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,17 @@ pub macro assert_matches {
/// ```
/// #![feature(cfg_select)]
///
/// let _some_string = cfg_select! {{
/// let _some_string = cfg_select! {
/// unix => { "With great power comes great electricity bills" }
/// _ => { "Behind every successful diet is an unwatched pizza" }
/// }};
/// };
/// ```
#[unstable(feature = "cfg_select", issue = "115585")]
#[rustc_diagnostic_item = "cfg_select"]
#[rustc_macro_transparency = "semitransparent"]
pub macro cfg_select {
({ $($tt:tt)* }) => {{
$crate::cfg_select! { $($tt)* }
}},
(_ => { $($output:tt)* }) => {
$($output)*
},
(
$cfg:meta => $output:tt
$($( $rest:tt )+)?
) => {
#[cfg($cfg)]
$crate::cfg_select! { _ => $output }
$(
#[cfg(not($cfg))]
$crate::cfg_select! { $($rest)+ }
)?
},
#[rustc_builtin_macro]
pub macro cfg_select($($tt:tt)*) {
/* compiler built-in */
}

/// Asserts that a boolean expression is `true` at runtime.
Expand Down
Loading
Loading