{"record":{"id":"1f7571e4ca251d60","repo":"stride3d/stride","slug":"exception-of-type-system-invalidoperationexception-was-1f7571","errorCode":null,"errorMessage":"Exception of type 'System.InvalidOperationException' was thrown.","messagePattern":"Exception of type 'System\\.InvalidOperationException' was thrown\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs","lineNumber":84,"sourceCode":"        {\n            // Apply swizzle\n            var resultType = new VectorType(v.BaseType, swizzleIndices.Length);\n            var shuffle = InsertData(new OpVectorShuffle(context.GetOrRegister(resultType), context.Bound++, value.Id, value.Id, new(swizzleIndices)));\n            value = new(shuffle);\n\n            return (value, resultType);\n        }\n        else if (swizzleIndices.Length == 1)\n        {\n            // Apply swizzle\n            var resultType = v.BaseType;\n            var extract = InsertData(new OpCompositeExtract(context.GetOrRegister(resultType), context.Bound++, value.Id, [swizzleIndices[0]]));\n            value = new(extract);\n\n            return (value, resultType);\n        }\n        else\n            throw new InvalidOperationException();\n    }\n\n    public (SpirvValue, SymbolType) ApplyMatrixSwizzles(SpirvContext context, SpirvValue value, MatrixType m, Span<(int Column, int Row)> swizzles)\n    {\n        Span<int> elements = stackalloc int[swizzles.Length];\n        for (var swizzleIndex = 0; swizzleIndex < swizzles.Length; swizzleIndex++)\n        {\n            var swizzle = swizzles[swizzleIndex];\n            elements[swizzleIndex] = Insert(new OpCompositeExtract(context.GetOrRegister(m.BaseType), context.Bound++, value.Id, [swizzle.Column, swizzle.Row])).ResultId;\n        }\n\n        var resultType = m.BaseType.GetVectorOrScalar(swizzles.Length);\n        value = swizzles.Length > 1\n            ? new(InsertData(new OpCompositeConstruct(context.GetOrRegister(resultType), context.Bound++, [.. elements])))\n            : new(elements[0], context.GetOrRegister(resultType));\n\n        return (value, resultType);\n    }","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L66-L102","documentation":"ApplyVectorSwizzles handles a single-index swizzle by extracting one component; the multi-index path builds a composite. The final else throws a bare InvalidOperationException, representing an unhandled/unexpected swizzle case (e.g. zero-length swizzle) that fell through both branches.","triggerScenarios":"ApplySwizzles calls ApplyVectorSwizzles with an empty (Length == 0) swizzle index span, so swizzleIndices.Length > 1 is false and the single-element path is also invalid, hitting the bare throw.","commonSituations":"Frontend/parser emitting a swizzle with no components (e.g. empty swizzle from an empty member-access token); corrupted AST or tokenization bug.","solutions":["Debug the upstream parser to find why a zero-length swizzle was produced","Add a guard before calling ApplySwizzles to reject empty swizzle index spans","Treat empty swizzle as identity (return the value unchanged) in the builder","Enable logging of the offending expression/type to locate the source construct"],"exampleFix":"// before\npublic (SpirvValue, SymbolType) ApplyVectorSwizzles(...) { ... }\n// after\nif (swizzleIndices.Length == 0) return (value, (SymbolType)v); // identity fast-path before switch","handlingStrategy":"validation","validationCode":"// Reject empty swizzles before calling the builder\nif (swizzleIndices.Length == 0)\n    throw new ShaderSemanticException(\"Empty swizzle expression\");","typeGuard":"bool IsNonEmptySwizzle(ReadOnlySpan<int> idx) => idx.Length > 0;","tryCatchPattern":"try { var r = ApplySwizzles(context, value, swizzle); }\ncatch (InvalidOperationException ex) when (ex.Message is \"\" or \"Exception of type 'System.InvalidOperationException' was thrown.\") { Log.Error(ex, \"Unhandled swizzle case (likely empty swizzle) for {Type}\", context.ReverseTypes[value.TypeId]); throw; }","preventionTips":["Reject empty member-access/swizzle tokens in the parser","Log the offending AST node when a bare InvalidOperationException occurs","Add parser tests for degenerate swizzle inputs"],"tags":["shader","swizzle","internal","spirv"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}