Skip to content

Commit d15006c

Browse files
authored
Rollup merge of #94295 - Urgau:cfg-always-eval-all-predicate, r=petrochenkov
Always evaluate all cfg predicate in all() and any() This pull-request adjust the handling of the `all()` and `any()` to always evaluate every cfg predicate because not doing so result in accepting incorrect `cfg`: ```rust #[cfg(any(unix, foo::bar))] // Should error on foo::bar, but does not on unix platform (but does on non unix platform) fn foo1() {} #[cfg(all(foo, foo::bar))] // Should error on foo::bar, but does not fn foo2() {} #[cfg(all(foo::bar, foo))] // Correctly error on foo::bar fn foo3() {} #[cfg(any(foo::bar, foo))] // Correctly error on foo::bar fn foo4() {} ``` This pull-request take the side to directly turn it into a hard error instead of having a future incompatibility lint because the combination to get this incorrect behavior is unusual and highly probable that some code have this without noticing. A [search](https://cs.github.com/?scopeName=All+repos&scope=&q=lang%3Arust+%2Fany%5C%28%5Ba-zA-Z%5D%2C+%5Ba-zA-Z%5D%2B%3A%3A%5Ba-zA-Z%5D%2B%2F) on Github reveal no such instance nevertheless a Crater run should probably be done before merging this. This was discover in #94175 when trying to lint on the second predicate. Also note that this seems to have being introduce with Rust 1.27.0: https://rust.godbolt.org/z/KnfqKv15f. r? `@petrochenkov`
2 parents c183d4a + f57cc8c commit d15006c

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

compiler/rustc_attr/src/builtin.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,18 @@ pub fn eval_condition(
603603
match cfg.name_or_empty() {
604604
sym::any => mis
605605
.iter()
606-
.any(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
606+
// We don't use any() here, because we want to evaluate all cfg condition
607+
// as eval_condition can (and does) extra checks
608+
.fold(false, |res, mi| {
609+
res | eval_condition(mi.meta_item().unwrap(), sess, features, eval)
610+
}),
607611
sym::all => mis
608612
.iter()
609-
.all(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
613+
// We don't use all() here, because we want to evaluate all cfg condition
614+
// as eval_condition can (and does) extra checks
615+
.fold(true, |res, mi| {
616+
res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
617+
}),
610618
sym::not => {
611619
if mis.len() != 1 {
612620
struct_span_err!(

src/test/ui/cfg/cfg-path-error.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-fail
2+
3+
#[cfg(any(foo, foo::bar))]
4+
//~^ERROR `cfg` predicate key must be an identifier
5+
fn foo1() {}
6+
7+
#[cfg(any(foo::bar, foo))]
8+
//~^ERROR `cfg` predicate key must be an identifier
9+
fn foo2() {}
10+
11+
#[cfg(all(foo, foo::bar))]
12+
//~^ERROR `cfg` predicate key must be an identifier
13+
fn foo3() {}
14+
15+
#[cfg(all(foo::bar, foo))]
16+
//~^ERROR `cfg` predicate key must be an identifier
17+
fn foo4() {}
18+
19+
fn main() {}

src/test/ui/cfg/cfg-path-error.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: `cfg` predicate key must be an identifier
2+
--> $DIR/cfg-path-error.rs:3:16
3+
|
4+
LL | #[cfg(any(foo, foo::bar))]
5+
| ^^^^^^^^
6+
7+
error: `cfg` predicate key must be an identifier
8+
--> $DIR/cfg-path-error.rs:7:11
9+
|
10+
LL | #[cfg(any(foo::bar, foo))]
11+
| ^^^^^^^^
12+
13+
error: `cfg` predicate key must be an identifier
14+
--> $DIR/cfg-path-error.rs:11:16
15+
|
16+
LL | #[cfg(all(foo, foo::bar))]
17+
| ^^^^^^^^
18+
19+
error: `cfg` predicate key must be an identifier
20+
--> $DIR/cfg-path-error.rs:15:11
21+
|
22+
LL | #[cfg(all(foo::bar, foo))]
23+
| ^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+

0 commit comments

Comments
 (0)