Skip to content

lint on more incorrect usages of core::ffi::c_void - #159986

Open
Jules-Bertholet wants to merge 5 commits into
rust-lang:mainfrom
Jules-Bertholet:c-void-refs
Open

lint on more incorrect usages of core::ffi::c_void#159986
Jules-Bertholet wants to merge 5 commits into
rust-lang:mainfrom
Jules-Bertholet:c-void-refs

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

View all comments

core::ffi::c_void has size 1 and a highly non-trivial validity invariant. It should only ever be used behind a raw pointer. However, people mess this up all the time.

Also fixes false negatives in the pre-exisiting c_void_returns. Like that lint, these new lints are warn-by-default, but should eventually be upgraded to deny.

See also #159935

@rustbot label T-lang needs-fcp A-FFI

@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 27, 2026
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 17 candidates

@rustbot rustbot added A-FFI Area: Foreign function interface (FFI) needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-lang Relevant to the language team labels Jul 27, 2026
@rust-log-analyzer

This comment has been minimized.

Also, fix false negatives in `c_void_returns`.
Comment thread src/tools/miri/src/shims/native_lib/mod.rs
@RalfJung

Copy link
Copy Markdown
Member

The lint should probably also complain about statics of type c_void.

@Jules-Bertholet Jules-Bertholet changed the title lint on core::ffi::c_void as an &/&mut referent lint on more incorrect usages of core::ffi::c_void Jul 28, 2026
Also, fix another false negative in `c_void_returns`.
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

The lint should probably also complain about statics of type c_void.

Added another lint for this

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Added a third lint, against c_void function parameters.

@theemathas

Copy link
Copy Markdown
Contributor

Idea (not sure if this is feasible): If we do anything that asks for the size of c_void, emit a warning unless it's for a pointer offset operation

@mejrs

mejrs commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Idea (not sure if this is feasible): If we do anything that asks for the size of c_void, emit a warning unless it's for a pointer offset operation

We can emit a warning regardless, with a note to just use byte_add for offset operations. (Is that what people are using size_of for..?)

@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Added a fourth lint to check for incorrect usage of c_void in MIR.

@rust-log-analyzer

This comment has been minimized.

Also, extend `c_void_references` to lint on
references to `c_void` in MIR.
Comment on lines +19 to +20
unsafe { r.read() };
//~^ ERROR c_void

@tgross35 tgross35 Jul 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps also:

let x: c_void;

View changes since the review

@tgross35

Copy link
Copy Markdown
Contributor

Apologies if missed, but do any of these catch patterns like struct S(core::ffi::c_void);? This is rejected by C compilers.

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Apologies if missed, but do any of these catch patterns like struct S(core::ffi::c_void);? This is rejected by C compilers.

No. For something like a #[repr(transparent)] pub struct Foo(c_void); that is only used behind a raw pointer, I'm not confident we want to lint, so I decided to start with the most clearly wrong stuff first. But if the lang team is fine with it, sure we can lint on that too

@tgross35

tgross35 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Assuming this needs lang to take a look and that it is about ready for it:

@rustbot label +I-lang-nominated

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jul 29, 2026
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

One thing I would like lang's input on: should we merge the lints? We could have a single lint for all incorrect c_void usage, or one lint for by-ref and one for by-value. Note that the pre-existing c_void_returns hits stable in 3 weeks.

@tgross35

Copy link
Copy Markdown
Contributor

My 2cents: all of these (including c_void_returns) should be a single improper_c_void lint. A lot of the value of splitting lints is giving users the ability to toggle a single lint on or off without affecting the others; however, since there aren't any valid uses of the code this lints against, I don't think there's any point. (Assuming no single lint is more prone to false positives than the others.)

One counterpoint is that maybe we'd want to deny-by-default everything except c_void_returns in the near future, deferring that one since it probably has the most use. I still think we could do without, though.

@mejrs

mejrs commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

And all of them are basically different variants of "this c_void isn't behind a raw pointer". it makes most sense to me to just have a single lint for that.

@asquared31415

Copy link
Copy Markdown
Contributor

I could see a world in which for "practical reasons" someone might want to ignore the -> c_void case specifically, but it's a weak argument. I think that a general invalid_c_void lint would likely suffice.

@rust-bors

rust-bors Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #160238) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

Labels

A-FFI Area: Foreign function interface (FFI) I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants