{"record":{"id":"2dd16ef8d00039f3","repo":"stride3d/stride","slug":"instruction-has-no-result-id","errorCode":null,"errorMessage":"Instruction has no result id","messagePattern":"Instruction has no result id","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Spirv/MemoryInstruction.cs","lineNumber":18,"sourceCode":"﻿using System.Runtime.CompilerServices;\nusing Stride.Shaders.Spirv.Core.Buffers;\nusing Stride.Shaders.Spirv.Core.Parsing;\nusing static Stride.Shaders.Spirv.Specification;\n\n\nnamespace Stride.Shaders.Spirv.Core;\n\n/// <summary>\n/// Representation of an instruction from a memory slice.\n/// </summary>\n/// <param name=\"Buffer\"></param>\n/// <param name=\"Words\"></param>\npublic record struct Instruction(Memory<int> Memory)\n{\n    public static Instruction Empty { get; } = new(Memory<int>.Empty);\n\n    public static implicit operator IdRef(Instruction i) => new(i.ResultId ?? throw new Exception(\"Instruction has no result id\"));\n    public static implicit operator IdResultType(Instruction i) => new(i.ResultId ?? throw new Exception(\"Instruction has no result id\"));\n\n\n    public readonly Op OpCode => (Op)(Words[0] & 0xFFFF);\n    public int? ResultId { get => GetResultId(); set => SetResultId(value); }\n    public int? ResultType { get => GetResultType(); set => SetResultType(value); }\n    public readonly int WordCount => Words.Length;\n    public readonly Span<int> Operands => Memory[1..].Span;\n\n    public readonly Span<int> Words => Memory.Span;\n\n    public bool IsEmpty => Words.IsEmpty;\n\n    public TWrapper UnsafeAs<TWrapper>()\n        where TWrapper : struct, IWrapperInstruction, allows ref struct\n    {\n        return new TWrapper()\n        {","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Spirv/MemoryInstruction.cs#L1-L36","documentation":"The implicit conversion Instruction -> IdRef requires the instruction to have a result id (ResultId). SPIR-V instructions like OpReturn, OpStore, or OpBranch produce no result, so ResultId is null and the conversion throws. This guards against treating a non-result instruction as a referenceable id.","triggerScenarios":"Implicitly casting an Instruction that produces no result to IdRef, e.g. IdRef r = module.Instructions.First(i => i.OpCode == Op.OpReturn); or passing such an instruction to a method accepting IdRef.","commonSituations":"Linq lookups over instruction streams returning the first matching opcode without filtering for result-producing ops (OpTypeInt, OpConstant, etc.); iterating instructions after SPIR-V parsing where result-less ops appear (most Op* control-flow/store instructions).","solutions":["Check i.ResultId.HasValue before converting; only convert result-producing instructions.","Filter the instruction stream, e.g. Where(i => i.ResultId != null) before selecting.","Verify the target instruction's opcode is one that defines a result (per the SPIR-V spec grammar).","If the instruction genuinely should have a result id, inspect parsing — the word offset for the result id may be miscomputed."],"exampleFix":"// before\nIdRef id = instructions.First(i => i.OpCode == Op.OpTypeFloat);\n// after\nIdRef id = instructions.First(i => i.OpCode == Op.OpTypeFloat && i.ResultId != null);","handlingStrategy":"type-guard","validationCode":"if (instruction.ResultId is null)\n    throw new InvalidOperationException(\"Instruction produces no result id; cannot convert to IdRef\");","typeGuard":"static bool HasResultId(this Instruction i) => i.ResultId.HasValue;","tryCatchPattern":"try { IdRef id = instruction; }\ncatch (Exception ex) when (ex.Message == \"Instruction has no result id\") { /* select a different instruction or skip */ }","preventionTips":["Filter instruction streams with Where(i => i.ResultId != null) before converting to IdRef.","Know which opcodes define results (types, constants, variables) vs. which don't (branches, stores, returns).","Prefer explicit i.ResultId.Value access over implicit conversion so failures are local and clear."],"tags":["spirv","instruction","null-result-id","implicit-conversion"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}