I've just started playing with code contracts, and while promising, they seem to have some limitations with respect to value types. For instance:
public struct Wrap<T>
where T : class
{
readonly T value;
public Wrap(T value)
{
Contract.Requires(value != null);
this.value = value;
}
public T Value
{
get
{
Contract.Requires(Value != null);
return value;
}
}
[Pure]
[ContractInvariantMethod]
void Invariant()
{
Contract.Invariant(value != null);
}
public static T BigError()
{
Contract.Ensures(Contract.Result<T>() != null);
var x = default(Wrap<T>);
Contract.Assert(x.Value != null);
return x.Value;
}
}
Wrap.BigError clearly demonstrates the problem. This sample compiles and ccheck verifies 4 assertions, yet the assertions will clearly fail at runtime. Some of these assertions are redundant and I inserted them just be sure the verifier is checking these properties at the designated points.
I don't see this sort of thing listed as a known problem in MS's docs for code contracts, but it seems too obvious to be an omission. Am I missing something?