diff --git a/compiler/rustc_builtin_macros/src/cfg_select.rs b/compiler/rustc_builtin_macros/src/cfg_select.rs index f2e454c3d4373..b77a121ca0b95 100644 --- a/compiler/rustc_builtin_macros/src/cfg_select.rs +++ b/compiler/rustc_builtin_macros/src/cfg_select.rs @@ -10,6 +10,7 @@ use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable}; /// Selects the first arm whose predicate evaluates to true. fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> { + let mut result = None; for (cfg, tt, arm_span) in branches.reachable { if let EvalConfigResult::True = attr::eval_config_entry( &ecx.sess, @@ -17,11 +18,13 @@ fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenSt ecx.current_expansion.lint_node_id, ShouldEmit::ErrorsAndLints, ) { - return Some((tt, arm_span)); + // FIXME(#149215) Ideally we should short-circuit here, but `eval_config_entry` currently emits lints so we cannot do this yet. + result.get_or_insert((tt, arm_span)); } } - branches.wildcard.map(|(_, tt, span)| (tt, span)) + let wildcard = branches.wildcard.map(|(_, tt, span)| (tt, span)); + result.or(wildcard) } pub(super) fn expand_cfg_select<'cx>( diff --git a/tests/ui/check-cfg/cfg-select.rs b/tests/ui/check-cfg/cfg-select.rs new file mode 100644 index 0000000000000..39703489818d8 --- /dev/null +++ b/tests/ui/check-cfg/cfg-select.rs @@ -0,0 +1,18 @@ +//@ check-pass + +#![feature(cfg_select)] +#![crate_type = "lib"] + +cfg_select! { + true => {} + invalid_cfg1 => {} + //~^ WARN unexpected `cfg` condition name + _ => {} +} + +cfg_select! { + invalid_cfg2 => {} + //~^ WARN unexpected `cfg` condition name + true => {} + _ => {} +} diff --git a/tests/ui/check-cfg/cfg-select.stderr b/tests/ui/check-cfg/cfg-select.stderr new file mode 100644 index 0000000000000..23ed5918e71ce --- /dev/null +++ b/tests/ui/check-cfg/cfg-select.stderr @@ -0,0 +1,22 @@ +warning: unexpected `cfg` condition name: `invalid_cfg1` + --> $DIR/cfg-select.rs:8:5 + | +LL | invalid_cfg1 => {} + | ^^^^^^^^^^^^ + | + = help: expected names are: `FALSE` and `test` and 31 more + = help: to expect this configuration use `--check-cfg=cfg(invalid_cfg1)` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + +warning: unexpected `cfg` condition name: `invalid_cfg2` + --> $DIR/cfg-select.rs:14:5 + | +LL | invalid_cfg2 => {} + | ^^^^^^^^^^^^ + | + = help: to expect this configuration use `--check-cfg=cfg(invalid_cfg2)` + = note: see for more information about checking conditional configuration + +warning: 2 warnings emitted +