Rollup merge of #128989 - s7tya:check-linkage-attribute-pos, r=petrochenkov

Emit an error for invalid use of the linkage attribute

fixes #128486

Currently, the use of the linkage attribute for Mod, Impl,... is incorrectly permitted. This PR will correct this issue by generating errors, and I've also added some UI test cases for it.

Related: #128552.
This commit is contained in:
Matthias Krüger
2024-08-17 18:18:18 +02:00
committed by GitHub
5 changed files with 124 additions and 1 deletions

View File

@@ -243,6 +243,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::coroutine, ..] => {
self.check_coroutine(attr, target);
}
[sym::linkage, ..] => self.check_linkage(attr, span, target),
[
// ok
sym::allow
@@ -256,7 +257,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| sym::cfi_encoding // FIXME(cfi_encoding)
| sym::may_dangle // FIXME(dropck_eyepatch)
| sym::pointee // FIXME(derive_smart_pointer)
| sym::linkage // FIXME(linkage)
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
| sym::used // handled elsewhere to restrict to static items
| sym::repr // handled elsewhere to restrict to type decls items
@@ -2347,6 +2347,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
}
fn check_linkage(&self, attr: &Attribute, span: Span, target: Target) {
match target {
Target::Fn
| Target::Method(..)
| Target::Static
| Target::ForeignStatic
| Target::ForeignFn => {}
_ => {
self.dcx().emit_err(errors::Linkage { attr_span: attr.span, span });
}
}
}
}
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {