Emit an error for invalid use of the linkage attribute
This commit is contained in:
parent
730d5d4095
commit
3c8dad152b
@ -427,6 +427,10 @@ passes_link_section =
|
||||
.warn = {-passes_previously_accepted}
|
||||
.label = not a function or static
|
||||
|
||||
passes_linkage =
|
||||
attribute should be applied to a function or static
|
||||
.label = not a function definition or static
|
||||
|
||||
passes_macro_export =
|
||||
`#[macro_export]` only has an effect on macro definitions
|
||||
|
||||
|
@ -243,6 +243,7 @@ fn check_attributes(
|
||||
[sym::coroutine, ..] => {
|
||||
self.check_coroutine(attr, target);
|
||||
}
|
||||
[sym::linkage, ..] => self.check_linkage(attr, span, target),
|
||||
[
|
||||
// ok
|
||||
sym::allow
|
||||
@ -256,7 +257,6 @@ fn check_attributes(
|
||||
| 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
|
||||
@ -2349,6 +2349,19 @@ fn check_coroutine(&self, attr: &Attribute, target: Target) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
|
@ -643,6 +643,15 @@ pub struct CoroutineOnNonClosure {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(passes_linkage)]
|
||||
pub struct Linkage {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(passes_empty_confusables)]
|
||||
pub(crate) struct EmptyConfusables {
|
||||
|
42
tests/ui/attributes/linkage.rs
Normal file
42
tests/ui/attributes/linkage.rs
Normal file
@ -0,0 +1,42 @@
|
||||
#![feature(linkage)]
|
||||
#![feature(stmt_expr_attributes)]
|
||||
#![deny(unused_attributes)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
|
||||
type InvalidTy = ();
|
||||
|
||||
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
|
||||
mod invalid_module {}
|
||||
|
||||
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
|
||||
struct F;
|
||||
|
||||
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
|
||||
impl F {
|
||||
#[linkage = "weak"]
|
||||
fn valid(&self) {}
|
||||
}
|
||||
|
||||
#[linkage = "weak"]
|
||||
fn f() {
|
||||
#[linkage = "weak"]
|
||||
{
|
||||
1
|
||||
};
|
||||
//~^^^^ ERROR attribute should be applied to a function or static
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
#[linkage = "weak"]
|
||||
static A: *const ();
|
||||
|
||||
#[linkage = "weak"]
|
||||
fn bar();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = #[linkage = "weak"]
|
||||
(|| 1);
|
||||
//~^^ ERROR attribute should be applied to a function or static
|
||||
}
|
55
tests/ui/attributes/linkage.stderr
Normal file
55
tests/ui/attributes/linkage.stderr
Normal file
@ -0,0 +1,55 @@
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:6:1
|
||||
|
|
||||
LL | #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | type InvalidTy = ();
|
||||
| -------------------- not a function definition or static
|
||||
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:9:1
|
||||
|
|
||||
LL | #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | mod invalid_module {}
|
||||
| --------------------- not a function definition or static
|
||||
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:12:1
|
||||
|
|
||||
LL | #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | struct F;
|
||||
| --------- not a function definition or static
|
||||
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:15:1
|
||||
|
|
||||
LL | #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | / impl F {
|
||||
LL | | #[linkage = "weak"]
|
||||
LL | | fn valid(&self) {}
|
||||
LL | | }
|
||||
| |_- not a function definition or static
|
||||
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:23:5
|
||||
|
|
||||
LL | #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | / {
|
||||
LL | | 1
|
||||
LL | | };
|
||||
| |_____- not a function definition or static
|
||||
|
||||
error: attribute should be applied to a function or static
|
||||
--> $DIR/linkage.rs:39:13
|
||||
|
|
||||
LL | let _ = #[linkage = "weak"]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
LL | (|| 1);
|
||||
| ------ not a function definition or static
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user