peteroupc / CBOR

A C# implementation of Concise Binary Object Representation (RFC 8949).
The Unlicense
206 stars 29 forks source link

Typed CBORObject.FromInt etc. #57

Closed charlesroddie closed 1 year ago

charlesroddie commented 3 years ago

Currently to convert a primitive to CBOR, CBORObject.FromObject is used which has a lot of overloads. This gives a lack of safety, as overload resolution is affected by op_Implicit, and also has an obj overload which makes it extra unsafe.

Instead there should be added FromInt, FromInt64, FromChar, FromArray, FromGuid etc..

charlesroddie commented 1 year ago

This architecture is causing NativeAOT incompatibilities now:

ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter '#1' of method 'PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Activator.CreateInstance(Type,Object[])'. The parameter '#1' of method 'PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Array.CreateInstance(Type,Int32[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter '#1' of method 'PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
ILC : AOT analysis warning IL3050: PeterO.Cbor.PropertyMap.TypeToObject(CBORObject,Type,CBORTypeMapper,PODOptions,Int32): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
ILC : Trim analysis warning IL2070: PeterO.Cbor.PropertyMap.GetTypeInterfaces(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Reflection.TypeInfo.ImplementedInterfaces.get'. The parameter '#0' of method 'PeterO.Cbor.PropertyMap.GetTypeInterfaces(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.ObjectWithProperties(Type,IEnumerable`1<KeyValuePair`2<String,CBORObject>>,CBORTypeMapper,PODOptions,Int32): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter '#0' of method 'PeterO.Cbor.PropertyMap.ObjectWithProperties(Type,IEnumerable`1<KeyValuePair`2<String,CBORObject>>,CBORTypeMapper,PODOptions,Int32)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.GetTypeMethod(Type,String,Type[]): 'name' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethod(Type,String,Type[])'. The parameter '#0' of method 'PeterO.Cbor.PropertyMap.GetTypeMethod(Type,String,Type[])' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.GetTypeProperties(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeProperties(Type)'. The parameter '#0' of method 'PeterO.Cbor.PropertyMap.GetTypeProperties(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
ILC : Trim analysis warning IL2067: PeterO.Cbor.PropertyMap.GetTypeFields(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeFields(Type)'. The parameter '#0' of method 'PeterO.Cbor.PropertyMap.GetTypeFields(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
peteroupc commented 1 year ago

I don't see how to address the warnings you mention and I don't see what to do to show those warnings, even assuming I add methods like CBORObject.FromInt32, etc.

charlesroddie commented 1 year ago

I've made a draft PR with the scaffolding to reproduce this: https://github.com/peteroupc/CBOR/pull/69

charlesroddie commented 1 year ago

I did some initial work on this.

Effectively there needs to be a division between a type-safe part of the library and a reflection-based part. The reflection-based parts should be marked as such, and the type-safe parts shouldn't call them. The best such line is between things that take one normal type to another, which are safe, and things that start with obj and do type testing, or things that take a Type argument, which are not.