Skip to content

Commit 30ac9b7

Browse files
authored
Rollup merge of rust-lang#128989 - s7tya:check-linkage-attribute-pos, r=petrochenkov
Emit an error for invalid use of the linkage attribute fixes rust-lang#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: rust-lang#128552.
2 parents 8dd5939 + 3c8dad1 commit 30ac9b7

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ passes_link_section =
430430
.warn = {-passes_previously_accepted}
431431
.label = not a function or static
432432
433+
passes_linkage =
434+
attribute should be applied to a function or static
435+
.label = not a function definition or static
436+
433437
passes_macro_export =
434438
`#[macro_export]` only has an effect on macro definitions
435439

compiler/rustc_passes/src/check_attr.rs

Lines changed: 14 additions & 1 deletion
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
@@ -2347,6 +2347,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23472347
}
23482348
}
23492349
}
2350+
2351+
fn check_linkage(&self, attr: &Attribute, span: Span, target: Target) {
2352+
match target {
2353+
Target::Fn
2354+
| Target::Method(..)
2355+
| Target::Static
2356+
| Target::ForeignStatic
2357+
| Target::ForeignFn => {}
2358+
_ => {
2359+
self.dcx().emit_err(errors::Linkage { attr_span: attr.span, span });
2360+
}
2361+
}
2362+
}
23502363
}
23512364

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

compiler/rustc_passes/src/errors.rs

Lines changed: 9 additions & 0 deletions
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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 function or static
7+
type InvalidTy = ();
8+
9+
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
10+
mod invalid_module {}
11+
12+
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
13+
struct F;
14+
15+
#[linkage = "weak"] //~ ERROR attribute should be applied to a function or 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 function or static
28+
}
29+
30+
extern "C" {
31+
#[linkage = "weak"]
32+
static A: *const ();
33+
34+
#[linkage = "weak"]
35+
fn bar();
36+
}
37+
38+
fn main() {
39+
let _ = #[linkage = "weak"]
40+
(|| 1);
41+
//~^^ ERROR attribute should be applied to a function or static
42+
}

tests/ui/attributes/linkage.stderr

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

0 commit comments

Comments
 (0)