Skip to content
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

Attribute shorthand for check blocks that could just be an expression #33

Open
ErichDonGubler opened this issue Oct 16, 2017 · 2 comments
Assignees
Milestone

Comments

@ErichDonGubler
Copy link
Owner

@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:

  1. 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 of libhoare's precond, double_check instead of invariant)
    • It lets users depend on a single library.
  2. 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. :)
  3. 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...
  4. 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 a contract! block -- but I wonder if users might get confused if they try this outside of the macro usage. Some cases:
    1. Attempted usage outside of contract! macro?
    2. Attempted usage INSIDE a contract! macro?
    3. Trying to specify pre and post 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?
  5. Currently, Adhesion doesn't control conditional compilation -- that's still determined by individual statements and cfgs 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.

@ErichDonGubler ErichDonGubler added this to the 1.0 milestone Oct 16, 2017
@ErichDonGubler ErichDonGubler self-assigned this Oct 16, 2017
@ErichDonGubler ErichDonGubler changed the title Consider a shorthand for check blocks that could just be an expression Attribute shorthand for check blocks that could just be an expression Oct 16, 2017
@hellow554
Copy link

I'd like to have this feature too. I saw it on libhoare and there it worked (a long time ago) maybe you could just adopt the old code to work with the current rustc?

@ErichDonGubler
Copy link
Owner Author

ErichDonGubler commented Jan 12, 2018

Maybe -- I'd definitely want to add it as an unstable feature for now, since adding new attributes is unstable.

I would definitely be open to experimenting, though. Looks like the code libhoare uses isn't too bad, if somewhat boilerplate-ish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants