Skip to content

Commit

Permalink
Remove remaining usage of Contract.* (dotnet/corefx#36002)
Browse files Browse the repository at this point in the history
Only remaining use of System.Diagnostics.Contracts in corefx is tests for the library itself, and usage of the [Pure] attribute.

Commit migrated from dotnet/corefx@940ecee
  • Loading branch information
stephentoub authored Mar 14, 2019
1 parent 22435bc commit e7e11b1
Show file tree
Hide file tree
Showing 78 changed files with 82 additions and 543 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public static ImmutableDictionary<TKey, TValue> ToImmutableDictionary<TSource, T
Requires.NotNull(source, nameof(source));
Requires.NotNull(keySelector, nameof(keySelector));
Requires.NotNull(elementSelector, nameof(elementSelector));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

return ImmutableDictionary<TKey, TValue>.Empty.WithComparers(keyComparer, valueComparer)
.AddRange(source.Select(element => new KeyValuePair<TKey, TValue>(keySelector(element), elementSelector(element))));
Expand Down Expand Up @@ -255,7 +254,6 @@ public static ImmutableDictionary<TKey, TValue> ToImmutableDictionary<TSource, T
public static ImmutableDictionary<TKey, TValue> ToImmutableDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
{
Requires.NotNull(source, nameof(source));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

var existingDictionary = source as ImmutableDictionary<TKey, TValue>;
if (existingDictionary != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ public Builder ToBuilder()
public ImmutableDictionary<TKey, TValue> Add(TKey key, TValue value)
{
Requires.NotNullAllowStructs(key, nameof(key));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

var result = Add(key, value, KeyCollisionBehavior.ThrowIfValueDifferent, this.Origin);
return result.Finalize(this);
Expand All @@ -318,7 +317,6 @@ public ImmutableDictionary<TKey, TValue> Add(TKey key, TValue value)
public ImmutableDictionary<TKey, TValue> AddRange(IEnumerable<KeyValuePair<TKey, TValue>> pairs)
{
Requires.NotNull(pairs, nameof(pairs));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

return this.AddRange(pairs, false);
}
Expand All @@ -330,8 +328,6 @@ public ImmutableDictionary<TKey, TValue> AddRange(IEnumerable<KeyValuePair<TKey,
public ImmutableDictionary<TKey, TValue> SetItem(TKey key, TValue value)
{
Requires.NotNullAllowStructs(key, nameof(key));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);
Contract.Ensures(!Contract.Result<ImmutableDictionary<TKey, TValue>>().IsEmpty);

var result = Add(key, value, KeyCollisionBehavior.SetValue, this.Origin);
return result.Finalize(this);
Expand All @@ -347,7 +343,6 @@ public ImmutableDictionary<TKey, TValue> SetItem(TKey key, TValue value)
public ImmutableDictionary<TKey, TValue> SetItems(IEnumerable<KeyValuePair<TKey, TValue>> items)
{
Requires.NotNull(items, nameof(items));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

var result = AddRange(items, this.Origin, KeyCollisionBehavior.SetValue);
return result.Finalize(this);
Expand All @@ -360,7 +355,6 @@ public ImmutableDictionary<TKey, TValue> SetItems(IEnumerable<KeyValuePair<TKey,
public ImmutableDictionary<TKey, TValue> Remove(TKey key)
{
Requires.NotNullAllowStructs(key, nameof(key));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

var result = Remove(key, this.Origin);
return result.Finalize(this);
Expand All @@ -373,7 +367,6 @@ public ImmutableDictionary<TKey, TValue> Remove(TKey key)
public ImmutableDictionary<TKey, TValue> RemoveRange(IEnumerable<TKey> keys)
{
Requires.NotNull(keys, nameof(keys));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

int count = _count;
var root = _root;
Expand Down Expand Up @@ -1091,7 +1084,6 @@ private ImmutableDictionary<TKey, TValue> Wrap(SortedInt32KeyNode<HashBucket> ro
private ImmutableDictionary<TKey, TValue> AddRange(IEnumerable<KeyValuePair<TKey, TValue>> pairs, bool avoidToHashMap)
{
Requires.NotNull(pairs, nameof(pairs));
Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);

// Some optimizations may apply if we're an empty list.
if (this.IsEmpty && !avoidToHashMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -167,7 +166,6 @@ internal static bool CollectionEquals<T>(this IEnumerable<T> sequence1, IEnumera
internal static IOrderedCollection<T> AsOrderedCollection<T>(this IEnumerable<T> sequence)
{
Requires.NotNull(sequence, nameof(sequence));
Contract.Ensures(Contract.Result<IOrderedCollection<T>>() != null);

var orderedCollection = sequence as IOrderedCollection<T>;
if (orderedCollection != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ private ImmutableHashSet(SortedInt32KeyNode<HashBucket> root, IEqualityComparer<
/// </summary>
public ImmutableHashSet<T> Clear()
{
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>().IsEmpty);
return this.IsEmpty ? this : ImmutableHashSet<T>.Empty.WithComparer(_equalityComparer);
}

Expand Down Expand Up @@ -195,8 +193,6 @@ public Builder ToBuilder()
[Pure]
public ImmutableHashSet<T> Add(T item)
{
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);

var result = Add(item, this.Origin);
return result.Finalize(this);
}
Expand All @@ -206,8 +202,6 @@ public ImmutableHashSet<T> Add(T item)
/// </summary>
public ImmutableHashSet<T> Remove(T item)
{
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);

var result = Remove(item, this.Origin);
return result.Finalize(this);
}
Expand Down Expand Up @@ -245,7 +239,6 @@ public bool TryGetValue(T equalValue, out T actualValue)
public ImmutableHashSet<T> Union(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);

return this.Union(other, avoidWithComparer: false);
}
Expand All @@ -257,7 +250,6 @@ public ImmutableHashSet<T> Union(IEnumerable<T> other)
public ImmutableHashSet<T> Intersect(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);

var result = Intersect(other, this.Origin);
return result.Finalize(this);
Expand All @@ -283,7 +275,6 @@ public ImmutableHashSet<T> Except(IEnumerable<T> other)
public ImmutableHashSet<T> SymmetricExcept(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
Contract.Ensures(Contract.Result<IImmutableSet<T>>() != null);

var result = SymmetricExcept(other, this.Origin);
return result.Finalize(this);
Expand Down Expand Up @@ -446,7 +437,6 @@ public bool Contains(T item)
[Pure]
public ImmutableHashSet<T> WithComparer(IEqualityComparer<T> equalityComparer)
{
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);
if (equalityComparer == null)
{
equalityComparer = EqualityComparer<T>.Default;
Expand All @@ -460,6 +450,7 @@ public ImmutableHashSet<T> WithComparer(IEqualityComparer<T> equalityComparer)
{
var result = new ImmutableHashSet<T>(equalityComparer);
result = result.Union(this, avoidWithComparer: true);
Debug.Assert(result != null);
return result;
}
}
Expand Down Expand Up @@ -1041,7 +1032,6 @@ private ImmutableHashSet<T> Wrap(SortedInt32KeyNode<HashBucket> root, int adjust
private ImmutableHashSet<T> Union(IEnumerable<T> items, bool avoidWithComparer)
{
Requires.NotNull(items, nameof(items));
Contract.Ensures(Contract.Result<ImmutableHashSet<T>>() != null);

// Some optimizations may apply if we're an empty set.
if (this.IsEmpty && !avoidWithComparer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;

namespace System.Collections.Immutable
{
Expand Down Expand Up @@ -249,9 +248,6 @@ private void ResetStack()
/// </summary>
private void ThrowIfDisposed()
{
Contract.Ensures(_root != null);
Contract.EnsuresOnThrow<ObjectDisposedException>(_root == null);

// Since this is a struct, copies might not have been marked as disposed.
// But the stack we share across those copies would know.
// This trick only works when we have a non-null stack.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ internal sealed class Node : IBinaryTree<T>, IEnumerable<T>
/// </summary>
private Node()
{
Contract.Ensures(this.IsEmpty);
_frozen = true; // the empty node is *always* frozen.
Debug.Assert(this.IsEmpty);
}

/// <summary>
Expand All @@ -86,14 +86,15 @@ private Node(T key, Node left, Node right, bool frozen = false)
Requires.NotNull(left, nameof(left));
Requires.NotNull(right, nameof(right));
Debug.Assert(!frozen || (left._frozen && right._frozen));
Contract.Ensures(!this.IsEmpty);

_key = key;
_left = left;
_right = right;
_height = ParentHeight(left, right);
_count = ParentCount(left, right);
_frozen = frozen;

Debug.Assert(!this.IsEmpty);
}

/// <summary>
Expand All @@ -106,7 +107,6 @@ public bool IsEmpty
{
get
{
Contract.Ensures(Contract.Result<bool>() == (_left == null));
Debug.Assert(!(_left == null ^ _right == null));
return _left == null;
}
Expand Down Expand Up @@ -441,7 +441,6 @@ internal Node RemoveAt(int index)
internal Node RemoveAll(Predicate<T> match)
{
Requires.NotNull(match, nameof(match));
Contract.Ensures(Contract.Result<Node>() != null);

var result = this;
var enumerator = new Enumerator(result);
Expand All @@ -467,6 +466,7 @@ internal Node RemoveAll(Predicate<T> match)
enumerator.Dispose();
}

Debug.Assert(result != null);
return result;
}

Expand Down Expand Up @@ -558,7 +558,6 @@ internal Node Reverse(int index, int count)
internal Node Sort(Comparison<T> comparison)
{
Requires.NotNull(comparison, nameof(comparison));
Contract.Ensures(Contract.Result<Node>() != null);

// PERF: Eventually this might be reimplemented in a way that does not require allocating an array.
var array = new T[this.Count];
Expand Down Expand Up @@ -1018,7 +1017,6 @@ internal T Find(Predicate<T> match)
internal ImmutableList<T> FindAll(Predicate<T> match)
{
Requires.NotNull(match, nameof(match));
Contract.Ensures(Contract.Result<ImmutableList<T>>() != null);

if (this.IsEmpty)
{
Expand Down Expand Up @@ -1060,7 +1058,6 @@ internal ImmutableList<T> FindAll(Predicate<T> match)
internal int FindIndex(Predicate<T> match)
{
Requires.NotNull(match, nameof(match));
Contract.Ensures(Contract.Result<int>() >= -1);

return this.FindIndex(0, _count, match);
}
Expand Down Expand Up @@ -1168,7 +1165,6 @@ internal T FindLast(Predicate<T> match)
internal int FindLastIndex(Predicate<T> match)
{
Requires.NotNull(match, nameof(match));
Contract.Ensures(Contract.Result<int>() >= -1);

return this.IsEmpty ? -1 : this.FindLastIndex(this.Count - 1, this.Count, match);
}
Expand Down Expand Up @@ -1259,7 +1255,6 @@ private Node RotateLeft()
{
Debug.Assert(!this.IsEmpty);
Debug.Assert(!_right.IsEmpty);
Contract.Ensures(Contract.Result<Node>() != null);

return _right.MutateLeft(this.MutateRight(_right._left));
}
Expand All @@ -1272,7 +1267,6 @@ private Node RotateRight()
{
Debug.Assert(!this.IsEmpty);
Debug.Assert(!_left.IsEmpty);
Contract.Ensures(Contract.Result<Node>() != null);

return _left.MutateRight(this.MutateLeft(_left._right));
}
Expand All @@ -1286,7 +1280,6 @@ private Node DoubleLeft()
Debug.Assert(!this.IsEmpty);
Debug.Assert(!_right.IsEmpty);
Debug.Assert(!_right._left.IsEmpty);
Contract.Ensures(Contract.Result<Node>() != null);

// The following is an optimized version of rotating the right child right, then rotating the parent left.
Node right = _right;
Expand All @@ -1305,7 +1298,6 @@ private Node DoubleRight()
Debug.Assert(!this.IsEmpty);
Debug.Assert(!_left.IsEmpty);
Debug.Assert(!_left._right.IsEmpty);
Contract.Ensures(Contract.Result<Node>() != null);

// The following is an optimized version of rotating the left child left, then rotating the parent right.
Node left = _left;
Expand Down
Loading

0 comments on commit e7e11b1

Please sign in to comment.