-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add a lint to catch clashing extern
fn declarations.
#70946
Conversation
Just as a reminder, per #69390 (comment), this PR should go through a crater-check run before merging (which should also let us see if changing the lint to be |
r? @nagisa cc @hanna-kruppe (LGTM at a glance though) |
This is missing a test (and I think lint will fail to correctly handle) for two two not clashing declarations that use independently declared types. That is: mod one {
#[repr(C)] struct Banana { weight: u64 }
extern "C" { fn weigh_banana(count: *const Banana) -> u64; }
}
mod two {
#[repr(C)] struct Banana { weight: u64 } // note: distinct type
// For a weirder corner case (may still be valid depending on how C code is written):
// #[repr(C)] struct Banana { weight: u64, some_optional_field: u64 }
extern "C" { fn weigh_banana(count: *const Banana) -> u64; }
} |
01e6771
to
9c08571
Compare
extern
declarations.extern
fn declarations.
Should this also check across multiple crates? |
Current thinking is no, because two crates may bind to the same extern function with signatures that are different, but compatible under that language or ABI. |
☔ The latest upstream changes (presumably #71566) made this pull request unmergeable. Please resolve the merge conflicts. |
9c08571
to
6f7149b
Compare
☔ The latest upstream changes (presumably #72036) made this pull request unmergeable. Please resolve the merge conflicts. |
6f7149b
to
6022d63
Compare
☔ The latest upstream changes (presumably #72276) made this pull request unmergeable. Please resolve the merge conflicts. |
6022d63
to
42e75eb
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
42e75eb
to
8f6f3d6
Compare
Hey @nagisa, this is ready for re-review. |
This overall LGTM now. I think it does get the balance right. Can you please clean up the commits/history a little? Squashing some together, especially those that refer to rustfmt, etc would be ideal. r=me once that's done. |
8f6f3d6
to
337abf1
Compare
@bors r+ |
📌 Commit 337abf1 has been approved by |
🌲 The tree is currently closed for pull requests below priority 9000, this pull request will be tested once the tree is reopened |
Should we do a crater-check run first? Given this fixes a soundness issue, I'd like to make this lint Deny by default, but want to see if it'd break any existing crates. |
Add a lint to catch clashing `extern` fn declarations. Closes rust-lang#69390. Adds lint `clashing_extern_decl` to detect when, within a single crate, an extern function of the same name is declared with different types. Because two symbols of the same name cannot be resolved to two different functions at link time, and one function cannot possibly have two types, a clashing extern declaration is almost certainly a mistake. This lint does not run between crates because a project may have dependencies which both rely on the same extern function, but declare it in a different (but valid) way. For example, they may both declare an opaque type for one or more of the arguments (which would end up distinct types), or use types that are valid conversions in the language the extern fn is defined in. In these cases, we can't say that the clashing declaration is incorrect. r? @eddyb
This lint checks that all declarations for extern fns of the same name are declared with the same types.
- Allow ClashingExternDecl for lint-dead-code-3 - Update test case for rust-lang#5791 - Update test case for rust-lang#1866 - Update extern-abi-from-macro test case
e1eee56
to
556b7ba
Compare
Hey @nagisa, can I get another review? Fingers crossed this can get merged soon. |
@bors r+ |
📌 Commit 556b7ba has been approved by |
⌛ Testing commit 556b7ba with merge 4399d4f09c61ea4b3a9070ddc591ab4d72a563d9... |
💥 Test timed out |
going to retry again @bors retry |
☀️ Test successful - checks-azure |
The lint doesn't follow the lint naming conventions - https://github.com/rust-lang/rfcs/blob/master/text/0344-conventions-galore.md#lints. It should preferably be renamed |
Closes #69390.
Adds lint
clashing_extern_decl
to detect when, within a single crate, an extern function of the same name is declared with different types. Because two symbols of the same name cannot be resolved to two different functions at link time, and one function cannot possibly have two types, a clashing extern declaration is almost certainly a mistake.This lint does not run between crates because a project may have dependencies which both rely on the same extern function, but declare it in a different (but valid) way. For example, they may both declare an opaque type for one or more of the arguments (which would end up distinct types), or use types that are valid conversions in the language the extern fn is defined in. In these cases, we can't say that the clashing declaration is incorrect.
r? @eddyb