-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
new lint: add call_missing_target_feature
lint
#13240
base: master
Are you sure you want to change the base?
new lint: add call_missing_target_feature
lint
#13240
Conversation
de11ecb
to
503517a
Compare
probably rust-lang/rust#128852 is needed to fix the suggestion here and correctly insert the unsafe keyword |
bda8541
to
711a22c
Compare
r? @Jarcho I'll take a look at this within a couple of days. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did an initial review. When ordering and performing checks try to make sure to optimize for the common case. For this it would be something like:
- Not a function call
- Called function has no
target_features
- Not from an external macro (Not always true, but true for most crates)
3ea48d3
to
43f1f1b
Compare
43f1f1b
to
cf88b49
Compare
Thanks for the review! I've handled the comments and also rebased because some CI things had changed. |
changelog: [
call_missing_target_feature
]: was addedThis PR adds a lint for missing target features.
Motivation
A function that requires a target feature (e.g.
#[target_feature(enable = "avx2")]
to indicate that avx2 instructions can be used) must be called from a context where these instructions are actually available. That check is part of the safety contract of the function. For correctness and performance, any function calling a function with a target feature must eitherHowever, currently no warning is emitted when the user forgets to correctly annotate target features. My personal experience with zlib-rs is that it's just really easy to forget the annotation on some helper function, and to forget to check for target features in code review. Hence, this check.
Implementation
The implementation looks for function calls, then looks up if that function (the callee) has any target features, and if so, checks whether the caller also has these target features. Any missing target features are reported.
This lint is not always accurate, and I think should be off by default, but is very useful in e.g. simd-heavy codebases. Some
#[cfg(allow(...)]
are always required though in the function where the feature detection macro is called.Questions
The tests run into
despite the suggestion being
Applicability::MaybeIncorrect
. I must be missing something here. The problem is that the function must also be marked as unsafe in this case. Still I think the suggestion is useful. Also inserting the unsafe seems complicated, but maybe that is possible?cc @bjorn3 maybe you have phrasing suggestions?