Issue with calling T[] Span<T>.ToArray()
See original GitHub issueThe following code produces an error that indicates there’s an issue with either this library or Mono.Cecil (or I’ve severly misunderstood the error):
IL.DeclareLocals(true, new LocalVar(typeof(Span<int>)));
IL.Emit.Sizeof<int>();
IL.Emit.Ldc_I4(10);
IL.Emit.Mul();
IL.Emit.Conv_U();
IL.Emit.Localloc();
IL.Emit.Ldc_I4(10);
IL.Emit.Newobj(MethodRef.Constructor(typeof(Span<int>), typeof(void).MakePointerType(), typeof(int)));
IL.Emit.Stloc_0();
IL.Emit.Ldloca(0);
IL.Emit.Call(MethodRef.Method(typeof(Span<int>), "ToArray", TypeRef.TypeGenericParameters[0].MakeArrayType(), 0, Array.Empty<TypeRef>()));
int[] arr;
IL.Pop(out arr);
(This should generate something very similar to)
Span<int> span = stackalloc int[10];
int[] arr = span.ToArray();
Generates the error:
Severity | Code | Description | Project | File | Line | Suppression State |
---|---|---|---|---|---|---|
Error | Fody/InlineIL: Unexpected error occured while processing method System.Void CSTest.Program::Main(System.String[]) at instruction IL_00d2: call System.Void InlineIL.IL/Emit::Call(InlineIL.MethodRef): System.NullReferenceException: Object reference not set to an instance of an object. at Mono.Cecil.ImportGenericContext.TypeParameter(String type, Int32 position) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 94 at Mono.Cecil.DefaultMetadataImporter.ImportTypeSpecification(TypeReference type, ImportGenericContext context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 645 at Mono.Cecil.DefaultMetadataImporter.ImportType(TypeReference type, ImportGenericContext context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 492 at Mono.Cecil.DefaultMetadataImporter.ImportReference(TypeReference type, IGenericParameterProvider context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 732 at InlineIL.Fody.Model.TypeRefBuilder.GenericParameterTypeRefResolver.TryResolve(ModuleDefinition module, IGenericParameterProvider context) at InlineIL.Fody.Model.TypeRefBuilder.TypeSpecTypeRefResolver.TryResolve(ModuleDefinition module, IGenericParameterProvider context) at InlineIL.Fody.Model.MethodRefBuilder.<>c__DisplayClass7_0.<FindMethod>b__3(MethodDefinition m) at System.Linq.Enumerable.<>c__DisplayClass6_0`1.<CombinePredicates>b__0(TSource x) at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Collections.Generic.List`1…ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at InlineIL.Fody.Model.MethodRefBuilder.FindMethod(TypeReference typeRef, String methodName, Nullable`1 genericArity, TypeRefBuilder returnType, IReadOnlyList`1 paramTypes) at InlineIL.Fody.Model.MethodRefBuilder.MethodByNameAndSignature(ModuleDefinition module, TypeReference typeRef, String methodName, Nullable`1 genericArity, TypeRefBuilder returnType, IReadOnlyList`1 paramTypes) at InlineIL.Fody.Processing.ArgumentConsumer.<ConsumeArgMethodRef>g__ConsumeArgMethodRefBuilder|15_0(Instruction instruction) at InlineIL.Fody.Processing.MethodWeaver.<ProcessIlEmitMethodCall>g__CreateInstructionToEmit|25_0(<>c__DisplayClass25_0& ) at InlineIL.Fody.Processing.MethodWeaver.ProcessIlEmitMethodCall(Instruction emitCallInstruction, Instruction& nextInstruction) at InlineIL.Fody.Processing.MethodWeaver.ProcessMethodCalls(CallInstructionHandler callHandler) | CSTest | A:\Code\Tests\TestInlineILFody\CSTest\Program.cs | 19 |
It’s this line that seems to cause the issue:
IL.Emit.Call(MethodRef.Method(typeof(Span<int>), "ToArray", TypeRef.TypeGenericParameters[0].MakeArrayType(), 0, Array.Empty<TypeRef>()));
Stack trace in a more readable form
Fody/InlineIL: Unexpected error occured while processing method System.Void CSTest.Program::Main(System.String[]) at instruction IL_00d2: call System.Void InlineIL.IL/Emit::Call(InlineIL.MethodRef): System.NullReferenceException: Object reference not set to an instance of an object. at Mono.Cecil.ImportGenericContext.TypeParameter(String type, Int32 position) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 94 at Mono.Cecil.DefaultMetadataImporter.ImportTypeSpecification(TypeReference type, ImportGenericContext context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 645 at Mono.Cecil.DefaultMetadataImporter.ImportType(TypeReference type, ImportGenericContext context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 492 at Mono.Cecil.DefaultMetadataImporter.ImportReference(TypeReference type, IGenericParameterProvider context) in C:\projects\fody\cecil\Mono.Cecil\Import.cs:line 732 at InlineIL.Fody.Model.TypeRefBuilder.GenericParameterTypeRefResolver.TryResolve(ModuleDefinition module, IGenericParameterProvider context) at InlineIL.Fody.Model.TypeRefBuilder.TypeSpecTypeRefResolver.TryResolve(ModuleDefinition module, IGenericParameterProvider context) at InlineIL.Fody.Model.MethodRefBuilder.<>c__DisplayClass7_0.<FindMethod>b__3(MethodDefinition m) at System.Linq.Enumerable.<>c__DisplayClass6_0`1.<CombinePredicates>b__0(TSource x) at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at InlineIL.Fody.Model.MethodRefBuilder.FindMethod(TypeReference typeRef, String methodName, Nullable`1 genericArity, TypeRefBuilder returnType, IReadOnlyList`1 paramTypes) at InlineIL.Fody.Model.MethodRefBuilder.MethodByNameAndSignature(ModuleDefinition module, TypeReference typeRef, String methodName, Nullable`1 genericArity, TypeRefBuilder returnType, IReadOnlyList`1 paramTypes) at InlineIL.Fody.Processing.ArgumentConsumer.<ConsumeArgMethodRef>g__ConsumeArgMethodRefBuilder|15_0(Instruction instruction) at InlineIL.Fody.Processing.MethodWeaver.<ProcessIlEmitMethodCall>g__CreateInstructionToEmit|25_0(<>c__DisplayClass25_0& ) at InlineIL.Fody.Processing.MethodWeaver.ProcessIlEmitMethodCall(Instruction emitCallInstruction, Instruction& nextInstruction) at InlineIL.Fody.Processing.MethodWeaver.ProcessMethodCalls(CallInstructionHandler callHandler)
Issue Analytics
- State:
- Created 9 months ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
c# - Which common operations can be made more efficient ...
ToArray (); are the problem here: we have to create a new array just to split the two parts of the byte array....
Read more >"string.ToCharArray()" and "ReadOnlySpan<T>.ToArray()" ...
ToCharArray()" and "ReadOnlySpan<T>.ToArray()" should not be called redundantly ... Calls to "async" methods should not be blocking. Code Smell ...
Read more >API for inserting a Span<T> into a List<T> efficiently #1530
Perhaps an AddRange overload taking a Span<T> can be added to the List ... while avoiding the allocation that would come from ToArray()...
Read more >Writing High-Performance Code Using Span<T> and ...
I'll take a deep dive into Span<T> and Memory<T> and demonstrate how to work with ... Resides in the unmanaged heap and is...
Read more >Span<T>.ToArray Method (System)
An array containing the data in the current span. Remarks. This method performs a heap allocation and therefore should be avoided if possible....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve thought about it more, and I probably don’t really need this currently, I was thinking in the case of having some funky method I wanted to call.
The fix is released in v1.7.4.