{"record":{"id":"855573c4ebe92354","repo":"dotnet/BenchmarkDotNet","slug":"the-optionallocalthis-should-be-null-for-static-ca","errorCode":null,"errorMessage":"The optionalLocalThis should be null for static calls","messagePattern":"The optionalLocalThis should be null for static calls","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs","lineNumber":81,"sourceCode":"            MethodInfo methodToCall,\n            IEnumerable<LocalBuilder> argLocals,\n            bool forceDirectCall = false)\n        {\n            if (methodToCall.DeclaringType == null)\n                throw new ArgumentException($\"The {nameof(methodToCall)} should have non-null {nameof(methodToCall.DeclaringType)}.\");\n\n            /*\n                IL_0000: call class [mscorlib]System.Threading.Tasks.Task [mscorlib]System.Threading.Tasks.Task::get_CompletedTask()\n                -or-\n                IL_0007: callvirt instance valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [mscorlib]System.Threading.Tasks.Task`1<int32>::GetAwaiter()\n                --or-\n                IL_000d: ldloca.s 0\n                IL_000f: call instance !0 valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::GetResult()\n            */\n            if (methodToCall.IsStatic)\n            {\n                if (optionalLocalThis != null)\n                    throw new ArgumentException($\"The {nameof(optionalLocalThis)} should be null for static calls\", nameof(optionalLocalThis));\n\n                ilBuilder.EmitLdLocals(argLocals);\n                ilBuilder.Emit(OpCodes.Call, methodToCall);\n            }\n            else if (methodToCall.DeclaringType.IsValueType)\n            {\n                ArgumentNullException.ThrowIfNull(optionalLocalThis);\n\n                ilBuilder.EmitLdloca(optionalLocalThis);\n                ilBuilder.EmitLdLocals(argLocals);\n                ilBuilder.Emit(OpCodes.Call, methodToCall);\n            }\n            else\n            {\n                if (optionalLocalThis != null)\n                    ilBuilder.EmitLdloc(optionalLocalThis);\n\n                ilBuilder.EmitLdLocals(argLocals);","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs#L63-L99","documentation":"EmitCallCore (internal) throws ArgumentException when emitting a static call (methodToCall.IsStatic) but optionalLocalThis is non-null. A static call has no 'this', so providing a local for 'this' is contradictory and would produce invalid IL.","triggerScenarios":"The emitter routes to the static-call branch while a LocalBuilder for 'this' was also supplied, i.e. an internal caller passed conflicting arguments.","commonSituations":"Library codegen bug misclassifying a call as static while still supplying a this-local; not user-facing configuration.","solutions":["Retry with a simpler benchmark method (plain instance method) to avoid the misrouted emit path.","Upgrade BenchmarkDotNet; report a reproducible issue if it continues."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { runner.Run(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"should be null for static calls\")) { /* simplify benchmark, report issue */ }","preventionTips":["Use conventional benchmark method shapes to avoid emit misrouting.","Keep BenchmarkDotNet current.","File reproducible codegen issues."],"tags":["il-emit","reflection","internal","codegen"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}