Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Fix some nullable annotations from API Review #24937

Merged
merged 5 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/System.Private.CoreLib/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- Remove CS8609 once https://github.com/dotnet/roslyn/issues/23268 is resolved -->
<NoWarn>$(NoWarn);649,1573,1591,0419,3021,CS8609</NoWarn>
<NoWarn>$(NoWarn);649,1573,1591,0419,3021</NoWarn>
<Nullable>enable</Nullable>

<!-- Ignore all previous constants since SPCL is sensitive to what is defined and the Sdk adds some by default -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static partial class Activator
if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
{
// WinRT type - we have to use Type.GetType
type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
type = Type.GetType(typeName + ", " + assemblyString, throwOnError: true, ignoreCase);
}
else
{
Expand All @@ -131,7 +131,7 @@ public static partial class Activator
type = assembly!.GetType(typeName, throwOnError: true, ignoreCase);
}

object? o = CreateInstance(type, bindingAttr, binder, args, culture, activationAttributes);
object? o = CreateInstance(type!, bindingAttr, binder, args, culture, activationAttributes);

return o != null ? new ObjectHandle(o) : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.Private.CoreLib/shared/System/Activator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static partial class Activator
public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes)
{
Assembly assembly = Assembly.LoadFrom(assemblyFile);
Type t = assembly.GetType(typeName, true, ignoreCase);
Type t = assembly.GetType(typeName, throwOnError: true, ignoreCase)!;

object? o = CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);

Expand Down
4 changes: 2 additions & 2 deletions src/System.Private.CoreLib/shared/System/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public static partial class AppContext
private static Dictionary<string, bool>? s_switches;
private static string? s_defaultBaseDirectory;

public static string? BaseDirectory
public static string BaseDirectory
{
get
{
// The value of APP_CONTEXT_BASE_DIRECTORY key has to be a string and it is not allowed to be any other type.
// Otherwise the caller will get invalid cast exception
return (string?)GetData("APP_CONTEXT_BASE_DIRECTORY") ??
(s_defaultBaseDirectory ?? (s_defaultBaseDirectory = GetBaseDirectoryCore()));
(s_defaultBaseDirectory ?? (s_defaultBaseDirectory = GetBaseDirectoryCore())) ?? string.Empty;
safern marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DefaultValueAttribute : Attribute
/// class, converting the specified value to the specified type, and using the U.S. English
/// culture as the translation context.
/// </summary>
public DefaultValueAttribute(Type? type, string? value)
public DefaultValueAttribute(Type type, string? value)
{
// The null check and try/catch here are because attributes should never throw exceptions.
// We would fail to load an otherwise normal class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3521,19 +3521,19 @@ private static void AddProviderEnumKind(ManifestBuilder manifest, FieldInfo stat
if (!reflectionOnly && (staticFieldType == typeof(EventOpcode)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventOpcode)))
{
if (providerEnumKind != "Opcodes") goto Error;
int value = (int)staticField.GetRawConstantValue();
int value = (int)staticField.GetRawConstantValue()!;
manifest.AddOpcode(staticField.Name, value);
}
else if (!reflectionOnly && (staticFieldType == typeof(EventTask)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventTask)))
{
if (providerEnumKind != "Tasks") goto Error;
int value = (int)staticField.GetRawConstantValue();
int value = (int)staticField.GetRawConstantValue()!;
manifest.AddTask(staticField.Name, value);
}
else if (!reflectionOnly && (staticFieldType == typeof(EventKeywords)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventKeywords)))
{
if (providerEnumKind != "Keywords") goto Error;
ulong value = unchecked((ulong)(long)staticField.GetRawConstantValue());
ulong value = unchecked((ulong)(long)staticField.GetRawConstantValue()!);
manifest.AddKeyword(staticField.Name, value);
}
#if FEATURE_MANAGED_ETW_CHANNELS && FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
Expand Down Expand Up @@ -3730,7 +3730,7 @@ private static int GetHelperCallFirstArg(MethodInfo method)
#if ES_BUILD_STANDALONE
(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)).Assert();
#endif
byte[] instrs = method.GetMethodBody().GetILAsByteArray()!;
byte[] instrs = method.GetMethodBody()!.GetILAsByteArray()!;
int retVal = -1;
for (int idx = 0; idx < instrs.Length;)
{
Expand Down Expand Up @@ -5828,7 +5828,7 @@ private string CreateManifestString()
bool anyValuesWritten = false;
foreach (FieldInfo staticField in staticFields)
{
object constantValObj = staticField.GetRawConstantValue();
object? constantValObj = staticField.GetRawConstantValue();

if (constantValObj != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,15 +655,15 @@ internal static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char>
return false;
}

internal static TimeSpan ParseExactMultiple(ReadOnlySpan<char> input, string?[] formats, IFormatProvider? formatProvider, TimeSpanStyles styles)
internal static TimeSpan ParseExactMultiple(ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, TimeSpanStyles styles)
{
var parseResult = new TimeSpanResult(throwOnFailure: true, originalTimeSpanString: input);
bool success = TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult);
Debug.Assert(success, "Should have thrown on failure");
return parseResult.parsedTimeSpan;
}

internal static bool TryParseExactMultiple(ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, TimeSpanStyles styles, out TimeSpan result)
internal static bool TryParseExactMultiple(ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, TimeSpanStyles styles, out TimeSpan result)
{
var parseResult = new TimeSpanResult(throwOnFailure: false, originalTimeSpanString: input);

Expand Down Expand Up @@ -1669,7 +1669,7 @@ internal void SkipBlanks()
}

/// <summary>Common private ParseExactMultiple method called by both ParseExactMultiple and TryParseExactMultiple.</summary>
private static bool TryParseExactMultipleTimeSpan(ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
private static bool TryParseExactMultipleTimeSpan(ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
{
if (formats == null)
{
Expand All @@ -1690,8 +1690,7 @@ private static bool TryParseExactMultipleTimeSpan(ReadOnlySpan<char> input, stri
// one of the formats.
for (int i = 0; i < formats.Length; i++)
{
// TODO-NULLABLE: Indexer nullability tracked (https://github.com/dotnet/roslyn/issues/34644)
if (formats[i] == null || formats[i]!.Length == 0)
if (formats[i] == null || formats[i].Length == 0)
{
return result.SetBadFormatSpecifierFailure();
}
Expand Down
24 changes: 12 additions & 12 deletions src/System.Private.CoreLib/shared/System/Reflection/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public virtual Type[] GetTypes()
public virtual AssemblyName GetName() => GetName(copiedName: false);
public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; }

public virtual Type GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false);
public virtual Type GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false);
public virtual Type GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }
public virtual Type? GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false);
public virtual Type? GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false);
public virtual Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }

public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }

Expand All @@ -110,7 +110,7 @@ public virtual Type[] GetTypes()
public object? CreateInstance(string typeName, bool ignoreCase) => CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null);
public virtual object? CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object[]? args, CultureInfo? culture, object[]? activationAttributes)
{
Type t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase);
Type? t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase);
if (t == null)
return null;

Expand All @@ -119,8 +119,8 @@ public virtual Type[] GetTypes()

public virtual event ModuleResolveEventHandler ModuleResolve { add { throw NotImplemented.ByDesign; } remove { throw NotImplemented.ByDesign; } }

public virtual Module? ManifestModule { get { throw NotImplemented.ByDesign; } }
public virtual Module GetModule(string name) { throw NotImplemented.ByDesign; }
public virtual Module ManifestModule { get { throw NotImplemented.ByDesign; } }
public virtual Module? GetModule(string name) { throw NotImplemented.ByDesign; }

public Module[] GetModules() => GetModules(getResourceModules: false);
public virtual Module[] GetModules(bool getResourceModules) { throw NotImplemented.ByDesign; }
Expand Down Expand Up @@ -324,19 +324,19 @@ public static Assembly LoadFrom(string assemblyFile)
return AssemblyLoadContext.Default.LoadFromAssemblyPath(fullPath);
}

public static Assembly LoadFrom(string? assemblyFile, byte[]? hashValue, AssemblyHashAlgorithm hashAlgorithm)
public static Assembly LoadFrom(string assemblyFile, byte[]? hashValue, AssemblyHashAlgorithm hashAlgorithm)
{
throw new NotSupportedException(SR.NotSupported_AssemblyLoadFromHash);
}

public static Assembly UnsafeLoadFrom(string assemblyFile) => LoadFrom(assemblyFile);

public Module LoadModule(string? moduleName, byte[]? rawModule) => LoadModule(moduleName, rawModule, null);
public virtual Module LoadModule(string? moduleName, byte[]? rawModule, byte[]? rawSymbolStore) { throw NotImplemented.ByDesign; }
public Module LoadModule(string moduleName, byte[]? rawModule) => LoadModule(moduleName, rawModule, null);
public virtual Module LoadModule(string moduleName, byte[]? rawModule, byte[]? rawSymbolStore) { throw NotImplemented.ByDesign; }

