5

I'm implementing a class which implements IDictionary. But I'm not able to match the code contract for TryGetValue.

Here's the relevant part of the code:

class Wrapper : IDictionary<string, object> 
{
    ...
    IDictionary<string,object> dictionary;  

    public bool TryGetValue(string key, out object value)
    {
        var result = dictionary.TryGetValue(key, out value);
        Contract.Assume(result == ContainsKey(key));
        return result;
    }

    public bool ContainsKey(string key)
    {
        Contract.Ensures(Contract.Result<bool>() == dictionary.ContainsKey(key));
        return dictionary.ContainsKey(key);
    }

    ...
}

The static analysis complains about:

CodeContracts: ensures unproven: Contract.Result<bool>() == @this.ContainsKey(key)

What should I do to meet the contract requirements?

Remarks: The problem doesn't appear, when the key is generic (like Wrapper<TKey> : IDictionary<TKey, object>).

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
Michael Stoll
  • 1,334
  • 3
  • 13
  • 35
  • A workaround, since you already noticed that there is no problem if `Wrapper` is generic, is to expand on a generic wrapper: `class Wrapper : Wrapper`. –  Sep 24 '13 at 19:52
  • @hvd That works. But before doing that I'd add a ContractVerification(false) attribute. – Michael Stoll Sep 25 '13 at 08:48
  • This looks like a bug on the static checker side. I'll investigate. – Manuel Fahndrich Sep 27 '13 at 19:13
  • 2
    I ran into the exact same error. I think the problem is that the dictionary can be modified between a call to `ContainsKey()` and a call to `TryGetValue()`? That would explain why the post condition cannot be statically proven. Haven't found a solution yet. – Steven Liekens Jul 27 '14 at 16:28

0 Answers0