{"record":{"id":"c8ea9ba590c99bb6","repo":"dotnet/BenchmarkDotNet","slug":"cannot-emit-default-for-resulttype","errorCode":null,"errorMessage":"Cannot emit default for {resultType}.","messagePattern":"Cannot emit default for (.+?)\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs","lineNumber":156,"sourceCode":"                ilBuilder.Emit(OpCodes.Ldc_I4_0);\n                ilBuilder.Emit(OpCodes.Conv_U);\n                break;\n            case Type t when t == typeof(double):\n                ilBuilder.Emit(OpCodes.Ldc_R8, 0.0d);\n                break;\n            case Type t when t == typeof(float):\n                ilBuilder.Emit(OpCodes.Ldc_R4, 0.0f);\n                break;\n            case Type t when t == typeof(decimal):\n                /*\n                    // return decimal.Zero;\n                    IL_0011: ldsfld valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Zero\n                 */\n                var zeroField = typeof(decimal).GetField(nameof(decimal.Zero))!;\n                ilBuilder.Emit(OpCodes.Ldsfld, zeroField);\n                break;\n            default:\n                throw new NotSupportedException($\"Cannot emit default for {resultType}.\");\n        }\n    }\n}","sourceCodeStart":138,"sourceCodeEnd":159,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs#L138-L159","documentation":"IlGeneratorDefaultValueExtensions emits IL to load a default value for a benchmark method's return type; the default arm throws NotSupportedException (\"Cannot emit default for {resultType}.\") for types it cannot handle (it covers primitives, decimal, etc.). This determines the placeholder returned when a benchmark does not complete a measurement iteration.","triggerScenarios":"The benchmark method returns a type for which the emitter has no default-value case, e.g. certain pointers, typed references, unsupported generic structs, or exotic types outside the primitive/decimal set.","commonSituations":"Benchmarking a method whose return type is unusual (ref structs, pointers, interop types) that the default-value emitter cannot synthesize.","solutions":["Change the benchmark's return type to a supported primitive or reference type (box/convert the real return value).","Wrap the unusual result into a supported type before returning it from the benchmark method.","Upgrade BenchmarkDotNet and report the unsupported return type if still needed."],"exampleFix":"// before\n[Benchmark] public IntPtr Run() => ...;\n\n// after\n[Benchmark] public long Run() => (...).ToInt64();","handlingStrategy":"validation","validationCode":"// ensure the benchmark returns a supported type before running\n[Benchmark] public long Run() => Compute(); // not IntPtr/ref struct","typeGuard":null,"tryCatchPattern":"try { runner.Run(); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"Cannot emit default\")) { /* change return type */ }","preventionTips":["Return a primitive or boxed/reference type from benchmarks.","Avoid pointers, ref structs, and exotic return types.","Upgrade BenchmarkDotNet for expanded default-value support."],"tags":["il-emit","reflection","return-type","codegen"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}