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 https://github.com/rust-lang/rust/pull/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`
This commit is contained in:
Matthias Krüger 2022-03-18 21:50:46 +01:00 committed by GitHub
commit d15006ceca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 2 deletions

View file

@ -603,10 +603,18 @@ pub fn eval_condition(
match cfg.name_or_empty() {
sym::any => mis
.iter()
.any(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
// We don't use any() here, because we want to evaluate all cfg condition
// as eval_condition can (and does) extra checks
.fold(false, |res, mi| {
res | eval_condition(mi.meta_item().unwrap(), sess, features, eval)
}),
sym::all => mis
.iter()
.all(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
// We don't use all() here, because we want to evaluate all cfg condition
// as eval_condition can (and does) extra checks
.fold(true, |res, mi| {
res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
}),
sym::not => {
if mis.len() != 1 {
struct_span_err!(

View file

@ -0,0 +1,19 @@
// check-fail
#[cfg(any(foo, foo::bar))]
//~^ERROR `cfg` predicate key must be an identifier
fn foo1() {}
#[cfg(any(foo::bar, foo))]
//~^ERROR `cfg` predicate key must be an identifier
fn foo2() {}
#[cfg(all(foo, foo::bar))]
//~^ERROR `cfg` predicate key must be an identifier
fn foo3() {}
#[cfg(all(foo::bar, foo))]
//~^ERROR `cfg` predicate key must be an identifier
fn foo4() {}
fn main() {}

View file

@ -0,0 +1,26 @@
error: `cfg` predicate key must be an identifier
--> $DIR/cfg-path-error.rs:3:16
|
LL | #[cfg(any(foo, foo::bar))]
| ^^^^^^^^
error: `cfg` predicate key must be an identifier
--> $DIR/cfg-path-error.rs:7:11
|
LL | #[cfg(any(foo::bar, foo))]
| ^^^^^^^^
error: `cfg` predicate key must be an identifier
--> $DIR/cfg-path-error.rs:11:16
|
LL | #[cfg(all(foo, foo::bar))]
| ^^^^^^^^
error: `cfg` predicate key must be an identifier
--> $DIR/cfg-path-error.rs:15:11
|
LL | #[cfg(all(foo::bar, foo))]
| ^^^^^^^^
error: aborting due to 4 previous errors