public static Assembly ReflectionOnlyLoad(byte[]? rawAssembly) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
public static Assembly ReflectionOnlyLoad(string? assemblyString) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
public static Assembly ReflectionOnlyLoadFrom(string? assemblyFile) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
public static Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
public static Assembly ReflectionOnlyLoad(string assemblyString) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
public static Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }

public virtual SecurityRuleSet SecurityRuleSet => SecurityRuleSet.None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void SetPublicKey(byte[]? publicKey)

// The compressed version of the public key formed from a truncated hash.
// Will throw a SecurityException if _publicKey is invalid
public byte[] GetPublicKeyToken()
public byte[]? GetPublicKeyToken()
{
if (_publicKeyToken == null)
_publicKeyToken = ComputePublicKeyToken();
safern marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -226,7 +226,7 @@ public string FullName
if (this.Name == null)
return string.Empty;
// Do not call GetPublicKeyToken() here - that latches the result into AssemblyName which isn't a side effect we want.
byte[] pkt = _publicKeyToken ?? ComputePublicKeyToken();
byte[]? pkt = _publicKeyToken ?? ComputePublicKeyToken();
return AssemblyNameFormatter.ComputeDisplayName(Name, Version, CultureName, pkt, Flags, ContentType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedA
public override string ToString()
{
if (m_memberInfo == null)
return base.ToString();
return base.ToString()!;

return string.Format("{0} = {1}", MemberInfo.Name, TypedValue.ToString(ArgumentType != typeof(object)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CustomAttributeTypedArgument(object value)
internal string ToString(bool typed)
{
if (m_argumentType == null)
return base.ToString();
return base.ToString()!;

if (ArgumentType.IsEnum)
return string.Format(typed ? "{0}" : "({1}){0}", Value, ArgumentType.FullName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ protected FieldInfo() { }

[DebuggerHidden]
[DebuggerStepThrough]
public void SetValue(object? obj, object value) => SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null);
public void SetValue(object? obj, object? value) => SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null);
public abstract void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture);

[CLSCompliant(false)]
public virtual void SetValueDirect(TypedReference obj, object value) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
[CLSCompliant(false)]
public virtual object GetValueDirect(TypedReference obj) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
public virtual object? GetValueDirect(TypedReference obj) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }

public virtual object GetRawConstantValue() { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
public virtual object? GetRawConstantValue() { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }

public virtual Type[] GetOptionalCustomModifiers() { throw NotImplemented.ByDesign; }
public virtual Type[] GetRequiredCustomModifiers() { throw NotImplemented.ByDesign; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public interface IReflect
// For the default binder, the most specific method will be selected.
//
// This will invoke a specific member...
object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters);
object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters);

// Return the underlying Type that represents the IReflect Object. For expando object,
// this is the (Object) IReflectInstance.GetType(). For Type object it is this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

namespace System.Reflection
{
public delegate bool MemberFilter(MemberInfo m, object filterCriteria);
public delegate bool MemberFilter(MemberInfo m, object? filterCriteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected MethodBase() { }
public abstract MethodAttributes Attributes { get; }
public virtual MethodImplAttributes MethodImplementationFlags => GetMethodImplementationFlags();
public abstract MethodImplAttributes GetMethodImplementationFlags();
public virtual MethodBody GetMethodBody() { throw new InvalidOperationException(); }
public virtual MethodBody? GetMethodBody() { throw new InvalidOperationException(); }
public virtual CallingConventions CallingConvention => CallingConventions.Standard;

public bool IsAbstract => (Attributes & MethodAttributes.Abstract) != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ protected MethodInfo() { }
public override MemberTypes MemberType => MemberTypes.Method;

public virtual ParameterInfo? ReturnParameter { get { throw NotImplemented.ByDesign; } }
safern marked this conversation as resolved.
Show resolved Hide resolved
public virtual Type? ReturnType { get { throw NotImplemented.ByDesign; } }
public virtual Type ReturnType { get { throw NotImplemented.ByDesign; } }

public override Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual MethodInfo? GetGenericMethodDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual MethodInfo? MakeGenericMethod(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual MethodInfo GetGenericMethodDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }

public abstract MethodInfo? GetBaseDefinition();
public abstract MethodInfo GetBaseDefinition();

public abstract ICustomAttributeProvider? ReturnTypeCustomAttributes { get; }
public abstract ICustomAttributeProvider ReturnTypeCustomAttributes { get; }

public virtual Delegate CreateDelegate(Type delegateType) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual Delegate CreateDelegate(Type delegateType, object? target) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
Expand Down
Loading