Skip to content

Commit

Permalink
Address some new Roslyn nullability warnings (#35782)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed May 5, 2020
1 parent 30c0d0a commit c9f9170
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static string FormatFileLoadExceptionMessage(string? fileName, int hRes
else
GetMessageForHR(hResult, new StringHandleOnStack(ref message));

return string.Format(format, fileName, message);
return string.Format(format!, fileName, message);
}

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMar
{
RuntimeAssembly? retAssembly = null;
GetExecutingAssemblyNative(new StackCrawlMarkHandle(ref stackMark), ObjectHandleOnStack.Create(ref retAssembly));
return retAssembly;
return retAssembly!;
}

// Get the assembly that the current code is running from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module)
new StackCrawlMarkHandle(ref stackMark),
(int)access,
ObjectHandleOnStack.Create(ref retAssembly));
_internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly;
_internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly!;

_assemblyData = new AssemblyBuilderData(_internalAssemblyBuilder, access);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ internal static RuntimeAssembly InternalLoad(AssemblyName assemblyName, ref Stac
throwOnFileNotFound,
ObjectHandleOnStack.Create(ref assemblyLoadContext),
ObjectHandleOnStack.Create(ref retAssembly));
return retAssembly;
return retAssembly!;
}

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.Runtime.CompilerServices
{
private void* _ptr;

internal StringHandleOnStack([NotNull] ref string? s)
internal StringHandleOnStack(ref string? s)
{
_ptr = Unsafe.AsPointer(ref s);
}
Expand All @@ -31,7 +31,7 @@ private ObjectHandleOnStack(void* pObject)
_ptr = pObject;
}

internal static ObjectHandleOnStack Create<T>([NotNull] ref T o) where T : class?
internal static ObjectHandleOnStack Create<T>(ref T o) where T : class?
{
return new ObjectHandleOnStack(Unsafe.AsPointer(ref o));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ private ConstantSplittableMap(KeyValuePair<TKey, TValue>[] items, int firstItemI

public TValue Lookup(TKey key)
{
bool found = TryGetValue(key, out TValue value);

if (!found)
if (!TryGetValue(key, out TValue value))
{
Debug.Assert(key != null);
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ private DictionaryToMapAdapter()
internal V Lookup<K, V>(K key) where K : notnull
{
IDictionary<K, V> _this = Unsafe.As<IDictionary<K, V>>(this);
bool keyFound = _this.TryGetValue(key, out V value);

if (!keyFound)
if (!_this.TryGetValue(key, out V value))
{
Debug.Assert(key != null);
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ private IReadOnlyDictionaryToIMapViewAdapter()
internal V Lookup<K, V>(K key) where K : notnull
{
IReadOnlyDictionary<K, V> _this = Unsafe.As<IReadOnlyDictionary<K, V>>(this);
bool keyFound = _this.TryGetValue(key, out V value);

if (!keyFound)
if (!_this.TryGetValue(key, out V value))
{
Debug.Assert(key != null);
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public static void Resize<T>([NotNull] ref T[]? array, int newSize)
Copy(larray, 0, newArray, 0, larray.Length > newSize ? newSize : larray.Length);
array = newArray;
}

Debug.Assert(array != null);
}

public static Array CreateInstance(Type elementType, params long[] lengths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool IProducerConsumerCollection<T>.TryAdd(T item)
/// <remarks>For <see cref="ConcurrentQueue{T}"/>, this operation will attempt to remove the object
/// from the beginning of the <see cref="ConcurrentQueue{T}"/>.
/// </remarks>
bool IProducerConsumerCollection<T>.TryTake(out T item) => TryDequeue(out item);
bool IProducerConsumerCollection<T>.TryTake([MaybeNullWhen(false)] out T item) => TryDequeue(out item);

/// <summary>
/// Gets a value that indicates whether the <see cref="ConcurrentQueue{T}"/> is empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct AsyncTaskMethodBuilder<TResult>
AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task);

internal static void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref Task<TResult>? taskField)
ref TAwaiter awaiter, ref TStateMachine stateMachine, ref Task<TResult>? taskField)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public ValueTask<TResult> Task
}

internal static void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref StateMachineBox? box)
ref TAwaiter awaiter, ref TStateMachine stateMachine, ref StateMachineBox? box)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static class LazyInitializer
/// </para>
/// </remarks>
public static T EnsureInitialized<T>([NotNull] ref T? target) where T : class =>
Volatile.Read(ref target) ?? EnsureInitializedCore(ref target);
Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target);

/// <summary>
/// Initializes a target reference type with the type's default constructor (slow path)
Expand Down Expand Up @@ -101,7 +101,7 @@ public static class LazyInitializer
/// </para>
/// </remarks>
public static T EnsureInitialized<T>([NotNull] ref T? target, Func<T> valueFactory) where T : class =>
Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, valueFactory);
Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target, valueFactory);

/// <summary>
/// Initialize the target using the given delegate (slow path).
Expand Down Expand Up @@ -133,9 +133,10 @@ public static class LazyInitializer
/// <param name="initialized">A reference to a boolean that determines whether the target has already
/// been initialized.</param>
/// <param name="syncLock">A reference to an object used as the mutually exclusive lock for initializing
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, a new object will be instantiated.</param>
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, and if the target hasn't already
/// been initialized, a new object will be instantiated.</param>
/// <returns>The initialized value of type <typeparamref name="T"/>.</returns>
public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock)
public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock)
{
// Fast path.
if (Volatile.Read(ref initialized))
Expand Down Expand Up @@ -190,11 +191,12 @@ private static T EnsureInitializedCore<T>([AllowNull] ref T target, ref bool ini
/// <param name="initialized">A reference to a boolean that determines whether the target has already
/// been initialized.</param>
/// <param name="syncLock">A reference to an object used as the mutually exclusive lock for initializing
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, a new object will be instantiated.</param>
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, and if the target hasn't already
/// been initialized, a new object will be instantiated.</param>
/// <param name="valueFactory">The <see cref="System.Func{T}"/> invoked to initialize the
/// reference or value.</param>
/// <returns>The initialized value of type <typeparamref name="T"/>.</returns>
public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock, Func<T> valueFactory)
public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock, Func<T> valueFactory)
{
// Fast path.
if (Volatile.Read(ref initialized))
Expand Down Expand Up @@ -239,11 +241,12 @@ private static T EnsureInitializedCore<T>([AllowNull] ref T target, ref bool ini
/// <typeparam name="T">The type of the reference to be initialized. Has to be reference type.</typeparam>
/// <param name="target">A reference of type <typeparamref name="T"/> to initialize if it has not already been initialized.</param>
/// <param name="syncLock">A reference to an object used as the mutually exclusive lock for initializing
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, a new object will be instantiated.</param>
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, and if the target hasn't already
/// been initialized, a new object will be instantiated.</param>
/// <param name="valueFactory">The <see cref="System.Func{T}"/> invoked to initialize the reference.</param>
/// <returns>The initialized value of type <typeparamref name="T"/>.</returns>
public static T EnsureInitialized<T>([NotNull] ref T? target, [NotNull] ref object? syncLock, Func<T> valueFactory) where T : class =>
Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, ref syncLock, valueFactory);
public static T EnsureInitialized<T>([NotNull] ref T? target, [NotNullIfNotNull("syncLock")] ref object? syncLock, Func<T> valueFactory) where T : class =>
Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target, ref syncLock, valueFactory);

/// <summary>
/// Ensure the target is initialized and return the value (slow path). This overload works only for reference type targets.
Expand Down Expand Up @@ -272,7 +275,8 @@ private static T EnsureInitializedCore<T>([AllowNull] ref T target, ref bool ini
}
}

return target!; // TODO-NULLABLE: Compiler can't infer target's non-nullness (https://github.com/dotnet/roslyn/issues/37300)
Debug.Assert(target != null);
return target;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Threading/ref/System.Threading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ public static partial class Interlocked
public static partial class LazyInitializer
{
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target) where T : class { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock) { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock, System.Func<T> valueFactory) { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock) { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock, System.Func<T> valueFactory) { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, System.Func<T> valueFactory) where T : class { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock, System.Func<T> valueFactory) where T : class { throw null; }
public static T EnsureInitialized<T>([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock, System.Func<T> valueFactory) where T : class { throw null; }
}
public partial struct LockCookie
{
Expand Down

0 comments on commit c9f9170

Please sign in to comment.