-
Notifications
You must be signed in to change notification settings - Fork 108
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
Invariant-parameterize Ptr
#715
Comments
Review comments for this prototype: Invariant comments /// 7. `ptr` conforms to the aliasing invariant of `ALIASING_INVARIANT`
/// 8. `ptr` conforms to the alignment invariant of `ALIGNMENT_INVARIANT`
/// 9. `ptr` conforms to the validity invariant of `VALIDITY_INVARIANT`
Can we describe the invariants locally so it's easier to see what needs to be upheld? Something like: /// 7. `ptr` conforms to the aliasing invariant of `ALIASING_INVARIANT`; in particular:
/// - If `ALIASING_INVARIANT` is `Shared`, ...
/// - If `ALIASING_INVARIANT` is `Unique`, ...
Invariant descriptionsAliasing /// The pointer adheres to the aliasing rules of a `&T`.
pub(crate) const Shared: Aliasing = 1 << 2;
/// The pointer adheres to the aliasing rules of a `&mut T`.
pub(crate) const Unique: Aliasing = 1 << 3; Can we actually specify what these are? (And also wherever the description of the rules is inlined, as in the previous comment.) Alignment /// The referent is aligned.
pub(crate) const Aligned: Alignment = 1 << 5; Same comment; maybe: /// The address of this `Ptr<T>` satisfies `T`'s alignment requirement.
pub(crate) const Aligned: Alignment = 1 << 5; Validity /// Totally bit-valid.
pub(crate) const Valid: Validity = 1 << 8; Same comment; maybe:
|
More review comments for this prototype: ProvenanceIn various places, we have the following language:
Based on this discussion, I think a more accurate phrasing would be:
|
In #406, we introduced the
Ptr
type to describe pointers that lie somewhere between the invariants ofNonNull<T>
and&T
. This type has both reduced the labor required to track invariants in our documentation, and revealed further opportunities for improvement. Namely: do actually do many things with aPtr
, you must do so through unsafe methods that have further unencapsulated requirements.I propose that we extend
Ptr
to handle these conditional invariants, by encoding them as const generic parameters; e.g.:We would then provide methods for asserting invariants; e.g.:
...forgetting invariants; e.g.:
...and safe methods conditioned on those invariants:
Why should we do this?
Doing this will help us maintain and document the safety invariants on
Ptr
and its methods. The above proposal has enough fidelity that we will be able to remove caller-enforced safety conditions onTryFromBytes::is_bit_valid
to type invariants on aPtr<'_, Self, ASSUME_ALIASING = {invariants::Shared}, ASSUME_ALIGNMENT = false, ASSUME_VALIDITY = false>
and makeis_bit_valid
safe.Unresolved Questions
How should we name things? Does
ASSUME_VALIDITY
have enough fidelity? How should we express maybe-invalid-but-has-unsafecells-in-the-right-places?Prototype
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f4e7f9df5377bda62ab7e17c0287128a
The text was updated successfully, but these errors were encountered: