From f57cc8ca5cd344a6111650a8ba4744b4e4859da8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lo=C3=AFc=20BRANSTETT?= Date: Wed, 23 Feb 2022 15:44:57 +0100 Subject: [PATCH] Always evaluate all cfg predicate in all() and any() --- compiler/rustc_attr/src/builtin.rs | 12 ++++++++++-- src/test/ui/cfg/cfg-path-error.rs | 19 +++++++++++++++++++ src/test/ui/cfg/cfg-path-error.stderr | 26 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/cfg/cfg-path-error.rs create mode 100644 src/test/ui/cfg/cfg-path-error.stderr diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 49043e9f5f9..628da8b6bbb 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -594,10 +594,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!( diff --git a/src/test/ui/cfg/cfg-path-error.rs b/src/test/ui/cfg/cfg-path-error.rs new file mode 100644 index 00000000000..5bf80bd74b8 --- /dev/null +++ b/src/test/ui/cfg/cfg-path-error.rs @@ -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() {} diff --git a/src/test/ui/cfg/cfg-path-error.stderr b/src/test/ui/cfg/cfg-path-error.stderr new file mode 100644 index 00000000000..84b44b2b0c2 --- /dev/null +++ b/src/test/ui/cfg/cfg-path-error.stderr @@ -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 + -- 2.44.0