Skip to content

Commit cddcbec

Browse files
committed
Auto merge of rust-lang#158221 - JonathanBrouwer:export_stable_target, r=mejrs
Specify target list for `#[export_stable]` Work towards removing the `ALL_TARGETS` list. r? @mejrs
2 parents 9030e34 + 472c9f8 commit cddcbec

5 files changed

Lines changed: 88 additions & 48 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,18 @@ impl SingleAttributeParser for LinkSectionParser {
535535
pub(crate) struct ExportStableParser;
536536
impl 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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: `#[export_stable]` attribute cannot be used on traits
2+
--> $DIR/exportable.rs:135:5
3+
|
4+
LL | #[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+
9+
error: `#[export_stable]` attribute cannot be used on constants
10+
--> $DIR/exportable.rs:139:5
11+
|
12+
LL | #[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+
17+
error: aborting due to 2 previous errors
18+

tests/ui/attributes/export/exportable.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
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)]
712

813
mod 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

3136
pub 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

4853
pub mod impl_item {
@@ -51,19 +56,19 @@ 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);
6267

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

129134
pub 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

139144
fn main() {}

tests/ui/attributes/export/exportable.stderr renamed to tests/ui/attributes/export/exportable.sdylib.stderr

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,131 @@
1+
error: `#[export_stable]` attribute cannot be used on traits
2+
--> $DIR/exportable.rs:135:5
3+
|
4+
LL | #[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+
9+
error: `#[export_stable]` attribute cannot be used on constants
10+
--> $DIR/exportable.rs:139:5
11+
|
12+
LL | #[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+
117
error: private items are not exportable
2-
--> $DIR/exportable.rs:10:5
18+
--> $DIR/exportable.rs:15:5
319
|
420
LL | pub struct S;
521
| ^^^^^^^^^^^^
622
|
723
note: is only usable at visibility `pub(crate)`
8-
--> $DIR/exportable.rs:10:5
24+
--> $DIR/exportable.rs:15:5
925
|
1026
LL | pub struct S;
1127
| ^^^^^^^^^^^^
1228

1329
error: private items are not exportable
14-
--> $DIR/exportable.rs:123:5
30+
--> $DIR/exportable.rs:128:5
1531
|
1632
LL | enum E {
1733
| ^^^^^^
1834
|
1935
note: is only usable at visibility `pub(self)`
20-
--> $DIR/exportable.rs:123:5
36+
--> $DIR/exportable.rs:128:5
2137
|
2238
LL | enum E {
2339
| ^^^^^^
2440

25-
error: trait's are not exportable
26-
--> $DIR/exportable.rs:131:5
27-
|
28-
LL | pub trait Trait {}
29-
| ^^^^^^^^^^^^^^^
30-
31-
error: constant's are not exportable
32-
--> $DIR/exportable.rs:135:5
33-
|
34-
LL | pub const C: i32 = 0;
35-
| ^^^^^^^^^^^^^^^^
36-
3741
error: only functions with "C" ABI are exportable
38-
--> $DIR/exportable.rs:13:5
42+
--> $DIR/exportable.rs:18:5
3943
|
4044
LL | pub fn foo() -> i32 { 0 }
4145
| ^^^^^^^^^^^^^^^^^^^
4246

4347
error: types with unstable layout are not exportable
44-
--> $DIR/exportable.rs:27:5
48+
--> $DIR/exportable.rs:32:5
4549
|
4650
LL | pub struct S3;
4751
| ^^^^^^^^^^^^^
4852

4953
error: only functions with "C" ABI are exportable
50-
--> $DIR/exportable.rs:33:5
54+
--> $DIR/exportable.rs:38:5
5155
|
5256
LL | pub fn foo1() {}
5357
| ^^^^^^^^^^^^^
5458

5559
error: function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
56-
--> $DIR/exportable.rs:44:5
60+
--> $DIR/exportable.rs:49:5
5761
|
5862
LL | pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
5963
| ^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^^^^
6064
| |
6165
| not exportable
6266

6367
error: method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
64-
--> $DIR/exportable.rs:53:9
68+
--> $DIR/exportable.rs:58:9
6569
|
6670
LL | pub extern "C" fn foo1(&self) -> i32 { 0 }
6771
| ^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^
6872
| |
6973
| not exportable
7074

7175
error: method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
72-
--> $DIR/exportable.rs:57:9
76+
--> $DIR/exportable.rs:62:9
7377
|
7478
LL | pub extern "C" fn foo2(self) -> i32 { 0 }
7579
| ^^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^^
7680
| |
7781
| not exportable
7882

7983
error: generic functions are not exportable
80-
--> $DIR/exportable.rs:65:9
84+
--> $DIR/exportable.rs:70:9
8185
|
8286
LL | pub extern "C" fn foo1(&self) {}
8387
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8488

8589
error: function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
86-
--> $DIR/exportable.rs:81:5
90+
--> $DIR/exportable.rs:86:5
8791
|
8892
LL | pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
8993
| ^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^^^^^^
9094
| |
9195
| not exportable
9296

9397
error: function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
94-
--> $DIR/exportable.rs:88:5
98+
--> $DIR/exportable.rs:93:5
9599
|
96100
LL | pub extern "C" fn foo2(_x: Type) {}
97101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^----^
98102
| |
99103
| not exportable
100104

101105
error: function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
102-
--> $DIR/exportable.rs:97:5
106+
--> $DIR/exportable.rs:102:5
103107
|
104108
LL | pub extern "C" fn foo3(_x: S::Type) {}
105109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^
106110
| |
107111
| not exportable
108112

109113
error: function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
110-
--> $DIR/exportable.rs:101:5
114+
--> $DIR/exportable.rs:106:5
111115
|
112116
LL | pub extern "C" fn foo4() -> impl Copy {
113117
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------
114118
| |
115119
| not exportable
116120

117121
error: ADT types with private fields are not exportable
118-
--> $DIR/exportable.rs:116:5
122+
--> $DIR/exportable.rs:121:5
119123
|
120124
LL | pub struct S2 {
121125
| ^^^^^^^^^^^^^
122126
|
123127
note: `x` is private
124-
--> $DIR/exportable.rs:118:9
128+
--> $DIR/exportable.rs:123:9
125129
|
126130
LL | x: i32
127131
| ^^^^^^

0 commit comments

Comments
 (0)