Open
Description
@Johnny_Bob on Reddit made this interesting comment on the v0.4.0 release notice thread posted there:
This looks very useful. BTW in some simple situations it could be nice to use decorators to specify contracts. Did you consider adding something like this?
#[pre(x != i32::MAX, "cannot add one to input at max of number range")] #[post(y, y - 1 == x, "reverse operation did not produce input")] fn add_one(x: i32) -> i32 { x + 1 }
@ErichDonGubler's response was:
Huh, I hadn't considered this before, but this actually sounds really interesting. I would only want to list some concerns I have with this:
- Do you consider it worthwhile to implement this in Adhesion when this functionality already exists in libhoare? There are several arguments I can think of, but I'm uncertain of their value and wanted your thoughts:
- Especially nice is keeping the check names the same (i.e.,
pre
instead oflibhoare
'sprecond
,double_check
instead ofinvariant
)- It lets users depend on a single library.
- I don't believe that we can use non-string expressions in attributes right now, but I could be wrong. That's an implementation detail, though. :)
- Adhesion currently is expected to work on stable, while custom attributes generally involve some hackery to work. I'm not sure how to get this to work, and would need some support to ship this (if it's possible). As an alternative, however, we might be able to make an alternate syntax possible when considering the next point...
- How should this interact with the
contract!
macro? I might actually be able to implement an alternate syntax if the expectation is that a short version a la your suggestion will only work inside of acontract!
block -- but I wonder if users might get confused if they try this outside of the macro usage. Some cases:
- Attempted usage outside of
contract!
macro?- Attempted usage INSIDE a
contract!
macro?- Trying to specify
pre
andpost
blocks at the same time as the shorthand version should do...what? Should we accept it (if so, doing what?), fail silently, or throw an error? How important is it that the error be a good one?- Currently, Adhesion doesn't control conditional compilation -- that's still determined by individual statements and
cfg
s in the different check blocks. It would be necessary to offer similar control here, and some brief bikeshedding ( ;) ) on associated syntax would be necessary.What do you think? I'm more than happy to do the work to implement this suggestion, because I really like it -- I'm just worried that it may not be possible or worth the effort yet.
EDIT: more details and grammar fixes.