Skip to content

Commit

Permalink
Fix SimpleDiagnostic GetHashCode is more specific than Equals
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelvo committed Feb 6, 2015
1 parent ef583af commit 7b57680
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ private SimpleDiagnostic(
if ((warningLevel == 0 && severity != DiagnosticSeverity.Error) ||
(warningLevel != 0 && severity == DiagnosticSeverity.Error))
{
throw new ArgumentException("warningLevel");
throw new ArgumentException(nameof(warningLevel));
}

if(descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}

_descriptor = descriptor;
_severity = severity;
_warningLevel = warningLevel;
_location = location;
_location = location ?? Location.None;
_additionalLocations = additionalLocations == null ? SpecializedCollections.EmptyReadOnlyList<Location>() : additionalLocations.ToImmutableArray();
_messageArgs = messageArgs ?? SpecializedCollections.EmptyArray<object>();
}
Expand Down Expand Up @@ -131,10 +136,9 @@ public override bool Equals(object obj)
public override int GetHashCode()
{
return Hash.Combine(_descriptor,
Hash.Combine(_messageArgs.GetHashCode(),
Hash.Combine(_location.GetHashCode(),
Hash.Combine(_severity.GetHashCode(), _warningLevel)
)));
Hash.CombineValues(_messageArgs,
Hash.Combine(_warningLevel,
Hash.Combine(_location, (int)_severity))));
}

internal override Diagnostic WithLocation(Location location)
Expand Down
24 changes: 24 additions & 0 deletions src/Compilers/Core/Portable/InternalUtilities/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ internal static int CombineValues<T>(IEnumerable<T> values, int maxItemsToHash =
return hashCode;
}

internal static int CombineValues<T>(T[] values, int maxItemsToHash = int.MaxValue)
{
if (values == null)
{
return 0;
}

var maxSize = Math.Min(maxItemsToHash, values.Length);
var hashCode = 0;

for (int i = 0; i < maxSize; i++)
{
T value = values[i];

// Should end up with a constrained virtual call to object.GetHashCode (i.e. avoid boxing where possible).
if (value != null)
{
hashCode = Hash.Combine(value.GetHashCode(), hashCode);
}
}

return hashCode;
}

internal static int CombineValues<T>(ImmutableArray<T> values, int maxItemsToHash = int.MaxValue)
{
if (values.IsDefaultOrEmpty)
Expand Down

0 comments on commit 7b57680

Please sign in to comment.