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

dead_code lint warns about struct fields it can't actually know are dead due to C FFI #15101

Closed
lilyball opened this issue Jun 23, 2014 · 4 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@lilyball
Copy link
Contributor

The dead_code lint has started warning about struct fields, in 9bb8f88. The problem is, it does this even if it can't guarantee the field is actually dead. Notably, it warns about struct fields for structs that are passed as *mut pointers to C FFI functions. In this case, my Rust code only zero-initializes the field, and never reads it because it's purely for use by the C FFI function.

This reproduces with lilyball/rust-lua@be46e268c with the lua_Debug struct (in raw.rs). It has a single private field that's used by the Lua C functions internally, and is intentionally not touched (beyond being zero-initialized) by the Rust code.

I think the solution here is for the dead_code lint to assume that any extern { fn } that takes a *mut pointer to a struct may potentially use every field of the struct, and therefore to treat any fields of structs with said C FFI functions as live regardless of whether the Rust code touches them.

@lilyball
Copy link
Contributor Author

/cc @jakub-. The PR that introduced this behavior was #14696.

@lilyball
Copy link
Contributor Author

Hrm, ok I just took another look, and apparently #[repr(C)] squelches this warning. I didn't know that was already accepted as a non-unused annotation. There's been no activity on the tracking issue #14309 for the relevant RFC.

#14309 includes adding a lint for warning about non-#[repr(C)] structs that are used with C FFI. Perhaps that should be fast-tracked and implemented now even if no other work towards #14309 is going to be done in the short term, if that's the intended way to deal with this spurious dead_code warning.

/cc @cmr

@alexcrichton
Copy link
Member

Closing in favor of #14309. As you realized, #[repr(C)] turns off all the warnings, and once #14309 is implemented you will also have warnings for where you need to write #[repr(C)], which should turn down the noise on these warnings greatly.

@alexcrichton
Copy link
Member

If #14309 isn't implemented in a timely manner we can revisit opening this.

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 17, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 17, 2023
Purge of unwraps

Removes unnecessary unwraps that I have overlooked in rust-lang#15101 ( fixes rust-lang#15176 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

2 participants