1
Fork 0

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
This commit is contained in:
Matthias Krüger 2024-06-29 22:10:59 +02:00 committed by GitHub
commit 5ea1a03cca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 3 deletions

View file

@ -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() {

View file

@ -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)]