Skip to content

Commit

Permalink
v5 cleanup: Replace custom ReferenceEqualityComparer with the standar…
Browse files Browse the repository at this point in the history
…d one
  • Loading branch information
exyi committed Dec 20, 2024
1 parent 1fff36f commit b6de3a3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void AddNodeErrors(DothtmlNode node, int priority)
/// * the locations are processed using MapBindingLocation to make them useful in the context of a dothtml file </summary>
Dictionary<Exception, DotvvmCompilationSourceLocation?> AnnotateBindingExceptionWithLocation(ResolvedBinding binding, DotvvmProperty? relatedProperty, IEnumerable<Exception> errors)
{
var result = new Dictionary<Exception, DotvvmCompilationSourceLocation?>(ReferenceEqualityComparer<Exception>.Instance);
var result = new Dictionary<Exception, DotvvmCompilationSourceLocation?>(ReferenceEqualityComparer.Instance);

Check failure on line 74 in src/Framework/Framework/Compilation/ErrorCheckingVisitor.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 74 in src/Framework/Framework/Compilation/ErrorCheckingVisitor.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 74 in src/Framework/Framework/Compilation/ErrorCheckingVisitor.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 74 in src/Framework/Framework/Compilation/ErrorCheckingVisitor.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.
void recurse(Exception exception, DotvvmCompilationSourceLocation? location)
{
if (result.ContainsKey(exception))
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Framework/Controls/Repeater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private DotvvmControl CreateEmptyItem(IDotvvmRequestContext context)
return emptyDataContainer;
}

private readonly Dictionary<object, DataItemContainer> childrenCache = new(ReferenceEqualityComparer<object>.Instance);
private readonly Dictionary<object, DataItemContainer> childrenCache = new(ReferenceEqualityComparer.Instance);

Check failure on line 253 in src/Framework/Framework/Controls/Repeater.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 253 in src/Framework/Framework/Controls/Repeater.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 253 in src/Framework/Framework/Controls/Repeater.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 253 in src/Framework/Framework/Controls/Repeater.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.
private DotvvmControl AddItem(IList<DotvvmControl> c, IDotvvmRequestContext context, object? item = null, int? index = null, bool allowMemoizationRetrieve = false, bool allowMemoizationStore = false)
{
if (allowMemoizationRetrieve && item != null && childrenCache.TryGetValue(item, out var container2) && container2.Parent == null)
Expand Down
66 changes: 55 additions & 11 deletions src/Framework/Framework/Utils/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#if !NET_5_0_OR_GREATER

namespace DotVVM.Framework.Utils
using System.Runtime.CompilerServices;

namespace System.Collections.Generic
{
// TODO next version: Replace with System.Collections.Generic.ReferenceEqualityComparer
internal class ReferenceEqualityComparer<T> : IEqualityComparer<T>
where T : class
/// <summary>
/// An <see cref="IEqualityComparer{Object}"/> that uses reference equality (<see cref="object.ReferenceEquals(object?, object?)"/>)
/// instead of value equality (<see cref="object.Equals(object?)"/>) when comparing two object instances.
/// </summary>
/// <remarks>
/// The <see cref="ReferenceEqualityComparer"/> type cannot be instantiated. Instead, use the <see cref="Instance"/> property
/// to access the singleton instance of this type.
/// </remarks>
internal sealed class ReferenceEqualityComparer : IEqualityComparer<object?>, IEqualityComparer
{
public static ReferenceEqualityComparer<T> Instance { get; } = new ReferenceEqualityComparer<T>();


public bool Equals(T? x, T? y) => ReferenceEquals(x, y);
public int GetHashCode(T obj) => obj == null ? 0 : RuntimeHelpers.GetHashCode(obj);
private ReferenceEqualityComparer() { }

/// <summary>
/// Gets the singleton <see cref="ReferenceEqualityComparer"/> instance.
/// </summary>
public static ReferenceEqualityComparer Instance { get; } = new ReferenceEqualityComparer();

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

Check failure on line 24 in src/Framework/Framework/Utils/ReferenceEqualityComparer.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

The type 'ReferenceEqualityComparer' in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs' conflicts with the imported type 'ReferenceEqualityComparer' in 'System.Collections, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '/home/runner/work/dotvvm/dotvvm/src/Framework/Framework/Utils/ReferenceEqualityComparer.cs'.

/// <summary>
/// Determines whether two object references refer to the same object instance.
/// </summary>
/// <param name="x">The first object to compare.</param>
/// <param name="y">The second object to compare.</param>
/// <returns>
/// <see langword="true"/> if both <paramref name="x"/> and <paramref name="y"/> refer to the same object instance
/// or if both are <see langword="null"/>; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>
/// This API is a wrapper around <see cref="object.ReferenceEquals(object?, object?)"/>.
/// It is not necessarily equivalent to calling <see cref="object.Equals(object?, object?)"/>.
/// </remarks>
public new bool Equals(object? x, object? y) => ReferenceEquals(x, y);

/// <summary>
/// Returns a hash code for the specified object. The returned hash code is based on the object
/// identity, not on the contents of the object.
/// </summary>
/// <param name="obj">The object for which to retrieve the hash code.</param>
/// <returns>A hash code for the identity of <paramref name="obj"/>.</returns>
/// <remarks>
/// This API is a wrapper around <see cref="RuntimeHelpers.GetHashCode(object)"/>.
/// It is not necessarily equivalent to calling <see cref="object.GetHashCode()"/>.
/// </remarks>
public int GetHashCode(object? obj)
{
// Depending on target framework, RuntimeHelpers.GetHashCode might not be annotated
// with the proper nullability attribute. We'll suppress any warning that might
// result.
return RuntimeHelpers.GetHashCode(obj!);
}
}
}
#endif

0 comments on commit b6de3a3

Please sign in to comment.