Skip to content

Commit cdbd4ee

Browse files
authored
Rollup merge of rust-lang#74448 - davidtwco:improper-ctypes-definitions-boxes, r=davidtwco
improper_ctypes_definitions: allow `Box` Addresses rust-lang#72700 (comment). This PR stops linting against `Box` in `extern "C" fn`s for the `improper_ctypes_definitions` lint - boxes are documented to be FFI-safe. cc @alexcrichton @CryZe
2 parents eb4d139 + 95df802 commit cdbd4ee

File tree

5 files changed

+58
-64
lines changed

5 files changed

+58
-64
lines changed

src/librustc_lint/types.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
531531
match ty.kind {
532532
ty::FnPtr(_) => true,
533533
ty::Ref(..) => true,
534+
ty::Adt(def, _)
535+
if def.is_box() && matches!(self.mode, ImproperCTypesMode::Definitions) =>
536+
{
537+
true
538+
}
534539
ty::Adt(def, substs) if def.repr.transparent() && !def.is_union() => {
535540
let guaranteed_nonnull_optimization = self
536541
.cx
@@ -558,7 +563,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
558563
}
559564

560565
/// Check if this enum can be safely exported based on the "nullable pointer optimization".
561-
/// Currently restricted to function pointers, references, `core::num::NonZero*`,
566+
/// Currently restricted to function pointers, boxes, references, `core::num::NonZero*`,
562567
/// `core::ptr::NonNull`, and `#[repr(transparent)]` newtypes.
563568
fn is_repr_nullable_ptr(
564569
&self,
@@ -692,6 +697,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
692697
}
693698

694699
match ty.kind {
700+
ty::Adt(def, _)
701+
if def.is_box() && matches!(self.mode, ImproperCTypesMode::Definitions) =>
702+
{
703+
FfiSafe
704+
}
705+
695706
ty::Adt(def, substs) => {
696707
if def.is_phantom_data() {
697708
return FfiPhantom(ty);

src/test/ui/lint/lint-ctypes-fn.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub extern "C" fn str_type(p: &str) { }
7171
//~^ ERROR: uses type `str`
7272

7373
pub extern "C" fn box_type(p: Box<u32>) { }
74-
//~^ ERROR uses type `std::boxed::Box<u32>`
74+
75+
pub extern "C" fn opt_box_type(p: Option<Box<u32>>) { }
7576

7677
pub extern "C" fn char_type(p: char) { }
7778
//~^ ERROR uses type `char`
@@ -106,7 +107,6 @@ pub extern "C" fn fn_type2(p: fn()) { }
106107
//~^ ERROR uses type `fn()`
107108

108109
pub extern "C" fn fn_contained(p: RustBadRet) { }
109-
//~^ ERROR: uses type `std::boxed::Box<u32>`
110110

111111
pub extern "C" fn transparent_i128(p: TransparentI128) { }
112112
//~^ ERROR: uses type `i128`
@@ -115,7 +115,6 @@ pub extern "C" fn transparent_str(p: TransparentStr) { }
115115
//~^ ERROR: uses type `str`
116116

117117
pub extern "C" fn transparent_fn(p: TransparentBadFn) { }
118-
//~^ ERROR: uses type `std::boxed::Box<u32>`
119118

120119
pub extern "C" fn good3(fptr: Option<extern fn()>) { }
121120

src/test/ui/lint/lint-ctypes-fn.stderr

+14-41
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,8 @@ LL | pub extern "C" fn str_type(p: &str) { }
2121
= help: consider using `*const u8` and a length instead
2222
= note: string slices have no C equivalent
2323

24-
error: `extern` fn uses type `std::boxed::Box<u32>`, which is not FFI-safe
25-
--> $DIR/lint-ctypes-fn.rs:73:31
26-
|
27-
LL | pub extern "C" fn box_type(p: Box<u32>) { }
28-
| ^^^^^^^^ not FFI-safe
29-
|
30-
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
31-
= note: this struct has unspecified layout
32-
3324
error: `extern` fn uses type `char`, which is not FFI-safe
34-
--> $DIR/lint-ctypes-fn.rs:76:32
25+
--> $DIR/lint-ctypes-fn.rs:77:32
3526
|
3627
LL | pub extern "C" fn char_type(p: char) { }
3728
| ^^^^ not FFI-safe
@@ -40,23 +31,23 @@ LL | pub extern "C" fn char_type(p: char) { }
4031
= note: the `char` type has no C equivalent
4132

4233
error: `extern` fn uses type `i128`, which is not FFI-safe
43-
--> $DIR/lint-ctypes-fn.rs:79:32
34+
--> $DIR/lint-ctypes-fn.rs:80:32
4435
|
4536
LL | pub extern "C" fn i128_type(p: i128) { }
4637
| ^^^^ not FFI-safe
4738
|
4839
= note: 128-bit integers don't currently have a known stable ABI
4940

5041
error: `extern` fn uses type `u128`, which is not FFI-safe
51-
--> $DIR/lint-ctypes-fn.rs:82:32
42+
--> $DIR/lint-ctypes-fn.rs:83:32
5243
|
5344
LL | pub extern "C" fn u128_type(p: u128) { }
5445
| ^^^^ not FFI-safe
5546
|
5647
= note: 128-bit integers don't currently have a known stable ABI
5748

5849
error: `extern` fn uses type `(i32, i32)`, which is not FFI-safe
59-
--> $DIR/lint-ctypes-fn.rs:85:33
50+
--> $DIR/lint-ctypes-fn.rs:86:33
6051
|
6152
LL | pub extern "C" fn tuple_type(p: (i32, i32)) { }
6253
| ^^^^^^^^^^ not FFI-safe
@@ -65,7 +56,7 @@ LL | pub extern "C" fn tuple_type(p: (i32, i32)) { }
6556
= note: tuples have unspecified layout
6657

6758
error: `extern` fn uses type `(i32, i32)`, which is not FFI-safe
68-
--> $DIR/lint-ctypes-fn.rs:88:34
59+
--> $DIR/lint-ctypes-fn.rs:89:34
6960
|
7061
LL | pub extern "C" fn tuple_type2(p: I32Pair) { }
7162
| ^^^^^^^ not FFI-safe
@@ -74,7 +65,7 @@ LL | pub extern "C" fn tuple_type2(p: I32Pair) { }
7465
= note: tuples have unspecified layout
7566

7667
error: `extern` fn uses type `ZeroSize`, which is not FFI-safe
77-
--> $DIR/lint-ctypes-fn.rs:91:32
68+
--> $DIR/lint-ctypes-fn.rs:92:32
7869
|
7970
LL | pub extern "C" fn zero_size(p: ZeroSize) { }
8071
| ^^^^^^^^ not FFI-safe
@@ -88,7 +79,7 @@ LL | pub struct ZeroSize;
8879
| ^^^^^^^^^^^^^^^^^^^^
8980

9081
error: `extern` fn uses type `ZeroSizeWithPhantomData`, which is not FFI-safe
91-
--> $DIR/lint-ctypes-fn.rs:94:40
82+
--> $DIR/lint-ctypes-fn.rs:95:40
9283
|
9384
LL | pub extern "C" fn zero_size_phantom(p: ZeroSizeWithPhantomData) { }
9485
| ^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -101,15 +92,15 @@ LL | pub struct ZeroSizeWithPhantomData(PhantomData<i32>);
10192
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10293

10394
error: `extern` fn uses type `std::marker::PhantomData<bool>`, which is not FFI-safe
104-
--> $DIR/lint-ctypes-fn.rs:97:51
95+
--> $DIR/lint-ctypes-fn.rs:98:51
10596
|
10697
LL | pub extern "C" fn zero_size_phantom_toplevel() -> PhantomData<bool> {
10798
| ^^^^^^^^^^^^^^^^^ not FFI-safe
10899
|
109100
= note: composed only of `PhantomData`
110101

111102
error: `extern` fn uses type `fn()`, which is not FFI-safe
112-
--> $DIR/lint-ctypes-fn.rs:102:30
103+
--> $DIR/lint-ctypes-fn.rs:103:30
113104
|
114105
LL | pub extern "C" fn fn_type(p: RustFn) { }
115106
| ^^^^^^ not FFI-safe
@@ -118,23 +109,14 @@ LL | pub extern "C" fn fn_type(p: RustFn) { }
118109
= note: this function pointer has Rust-specific calling convention
119110

120111
error: `extern` fn uses type `fn()`, which is not FFI-safe
121-
--> $DIR/lint-ctypes-fn.rs:105:31
112+
--> $DIR/lint-ctypes-fn.rs:106:31
122113
|
123114
LL | pub extern "C" fn fn_type2(p: fn()) { }
124115
| ^^^^ not FFI-safe
125116
|
126117
= help: consider using an `extern fn(...) -> ...` function pointer instead
127118
= note: this function pointer has Rust-specific calling convention
128119

129-
error: `extern` fn uses type `std::boxed::Box<u32>`, which is not FFI-safe
130-
--> $DIR/lint-ctypes-fn.rs:108:35
131-
|
132-
LL | pub extern "C" fn fn_contained(p: RustBadRet) { }
133-
| ^^^^^^^^^^ not FFI-safe
134-
|
135-
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
136-
= note: this struct has unspecified layout
137-
138120
error: `extern` fn uses type `i128`, which is not FFI-safe
139121
--> $DIR/lint-ctypes-fn.rs:111:39
140122
|
@@ -152,25 +134,16 @@ LL | pub extern "C" fn transparent_str(p: TransparentStr) { }
152134
= help: consider using `*const u8` and a length instead
153135
= note: string slices have no C equivalent
154136

155-
error: `extern` fn uses type `std::boxed::Box<u32>`, which is not FFI-safe
156-
--> $DIR/lint-ctypes-fn.rs:117:37
157-
|
158-
LL | pub extern "C" fn transparent_fn(p: TransparentBadFn) { }
159-
| ^^^^^^^^^^^^^^^^ not FFI-safe
160-
|
161-
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
162-
= note: this struct has unspecified layout
163-
164137
error: `extern` fn uses type `std::marker::PhantomData<bool>`, which is not FFI-safe
165-
--> $DIR/lint-ctypes-fn.rs:161:43
138+
--> $DIR/lint-ctypes-fn.rs:160:43
166139
|
167140
LL | pub extern "C" fn unused_generic2<T>() -> PhantomData<bool> {
168141
| ^^^^^^^^^^^^^^^^^ not FFI-safe
169142
|
170143
= note: composed only of `PhantomData`
171144

172145
error: `extern` fn uses type `std::vec::Vec<T>`, which is not FFI-safe
173-
--> $DIR/lint-ctypes-fn.rs:174:39
146+
--> $DIR/lint-ctypes-fn.rs:173:39
174147
|
175148
LL | pub extern "C" fn used_generic4<T>(x: Vec<T>) { }
176149
| ^^^^^^ not FFI-safe
@@ -179,13 +152,13 @@ LL | pub extern "C" fn used_generic4<T>(x: Vec<T>) { }
179152
= note: this struct has unspecified layout
180153

181154
error: `extern` fn uses type `std::vec::Vec<T>`, which is not FFI-safe
182-
--> $DIR/lint-ctypes-fn.rs:177:41
155+
--> $DIR/lint-ctypes-fn.rs:176:41
183156
|
184157
LL | pub extern "C" fn used_generic5<T>() -> Vec<T> {
185158
| ^^^^^^ not FFI-safe
186159
|
187160
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
188161
= note: this struct has unspecified layout
189162

190-
error: aborting due to 20 previous errors
163+
error: aborting due to 17 previous errors
191164

src/test/ui/lint/lint-ctypes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ extern {
4848
pub fn slice_type(p: &[u32]); //~ ERROR: uses type `[u32]`
4949
pub fn str_type(p: &str); //~ ERROR: uses type `str`
5050
pub fn box_type(p: Box<u32>); //~ ERROR uses type `std::boxed::Box<u32>`
51+
pub fn opt_box_type(p: Option<Box<u32>>);
52+
//~^ ERROR uses type `std::option::Option<std::boxed::Box<u32>>`
5153
pub fn char_type(p: char); //~ ERROR uses type `char`
5254
pub fn i128_type(p: i128); //~ ERROR uses type `i128`
5355
pub fn u128_type(p: u128); //~ ERROR uses type `u128`

0 commit comments

Comments
 (0)