Skip to content

Commit f53cef3

Browse files
committed
lint/ctypes: stricter () return type checks
`()` is normally FFI-unsafe, but is FFI-safe when used as a return type. It is also desirable that a transparent newtype for `()` is FFI-safe when used as a return type. In order to support this, when an type was deemed FFI-unsafe, because of a `()` type, and was used in return type - then the type was considered FFI-safe. However, this was the wrong approach - it didn't check that the `()` was part of a transparent newtype! The consequence of this is that the presence of a `()` type in a more complex return type would make it the entire type be considered safe (as long as the `()` type was the first that the lint found) - which is obviously incorrect. Instead, this logic is removed, and a unit return type or a transparent wrapper around a unit is checked for directly for functions and fn-ptrs. Signed-off-by: David Wood <david@davidtw.co>
1 parent 0f16bd3 commit f53cef3

File tree

3 files changed

+115
-20
lines changed

3 files changed

+115
-20
lines changed

compiler/rustc_lint/src/types.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,30 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
943943
}
944944

945945
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
946+
// Returns `true` if `ty` is a `()`, or a `repr(transparent)` type whose only non-ZST field
947+
// is a generic substituted for `()` - in either case, the type is FFI-safe when used as a
948+
// return type.
949+
pub fn is_unit_or_equivalent(&self, ty: Ty<'tcx>) -> bool {
950+
if ty.is_unit() {
951+
return true;
952+
}
953+
954+
if let ty::Adt(def, substs) = ty.kind() && def.repr().transparent() {
955+
return def.variants()
956+
.iter()
957+
.filter_map(|variant| transparent_newtype_field(self.cx.tcx, variant))
958+
.all(|field| {
959+
let field_ty = field.ty(self.cx.tcx, substs);
960+
!field_ty.has_opaque_types() && {
961+
let field_ty = self.cx.tcx.normalize_erasing_regions(self.cx.param_env, field_ty);
962+
self.is_unit_or_equivalent(field_ty)
963+
}
964+
});
965+
}
966+
967+
false
968+
}
969+
946970
/// Check if the type is array and emit an unsafe type lint.
947971
fn check_for_array_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {
948972
if let ty::Array(..) = ty.kind() {
@@ -1220,25 +1244,19 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12201244
}
12211245

12221246
let sig = tcx.erase_late_bound_regions(sig);
1223-
if !sig.output().is_unit() {
1224-
let r = self.check_type_for_ffi(cache, sig.output());
1225-
match r {
1226-
FfiSafe => {}
1227-
_ => {
1228-
return r;
1229-
}
1230-
}
1231-
}
12321247
for arg in sig.inputs() {
1233-
let r = self.check_type_for_ffi(cache, *arg);
1234-
match r {
1248+
match self.check_type_for_ffi(cache, *arg) {
12351249
FfiSafe => {}
1236-
_ => {
1237-
return r;
1238-
}
1250+
r => return r,
12391251
}
12401252
}
1241-
FfiSafe
1253+
1254+
let ret_ty = sig.output();
1255+
if self.is_unit_or_equivalent(ret_ty) {
1256+
return FfiSafe;
1257+
}
1258+
1259+
self.check_type_for_ffi(cache, ret_ty)
12421260
}
12431261

12441262
ty::Foreign(..) => FfiSafe,
@@ -1357,9 +1375,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13571375
}
13581376

13591377
// Don't report FFI errors for unit return types. This check exists here, and not in
1360-
// `check_foreign_fn` (where it would make more sense) so that normalization has definitely
1378+
// the caller (where it would make more sense) so that normalization has definitely
13611379
// happened.
1362-
if is_return_type && ty.is_unit() {
1380+
if is_return_type && self.is_unit_or_equivalent(ty) {
13631381
return;
13641382
}
13651383

@@ -1373,9 +1391,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13731391
None,
13741392
);
13751393
}
1376-
// If `ty` is a `repr(transparent)` newtype, and the non-zero-sized type is a generic
1377-
// argument, which after substitution, is `()`, then this branch can be hit.
1378-
FfiResult::FfiUnsafe { ty, .. } if is_return_type && ty.is_unit() => {}
13791394
FfiResult::FfiUnsafe { ty, reason, help } => {
13801395
self.emit_ffi_unsafe_type_lint(ty, sp, reason, help);
13811396
}

tests/ui/lint/lint-ctypes-113436.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(improper_ctypes_definitions)]
2+
3+
#[repr(C)]
4+
pub struct Wrap<T>(T);
5+
6+
#[repr(transparent)]
7+
pub struct TransparentWrap<T>(T);
8+
9+
pub extern "C" fn f() -> Wrap<()> {
10+
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe
11+
todo!()
12+
}
13+
14+
const _: extern "C" fn() -> Wrap<()> = f;
15+
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe
16+
17+
pub extern "C" fn ff() -> Wrap<Wrap<()>> {
18+
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe
19+
todo!()
20+
}
21+
22+
const _: extern "C" fn() -> Wrap<Wrap<()>> = ff;
23+
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe
24+
25+
pub extern "C" fn g() -> TransparentWrap<()> {
26+
todo!()
27+
}
28+
29+
const _: extern "C" fn() -> TransparentWrap<()> = g;
30+
31+
pub extern "C" fn gg() -> TransparentWrap<TransparentWrap<()>> {
32+
todo!()
33+
}
34+
35+
const _: extern "C" fn() -> TransparentWrap<TransparentWrap<()>> = gg;
36+
37+
fn main() {}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: `extern` fn uses type `()`, which is not FFI-safe
2+
--> $DIR/lint-ctypes-113436.rs:9:26
3+
|
4+
LL | pub extern "C" fn f() -> Wrap<()> {
5+
| ^^^^^^^^ not FFI-safe
6+
|
7+
= help: consider using a struct instead
8+
= note: tuples have unspecified layout
9+
note: the lint level is defined here
10+
--> $DIR/lint-ctypes-113436.rs:1:9
11+
|
12+
LL | #![deny(improper_ctypes_definitions)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: `extern` fn uses type `()`, which is not FFI-safe
16+
--> $DIR/lint-ctypes-113436.rs:14:10
17+
|
18+
LL | const _: extern "C" fn() -> Wrap<()> = f;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
20+
|
21+
= help: consider using a struct instead
22+
= note: tuples have unspecified layout
23+
24+
error: `extern` fn uses type `()`, which is not FFI-safe
25+
--> $DIR/lint-ctypes-113436.rs:17:27
26+
|
27+
LL | pub extern "C" fn ff() -> Wrap<Wrap<()>> {
28+
| ^^^^^^^^^^^^^^ not FFI-safe
29+
|
30+
= help: consider using a struct instead
31+
= note: tuples have unspecified layout
32+
33+
error: `extern` fn uses type `()`, which is not FFI-safe
34+
--> $DIR/lint-ctypes-113436.rs:22:10
35+
|
36+
LL | const _: extern "C" fn() -> Wrap<Wrap<()>> = ff;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
38+
|
39+
= help: consider using a struct instead
40+
= note: tuples have unspecified layout
41+
42+
error: aborting due to 4 previous errors
43+

0 commit comments

Comments
 (0)