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

Added a unsafe_ffi_drop_implementations lint. #22078

Closed

Conversation

DanielKeep
Copy link
Contributor

This detects cases where a struct or enum are annotated with #[repr(C)],
and do not have #[unsafe_no_drop_flag], whereby it warns the user that
the type may not have the expected size or layout.

The lint was set to "Warn" by default as I wasn't sure if "Deny" would be too strong.

A tangential change was to remove #[repr(C)] from Cleanup in src/libstd/sys/windows/backtrace.rs. I believe this to be justified in that Cleanup is only ever used exactly once as an RAII handle, and is never passed to any FFI function.

Two tests including to ensure the lint triggers when #[unsafe_no_drop_flag] is missing, and does not trigger if it is present.

Addresses #18380.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@alexcrichton
Copy link
Member

cc #21761

@nikomatsakis
Copy link
Contributor

The code certainly looks good. The need for this lint is probably somewhat time-limited, but in the meantime it can't hurt.

This detects cases where a struct or enum are annotated with `#[repr(C)]`,
and *do not* have `#[unsafe_no_drop_flag]`, whereby it warns the user that
the type may not have the expected size or layout.

Also includes tests to ensure the lint is triggered by FFI+Drop structs
and enums, *not* triggered by FFI+Drop+unsafe_no_drop_flag structs
and enums, and *not* triggered by FFI+!Drop structs and enums.

It also contains a tangential change to libstd/sys/windows/backtrace.rs.
Specifically, the `Cleanup` type had `#[repr(C)]` and Drop, but was never
passed to any FFI function.
@DanielKeep
Copy link
Contributor Author

@nikomatsakis Added a struct and enum with #[repr(C)] and no Drop to the rpass test.

@kornelski
Copy link
Contributor

I think I've ran into this problem and this lint would have saved me some hair-pulling :)

@alexcrichton
Copy link
Member

Gah sorry for letting this fall by the wayside! This was actually recently reimplemented in #24935, so I'm going to close this. Thanks regardless though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants