Skip to content

Commit 78bd6ab

Browse files
Rollup merge of #152341 - folkertdev:cfg-select-optional-comma, r=JonathanBrouwer
`cfg_select!`: allow optional comma after `{ /* ... */ }`
2 parents e01abd7 + 053f76e commit 78bd6ab

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

compiler/rustc_parse/src/parser/cfg_select.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ use crate::exp;
77
use crate::parser::{AttrWrapper, ForceCollect, Parser, Restrictions, Trailing, UsePreAttrPos};
88

99
impl<'a> Parser<'a> {
10-
/// Parses a `TokenTree` consisting either of `{ /* ... */ }` (and strip the braces) or an
11-
/// expression followed by a comma (and strip the comma).
10+
/// Parses a `TokenTree` consisting either of `{ /* ... */ }` optionally followed by a comma
11+
/// (and strip the braces and the optional comma) or an expression followed by a comma
12+
/// (and strip the comma).
1213
pub fn parse_delimited_token_tree(&mut self) -> PResult<'a, TokenStream> {
1314
if self.token == token::OpenBrace {
1415
// Strip the outer '{' and '}'.
1516
match self.parse_token_tree() {
16-
TokenTree::Token(..) => unreachable!("because of the expect above"),
17-
TokenTree::Delimited(.., tts) => return Ok(tts),
17+
TokenTree::Token(..) => unreachable!("because the current token is a '{{'"),
18+
TokenTree::Delimited(.., tts) => {
19+
// Optionally end with a comma.
20+
let _ = self.eat(exp!(Comma));
21+
return Ok(tts);
22+
}
1823
}
1924
}
2025
let expr = self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |p, _| {

tests/ui/macros/cfg_select.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn arm_rhs_expr_3() -> i32 {
3434
any(true) => 1,
3535
any(false) => 2,
3636
any(true) => { 42 }
37+
any(true) => { 42 },
3738
any(false) => -1 as i32,
3839
any(true) => 2 + 2,
3940
any(false) => "",
@@ -62,7 +63,7 @@ fn expand_to_statements() -> i32 {
6263
}
6364

6465
type ExpandToType = cfg_select! {
65-
unix => u32,
66+
unix => { u32 },
6667
_ => i32,
6768
};
6869

@@ -113,7 +114,7 @@ impl T for S {
113114
cfg_select! {
114115
false => {
115116
fn a() {}
116-
}
117+
},
117118
_ => {
118119
fn b() {}
119120
}
@@ -145,7 +146,7 @@ cfg_select! {
145146

146147
cfg_select! {
147148
unix => {}
148-
not(unix) => {}
149+
not(unix) => {},
149150
_ => {}
150151
//~^ WARN unreachable configuration predicate
151152
}

tests/ui/macros/cfg_select.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: none of the predicates in this `cfg_select` evaluated to true
2-
--> $DIR/cfg_select.rs:161:1
2+
--> $DIR/cfg_select.rs:162:1
33
|
44
LL | / cfg_select! {
55
LL | |
@@ -8,55 +8,55 @@ LL | | }
88
| |_^
99

1010
error: none of the predicates in this `cfg_select` evaluated to true
11-
--> $DIR/cfg_select.rs:166:1
11+
--> $DIR/cfg_select.rs:167:1
1212
|
1313
LL | cfg_select! {}
1414
| ^^^^^^^^^^^^^^
1515

1616
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `=>`
17-
--> $DIR/cfg_select.rs:170:5
17+
--> $DIR/cfg_select.rs:171:5
1818
|
1919
LL | => {}
2020
| ^^
2121

2222
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found expression
23-
--> $DIR/cfg_select.rs:175:5
23+
--> $DIR/cfg_select.rs:176:5
2424
|
2525
LL | () => {}
2626
| ^^ expressions are not allowed here
2727

2828
error[E0539]: malformed `cfg_select` macro input
29-
--> $DIR/cfg_select.rs:180:5
29+
--> $DIR/cfg_select.rs:181:5
3030
|
3131
LL | "str" => {}
3232
| ^^^^^ expected a valid identifier here
3333

3434
error[E0539]: malformed `cfg_select` macro input
35-
--> $DIR/cfg_select.rs:185:5
35+
--> $DIR/cfg_select.rs:186:5
3636
|
3737
LL | a::b => {}
3838
| ^^^^ expected a valid identifier here
3939

4040
error[E0537]: invalid predicate `a`
41-
--> $DIR/cfg_select.rs:190:5
41+
--> $DIR/cfg_select.rs:191:5
4242
|
4343
LL | a() => {}
4444
| ^^^
4545

4646
error: expected one of `(`, `::`, `=>`, or `=`, found `+`
47-
--> $DIR/cfg_select.rs:195:7
47+
--> $DIR/cfg_select.rs:196:7
4848
|
4949
LL | a + 1 => {}
5050
| ^ expected one of `(`, `::`, `=>`, or `=`
5151

5252
error: expected one of `(`, `::`, `=>`, or `=`, found `!`
53-
--> $DIR/cfg_select.rs:201:8
53+
--> $DIR/cfg_select.rs:202:8
5454
|
5555
LL | cfg!() => {}
5656
| ^ expected one of `(`, `::`, `=>`, or `=`
5757

5858
warning: unreachable configuration predicate
59-
--> $DIR/cfg_select.rs:136:5
59+
--> $DIR/cfg_select.rs:137:5
6060
|
6161
LL | _ => {}
6262
| - always matches
@@ -70,33 +70,33 @@ LL | #![warn(unreachable_cfg_select_predicates)] // Unused warnings are disabled
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7171

7272
warning: unreachable configuration predicate
73-
--> $DIR/cfg_select.rs:142:5
73+
--> $DIR/cfg_select.rs:143:5
7474
|
7575
LL | true => {}
7676
| ---- always matches
7777
LL | _ => {}
7878
| ^ this configuration predicate is never reached
7979

8080
warning: unreachable configuration predicate
81-
--> $DIR/cfg_select.rs:149:5
81+
--> $DIR/cfg_select.rs:150:5
8282
|
8383
LL | _ => {}
8484
| ^ this configuration predicate is never reached
8585

8686
warning: unreachable configuration predicate
87-
--> $DIR/cfg_select.rs:155:5
87+
--> $DIR/cfg_select.rs:156:5
8888
|
8989
LL | test => {}
9090
| ^^^^ this configuration predicate is never reached
9191

9292
warning: unreachable configuration predicate
93-
--> $DIR/cfg_select.rs:157:5
93+
--> $DIR/cfg_select.rs:158:5
9494
|
9595
LL | _ => {}
9696
| ^ this configuration predicate is never reached
9797

9898
warning: unexpected `cfg` condition name: `a`
99-
--> $DIR/cfg_select.rs:195:5
99+
--> $DIR/cfg_select.rs:196:5
100100
|
101101
LL | a + 1 => {}
102102
| ^ help: found config with similar value: `target_feature = "a"`
@@ -107,7 +107,7 @@ LL | a + 1 => {}
107107
= note: `#[warn(unexpected_cfgs)]` on by default
108108

109109
warning: unexpected `cfg` condition name: `cfg`
110-
--> $DIR/cfg_select.rs:201:5
110+
--> $DIR/cfg_select.rs:202:5
111111
|
112112
LL | cfg!() => {}
113113
| ^^^

0 commit comments

Comments
 (0)