Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
RFC: Attribute to distinguish safety preconditions from panic freedom #3893
base: main
Are you sure you want to change the base?
RFC: Attribute to distinguish safety preconditions from panic freedom #3893
Changes from 1 commit
673f679
600d95a
f7278e8
f75f27a
398e9cf
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 think this RFC contradicts the function contracts one. We have never restricted
requires
to safety conditions. If this is the way we want to go, the contracts RFC should be updated and we should consider at least warning users that addrequires
to safe functions.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 don't know that it's a contradiction--we have enabled modular safety verification, that's just not all that we've enabled (i.e., you can write preconditions for safe functions too).
It so happens that we've found preconditions most useful for unsafe functions, but if people want to use it differently (perhaps as a form of documentation on a safe function), I have no issue with that, and I don't think anything in this RFC contradicts that?
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'm rather confused now. What exactly are you proposing to be the semantic of
requires
andmay_panic
? Do you expect themay_panic
to be a subset of therequires
condition or do you expect that the correctness of the function to be described as a conjuction of both?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.
Note that in the only example of this PR implies that the correctness contract is a conjunction of
requires
andmay_panic
. It has the following attributes:This is literally implying that
requires
should always betrue
for safe functions. I.e.,requires
specifies only the safety contract, and it contradicts all the examples from the contracts RFC.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 am quite confused now as well, and I'm not optimistic that we're going to sort this out in Github discussion comments. I would recommend that perhaps @tautschnig schedules an optional meeting to discuss this RFC further--I think we'll make a lot more progress in a discussion than in Github comments. (Alternatively, we can block out more time at the Tuesday meeting for this RFC).
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.
How will this affect stubbing with contracts?
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'm wondering if this should be part of the function contracts RFC since it hasn't been stabilized yet.
Users would also be able to reuse
-Z function-contracts
?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.
+1 to using the same unstable flag. I could take or leave it being part of the function contracts RFC--I like it separate I think.
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.
Naming this "may" makes it seem like panic may also not happen.
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.
This seems sound to me, which is obviously our first priority -- I would love if we could come up with something that didn't require running twice, but I don't currently have any ideas.
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.
Do you have an example?
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.
#3567 (comment)
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.
This sounds to me like a corner case. The more common case is that a function's documentation includes a section of the panic condition (e.g. https://doc.rust-lang.org/std/string/struct.String.html#panics), and thus is clearly defined in terms of the function's input parameters.
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.
Okay, so your argument is that it's better to introduce
#[panics_if(cond)]
that provides a stronger guarantee that it must panic, with the tradeoff that it's not applicable to cases like the linked issue?Also, there isn't fundamentally a reason we can't just have both
#[panics_if(cond)]
and#[may_panic_if(cond)]
, as long as we document the differences well. Although at that point it may make more sense to just have#[panics_if(cond)]
and#[may_panic]
, where#[may_panic]
just says "if it panics, succeed; otherwise, succeed iff the postcondition holds.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.
Also, per #3567 (comment):
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.
Which would be the same as the
#[should_panic]
that harnesses can be annotated with?I don't see how the two are equivalent since
cond
is essentially a pre-condition, so its not clear to me how adding#[ensures(!cond)]
would provide the stronger guarantee that#[panics_if]
provides.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.
Not quite, e.g.:
fails with:
whereas with
#[may_panic]
, it would succeed. (If I could go back and bikeshed, I would have named#[should_panic]
#[must_panic]
instead 😄 )We only check the postcondition if the function does not panic, so if we reach the point where we are enforcing the postcondition,
cond
must not hold. (If it does hold, then we should have panicked).Taking your example from #3567 (comment),
would become
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.
Perhaps it needs to be
#[ensures(old(!cond))]
instead in case the function mutatesself
?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.
should, may, are both indicating the behaviour is optional. I find this misleading