Skip to content

Commit

Permalink
Nullable annotations for System.Runtime.Serialization.Xml and System.…
Browse files Browse the repository at this point in the history
…Runtime.Serialization.Json (#41476)

* Initial nullable annotations of System.Private.DataContractSerialization

Contributes to #2339

* Mark DataMember.Name as non-nullable.

* Fix a few simple nullable compile errors.

* Assert attributes is non-null in XmlObjectSerializerReadContext

* Ensure XmlObjectSerializerContext.serializer is never null

* Fix a few simple nullable errors

* Remove any checks that DataMember.MemberInfo can be null.

* Mark EnumDataContract.Members as non-nullable.

Fix nullable errors in SchemaExporter.

* Correctly annotate CollectionDataContract.IsCollectionOrTryCreate.

* Assert DataContractResolver is non-null.

* Suppress #41465

* Update System.Runtime.Serialization.Json ref source for nullable annotations.

* Update System.Runtime.Serializaiton.Xml ref source for nullable annotations.

* Update for Xml.ReaderWriter nullable annotations

* Work around compiler issue.

* Fix test failure.

Reference compiler issue in TODO comment.

* Revert nullable suppression now that XmlSchemaAppInfo.Markup is annotated correctly.

* Fix build for latest annotations in master.

* PR feedback round 1

* Address PR feedback round 2
  • Loading branch information
eerhardt authored Aug 28, 2020
1 parent 48b299c commit 722d550
Show file tree
Hide file tree
Showing 115 changed files with 3,104 additions and 2,859 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/CodeDom/CodeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CodeObject
internal class CodeObject
#endif
{
private IDictionary _userData;
private IDictionary? _userData;

public CodeObject() { }

Expand Down
16 changes: 8 additions & 8 deletions src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class CodeTypeReference : CodeObject
internal class CodeTypeReference : CodeObject
#endif
{
private string _baseType;
private string? _baseType;
private readonly bool _isInterface;
private CodeTypeReferenceCollection _typeArguments;
private CodeTypeReferenceCollection? _typeArguments;
private bool _needsFixup;

public CodeTypeReference()
Expand All @@ -50,7 +50,7 @@ public CodeTypeReference(Type type)
if (type.IsArray)
{
ArrayRank = type.GetArrayRank();
ArrayElementType = new CodeTypeReference(type.GetElementType());
ArrayElementType = new CodeTypeReference(type.GetElementType()!);
_baseType = null;
}
else
Expand Down Expand Up @@ -86,7 +86,7 @@ private void InitializeFromType(Type type)
Type currentType = type;
while (currentType.IsNested)
{
currentType = currentType.DeclaringType;
currentType = currentType.DeclaringType!;
_baseType = currentType.Name + "+" + _baseType;
}

Expand Down Expand Up @@ -116,17 +116,17 @@ private void InitializeFromType(Type type)
}
}

private void Initialize(string typeName)
private void Initialize(string? typeName)
{
Initialize(typeName, Options);
}

private void Initialize(string typeName, CodeTypeReferenceOptions options)
private void Initialize(string? typeName, CodeTypeReferenceOptions options)
{
Options = options;
if (string.IsNullOrEmpty(typeName))
{
typeName = typeof(void).FullName;
typeName = typeof(void).FullName!;
_baseType = typeName;
ArrayRank = 0;
ArrayElementType = null;
Expand Down Expand Up @@ -303,7 +303,7 @@ public CodeTypeReference(CodeTypeReference arrayType, int rank)
ArrayElementType = arrayType;
}

public CodeTypeReference ArrayElementType { get; set; }
public CodeTypeReference? ArrayElementType { get; set; }

public int ArrayRank { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CodeTypeReferenceCollection(CodeTypeReference[] value)

public CodeTypeReference this[int index]
{
get { return ((CodeTypeReference)(List[index])); }
get { return ((CodeTypeReference)(List[index]!)); }
set { List[index] = value; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);1634;1691;649</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -7,6 +7,7 @@
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<!-- Too much private reflection. Do not bother with trimming -->
<ILLinkTrimAssembly>false</ILLinkTrimAssembly>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<RuntimeSerializationSources>System\Runtime\Serialization</RuntimeSerializationSources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace System.Runtime.Serialization
{
internal static class FastInvokerBuilder
{
public delegate void Setter(ref object obj, object value);
public delegate object Getter(object obj);
public delegate void Setter(ref object obj, object? value);
public delegate object? Getter(object obj);

private delegate void StructSetDelegate<T, TArg>(ref T obj, TArg value);
private delegate TResult StructGetDelegate<T, out TResult>(ref T obj);

private static readonly MethodInfo s_createGetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateGetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_createSetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateSetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_make = typeof(FastInvokerBuilder).GetMethod(nameof(Make), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_createGetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateGetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;
private static readonly MethodInfo s_createSetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateSetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;
private static readonly MethodInfo s_make = typeof(FastInvokerBuilder).GetMethod(nameof(Make), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;

public static Func<object> GetMakeNewInstanceFunc(Type type)
{
Expand All @@ -31,25 +31,23 @@ public static Func<object> GetMakeNewInstanceFunc(Type type)

public static Getter CreateGetter(MemberInfo memberInfo)
{
if (memberInfo is PropertyInfo)
if (memberInfo is PropertyInfo propInfo)
{
var propInfo = (PropertyInfo)memberInfo;
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(propInfo.DeclaringType, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
Getter accessor = createGetterGeneric(propInfo);
return accessor;
}
else if (memberInfo is FieldInfo)
else if (memberInfo is FieldInfo fieldInfo)
{
return (obj) =>
{
FieldInfo fieldInfo = (FieldInfo)memberInfo;
var value = fieldInfo.GetValue(obj);
return value;
};
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name)));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
}
}

Expand All @@ -60,7 +58,7 @@ public static Setter CreateSetter(MemberInfo memberInfo)
PropertyInfo propInfo = (PropertyInfo)memberInfo;
if (propInfo.CanWrite)
{
var buildSetAccessorGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
var buildSetAccessorGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
Setter accessor = buildSetAccessorGeneric(propInfo);
return accessor;
}
Expand All @@ -72,14 +70,14 @@ public static Setter CreateSetter(MemberInfo memberInfo)
else if (memberInfo is FieldInfo)
{
FieldInfo fieldInfo = (FieldInfo)memberInfo;
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
fieldInfo.SetValue(obj, val);
};
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name)));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
}
}

Expand Down Expand Up @@ -111,7 +109,7 @@ private static Getter CreateGetterInternal<DeclaringType, PropertyType>(Property

if (typeof(DeclaringType).IsValueType)
{
var getMethod = propInfo.GetMethod.CreateDelegate<StructGetDelegate<DeclaringType, PropertyType>>();
var getMethod = propInfo.GetMethod!.CreateDelegate<StructGetDelegate<DeclaringType, PropertyType>>();

return (obj) =>
{
Expand All @@ -121,7 +119,7 @@ private static Getter CreateGetterInternal<DeclaringType, PropertyType>(Property
}
else
{
var getMethod = propInfo.GetMethod.CreateDelegate<Func<DeclaringType, PropertyType>>();
var getMethod = propInfo.GetMethod!.CreateDelegate<Func<DeclaringType, PropertyType>>();

return (obj) =>
{
Expand All @@ -136,14 +134,14 @@ private static Setter CreateSetterInternal<DeclaringType, PropertyType>(Property
{
if (propInfo.Name == "Key")
{
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
((IKeyValue)obj).Key = val;
};
}
else
{
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
((IKeyValue)obj).Value = val;
};
Expand All @@ -152,22 +150,22 @@ private static Setter CreateSetterInternal<DeclaringType, PropertyType>(Property

if (typeof(DeclaringType).IsValueType)
{
var setMethod = propInfo.SetMethod.CreateDelegate<StructSetDelegate<DeclaringType, PropertyType>>();
var setMethod = propInfo.SetMethod!.CreateDelegate<StructSetDelegate<DeclaringType, PropertyType>>();

return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
var unboxed = (DeclaringType)obj;
setMethod(ref unboxed, (PropertyType)val);
obj = unboxed;
setMethod(ref unboxed, (PropertyType)val!);
obj = unboxed!;
};
}
else
{
var setMethod = propInfo.SetMethod.CreateDelegate<Action<DeclaringType, PropertyType>>();
var setMethod = propInfo.SetMethod!.CreateDelegate<Action<DeclaringType, PropertyType>>();

return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
setMethod((DeclaringType)obj, (PropertyType)val);
setMethod((DeclaringType)obj, (PropertyType)val!);
};
}
}
Expand All @@ -180,11 +178,11 @@ public static T CreateDelegate<T>(this MethodInfo method) where T : class
{
try
{
return method.CreateDelegate(typeof(T)) as T;
return (method.CreateDelegate(typeof(T)) as T)!;
}
catch (Exception e)
{
throw new InvalidOperationException(SR.Format(SR.FailedToCreateMethodDelegate, method.Name, method.DeclaringType.FullName), e);
throw new InvalidOperationException(SR.Format(SR.FailedToCreateMethodDelegate, method.Name, method.DeclaringType!.FullName), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ internal class Attributes
DictionaryGlobals.XsiTypeLocalName
};

internal string Id;
internal string Ref;
internal string XsiTypeName;
internal string XsiTypeNamespace;
internal string XsiTypePrefix;
internal string Id = null!; // initialized in Reset
internal string Ref = null!; // initialized in Reset
internal string? XsiTypeName;
internal string? XsiTypeNamespace;
internal string? XsiTypePrefix;
internal bool XsiNil;
internal string ClrAssembly;
internal string ClrType;
internal string? ClrAssembly;
internal string? ClrType;
internal int ArraySZSize;
internal string FactoryTypeName;
internal string FactoryTypeNamespace;
internal string FactoryTypePrefix;
internal string? FactoryTypeName;
internal string? FactoryTypeNamespace;
internal string? FactoryTypePrefix;
internal bool UnrecognizedAttributesFound;

internal void Read(XmlReaderDelegator reader)
Expand Down
Loading

0 comments on commit 722d550

Please sign in to comment.