question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with calling T[] Span<T>.ToArray()

See original GitHub issue

The 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:closed
  • Created 9 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hamarb123commented, Dec 20, 2022

May I ask why you’d like to have such a feature?

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.

0reactions
ltrzesniewskicommented, Dec 23, 2022

The fix is released in v1.7.4.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found