From 5eb863698964b026149f4ed9f958639eb290f5b3 Mon Sep 17 00:00:00 2001 From: Urgau Date: Wed, 16 Oct 2024 09:41:49 +0200 Subject: [PATCH] Handle gracefully true/false in `cfg(target(..))` compact --- compiler/rustc_attr/src/builtin.rs | 8 +++++++- tests/ui/cfg/cfg-target-compact-errors.rs | 4 ++++ tests/ui/cfg/cfg-target-compact-errors.stderr | 8 +++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index c1db4d07dfc..bbb17497684 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -723,7 +723,13 @@ pub fn eval_condition( } mis.iter().fold(true, |res, mi| { - let mut mi = mi.meta_item().unwrap().clone(); + let Some(mut mi) = mi.meta_item().cloned() else { + dcx.emit_err(session_diagnostics::CfgPredicateIdentifier { + span: mi.span(), + }); + return false; + }; + if let [seg, ..] = &mut mi.path.segments[..] { seg.ident.name = Symbol::intern(&format!("target_{}", seg.ident.name)); } diff --git a/tests/ui/cfg/cfg-target-compact-errors.rs b/tests/ui/cfg/cfg-target-compact-errors.rs index daacbb2851d..e00d42fe4d9 100644 --- a/tests/ui/cfg/cfg-target-compact-errors.rs +++ b/tests/ui/cfg/cfg-target-compact-errors.rs @@ -14,4 +14,8 @@ fn two() {} //~^ ERROR invalid predicate `target_pointer` fn three() {} +#[cfg(target(true))] +//~^ ERROR `cfg` predicate key must be an identifier +fn four() {} + fn main() {} diff --git a/tests/ui/cfg/cfg-target-compact-errors.stderr b/tests/ui/cfg/cfg-target-compact-errors.stderr index bb858301eb5..219d9732c32 100644 --- a/tests/ui/cfg/cfg-target-compact-errors.stderr +++ b/tests/ui/cfg/cfg-target-compact-errors.stderr @@ -16,7 +16,13 @@ error[E0537]: invalid predicate `target_pointer` LL | #[cfg(target(os = "linux", pointer(width = "64")))] | ^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 3 previous errors +error: `cfg` predicate key must be an identifier + --> $DIR/cfg-target-compact-errors.rs:17:14 + | +LL | #[cfg(target(true))] + | ^^^^ + +error: aborting due to 4 previous errors Some errors have detailed explanations: E0537, E0565. For more information about an error, try `rustc --explain E0537`.