about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-06-29 22:10:59 +0200
committerGitHub <noreply@github.com>2024-06-29 22:10:59 +0200
commit5ea1a03ccaa98e880a7e32290c1e929955326a1a (patch)
tree704cd8e9551edcb621d3e5a02863449c5cc071a3
parent9879b4606cab3f93cdae4f0546caca4547f0b093 (diff)
parent9c0ce05d246811a977b6564b7df2b6947bd6903b (diff)
Rollup merge of #127118 - surechen:fix_126789, r=jieyouxu
Show `used attribute`'s kind for user when find it isn't applied to a `static` variable.

For example :
```rust
extern "C" {
    #[used] //~ ERROR attribute must be applied to a `static` variable
    static FOO: i32; // show the kind of this item to help user understand why the error is reported.
}
```

fixes #126789
-rw-r--r--compiler/rustc_passes/messages.ftl1
-rw-r--r--compiler/rustc_passes/src/check_attr.rs10
-rw-r--r--compiler/rustc_passes/src/errors.rs3
-rw-r--r--tests/ui/attributes/used-issue-126789.rs6
-rw-r--r--tests/ui/attributes/used-issue-126789.stderr10
-rw-r--r--tests/ui/used.stderr8
6 files changed, 35 insertions, 3 deletions
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl
index 5a560325ab9..d7513fbad63 100644
--- a/compiler/rustc_passes/messages.ftl
+++ b/compiler/rustc_passes/messages.ftl
@@ -782,6 +782,7 @@ passes_used_compiler_linker =
 
 passes_used_static =
     attribute must be applied to a `static` variable
+    .label = but this is a {$target}
 
 passes_useless_assignment =
     useless assignment of {$is_field_assign ->
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 5f8e4a8b7a7..0eb9d1ce59f 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -274,7 +274,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
         }
 
         self.check_repr(attrs, span, target, item, hir_id);
-        self.check_used(attrs, target);
+        self.check_used(attrs, target, span);
     }
 
     fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
@@ -1930,12 +1930,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
         }
     }
 
-    fn check_used(&self, attrs: &[Attribute], target: Target) {
+    fn check_used(&self, attrs: &[Attribute], target: Target, target_span: Span) {
         let mut used_linker_span = None;
         let mut used_compiler_span = None;
         for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) {
             if target != Target::Static {
-                self.dcx().emit_err(errors::UsedStatic { span: attr.span });
+                self.dcx().emit_err(errors::UsedStatic {
+                    attr_span: attr.span,
+                    span: target_span,
+                    target: target.name(),
+                });
             }
             let inner = attr.meta_item_list();
             match inner.as_deref() {
diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs
index 7734dba3670..a026ff3b13b 100644
--- a/compiler/rustc_passes/src/errors.rs
+++ b/compiler/rustc_passes/src/errors.rs
@@ -551,7 +551,10 @@ pub struct ReprConflictingLint;
 #[diag(passes_used_static)]
 pub struct UsedStatic {
     #[primary_span]
+    pub attr_span: Span,
+    #[label]
     pub span: Span,
+    pub target: &'static str,
 }
 
 #[derive(Diagnostic)]
diff --git a/tests/ui/attributes/used-issue-126789.rs b/tests/ui/attributes/used-issue-126789.rs
new file mode 100644
index 00000000000..90a1aa8d5cc
--- /dev/null
+++ b/tests/ui/attributes/used-issue-126789.rs
@@ -0,0 +1,6 @@
+extern "C" {
+    #[used] //~ ERROR attribute must be applied to a `static` variable
+    static FOO: i32;
+}
+
+fn main() {}
diff --git a/tests/ui/attributes/used-issue-126789.stderr b/tests/ui/attributes/used-issue-126789.stderr
new file mode 100644
index 00000000000..6014f7af95c
--- /dev/null
+++ b/tests/ui/attributes/used-issue-126789.stderr
@@ -0,0 +1,10 @@
+error: attribute must be applied to a `static` variable
+  --> $DIR/used-issue-126789.rs:2:5
+   |
+LL |     #[used]
+   |     ^^^^^^^
+LL |     static FOO: i32;
+   |     ---------------- but this is a foreign static item
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/used.stderr b/tests/ui/used.stderr
index ea77f129d8e..c586dc72293 100644
--- a/tests/ui/used.stderr
+++ b/tests/ui/used.stderr
@@ -3,24 +3,32 @@ error: attribute must be applied to a `static` variable
    |
 LL | #[used]
    | ^^^^^^^
+LL | fn foo() {}
+   | ----------- but this is a function
 
 error: attribute must be applied to a `static` variable
   --> $DIR/used.rs:7:1
    |
 LL | #[used]
    | ^^^^^^^
+LL | struct Foo {}
+   | ------------- but this is a struct
 
 error: attribute must be applied to a `static` variable
   --> $DIR/used.rs:10:1
    |
 LL | #[used]
    | ^^^^^^^
+LL | trait Bar {}
+   | ------------ but this is a trait
 
 error: attribute must be applied to a `static` variable
   --> $DIR/used.rs:13:1
    |
 LL | #[used]
    | ^^^^^^^
+LL | impl Bar for Foo {}
+   | ------------------- but this is a implementation block
 
 error: aborting due to 4 previous errors