Skip to content

Commit

Permalink
Fix NRE in TypedConstantComparer (#69062)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Jul 17, 2023
1 parent d49a1c2 commit 4b7d601
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,25 @@ public void Type_Attribute_Delete_NotSupportedByRuntime2()
capabilities: EditAndContinueCapabilities.Baseline);
}

[Fact]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1831006")]
public void Type_Attribute_Update_Null()
{
var attribute = @"
using System;
public class A : Attribute { public A1(int[] array, Type type, Type[] types) {} }
";

var src1 = attribute + "[A(null, null, new Type[] { typeof(C) })]class C { }";
var src2 = attribute + "[A(null, null, null)]class C { }";

var edits = GetTopEdits(src1, src2);

edits.VerifySemantics(
semanticEdits: new[] { SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C")) },
capabilities: EditAndContinueCapabilities.ChangeCustomAttributes);
}

[Fact]
public void Type_Attribute_Change_Reloadable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6632,17 +6632,17 @@ internal static void AddNodes<T>(ArrayBuilder<SyntaxNode> nodes, SeparatedSyntax

private sealed class TypedConstantComparer : IEqualityComparer<TypedConstant>
{
public static TypedConstantComparer Instance = new TypedConstantComparer();
public static readonly TypedConstantComparer Instance = new();

public bool Equals(TypedConstant x, TypedConstant y)
=> x.Kind.Equals(y.Kind) &&
x.IsNull.Equals(y.IsNull) &&
=> x.Kind == y.Kind &&
x.IsNull == y.IsNull &&
SymbolEquivalenceComparer.Instance.Equals(x.Type, y.Type) &&
x.Kind switch
{
TypedConstantKind.Array => x.Values.SequenceEqual(y.Values, TypedConstantComparer.Instance),
TypedConstantKind.Type => TypesEquivalent(x.Value as ITypeSymbol, y.Value as ITypeSymbol, exact: false),
_ => object.Equals(x.Value, y.Value)
TypedConstantKind.Array => x.Values.IsDefault || x.Values.SequenceEqual(y.Values, Instance),
TypedConstantKind.Type => TypesEquivalent((ITypeSymbol?)x.Value, (ITypeSymbol?)y.Value, exact: false),
_ => Equals(x.Value, y.Value)
};

public int GetHashCode(TypedConstant obj)
Expand All @@ -6651,7 +6651,7 @@ public int GetHashCode(TypedConstant obj)

private sealed class NamedArgumentComparer : IEqualityComparer<KeyValuePair<string, TypedConstant>>
{
public static NamedArgumentComparer Instance = new NamedArgumentComparer();
public static readonly NamedArgumentComparer Instance = new();

public bool Equals(KeyValuePair<string, TypedConstant> x, KeyValuePair<string, TypedConstant> y)
=> x.Key.Equals(y.Key) &&
Expand Down

0 comments on commit 4b7d601

Please sign in to comment.