I'm having an argument with the CodeContracts static analysis tool.
My code:
The tool tells me that instance.bar
may be a null reference. I believe the opposite.
Who is right? How can I prove it wrong?
I'm having an argument with the CodeContracts static analysis tool.
My code:
The tool tells me that instance.bar
may be a null reference. I believe the opposite.
Who is right? How can I prove it wrong?
CodeContracts is right. There is nothing stopping you from setting instance.bar = null
prior to calling the BarLength()
method.
I agree with you. instance
and bar
are both private, so CodeContracts should be able to know instance.bar
is never set to null.
If you mark the field 'bar' as readonly, that should satisfy the static analyzer that the field will never be set to anything else after the ctor executes.
Your code includes a private static initialized instance:
private static Foo instance = new Foo();
Are you assuming that this means the instance constructor will always have run before access to any static method, therefore ensuring bar
has been initialized?
In the single threaded case, I think you're right.
The sequence of events would be:
Foo.BarLength()
Foo
(if not already completed)instance
with instance of Foo
Foo.BarLength()
However, static initialization of a class is only ever triggered once per App Domain - and IIRC there's no blocking to ensure it's completed before any other static methods are called.
So, you could have this scenario:
Foo.BarLength()
Foo
(if not already completed) startsFoo.BarLength()
Foo
because that's already underwayFoo.BarLength()
null
static member instance
There's no way the Contracts analyser can know that you'd never run the code in a multithreaded way, so it has to err on the side of caution.
Update: It seems the problem is that invariants are not supported for static fields.
2nd Update: The method outlined below is currently the recommended solution.
A possible workaround is to create a property for instance
that Ensure
s the invariants that you want to hold. (Of course, you need to Assume
them for the Ensure
to be proven.) Once you have done this, you can just use the property and all the invariants should be proven correctly.
Here's your example using this method:
class Foo
{
private static readonly Foo instance = new Foo();
private readonly string bar;
public static Foo Instance
// workaround for not being able to put invariants on static fields
{
get
{
Contract.Ensures(Contract.Result<Foo>() != null);
Contract.Ensures(Contract.Result<Foo>().bar != null);
Contract.Assume(instance.bar != null);
return instance;
}
}
public Foo()
{
Contract.Ensures(bar != null);
bar = "Hello world!";
}
public static int BarLength()
{
Contract.Assert(Instance != null);
Contract.Assert(Instance.bar != null);
// both of these are proven ok
return Instance.bar.Length;
}
}