Skip to content

improper_ctypes diagnostic should advise to avoid improper C types because their ABI is unstable #87824

Open

Description

Spawned off of this zulip thread.

Given the following code:

extern {
    fn __enzyme_autodiff(_: usize, a: &[f32], d_a: &mut [f32]);
}

The current output is:

warning: `extern` block uses type `[f32]`, which is not FFI-safe
 --> src/lib.rs:2:39
  |
2 |     fn __enzyme_autodiff(_: usize, a: &[f32], d_a: &mut [f32]);
  |                                       ^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes)]` on by default
  = help: consider using a raw pointer instead
  = note: slices have no C equivalent

warning: `extern` block uses type `[f32]`, which is not FFI-safe
 --> src/lib.rs:2:52
  |
2 |     fn __enzyme_autodiff(_: usize, a: &[f32], d_a: &mut [f32]);
  |                                                    ^^^^^^^^^^ not FFI-safe
  |
  = help: consider using a raw pointer instead
  = note: slices have no C equivalent

warning: 2 warnings emitted

Ideally the output should look like:

warning: `extern` block uses type `[f32]`, which is not FFI-safe
 --> src/lib.rs:2:39
  |
2 |     fn __enzyme_autodiff(_: usize, a: &[f32], d_a: &mut [f32]);
  |                                       ^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes)]` on by default
  = help: consider using a raw pointer instead
  = note: slices have no C equivalent, and their ABI is unstable

warning: `extern` block uses type `[f32]`, which is not FFI-safe
 --> src/lib.rs:2:52
  |
2 |     fn __enzyme_autodiff(_: usize, a: &[f32], d_a: &mut [f32]);
  |                                                    ^^^^^^^^^^ not FFI-safe
  |
  = help: consider using a raw pointer instead
  = note: slices have no C equivalent, and their ABI is unstable

warning: 2 warnings emitted

Why: Currently, people might reverse-engineer the current ABI, and use a Rust type in an extern "C" block despite the warning. Reminding the reader that the ABI is unstable will help discourage that practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.L-improper_ctypesLint: improper_ctypesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions