Skip to content

Commit 010e3ef

Browse files
Rollup merge of #143941 - folkertdev:cfg-select-docs, r=traviscross
update `cfg_select!` documentation tracking issue: #115585 After #143461, and with an eye on a soon(ish) stabilization, I think the docs need some work. The existing text read more like a motivation for the feature existing to me, so I've tried to now be a bit more descriptive. Still, suggestions are very welcome. I also added a test for an empty `select! {}` because it's just the sort of thing that might break. r? ``@traviscross``
2 parents 305befe + d0153f5 commit 010e3ef

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ builtin_macros_cfg_accessible_literal_path = `cfg_accessible` path cannot be a l
8181
builtin_macros_cfg_accessible_multiple_paths = multiple `cfg_accessible` paths are specified
8282
builtin_macros_cfg_accessible_unspecified_path = `cfg_accessible` path is not specified
8383
84-
builtin_macros_cfg_select_no_matches = none of the rules in this `cfg_select` evaluated to true
84+
builtin_macros_cfg_select_no_matches = none of the predicates in this `cfg_select` evaluated to true
8585
86-
builtin_macros_cfg_select_unreachable = unreachable rule
86+
builtin_macros_cfg_select_unreachable = unreachable predicate
8787
.label = always matches
88-
.label2 = this rules is never reached
88+
.label2 = this predicate is never reached
8989
9090
builtin_macros_coerce_pointee_requires_maybe_sized = `derive(CoercePointee)` requires `{$name}` to be marked `?Sized`
9191

compiler/rustc_builtin_macros/src/cfg_select.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustc_ast::tokenstream::TokenStream;
22
use rustc_attr_parsing as attr;
33
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
4-
use rustc_parse::parser::cfg_select::{CfgSelectBranches, CfgSelectRule, parse_cfg_select};
4+
use rustc_parse::parser::cfg_select::{CfgSelectBranches, CfgSelectPredicate, parse_cfg_select};
55
use rustc_span::{Ident, Span, sym};
66

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

