Skip to content

Commit 9c0ce05

Browse files
committed
Show used attribute's kind for user when find it isn't applied to a static variable.
fixes #126789
1 parent 4e63822 commit 9c0ce05

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

compiler/rustc_passes/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ passes_used_compiler_linker =
786786
787787
passes_used_static =
788788
attribute must be applied to a `static` variable
789+
.label = but this is a {$target}
789790
790791
passes_useless_assignment =
791792
useless assignment of {$is_field_assign ->

compiler/rustc_passes/src/check_attr.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
278278
}
279279

280280
self.check_repr(attrs, span, target, item, hir_id);
281-
self.check_used(attrs, target);
281+
self.check_used(attrs, target, span);
282282
}
283283

284284
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
@@ -1978,12 +1978,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19781978
}
19791979
}
19801980

1981-
fn check_used(&self, attrs: &[Attribute], target: Target) {
1981+
fn check_used(&self, attrs: &[Attribute], target: Target, target_span: Span) {
19821982
let mut used_linker_span = None;
19831983
let mut used_compiler_span = None;
19841984
for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) {
19851985
if target != Target::Static {
1986-
self.dcx().emit_err(errors::UsedStatic { span: attr.span });
1986+
self.dcx().emit_err(errors::UsedStatic {
1987+
attr_span: attr.span,
1988+
span: target_span,
1989+
target: target.name(),
1990+
});
19871991
}
19881992
let inner = attr.meta_item_list();
19891993
match inner.as_deref() {

compiler/rustc_passes/src/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ pub struct ReprConflictingLint;
563563
#[diag(passes_used_static)]
564564
pub struct UsedStatic {
565565
#[primary_span]
566+
pub attr_span: Span,
567+
#[label]
566568
pub span: Span,
569+
pub target: &'static str,
567570
}
568571

569572
#[derive(Diagnostic)]
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern "C" {
2+
#[used] //~ ERROR attribute must be applied to a `static` variable
3+
static FOO: i32;
4+
}
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: attribute must be applied to a `static` variable
2+
--> $DIR/used-issue-126789.rs:2:5
3+
|
4+
LL | #[used]
5+
| ^^^^^^^
6+
LL | static FOO: i32;
7+
| ---------------- but this is a foreign static item
8+
9+
error: aborting due to 1 previous error
10+

tests/ui/used.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,32 @@ error: attribute must be applied to a `static` variable
33
|
44
LL | #[used]
55
| ^^^^^^^
6+
LL | fn foo() {}
7+
| ----------- but this is a function
68

79
error: attribute must be applied to a `static` variable
810
--> $DIR/used.rs:7:1
911
|
1012
LL | #[used]
1113
| ^^^^^^^
14+
LL | struct Foo {}
15+
| ------------- but this is a struct
1216

1317
error: attribute must be applied to a `static` variable
1418
--> $DIR/used.rs:10:1
1519
|
1620
LL | #[used]
1721
| ^^^^^^^
22+
LL | trait Bar {}
23+
| ------------ but this is a trait
1824

1925
error: attribute must be applied to a `static` variable
2026
--> $DIR/used.rs:13:1
2127
|
2228
LL | #[used]
2329
| ^^^^^^^
30+
LL | impl Bar for Foo {}
31+
| ------------------- but this is a implementation block
2432

2533
error: aborting due to 4 previous errors
2634

0 commit comments

Comments
 (0)