Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NRE in TypedConstantComparer #69062

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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