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

Error when contract validator method contains Contract.Requires/Ensures #15

Open
SergeyTeplyakov opened this issue Jul 12, 2014 · 2 comments
Assignees
Milestone

Comments

@SergeyTeplyakov
Copy link
Owner

Custom contract validators (methods marked with ContractArgumentValidator) should not contains any other calls of the Contract class except the call to the Contract.EndContractBlock:

[ContractArgumentValidator]
public void Guard()
{
    // error CC1054: Contract argument validator 'InstanceValidator.Guard' cannot contain
    // ordinary contracts. Only if-then-throw or validator calls are allowed.
    if (!_condition)
        throw new ArgumentException();
    Contract.Ensures(false);
    Contract.EndContractBlock();
}

Actually as part of this issue all other rules of the custom validators should be considered.

@pfurini
Copy link

pfurini commented Dec 18, 2014

Please consider the following scenario (tested and working):

  • You have an external library with some utility methods that perform standard argument validations (if/then/throw), but preceding that with standard Contract.Require calls to document the preconditions for the static checker (the static checker doesn't work very well with if/then/throw checks).
  • You want to compile this library emitting only the xxx.Contracts.dll file, to use alongside the aforementioned utility assembly. Doing so the Contract.Require calls do nothing in the released bits, but augment the CC reference library, used by the static checker.
  • You reference this assembly in a project that uses CC in Custom mode and with static checker enabled

Now the static checker will work as expected, thanks to the Contracts reference assembly augmented with standard Contract.Require calls. Otherwise you'll get weird warnings by the tool.

IMHO this enhancement should be removed or I should be able to selectively disable it..
Thanx

@SergeyTeplyakov
Copy link
Owner Author

I believe that I didn't implement this enhancement yet. When I'll start working on it, I'll definitely will consider your suggestion.

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