authorbors <bors@rust-lang.org> 2026-06-22 11:48:19 UTC
committerbors <bors@rust-lang.org> 2026-06-22 11:48:19 UTC
logcddcbec198760511240bf0e728193bf4d700acb4
tree36371d2b71c5c4b11c26604f3352f06716b2cb89
parent9030e345fe92df1ccefd0a8cdf61a9a9a5b73cb0
parent472c9f8f703dbf4b7ee98e51a1252a800df9a702

Auto merge of #158221 - JonathanBrouwer:export_stable_target, r=mejrs

Specify target list for `#[export_stable]` Work towards removing the `ALL_TARGETS` list. r? @mejrs

6 files changed, 189 insertions(+), 149 deletions(-)

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs+12-1
......@@ -535,7 +535,18 @@ impl SingleAttributeParser for LinkSectionParser {
535535pub(crate) struct ExportStableParser;
536536impl NoArgsAttributeParser for ExportStableParser {
537537 const PATH: &[Symbol] = &[sym::export_stable];
538 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
538 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
539 Allow(Target::Fn),
540 Allow(Target::Method(MethodKind::Inherent)),
541 Allow(Target::Struct),
542 Allow(Target::Enum),
543 Allow(Target::Union),
544 Allow(Target::TyAlias),
545 Allow(Target::AssocTy),
546 Allow(Target::Use),
547 Allow(Target::Mod),
548 Allow(Target::Impl { of_trait: false }),
549 ]);
539550 const STABILITY: AttributeStability = unstable!(export_stable);
540551 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
541552}
compiler/rustc_passes/src/check_export.rs+3-1
......@@ -147,7 +147,9 @@ impl<'tcx> Visitor<'tcx> for ExportableItemCollector<'tcx> {
147147 hir::ItemKind::Impl(impl_) if impl_.of_trait.is_none() => {
148148 unreachable!();
149149 }
150 _ => self.report_wrong_site(def_id),
150 _ => {
151 self.tcx.dcx().delayed_bug("Target is checked by attribute parser");
152 }
151153 }
152154 }
153155
tests/ui/attributes/export/exportable.binary.stderr created+18
......@@ -0,0 +1,18 @@
1error: `#[export_stable]` attribute cannot be used on traits
2 --> $DIR/exportable.rs:135:5
3 |
4LL | #[export_stable]
5 | ^^^^^^^^^^^^^^^^
6 |
7 = help: `#[export_stable]` can be applied to associated types, data types, functions, inherent impl blocks, modules, type aliases, and use statements
8
9error: `#[export_stable]` attribute cannot be used on constants
10 --> $DIR/exportable.rs:139:5
11 |
12LL | #[export_stable]
13 | ^^^^^^^^^^^^^^^^
14 |
15 = help: `#[export_stable]` can be applied to associated types, data types, functions, inherent impl blocks, modules, type aliases, and use statements
16
17error: aborting due to 2 previous errors
18
tests/ui/attributes/export/exportable.rs+22-17
......@@ -1,6 +1,11 @@
1//@ revisions: sdylib binary
2// Currently the two revisions have different expectations, this is wrong.
3// There is an issue open to fix this: https://github.com/rust-lang/rust/issues/158227
4
15//@ compile-flags: -Zunstable-options -Csymbol-mangling-version=v0
6//@[sdylib] compile-flags: --crate-type=sdylib
7//@[binary] compile-flags: --crate-type=bin
28
3#![crate_type = "sdylib"]
49#![allow(incomplete_features, improper_ctypes_definitions)]
510#![feature(export_stable)]
611#![feature(inherent_associated_types)]
......@@ -8,10 +13,10 @@
813mod m {
914 #[export_stable]
1015 pub struct S;
11 //~^ ERROR private items are not exportable
16 //[sdylib]~^ ERROR private items are not exportable
1217
1318 pub fn foo() -> i32 { 0 }
14 //~^ ERROR only functions with "C" ABI are exportable
19 //[sdylib]~^ ERROR only functions with "C" ABI are exportable
1520}
1621
1722#[export_stable]
......@@ -25,13 +30,13 @@ pub mod m1 {
2530 struct S2;
2631
2732 pub struct S3;
28 //~^ ERROR types with unstable layout are not exportable
33 //[sdylib]~^ ERROR types with unstable layout are not exportable
2934}
3035
3136pub mod fn_sig {
3237 #[export_stable]
3338 pub fn foo1() {}
34 //~^ ERROR only functions with "C" ABI are exportable
39 //[sdylib]~^ ERROR only functions with "C" ABI are exportable
3540
3641 #[export_stable]
3742 #[repr(C)]
......@@ -42,7 +47,7 @@ pub mod fn_sig {
4247
4348 #[export_stable]
4449 pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
45 //~^ ERROR function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
50 //[sdylib]~^ ERROR function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
4651}
4752
4853pub mod impl_item {
......@@ -51,11 +56,11 @@ pub mod impl_item {
5156 impl S {
5257 #[export_stable]
5358 pub extern "C" fn foo1(&self) -> i32 { 0 }
54 //~^ ERROR method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
59 //[sdylib]~^ ERROR method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
5560
5661 #[export_stable]
5762 pub extern "C" fn foo2(self) -> i32 { 0 }
58 //~^ ERROR method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
63 //[sdylib]~^ ERROR method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
5964 }
6065
6166 pub struct S2<T>(T);
......@@ -63,7 +68,7 @@ pub mod impl_item {
6368 impl<T> S2<T> {
6469 #[export_stable]
6570 pub extern "C" fn foo1(&self) {}
66 //~^ ERROR generic functions are not exportable
71 //[sdylib]~^ ERROR generic functions are not exportable
6772 }
6873}
6974
......@@ -79,14 +84,14 @@ pub mod tys {
7984
8085 #[export_stable]
8186 pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
82 //~^ ERROR function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
87 //[sdylib]~^ ERROR function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
8388
8489 #[export_stable]
8590 pub type Type = [i32; 4];
8691
8792 #[export_stable]
8893 pub extern "C" fn foo2(_x: Type) {}
89 //~^ ERROR function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
94 //[sdylib]~^ ERROR function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
9095
9196 impl S {
9297 #[export_stable]
......@@ -95,11 +100,11 @@ pub mod tys {
95100
96101 #[export_stable]
97102 pub extern "C" fn foo3(_x: S::Type) {}
98 //~^ ERROR function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
103 //[sdylib]~^ ERROR function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
99104
100105 #[export_stable]
101106 pub extern "C" fn foo4() -> impl Copy {
102 //~^ ERROR function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
107 //[sdylib]~^ ERROR function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
103108 0
104109 }
105110}
......@@ -114,26 +119,26 @@ pub mod privacy {
114119 #[export_stable]
115120 #[repr(C)]
116121 pub struct S2 {
117 //~^ ERROR ADT types with private fields are not exportable
122 //[sdylib]~^ ERROR ADT types with private fields are not exportable
118123 x: i32
119124 }
120125
121126 #[export_stable]
122127 #[repr(i32)]
123128 enum E {
124 //~^ ERROR private items are not exportable
129 //[sdylib]~^ ERROR private items are not exportable
125130 Variant1 { x: i32 }
126131 }
127132}
128133
129134pub mod use_site {
130135 #[export_stable]
136 //~^ ERROR cannot be used on
131137 pub trait Trait {}
132 //~^ ERROR trait's are not exportable
133138
134139 #[export_stable]
140 //~^ ERROR cannot be used on
135141 pub const C: i32 = 0;
136 //~^ ERROR constant's are not exportable
137142}
138143
139144fn main() {}
tests/ui/attributes/export/exportable.sdylib.stderr created+134
......@@ -0,0 +1,134 @@
1error: `#[export_stable]` attribute cannot be used on traits
2 --> $DIR/exportable.rs:135:5
3 |
4LL | #[export_stable]
5 | ^^^^^^^^^^^^^^^^
6 |
7 = help: `#[export_stable]` can be applied to associated types, data types, functions, inherent impl blocks, modules, type aliases, and use statements
8
9error: `#[export_stable]` attribute cannot be used on constants
10 --> $DIR/exportable.rs:139:5
11 |
12LL | #[export_stable]
13 | ^^^^^^^^^^^^^^^^
14 |
15 = help: `#[export_stable]` can be applied to associated types, data types, functions, inherent impl blocks, modules, type aliases, and use statements
16
17error: private items are not exportable
18 --> $DIR/exportable.rs:15:5
19 |
20LL | pub struct S;
21 | ^^^^^^^^^^^^
22 |
23note: is only usable at visibility `pub(crate)`
24 --> $DIR/exportable.rs:15:5
25 |
26LL | pub struct S;
27 | ^^^^^^^^^^^^
28
29error: private items are not exportable
30 --> $DIR/exportable.rs:128:5
31 |
32LL | enum E {
33 | ^^^^^^
34 |
35note: is only usable at visibility `pub(self)`
36 --> $DIR/exportable.rs:128:5
37 |
38LL | enum E {
39 | ^^^^^^
40
41error: only functions with "C" ABI are exportable
42 --> $DIR/exportable.rs:18:5
43 |
44LL | pub fn foo() -> i32 { 0 }
45 | ^^^^^^^^^^^^^^^^^^^
46
47error: types with unstable layout are not exportable
48 --> $DIR/exportable.rs:32:5
49 |
50LL | pub struct S3;
51 | ^^^^^^^^^^^^^
52
53error: only functions with "C" ABI are exportable
54 --> $DIR/exportable.rs:38:5
55 |
56LL | pub fn foo1() {}
57 | ^^^^^^^^^^^^^
58
59error: function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
60 --> $DIR/exportable.rs:49:5
61 |
62LL | pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
63 | ^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^^^^
64 | |
65 | not exportable
66
67error: method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
68 --> $DIR/exportable.rs:58:9
69 |
70LL | pub extern "C" fn foo1(&self) -> i32 { 0 }
71 | ^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^
72 | |
73 | not exportable
74
75error: method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
76 --> $DIR/exportable.rs:62:9
77 |
78LL | pub extern "C" fn foo2(self) -> i32 { 0 }
79 | ^^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^^
80 | |
81 | not exportable
82
83error: generic functions are not exportable
84 --> $DIR/exportable.rs:70:9
85 |
86LL | pub extern "C" fn foo1(&self) {}
87 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
89error: function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
90 --> $DIR/exportable.rs:86:5
91 |
92LL | pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
93 | ^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^^^^^^
94 | |
95 | not exportable
96
97error: function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
98 --> $DIR/exportable.rs:93:5
99 |
100LL | pub extern "C" fn foo2(_x: Type) {}
101 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^----^
102 | |
103 | not exportable
104
105error: function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
106 --> $DIR/exportable.rs:102:5
107 |
108LL | pub extern "C" fn foo3(_x: S::Type) {}
109 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^
110 | |
111 | not exportable
112
113error: function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
114 --> $DIR/exportable.rs:106:5
115 |
116LL | pub extern "C" fn foo4() -> impl Copy {
117 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------
118 | |
119 | not exportable
120
121error: ADT types with private fields are not exportable
122 --> $DIR/exportable.rs:121:5
123 |
124LL | pub struct S2 {
125 | ^^^^^^^^^^^^^
126 |
127note: `x` is private
128 --> $DIR/exportable.rs:123:9
129 |
130LL | x: i32
131 | ^^^^^^
132
133error: aborting due to 16 previous errors
134
tests/ui/attributes/export/exportable.stderr deleted-130
......@@ -1,130 +0,0 @@
1error: private items are not exportable
2 --> $DIR/exportable.rs:10:5
3 |
4LL | pub struct S;
5 | ^^^^^^^^^^^^
6 |
7note: is only usable at visibility `pub(crate)`
8 --> $DIR/exportable.rs:10:5
9 |
10LL | pub struct S;
11 | ^^^^^^^^^^^^
12
13error: private items are not exportable
14 --> $DIR/exportable.rs:123:5
15 |
16LL | enum E {
17 | ^^^^^^
18 |
19note: is only usable at visibility `pub(self)`
20 --> $DIR/exportable.rs:123:5
21 |
22LL | enum E {
23 | ^^^^^^
24
25error: trait's are not exportable
26 --> $DIR/exportable.rs:131:5
27 |
28LL | pub trait Trait {}
29 | ^^^^^^^^^^^^^^^
30
31error: constant's are not exportable
32 --> $DIR/exportable.rs:135:5
33 |
34LL | pub const C: i32 = 0;
35 | ^^^^^^^^^^^^^^^^
36
37error: only functions with "C" ABI are exportable
38 --> $DIR/exportable.rs:13:5
39 |
40LL | pub fn foo() -> i32 { 0 }
41 | ^^^^^^^^^^^^^^^^^^^
42
43error: types with unstable layout are not exportable
44 --> $DIR/exportable.rs:27:5
45 |
46LL | pub struct S3;
47 | ^^^^^^^^^^^^^
48
49error: only functions with "C" ABI are exportable
50 --> $DIR/exportable.rs:33:5
51 |
52LL | pub fn foo1() {}
53 | ^^^^^^^^^^^^^
54
55error: function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
56 --> $DIR/exportable.rs:44:5
57 |
58LL | pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
59 | ^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^^^^
60 | |
61 | not exportable
62
63error: method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
64 --> $DIR/exportable.rs:53:9
65 |
66LL | pub extern "C" fn foo1(&self) -> i32 { 0 }
67 | ^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^
68 | |
69 | not exportable
70
71error: method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
72 --> $DIR/exportable.rs:57:9
73 |
74LL | pub extern "C" fn foo2(self) -> i32 { 0 }
75 | ^^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^^
76 | |
77 | not exportable
78
79error: generic functions are not exportable
80 --> $DIR/exportable.rs:65:9
81 |
82LL | pub extern "C" fn foo1(&self) {}
83 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
85error: function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
86 --> $DIR/exportable.rs:81:5
87 |
88LL | pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
89 | ^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^^^^^^
90 | |
91 | not exportable
92
93error: function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
94 --> $DIR/exportable.rs:88:5
95 |
96LL | pub extern "C" fn foo2(_x: Type) {}
97 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^----^
98 | |
99 | not exportable
100
101error: function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
102 --> $DIR/exportable.rs:97:5
103 |
104LL | pub extern "C" fn foo3(_x: S::Type) {}
105 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^
106 | |
107 | not exportable
108
109error: function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
110 --> $DIR/exportable.rs:101:5
111 |
112LL | pub extern "C" fn foo4() -> impl Copy {
113 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------
114 | |
115 | not exportable
116
117error: ADT types with private fields are not exportable
118 --> $DIR/exportable.rs:116:5
119 |
120LL | pub struct S2 {
121 | ^^^^^^^^^^^^^
122 |
123note: `x` is private
124 --> $DIR/exportable.rs:118:9
125 |
126LL | x: i32
127 | ^^^^^^
128
129error: aborting due to 16 previous errors
130