From 45a441b61caa30159e8832bed73849fbe8ff59d2 Mon Sep 17 00:00:00 2001 From: cynecx Date: Fri, 25 Feb 2022 22:52:17 +0100 Subject: [PATCH] `check_used` should only look at actual `used` attributes --- compiler/rustc_passes/src/check_attr.rs | 4 ++-- src/test/ui/attributes/used_with_arg_no_mangle.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/attributes/used_with_arg_no_mangle.rs diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 68efbbb74c300..6e4907fe518ae 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1740,8 +1740,8 @@ impl CheckAttrVisitor<'_> { fn check_used(&self, attrs: &[Attribute], target: Target) { let mut used_linker_span = None; let mut used_compiler_span = None; - for attr in attrs { - if attr.has_name(sym::used) && target != Target::Static { + for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) { + if target != Target::Static { self.tcx .sess .span_err(attr.span, "attribute must be applied to a `static` variable"); diff --git a/src/test/ui/attributes/used_with_arg_no_mangle.rs b/src/test/ui/attributes/used_with_arg_no_mangle.rs new file mode 100644 index 0000000000000..d0bbe76ef3e57 --- /dev/null +++ b/src/test/ui/attributes/used_with_arg_no_mangle.rs @@ -0,0 +1,9 @@ +// check-pass + +#![feature(used_with_arg)] + +#[used(linker)] +#[no_mangle] // accidentally detected as `used(compiler)` +pub static GLOB: usize = 0; + +fn main() {}