Skip to content

extern "C" functions returning ZSTs #115091

Open
@ChayimFriedman2

Description

@ChayimFriedman2

Currently, for extern "C" functions returning ZSTs the compiler returns void (as well as for other ABIs, but this is not related). However, improper_ctypes_definitions fire for such functions (if they don't return ()), e.g.:

// #[repr(C)] // Adding that does not change the outcome.
pub struct Zst;

pub extern "C" fn foo() -> Zst {
    Zst
}
warning: `extern` fn uses type `Zst`, which is not FFI-safe
 --> src/lib.rs:4:28
  |
4 | pub extern "C" fn foo() -> Zst {
  |                            ^^^ not FFI-safe
  |
  = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
  = note: this struct has unspecified layout
note: the type is defined here
 --> src/lib.rs:2:1
  |
2 | pub struct Zst;
  | ^^^^^^^^^^^^^^
  = note: `#[warn(improper_ctypes_definitions)]` on by default

Playground.

It's pretty clear we do not want to guarantee the FFI-safety of ZST arguments. But what about the FFI safety of ZST return types? Do we want to guarantee they'll keep translating to void, or keep this not guaranteed?

Context: https://stackoverflow.com/q/76950446/7884305.

@rustbot label T-lang

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ABIArea: Concerning the application binary interface (ABI)A-FFIArea: Foreign function interface (FFI)C-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions