未验证 提交 436671f5 编写于 作者: J Julien Couvreur 提交者: GitHub

Use Equals to compare array types in attributes (#31741)

上级 6d88f91c
......@@ -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).
......
......@@ -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()
{
......@@ -109,6 +109,31 @@ Namespace System
End Namespace
"
<Fact>
Public Sub TupleNamesInArrayInAttribute()
Dim comp = CreateCompilation(
<compilation>
<file name="a.vb"><![CDATA[
Imports System
<My(New (String, bob As String)() { })>
Public Class MyAttribute
Inherits System.Attribute
Public Sub New(x As (alice As String, String)())
End Sub
End Class
]]></file>
</compilation>)
comp.AssertTheseDiagnostics(<errors><![CDATA[
BC30045: Attribute constructor has a parameter of type '(alice As String, String)()', which is not an integral, floating-point or Enum type or one of Object, Char, String, Boolean, System.Type or 1-dimensional array of these types.
<My(New (String, bob As String)() { })>
~~
]]></errors>)
End Sub
<Fact>
Public Sub TupleTypeBinding()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册