Skip to content

Commit 4e76c38

Browse files
committed
Fix ICE in improper_ctypes_definitions lint with all-ZST transparent types
1 parent 7069a8c commit 4e76c38

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

compiler/rustc_lint/src/types.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,18 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
851851
use FfiResult::*;
852852

853853
if def.repr.transparent() {
854-
// Can assume that only one field is not a ZST, so only check
854+
// Can assume that at most one field is not a ZST, so only check
855855
// that field's type for FFI-safety.
856856
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
857857
self.check_field_type_for_ffi(cache, field, substs)
858858
} else {
859-
bug!("malformed transparent type");
859+
// All fields are ZSTs; this means that the type should behave
860+
// like (), which is FFI-unsafe
861+
FfiUnsafe {
862+
ty,
863+
reason: "this struct contains only zero-sized fields".into(),
864+
help: None,
865+
}
860866
}
861867
} else {
862868
// We can't completely trust repr(C) markings; make sure the fields are
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regression test for the ICE described in #87496.
2+
3+
// check-pass
4+
5+
#[repr(transparent)]
6+
struct TransparentCustomZst(());
7+
extern "C" {
8+
fn good17(p: TransparentCustomZst);
9+
//~^ WARNING: `extern` block uses type `TransparentCustomZst`, which is not FFI-safe
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: `extern` block uses type `TransparentCustomZst`, which is not FFI-safe
2+
--> $DIR/repr-transparent-issue-87496.rs:8:18
3+
|
4+
LL | fn good17(p: TransparentCustomZst);
5+
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
6+
|
7+
= note: `#[warn(improper_ctypes)]` on by default
8+
= note: this struct contains only zero-sized fields
9+
note: the type is defined here
10+
--> $DIR/repr-transparent-issue-87496.rs:6:1
11+
|
12+
LL | struct TransparentCustomZst(());
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
warning: 1 warning emitted
16+

0 commit comments

Comments
 (0)