Skip to content

Commit

Permalink
Rollup merge of rust-lang#127118 - surechen:fix_126789, r=jieyouxu
Browse files Browse the repository at this point in the history
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 rust-lang#126789
  • Loading branch information
matthiaskrgr committed Jun 29, 2024
2 parents 5daa616 + 9c0ce05 commit fb181b2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/attributes/used-issue-126789.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern "C" {
#[used] //~ ERROR attribute must be applied to a `static` variable
static FOO: i32;
}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/attributes/used-issue-126789.stderr
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions tests/ui/used.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb181b2

Please sign in to comment.