Skip to content

Commit 01a580b

Browse files
committed
Emit an error for invalid use of the linkage attribute
1 parent 730d5d4 commit 01a580b

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ passes_link_section =
427427
.warn = {-passes_previously_accepted}
428428
.label = not a function or static
429429
430+
passes_linkage =
431+
attribute should be applied to a free function, impl method or static, foreign static
432+
.label = not a free function, impl method or static, foreign static
433+
430434
passes_macro_export =
431435
`#[macro_export]` only has an effect on macro definitions
432436

compiler/rustc_passes/src/check_attr.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
243243
[sym::coroutine, ..] => {
244244
self.check_coroutine(attr, target);
245245
}
246+
[sym::linkage, ..] => self.check_linkage(attr, span, target),
246247
[
247248
// ok
248249
sym::allow
@@ -256,7 +257,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
256257
| sym::cfi_encoding // FIXME(cfi_encoding)
257258
| sym::may_dangle // FIXME(dropck_eyepatch)
258259
| sym::pointee // FIXME(derive_smart_pointer)
259-
| sym::linkage // FIXME(linkage)
260260
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
261261
| sym::used // handled elsewhere to restrict to static items
262262
| sym::repr // handled elsewhere to restrict to type decls items
@@ -2349,6 +2349,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23492349
}
23502350
}
23512351
}
2352+
2353+
fn check_linkage(&self, attr: &Attribute, span: Span, target: Target) {
2354+
match target {
2355+
Target::Fn | Target::Method(..) | Target::Static | Target::ForeignStatic => {}
2356+
_ => {
2357+
self.dcx().emit_err(errors::Linkage { attr_span: attr.span, span });
2358+
}
2359+
}
2360+
}
23522361
}
23532362

23542363
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,15 @@ pub struct CoroutineOnNonClosure {
643643
pub span: Span,
644644
}
645645

646+
#[derive(Diagnostic)]
647+
#[diag(passes_linkage)]
648+
pub struct Linkage {
649+
#[primary_span]
650+
pub attr_span: Span,
651+
#[label]
652+
pub span: Span,
653+
}
654+
646655
#[derive(Diagnostic)]
647656
#[diag(passes_empty_confusables)]
648657
pub(crate) struct EmptyConfusables {

tests/ui/attributes/linkage.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(linkage)]
2+
#![feature(stmt_expr_attributes)]
3+
#![deny(unused_attributes)]
4+
#![allow(dead_code)]
5+
6+
#[linkage = "weak"] //~ ERROR attribute should be applied to a free function, impl method or static, foreign static
7+
type InvalidTy = ();
8+
9+
#[linkage = "weak"] //~ ERROR attribute should be applied to a free function, impl method or static, foreign static
10+
mod invalid_module {}
11+
12+
#[linkage = "weak"] //~ ERROR attribute should be applied to a free function, impl method or static, foreign static
13+
struct F;
14+
15+
#[linkage = "weak"] //~ ERROR attribute should be applied to a free function, impl method or static, foreign static
16+
impl F {
17+
#[linkage = "weak"]
18+
fn valid(&self) {}
19+
}
20+
21+
#[linkage = "weak"]
22+
fn f() {
23+
#[linkage = "weak"]
24+
{
25+
1
26+
};
27+
//~^^^^ ERROR attribute should be applied to a free function, impl method or static, foreign static
28+
}
29+
30+
extern "C" {
31+
#[linkage = "weak"]
32+
static A: *const ();
33+
}
34+
35+
fn main() {
36+
let _ = #[linkage = "weak"] //~ ERROR attribute should be applied to a free function, impl method or static, foreign static
37+
(|| 1);
38+
}

tests/ui/attributes/linkage.stderr

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error: attribute should be applied to a free function, impl method or static, foreign static
2+
--> $DIR/linkage.rs:6:1
3+
|
4+
LL | #[linkage = "weak"]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
LL | type InvalidTy = ();
7+
| -------------------- not a free function, impl method or static, foreign static
8+
9+
error: attribute should be applied to a free function, impl method or static, foreign static
10+
--> $DIR/linkage.rs:9:1
11+
|
12+
LL | #[linkage = "weak"]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
LL | mod invalid_module {}
15+
| --------------------- not a free function, impl method or static, foreign static
16+
17+
error: attribute should be applied to a free function, impl method or static, foreign static
18+
--> $DIR/linkage.rs:12:1
19+
|
20+
LL | #[linkage = "weak"]
21+
| ^^^^^^^^^^^^^^^^^^^
22+
LL | struct F;
23+
| --------- not a free function, impl method or static, foreign static
24+
25+
error: attribute should be applied to a free function, impl method or static, foreign static
26+
--> $DIR/linkage.rs:15:1
27+
|
28+
LL | #[linkage = "weak"]
29+
| ^^^^^^^^^^^^^^^^^^^
30+
LL | / impl F {
31+
LL | | #[linkage = "weak"]
32+
LL | | fn valid(&self) {}
33+
LL | | }
34+
| |_- not a free function, impl method or static, foreign static
35+
36+
error: attribute should be applied to a free function, impl method or static, foreign static
37+
--> $DIR/linkage.rs:23:5
38+
|
39+
LL | #[linkage = "weak"]
40+
| ^^^^^^^^^^^^^^^^^^^
41+
LL | / {
42+
LL | | 1
43+
LL | | };
44+
| |_____- not a free function, impl method or static, foreign static
45+
46+
error: attribute should be applied to a free function, impl method or static, foreign static
47+
--> $DIR/linkage.rs:36:13
48+
|
49+
LL | let _ = #[linkage = "weak"]
50+
| ^^^^^^^^^^^^^^^^^^^
51+
LL | (|| 1);
52+
| ------ not a free function, impl method or static, foreign static
53+
54+
error: aborting due to 6 previous errors
55+

0 commit comments

Comments
 (0)