Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An enum with one variant that directly contains PhantomData should be FFI-safe. #107854

Open
shepmaster opened this issue Feb 9, 2023 · 0 comments
Labels
A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug.

Comments

@shepmaster
Copy link
Member

I tried this code:

use core::marker::PhantomData;

#[repr(C)]
enum TheEnum {
    VariantWithPhantomData(u8, PhantomData<String>),
//    VariantWithStruct(StructWithPhantomData),
}

#[repr(C)]
struct StructWithPhantomData(u8, PhantomData<String>);

extern "C" fn demo(_x: TheEnum) {}

I expected to see no warnings about TheEnum.

Instead, this happened:

warning: `extern` fn uses type `TheEnum`, which is not FFI-safe
  --> src/lib.rs:12:24
   |
12 | extern "C" fn demo(_x: TheEnum) {}
   |                        ^^^^^^^ not FFI-safe
   |
   = note: this enum contains a PhantomData field
note: the type is defined here
  --> src/lib.rs:4:1
   |
4  | enum TheEnum {
   | ^^^^^^^^^^^^
   = note: `#[warn(improper_ctypes_definitions)]` on by default

If I move the PhantomData to a struct (see VariantWithPhantomData), then the problem goes away. The problem goes away even if I leave the original variant that it previously complained about.

Meta

This reproduces back to at least Rust 1.56 and to recent nightly versions:

rustc --version --verbose:

rustc 1.69.0-nightly (f3126500f 2023-02-02)
binary: rustc
commit-hash: f3126500f25114ba4e0ac3e76694dd45a22de56d
commit-date: 2023-02-02
host: aarch64-apple-darwin
release: 1.69.0-nightly
LLVM version: 15.0.7
@shepmaster shepmaster added the C-bug Category: This is a bug. label Feb 9, 2023
@workingjubilee workingjubilee added the A-FFI Area: Foreign function interface (FFI) label Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants