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)]
{
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.
new StackCrawlMarkHandle(ref stackMark),
(int)access,
ObjectHandleOnStack.Create(ref retAssembly));
- _internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly;
+ _internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly!;
_assemblyData = new AssemblyBuilderData(_internalAssemblyBuilder, access);
throwOnFileNotFound,
ObjectHandleOnStack.Create(ref assemblyLoadContext),
ObjectHandleOnStack.Create(ref retAssembly));
- return retAssembly;
+ return retAssembly!;
}
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
{
private void* _ptr;
- internal StringHandleOnStack([NotNull] ref string? s)
+ internal StringHandleOnStack(ref string? s)
{
_ptr = Unsafe.AsPointer(ref s);
}
_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));
}
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()));
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()));
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()));
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)
/// <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.
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
{
}
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
{
/// </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)
/// </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).
/// <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))
/// <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))
/// <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.
}
}
- 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>
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
{