9-
/// Selects the first arm whose rule evaluates to true.
9+
/// Selects the first arm whose predicate evaluates to true.
1010
fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> {
1111
for (cfg, tt, arm_span) in branches.reachable {
1212
if attr::cfg_matches(
@@ -30,11 +30,11 @@ pub(super) fn expand_cfg_select<'cx>(
3030
ExpandResult::Ready(match parse_cfg_select(&mut ecx.new_parser_from_tts(tts)) {
3131
Ok(branches) => {
3232
if let Some((underscore, _, _)) = branches.wildcard {
33-
// Warn for every unreachable rule. We store the fully parsed branch for rustfmt.
34-
for (rule, _, _) in &branches.unreachable {
35-
let span = match rule {
36-
CfgSelectRule::Wildcard(underscore) => underscore.span,
37-
CfgSelectRule::Cfg(cfg) => cfg.span(),
33+
// Warn for every unreachable predicate. We store the fully parsed branch for rustfmt.
34+
for (predicate, _, _) in &branches.unreachable {
35+
let span = match predicate {
36+
CfgSelectPredicate::Wildcard(underscore) => underscore.span,
37+
CfgSelectPredicate::Cfg(cfg) => cfg.span(),
3838
};
3939
let err = CfgSelectUnreachable { span, wildcard_span: underscore.span };
4040
ecx.dcx().emit_warn(err);
@@ -50,7 +50,7 @@ pub(super) fn expand_cfg_select<'cx>(
5050
Ident::with_dummy_span(sym::cfg_select),
5151
);
5252
} else {
53-
// Emit a compiler error when none of the rules matched.
53+
// Emit a compiler error when none of the predicates matched.
5454
let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp });
5555
DummyResult::any(sp, guar)
5656
}

compiler/rustc_parse/src/parser/cfg_select.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::Span;
77
use crate::exp;
88
use crate::parser::Parser;
99

10-
pub enum CfgSelectRule {
10+
pub enum CfgSelectPredicate {
1111
Cfg(MetaItemInner),
1212
Wildcard(Token),
1313
}
@@ -20,7 +20,7 @@ pub struct CfgSelectBranches {
2020
pub wildcard: Option<(Token, TokenStream, Span)>,
2121
/// All branches after the first wildcard, including further wildcards.
2222
/// These branches are kept for formatting.
23-
pub unreachable: Vec<(CfgSelectRule, TokenStream, Span)>,
23+
pub unreachable: Vec<(CfgSelectPredicate, TokenStream, Span)>,
2424
}
2525

2626
/// Parses a `TokenTree` that must be of the form `{ /* ... */ }`, and returns a `TokenStream` where
@@ -52,7 +52,7 @@ pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches
5252
match branches.wildcard {
5353
None => branches.wildcard = Some((underscore, tts, span)),
5454
Some(_) => {
55-
branches.unreachable.push((CfgSelectRule::Wildcard(underscore), tts, span))
55+
branches.unreachable.push((CfgSelectPredicate::Wildcard(underscore), tts, span))
5656
}
5757
}
5858
} else {
@@ -64,7 +64,9 @@ pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches
6464

6565
match branches.wildcard {
6666
None => branches.reachable.push((meta_item, tts, span)),
67-
Some(_) => branches.unreachable.push((CfgSelectRule::Cfg(meta_item), tts, span)),
67+
Some(_) => {
68+
branches.unreachable.push((CfgSelectPredicate::Cfg(meta_item), tts, span))
69+
}
6870
}
6971
}
7072
}

library/core/src/macros/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,14 @@ pub macro assert_matches {
196196
},
197197
}
198198

199-
/// A macro for defining `#[cfg]` match-like statements.
199+
/// Selects code at compile-time based on `cfg` predicates.
200200
///
201-
/// It is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of
202-
/// `#[cfg]` cases, emitting the implementation which matches first.
201+
/// This macro evaluates, at compile-time, a series of `cfg` predicates,
202+
/// selects the first that is true, and emits the code guarded by that
203+
/// predicate. The code guarded by other predicates is not emitted.
203204
///
204-
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
205-
/// without having to rewrite each clause multiple times.
206-
///
207-
/// Trailing `_` wildcard match arms are **optional** and they indicate a fallback branch when
208-
/// all previous declarations do not evaluate to true.
205+
/// An optional trailing `_` wildcard can be used to specify a fallback. If
206+
/// none of the predicates are true, a [`compile_error`] is emitted.
209207
///
210208
/// # Example
211209
///
@@ -225,7 +223,7 @@ pub macro assert_matches {
225223
/// }
226224
/// ```
227225
///
228-
/// If desired, it is possible to return expressions through the use of surrounding braces:
226+
/// The `cfg_select!` macro can also be used in expression position:
229227
///
230228
/// ```
231229
/// #![feature(cfg_select)]

tests/ui/macros/cfg_select.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ fn arm_rhs_must_be_in_braces() -> i32 {
1818
cfg_select! {
1919
_ => {}
2020
true => {}
21-
//~^ WARN unreachable rule
21+
//~^ WARN unreachable predicate
2222
}
2323

2424
cfg_select! {
25-
//~^ ERROR none of the rules in this `cfg_select` evaluated to true
25+
//~^ ERROR none of the predicates in this `cfg_select` evaluated to true
2626
false => {}
2727
}
28+
29+
cfg_select! {}
30+
//~^ ERROR none of the predicates in this `cfg_select` evaluated to true

tests/ui/macros/cfg_select.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: expected `{`, found `1`
44
LL | true => 1
55
| ^ expected `{`
66

7-
warning: unreachable rule
7+
warning: unreachable predicate
88
--> $DIR/cfg_select.rs:20:5
99
|
1010
LL | _ => {}
1111
| - always matches
1212
LL | true => {}
13-
| ^^^^ this rules is never reached
13+
| ^^^^ this predicate is never reached
1414

15-
error: none of the rules in this `cfg_select` evaluated to true
15+
error: none of the predicates in this `cfg_select` evaluated to true
1616
--> $DIR/cfg_select.rs:24:1
1717
|
1818
LL | / cfg_select! {
@@ -21,5 +21,11 @@ LL | | false => {}
2121
LL | | }
2222
| |_^
2323

24-
error: aborting due to 2 previous errors; 1 warning emitted
24+
error: none of the predicates in this `cfg_select` evaluated to true
25+
--> $DIR/cfg_select.rs:29:1
26+
|
27+
LL | cfg_select! {}
28+
| ^^^^^^^^^^^^^^
29+
30+
error: aborting due to 3 previous errors; 1 warning emitted
2531

0 commit comments

Comments
 (0)