Skip to content

Commit

Permalink
Remove code to throw an exception
Browse files Browse the repository at this point in the history
This fixes a regression introduced in a398f71.
I misunderstood that the `TryAddInternal` method *always* sets the out value even when returning false.
  • Loading branch information
AArnott committed Mar 16, 2023
1 parent 8a7cf22 commit 158c6ab
Showing 1 changed file with 3 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public bool TryAdd(Type key, Func<Type, TValue> valueFactory)
return this.TryAddInternal(key, valueFactory, out TValue? _);
}

private bool TryAddInternal(Type key, Func<Type, TValue> valueFactory, [MaybeNullWhen(false)] out TValue resultingValue)
private bool TryAddInternal(Type key, Func<Type, TValue> valueFactory, out TValue resultingValue)
{
lock (this.writerLock)
{
Expand Down Expand Up @@ -91,7 +91,7 @@ private bool TryAddInternal(Type key, Func<Type, TValue> valueFactory, [MaybeNul
}
}

private bool AddToBuckets(Entry[] buckets, Type newKey, Entry? newEntryOrNull, Func<Type, TValue>? valueFactory, out TValue? resultingValue)
private bool AddToBuckets(Entry[] buckets, Type newKey, Entry? newEntryOrNull, Func<Type, TValue>? valueFactory, out TValue resultingValue)
{
var h = (newEntryOrNull != null) ? newEntryOrNull.Hash : newKey.GetHashCode();
if (buckets[h & (buckets.Length - 1)] == null)
Expand Down Expand Up @@ -180,11 +180,7 @@ public TValue GetOrAdd(Type key, Func<Type, TValue> valueFactory)
return v;
}

if (!this.TryAddInternal(key, valueFactory, out v) && !this.TryGetValue(key, out v))
{
throw new InvalidOperationException("Failed to get or add.");
}

this.TryAddInternal(key, valueFactory, out v);
return v;
}

Expand Down

0 comments on commit 158c6ab

Please sign in to comment.