From 436671f5b230ac230c817780552df9fd7b782b8f Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 13 Dec 2018 19:03:22 -0800 Subject: [PATCH] Use Equals to compare array types in attributes (#31741) --- .../Portable/Binder/Binder_Attributes.cs | 4 +- .../Semantics/NullableReferenceTypesTests.cs | 79 +++++++++++++++++++ .../Test/Emit/CodeGen/CodeGenTuples.vb | 25 ++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs index a5d16ccba06..c50a272516e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs @@ -625,7 +625,9 @@ private TypeSymbol BindNamedAttributeArgumentType(AttributeArgumentSyntax namedA { hasErrors = true; } - else if (reorderedArgument.Kind == TypedConstantKind.Array && parameter.Type.TypeKind == TypeKind.Array && (TypeSymbol)reorderedArgument.Type != parameter.Type.TypeSymbol) + else if (reorderedArgument.Kind == TypedConstantKind.Array && + parameter.Type.TypeKind == TypeKind.Array && + !((TypeSymbol)reorderedArgument.Type).Equals(parameter.Type.TypeSymbol, TypeCompareKind.AllIgnoreOptions)) { // NOTE: As in dev11, we don't allow array covariance conversions (presumably, we don't have a way to // represent the conversion in metadata). diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index c4a178f5897..dd379807813 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -445,6 +445,85 @@ static void F2(object? w) Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(14, 26)); } + [Fact, WorkItem(31740, "https://github.com/dotnet/roslyn/issues/31740")] + public void Attribute_ArrayWithDifferentNullability() + { + var source = +@" +public class MyAttribute : System.Attribute +{ + public MyAttribute(string[] x) { } +} +#nullable enable +[My(new string[] { ""hello"" })] +class C { } +"; + var comp = CreateCompilation(source, options: TestOptions.DebugDll); + comp.VerifyDiagnostics(); + } + + [Fact, WorkItem(31740, "https://github.com/dotnet/roslyn/issues/31740")] + public void Attribute_ArrayWithDifferentNullability2() + { + var source = +@" +public class MyAttribute : System.Attribute +{ + public MyAttribute(string[] x) { } +} +#nullable enable +[My(new string[] { null })] +class C { } +"; + var comp = CreateCompilation(source, options: TestOptions.DebugDll); + comp.VerifyDiagnostics(); + // Expecting a warning. Issue tracked by https://github.com/dotnet/roslyn/issues/23697 + } + + [Fact, WorkItem(31740, "https://github.com/dotnet/roslyn/issues/31740")] + public void Attribute_ArrayWithDifferentNullability_DynamicAndTupleNames() + { + var source = @" +public class MyAttribute : System.Attribute +{ + public MyAttribute((string alice, string)[] x, dynamic[] y, object[] z) { } +} +#nullable enable +[My(new (string, string bob)[] { }, new object[] { }, new dynamic[] { })] +class C { } +"; + var comp = CreateCompilation(source, options: TestOptions.DebugDll); + comp.VerifyDiagnostics( + // (7,2): error CS0181: Attribute constructor parameter 'x' has type '(string alice, string)[]', which is not a valid attribute parameter type + // [My(new (string, string bob)[] { }, new object[] { }, new dynamic[] { })] + Diagnostic(ErrorCode.ERR_BadAttributeParamType, "My").WithArguments("x", "(string alice, string)[]").WithLocation(7, 2), + // (7,2): error CS0181: Attribute constructor parameter 'y' has type 'dynamic[]', which is not a valid attribute parameter type + // [My(new (string, string bob)[] { }, new object[] { }, new dynamic[] { })] + Diagnostic(ErrorCode.ERR_BadAttributeParamType, "My").WithArguments("y", "dynamic[]").WithLocation(7, 2) + ); + } + + [Fact, WorkItem(31740, "https://github.com/dotnet/roslyn/issues/31740")] + public void Attribute_ArrayWithDifferentNullability_Dynamic() + { + var source = +@" +public class MyAttribute : System.Attribute +{ + public MyAttribute(dynamic[] y) { } +} +#nullable enable +[My(new object[] { })] +class C { } +"; + var comp = CreateCompilation(source, options: TestOptions.DebugDll); + comp.VerifyDiagnostics( + // (7,2): error CS0181: Attribute constructor parameter 'y' has type 'dynamic[]', which is not a valid attribute parameter type + // [My(new object[] { })] + Diagnostic(ErrorCode.ERR_BadAttributeParamType, "My").WithArguments("y", "dynamic[]").WithLocation(7, 2) + ); + } + [Fact] public void NullableAndConditionalOperators() { diff --git a/src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTuples.vb b/src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTuples.vb index 02af9946306..98adac8db53 100644 --- a/src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTuples.vb +++ b/src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTuples.vb @@ -109,6 +109,31 @@ Namespace System End Namespace " + + Public Sub TupleNamesInArrayInAttribute() + + Dim comp = CreateCompilation( + + +Public Class MyAttribute + Inherits System.Attribute + + Public Sub New(x As (alice As String, String)()) + End Sub +End Class + ]]> +) + + comp.AssertTheseDiagnostics( + ~~ + ]]>) + + End Sub + Public Sub TupleTypeBinding() -- GitLab