Skip to content

Commit 5ea1a03

Browse files
authored
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
2 parents 9879b46 + 9c0ce05 commit 5ea1a03

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
@@ -782,6 +782,7 @@ passes_used_compiler_linker =
782782
783783
passes_used_static =
784784
attribute must be applied to a `static` variable
785+
.label = but this is a {$target}
785786
786787
passes_useless_assignment =
787788
useless assignment of {$is_field_assign ->

compiler/rustc_passes/src/check_attr.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
274274
}
275275

276276
self.check_repr(attrs, span, target, item, hir_id);
277-
self.check_used(attrs, target);
277+
self.check_used(attrs, target, span);
278278
}
279279

280280
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
@@ -1930,12 +1930,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19301930
}
19311931
}
19321932

1933-
fn check_used(&self, attrs: &[Attribute], target: Target) {
1933+
fn check_used(&self, attrs: &[Attribute], target: Target, target_span: Span) {
19341934
let mut used_linker_span = None;
19351935
let mut used_compiler_span = None;
19361936
for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) {
19371937
if target != Target::Static {
1938-
self.dcx().emit_err(errors::UsedStatic { span: attr.span });
1938+
self.dcx().emit_err(errors::UsedStatic {
1939+
attr_span: attr.span,
1940+
span: target_span,
1941+
target: target.name(),
1942+
});
19391943
}
19401944
let inner = attr.meta_item_list();
19411945
match inner.as_deref() {

compiler/rustc_passes/src/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ pub struct ReprConflictingLint;
551551
#[diag(passes_used_static)]
552552
pub struct UsedStatic {
553553
#[primary_span]
554+
pub attr_span: Span,
555+
#[label]
554556
pub span: Span,
557+
pub target: &'static str,
555558
}
556559

557560
#[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)