Skip to content

cfg_select: Support unbraced expressions #145233

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

Merged
merged 1 commit into from
Aug 15, 2025
Merged
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
30 changes: 19 additions & 11 deletions compiler/rustc_parse/src/parser/cfg_select.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rustc_ast::token::Token;
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::util::classify;
use rustc_ast::{MetaItemInner, token};
use rustc_errors::PResult;
use rustc_span::Span;

use crate::exp;
use crate::parser::Parser;
use crate::parser::{AttrWrapper, ForceCollect, Parser, Restrictions, Trailing, UsePreAttrPos};

pub enum CfgSelectPredicate {
Cfg(MetaItemInner),
Expand All @@ -23,19 +24,26 @@ pub struct CfgSelectBranches {
pub unreachable: Vec<(CfgSelectPredicate, TokenStream, Span)>,
}

/// Parses a `TokenTree` that must be of the form `{ /* ... */ }`, and returns a `TokenStream` where
/// the surrounding braces are stripped.
/// Parses a `TokenTree` consisting either of `{ /* ... */ }` (and strip the braces) or an
/// expression followed by a comma (and strip the comma).
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))?;
if p.token == token::OpenBrace {
// Strip the outer '{' and '}'.
match p.parse_token_tree() {
TokenTree::Token(..) => unreachable!("because of the expect above"),
TokenTree::Delimited(.., tts) => return Ok(tts),
}
}

// Strip the outer '{' and '}'.
match p.parse_token_tree() {
TokenTree::Token(..) => unreachable!("because of the expect above"),
TokenTree::Delimited(.., tts) => Ok(tts),
let expr = p.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |p, _| {
p.parse_expr_res(Restrictions::STMT_EXPR, AttrWrapper::empty())
.map(|(expr, _)| (expr, Trailing::No, UsePreAttrPos::No))
})?;
if !classify::expr_is_complete(&expr) && p.token != token::CloseBrace && p.token != token::Eof {
p.expect(exp!(Comma))?;
} else {
let _ = p.eat(exp!(Comma));
}
Ok(TokenStream::from_ast(&expr))
}

pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches> {
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ pub macro assert_matches {
/// }
/// ```
///
/// The `cfg_select!` macro can also be used in expression position:
/// The `cfg_select!` macro can also be used in expression position, with or without braces on the
/// right-hand side:
///
/// ```
/// #![feature(cfg_select)]
///
/// let _some_string = cfg_select! {
/// unix => { "With great power comes great electricity bills" }
/// unix => "With great power comes great electricity bills",
/// _ => { "Behind every successful diet is an unwatched pizza" }
/// };
/// ```
Expand Down
36 changes: 34 additions & 2 deletions tests/ui/macros/cfg_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,42 @@ fn print() {
});
}

fn arm_rhs_must_be_in_braces() -> i32 {
fn print_2() {
println!(cfg_select! {
unix => "unix",
_ => "not unix",
});
}

fn arm_rhs_expr_1() -> i32 {
cfg_select! {
true => 1
//~^ ERROR: expected `{`, found `1`
}
}

fn arm_rhs_expr_2() -> i32 {
cfg_select! {
true => 1,
false => 2
}
}

fn arm_rhs_expr_3() -> i32 {
cfg_select! {
true => 1,
false => 2,
true => { 42 }
false => -1 as i32,
true => 2 + 2,
false => "",
true => if true { 42 } else { 84 }
false => if true { 42 } else { 84 },
true => return 42,
false => loop {}
true => (1, 2),
false => (1, 2,),
true => todo!(),
false => println!("hello"),
}
}

Expand Down
14 changes: 4 additions & 10 deletions tests/ui/macros/cfg_select.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
error: expected `{`, found `1`
--> $DIR/cfg_select.rs:13:17
|
LL | true => 1
| ^ expected `{`

warning: unreachable predicate
--> $DIR/cfg_select.rs:20:5
--> $DIR/cfg_select.rs:52:5
|
LL | _ => {}
| - always matches
LL | true => {}
| ^^^^ this predicate is never reached

error: none of the predicates in this `cfg_select` evaluated to true
--> $DIR/cfg_select.rs:24:1
--> $DIR/cfg_select.rs:56:1
|
LL | / cfg_select! {
LL | |
Expand All @@ -22,10 +16,10 @@ LL | | }
| |_^

error: none of the predicates in this `cfg_select` evaluated to true
--> $DIR/cfg_select.rs:29:1
--> $DIR/cfg_select.rs:61:1
|
LL | cfg_select! {}
| ^^^^^^^^^^^^^^

error: aborting due to 3 previous errors; 1 warning emitted
error: aborting due to 2 previous errors; 1 warning emitted

Loading