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

improper_ctypes lint triggers on valid #[repr(transparent)] types #115457

Open
madsmtm opened this issue Sep 1, 2023 · 7 comments
Open

improper_ctypes lint triggers on valid #[repr(transparent)] types #115457

madsmtm opened this issue Sep 1, 2023 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-repr Area: the `#[repr(stuff)]` attribute C-bug Category: This is a bug. L-improper_ctypes Lint: improper_ctypes T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@madsmtm
Copy link
Contributor

madsmtm commented Sep 1, 2023

Code

#[repr(C)]
pub struct Foo {
    inner: [u8; 0]
}

extern "C" {
    // Lint does not trigger, as expected
    pub fn foo(_: &Foo);
}

#[repr(transparent)]
pub struct Bar(Foo);

extern "C" {
    // Lint triggers unexpectedly
    pub fn bar(_: &Bar);
}

Current output

warning: `extern` block uses type `Bar`, which is not FFI-safe
  --> <source>:16:19
   |
16 |     pub fn bar(_: &Bar);
   |                   ^^^^ not FFI-safe
   |
   = note: this struct contains only zero-sized fields
note: the type is defined here
  --> <source>:12:1
   |
12 | pub struct Bar(Foo);
   | ^^^^^^^^^^^^^^
   = note: `#[warn(improper_ctypes)]` on by default

warning: 1 warning emitted

Desired output

The lint should not fire

Rationale and extra context

Reproducible from Rust 1.57.0 (where #[repr(transparent)] on a zero-sized struct was made valid) to the current nightly-2023-08-31.

@madsmtm madsmtm added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 1, 2023
@asquared31415
Copy link
Contributor

I find it odd that this doesn't lint on Foo, it contains only zero sized fields too.

@madsmtm
Copy link
Contributor Author

madsmtm commented Sep 6, 2023

If it's an easier pill to swallow, it also lints on *const Bar but not on *const Foo.

Note that using zero-sized types to represent opaque/extern types like this is a documented pattern, so the lint should definitely not fire there.

@surechen
Copy link
Contributor

surechen commented Sep 8, 2023

@rustbot claim

@madsmtm
Copy link
Contributor Author

madsmtm commented Sep 9, 2023

Actually, I just saw this Pre-RFC about #[repr(transparent)] to ZSTs, so maybe this pattern was not actually fully specified?

@surechen
Copy link
Contributor

Actually, I just saw this Pre-RFC about #[repr(transparent)] to ZSTs, so maybe this pattern was not actually fully specified?
Hi, Thanks very much for finding the informations. So the current errors reported here is consistent with the reference ?

@surechen
Copy link
Contributor

I find that ignoring the warning conditions for the example seems easy to implement, but there doesn't seem to be a definite conclusion here. I think we should continue the discussion in this issue rust-lang/rust#100954

@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. and removed A-diagnostics Area: Messages for errors, warnings, and lints needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 29, 2023
@workingjubilee
Copy link
Member

nb: It also fails to trigger on invalid repr(transparent) types:

@surechen surechen removed their assignment Mar 13, 2024
@jieyouxu jieyouxu added the L-improper_ctypes Lint: improper_ctypes label May 13, 2024
@workingjubilee workingjubilee added the A-repr Area: the `#[repr(stuff)]` attribute label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-repr Area: the `#[repr(stuff)]` attribute C-bug Category: This is a bug. L-improper_ctypes Lint: improper_ctypes T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants