Skip to content

Should improper_ctypes_definitions lint warn against boxed DST? #85714

Closed
@marmeladema

Description

@marmeladema

Given the following code:

#![deny(improper_ctypes_definitions)]

#[no_mangle]
pub extern "C" fn test_boxed_str() -> Box<str> {
    "test_boxed_str".into()
}

#[no_mangle]
pub extern "C" fn test_boxed_slice() -> Box<[u8]> {
    b"test_boxed_slice"[..].into()
}

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dfca164488b7561826bbb61ee137b6bc

The current output is:

  • It compiles successfully

Ideally the output should look like:

warning: `extern` fn uses type `Box<str>`, which is not FFI-safe
  --> src/lib.rs:24:32
   |
24 | pub extern "C" fn test_boxed_str() -> Box<str> {
   |                                       ^^^^^^^^ not FFI-safe
   |
   = note: `#[warn(improper_ctypes_definitions)]` on by default
   = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
   = note: this struct has unspecified layout

From what I understand, fat pointers layout is not stable and should not be relied upon, pretty much in the same way that relying on the layout of a #[repr(rust)] type is an internal decision to the compiler. If they are now considered stable ABI, then the current lint is fine.

But in any case, I believe the documentation at https://doc.rust-lang.org/std/boxed/index.html#memory-layout could probably be enhanced to speak more about DST. Either to say they should be avoided because the memory layout is not stable, or to explicitly document that it is and makes this a commitment from the compiler.

If those arguments are compelling and the compiler team agrees to those changes, I am willing to implement them, providing a little bit of mentoring from someone 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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