-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Can the standard library rely on stated but unenforceable invariants of traits like TotalOrd? #14418
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
Comments
I think this is a bit of a sticky issue, but safe code should never cause memory unsafety, ever. If relying on those assumptions would cause memory unsafety, they should be marked unsafe. |
As a very wild suggestion: if prove!((a < b) ^ (a == b) ^ (a > b) forall a, b in T) {
/* fast implementation */
} else {
/* potentially slower implementation */
} cc @regehr might as well toy with the idea if we have access to the tools |
This is really interesting. I have been playing with the design of something similar to this -- an interface between optimizer and prover for Rust. I need to think on it more. Thanks for ccing me. |
P-low, not 1.0 |
There's a simple solution to this, so I don't think it needs to be an issue. Add a default method |
…t-lang#14420) Commit efe3fe9 removed the ability for `single_match` and `single_match_else` to trigger if comments were present outside of the arms, as those comments would be lost while rewriting the `match` expression. This reinstates the lint, but prevents the suggestion from being applied automatically in the presence of comments by using the `MaybeIncorrect` applicability. Also, a note is added to the lint message to warn the user about the need to preserve the comments if acting upon the suggestion. changelog: [`single_match`, `single_match_else`]: reinstate lint when comments are inside the `match` but do not autofix the code Fix rust-lang#14418
Some traits like
TotalOrd
andTotalEq
have invariants (likea < b || a == b || a > b
for alla
andb
forTotalOrd
). These can be stated in documentation but not enforced by the compiler or in the type system.Some algorithms (e.g. a
sort
, or a data structure) may wish to rely on these stated invariants in a way that means a violation could be memory unsafe. E.g. a highly-optimisedsort
might run out of bounds when a comparison function is incorrect in exactly the right (well, wrong) way. Being safe in the face of incorrectness can come at the cost of efficiency by being forced to use a slower algorithm, or perform bounds checks etc.Nominating.
The text was updated successfully, but these errors were encountered: