提交 5cfba81c 编写于 作者: R Rikki Gibson

Add similar fix for CompareExchange and add more tests

上级 e4fdfc79
......@@ -2988,6 +2988,12 @@ private void LearnFromCompareExchangeMethod(MethodSymbol method, BoundCall node,
return;
}
var arguments = node.Arguments;
if (arguments.Length != method.ParameterCount)
{
return;
}
// In general a call to CompareExchange of the form:
//
// Interlocked.CompareExchange(ref location, value, comparand);
......@@ -2999,7 +3005,6 @@ private void LearnFromCompareExchangeMethod(MethodSymbol method, BoundCall node,
// location = value;
// }
var arguments = node.Arguments;
var locationSlot = MakeSlot(arguments[0]);
if (locationSlot != -1)
{
......
......@@ -111749,7 +111749,34 @@ static void M2(string s1, string s2)
}
[Fact]
[WorkItem(35816, "https://github.com/dotnet/roslyn/issues/35816")]
[WorkItem(38010, "https://github.com/dotnet/roslyn/issues/38010")]
public void ReferenceEquals_IncompleteCall()
{
var source = @"
#nullable enable
class C
{
static void M()
{
ReferenceEquals(
}
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (7,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'objA' of 'object.ReferenceEquals(object, object)'
// ReferenceEquals(
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "ReferenceEquals").WithArguments("objA", "object.ReferenceEquals(object, object)").WithLocation(7, 9),
// (7,25): error CS1026: ) expected
// ReferenceEquals(
Diagnostic(ErrorCode.ERR_CloseParenExpected, "").WithLocation(7, 25),
// (7,25): error CS1002: ; expected
// ReferenceEquals(
Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(7, 25));
}
[Fact]
[WorkItem(38010, "https://github.com/dotnet/roslyn/issues/38010")]
public void EqualsMethod_BadArgCount()
{
var source = @"
......@@ -111816,7 +111843,7 @@ public override bool Equals(object other)
}
[Fact]
[WorkItem(35816, "https://github.com/dotnet/roslyn/issues/35816")]
[WorkItem(38010, "https://github.com/dotnet/roslyn/issues/38010")]
public void EqualsMethod_DefaultArgument_01()
{
var source = @"
......@@ -111851,7 +111878,7 @@ static void M(C c1)
}
[Fact]
[WorkItem(35816, "https://github.com/dotnet/roslyn/issues/35816")]
[WorkItem(38010, "https://github.com/dotnet/roslyn/issues/38010")]
public void EqualsMethod_DefaultArgument_02()
{
var source = @"
......@@ -113351,6 +113378,41 @@ void M()
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "location").WithLocation(19, 13));
}
[Fact]
[WorkItem(38010, "https://github.com/dotnet/roslyn/issues/38010")]
public void CompareExchange_BadArgCount()
{
var source = @"
#pragma warning disable 0436
using System.Threading;
namespace System.Threading
{
public static class Interlocked
{
public static object? CompareExchange(ref object? location, object? value, object? comparand) { return location; }
}
}
class C
{
void M()
{
string? location = null;
Interlocked.CompareExchange(ref location, new object()); // 1
_ = location.ToString(); // 2
}
}
";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (17,21): error CS7036: There is no argument given that corresponds to the required formal parameter 'comparand' of 'Interlocked.CompareExchange(ref object?, object?, object?)'
// Interlocked.CompareExchange(ref location, new object()); // 1
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CompareExchange").WithArguments("comparand", "System.Threading.Interlocked.CompareExchange(ref object?, object?, object?)").WithLocation(17, 21),
// (18,13): warning CS8602: Dereference of a possibly null reference.
// _ = location.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "location").WithLocation(18, 13));
}
[Fact]
[WorkItem(37187, "https://github.com/dotnet/roslyn/issues/37187")]
public void IEquatableContravariantNullability()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册