-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: Safety Tags #3842
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
base: master
Are you sure you want to change the base?
RFC: Safety Tags #3842
Conversation
error: expected identifier, found keyword `use` --> src/main.rs:2:20 | 2 | #![clippy::safety::use(invariant::ValidPtr)] // 💡 | ^^^ expected identifier, found keyword
Co-authored-by: kennytm <kennytm@gmail.com>
Also * distinguish safety::requires and safety::checked * replace safety::r#use with safety::import * only allow uninhabited enum as tag item
@zjp-CN > Do you indicate tags work bad on nested unsafe calls? I've no opinion on this RFC, or the previous one, or when one should create some static analysis plugin interface. I'm only pointing out that an unsafe counter achieves lots, without increasing the langauge complexity much, so imho most developers would use an unsafe counter, not a claim we can make for more complex schemes. That doesn't say those schemes do not have a place. |
I agree forcing Maybe a mix between a previous version of this RFC and the current one would be better, namely that |
IIUC unsafe counter is a practice to alert unsafe operations are not checked against their safety requirements. It's a piece of linter work: undocumented_unsafe_blocks, but on unsafe operations (calls, and other unsafety), so maybe name it undocumented_unsafe_operations. Discharge of safety tags can be considered a rigorous undocumented_unsafe_operations lint as @ia0 points out, and a design on machine-readable/checkable safety requirements.
|
Right. What I meant was to also permit this when there are multiple unsafe calls within the unsafe block (or expression) where the |
Yeah, I thought about that too. And it's basically suggesting #[safety::checked(TagForCall1, TagForCall2)]
unsafe { call1(call2()) }
// or
#[safety::checked(TagForCall1, TagForCall2)]
unsafe { call1(); call2(); } It's not allowed in this RFC due to exactly what you said, instead split inner results into assignments as mentioned above. |
Ok, so there's no trade-off. That's fine for me, I'm just noticing that some may find this too strict. But then writing unsafe code is not necessarily supposed to be an enjoyable experience, so biasing towards "safety"/"precision" to the detriment of "user experience" is understandable. It just needs to be a deliberate choice and not an oversight of the consequences. |
Not too relevant to this RFC, but I just saw an OSDI paper Paralegal: Practical Static Analysis for Privacy Bugs (repo) introduces markers as a key abstraction for mapping the policy onto the program. Basically it maps English texts to lightweight attributes in source code to find bugs. |
This flavor of thing would be nice to introduce more structure (and the possibility for automated tooling) into complex unsafe code bases like |
…ntic granularity
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.
I really like the direction this is moving with requires
and checked
since the initial version.
Summary
This RFC introduces a concise safety-comment convention for unsafe code in standard libraries:
tag every public unsafe function with
#[safety::requires]
and call with#[safety::checked]
.Safety tags refine today’s safety-comment habits: a featherweight syntax that condenses every
requirement into a single, check-off reminder.
The following snippet compiles today if we enable enough nightly features, but we expect Clippy
and Rust-Analyzer to enforce tag checks and provide first-class IDE support.
Rendered