Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable annotations for System.Runtime.Serialization.Xml and System.Runtime.Serialization.Json #41476

Merged
merged 21 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
700689b
Initial nullable annotations of System.Private.DataContractSerialization
eerhardt Aug 21, 2020
765f1a5
Mark DataMember.Name as non-nullable.
eerhardt Aug 26, 2020
ae0cabb
Fix a few simple nullable compile errors.
eerhardt Aug 26, 2020
e0853c7
Assert attributes is non-null in XmlObjectSerializerReadContext
eerhardt Aug 26, 2020
3e471d5
Ensure XmlObjectSerializerContext.serializer is never null
eerhardt Aug 27, 2020
0c8edd9
Fix a few simple nullable errors
eerhardt Aug 27, 2020
7b44f23
Remove any checks that DataMember.MemberInfo can be null.
eerhardt Aug 27, 2020
22a4414
Mark EnumDataContract.Members as non-nullable.
eerhardt Aug 27, 2020
046b95f
Correctly annotate CollectionDataContract.IsCollectionOrTryCreate.
eerhardt Aug 27, 2020
70f9410
Assert DataContractResolver is non-null.
eerhardt Aug 27, 2020
97f152e
Suppress https://github.com/dotnet/runtime/issues/41465
eerhardt Aug 27, 2020
8e0086a
Update System.Runtime.Serialization.Json ref source for nullable anno…
eerhardt Aug 27, 2020
0d8c8f1
Update System.Runtime.Serializaiton.Xml ref source for nullable annot…
eerhardt Aug 27, 2020
9f83da6
Update for Xml.ReaderWriter nullable annotations
eerhardt Aug 27, 2020
f05d9ec
Work around compiler issue.
eerhardt Aug 27, 2020
ca6c1d2
Fix test failure.
eerhardt Aug 28, 2020
14e1302
Merge remote-tracking branch 'upstream/master' into DataContractSeria…
eerhardt Aug 28, 2020
b553d8f
Revert nullable suppression now that XmlSchemaAppInfo.Markup is annot…
eerhardt Aug 28, 2020
07e2816
Fix build for latest annotations in master.
eerhardt Aug 28, 2020
f981721
PR feedback round 1
eerhardt Aug 28, 2020
89e09eb
Address PR feedback round 2
eerhardt Aug 28, 2020
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
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;
eerhardt marked this conversation as resolved.
Show resolved Hide resolved

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
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
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; }

eerhardt marked this conversation as resolved.
Show resolved Hide resolved
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]!)); }
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
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