Skip to content

Commit 1b8bc34

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

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

Diff for: 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 = the kind is `{$target}`
789790
790791
passes_useless_assignment =
791792
useless assignment of {$is_field_assign ->

Diff for: 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, 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: span,
1989+
target: target.to_string(),
1990+
});
19871991
}
19881992
let inner = attr.meta_item_list();
19891993
match inner.as_deref() {

Diff for: 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: String,
567570
}
568571

569572
#[derive(Diagnostic)]

Diff for: tests/ui/attributes/used-issue-126789.rs

+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() {}

Diff for: tests/ui/attributes/used-issue-126789.stderr

+10
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+
| ---------------- the kind is `foreign static item`
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)