// (18,14): warning CS8619: Nullability of reference types in value of type '<anonymous type: CL1? p1, CL1 p2>' doesn't match target type '<anonymous type: CL1 p1, CL1? p2>'.
// (37,14): warning CS8619: Nullability of reference types in value of type '<anonymous type: <anonymous type: CL1? p1, CL1 p2> p0>' doesn't match target type '<anonymous type: <anonymous type: CL1 p1, CL1? p2> p0>'.
public static T CompareExchange<T>(ref T location, T value, T comparand) where T : class? {{ return location; }}
}}
}}
class C
{{
// locationNonNull is annotated, but its flow state is non-null
void M<T>(T? locationNonNull, T? locationMaybeNull, T comparandNonNull, T? comparandMaybeNull, T valueNonNull, T? valueMaybeNull) where T : class, new()
public static T CompareExchange<T>(ref T location, T value, T comparand) where T : class? {{ return location; }}
}}
}}
class C
{{
// locationNonNull is annotated, but its flow state is non-null
void M<T>(T? locationNonNull, T? locationMaybeNull, T comparandNonNull, T? comparandMaybeNull, T valueNonNull, T? valueMaybeNull) where T : class, new()
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(18, 58), // Unexpected warning, due to method type inference with null https://github.com/dotnet/roslyn/issues/43536
// (19,13): warning CS8602: Dereference of a possibly null reference.
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(18, 58), // Unexpected warning, due to method type inference with null https://github.com/dotnet/roslyn/issues/43536
public static T CompareExchange<T>(ref T location, T value, T comparand) where T : class? {{ return location; }}
}}
}}
class C
{{
// locationNonNull is annotated, but its flow state is non-null
void M<T>(T? locationNonNull, T? locationMaybeNull, T comparandNonNull, T? comparandMaybeNull, T valueNonNull, T? valueMaybeNull) where T : class, new()
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(18, 72) // Unexpected warning, due to method type inference with null https://github.com/dotnet/roslyn/issues/43536
);
source = $@"
class C
{{
...
...
@@ -128091,14 +128588,14 @@ public void CompareExchangeT_NonNullLocation_MaybeNullReturnValue()
var source = @"
#pragma warning disable 0436
using System.Threading;
using System.Diagnostics.CodeAnalysis;
namespace System.Threading
{
public static class Interlocked
{
public static T CompareExchange<T>(ref T location, T value, T comparand) where T : class? { return location; }
public static T CompareExchange<T>([NotNullIfNotNull(""value"")] ref T location, T value, T comparand) where T : class? { return location; }
}
}
class C
{
// location is annotated, but its flow state is non-null
...
...
@@ -128107,6 +128604,11 @@ void M<T>(T? location, T value, T comparand) where T : class, new()
location = new T();
var result = Interlocked.CompareExchange(ref location, value, comparand);
_ = location.ToString();
_ = result.ToString();
location = null;
result = Interlocked.CompareExchange(ref location, value, null);
_ = location.ToString();
_ = result.ToString(); // 1
}
}
...
...
@@ -128114,11 +128616,11 @@ void M<T>(T? location, T value, T comparand) where T : class, new()
// Note: the return value of CompareExchange is the value in `location` before the call.
// Therefore it might make sense to give the return value a flow state equal to the flow state of `location` before the call.
// Tracked by https://github.com/dotnet/roslyn/issues/36911
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
var comp = CreateCompilation(new[] { source, NotNullIfNotNullAttributeDefinition }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (20,13): warning CS8602: Dereference of a possibly null reference.
// (25,13): warning CS8602: Dereference of a possibly null reference.