Skip to content

Commit

Permalink
Rollup merge of rust-lang#87529 - FabianWolff:issue-87496, r=nikomats…
Browse files Browse the repository at this point in the history
…akis

Fix ICE in `improper_ctypes_definitions` lint with all-ZST transparent types

Fixes rust-lang#87496. There is also another function in the same file that looks fishy, but I haven't been able to produce an ICE there, and in any case, it's not related to rust-lang#87496:
https://github.com/rust-lang/rust/blob/fd853c00e255559255885aadff9e93a1760c8728/compiler/rustc_lint/src/types.rs#L720-L734

r? `@JohnTitor`
  • Loading branch information
Manishearth committed Sep 16, 2021
2 parents 4e5ad6e + 4e76c38 commit 4078cbf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,18 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
use FfiResult::*;

if def.repr.transparent() {
// Can assume that only one field is not a ZST, so only check
// Can assume that at most one field is not a ZST, so only check
// that field's type for FFI-safety.
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
self.check_field_type_for_ffi(cache, field, substs)
} else {
bug!("malformed transparent type");
// All fields are ZSTs; this means that the type should behave
// like (), which is FFI-unsafe
FfiUnsafe {
ty,
reason: "this struct contains only zero-sized fields".into(),
help: None,
}
}
} else {
// We can't completely trust repr(C) markings; make sure the fields are
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/repr/repr-transparent-issue-87496.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Regression test for the ICE described in #87496.

// check-pass

#[repr(transparent)]
struct TransparentCustomZst(());
extern "C" {
fn good17(p: TransparentCustomZst);
//~^ WARNING: `extern` block uses type `TransparentCustomZst`, which is not FFI-safe
}

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/repr/repr-transparent-issue-87496.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
warning: `extern` block uses type `TransparentCustomZst`, which is not FFI-safe
--> $DIR/repr-transparent-issue-87496.rs:8:18
|
LL | fn good17(p: TransparentCustomZst);
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= note: `#[warn(improper_ctypes)]` on by default
= note: this struct contains only zero-sized fields
note: the type is defined here
--> $DIR/repr-transparent-issue-87496.rs:6:1
|
LL | struct TransparentCustomZst(());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 1 warning emitted

0 comments on commit 4078cbf

Please sign in to comment.