{"record":{"id":"6c9351ea2e87b6c3","repo":"stride3d/stride","slug":"swizzle-accessors-i-1-is-out-of-bound-for-expression","errorCode":null,"errorMessage":"Swizzle {Accessors[i + 1]} is out of bound for expression {ToString(i)} of type {currentValueType}","messagePattern":"Swizzle (.+?) is out of bound for expression (.+?) of type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Expression.cs","lineNumber":926,"sourceCode":"                    VectorType v => (v.Size, v.BaseType),\n                    _ => throw new NotSupportedException($\"Unsupported type {vectorOrScalarType} for swizzle coalescing\"),\n                };\n\n                var swizzleIndices = new int[swizzle1.Length];\n                for (int j = 0; j < swizzle1.Length; ++j)\n                {\n                    swizzleIndices[j] = ConvertSwizzle(swizzle1[j]);\n                    if (swizzleIndices[j] >= size)\n                        throw new InvalidOperationException($\"Swizzle {Accessors[i]} is out of bound for expression {ToString(i)} of type {vectorOrScalarType}\");\n                }\n\n                // Combine swizzles with previous ones\n                var newSwizzleIndices = new int[swizzle2.Length];\n                for (int j = 0; j < swizzle2.Length; ++j)\n                {\n                    newSwizzleIndices[j] = swizzleIndices[ConvertSwizzle(swizzle2[j])];\n                    if (newSwizzleIndices[j] >= size)\n                        throw new InvalidOperationException($\"Swizzle {Accessors[i + 1]} is out of bound for expression {ToString(i)} of type {currentValueType}\");\n                }\n\n                Accessors.RemoveAt(i + 1);\n                Span<char> vectorFields = ['x', 'y', 'z', 'w'];\n                Span<char> newSwizzle = stackalloc char[swizzle2.Length];\n                for (int j = 0; j < swizzle2.Length; ++j)\n                {\n                    newSwizzle[j] = vectorFields[newSwizzleIndices[j]];\n                }\n\n                Accessors[i] = accessor = new Identifier(new(newSwizzle), default);\n            }\n        }\n\n        // Some accessors push up to 2 values on the stack\n        Span<int> accessChainIds = stackalloc int[Accessors.Count * 2];\n        Span<int> swizzleBuffer = stackalloc int[4]; // max swizzle length\n","sourceCodeStart":908,"sourceCodeEnd":944,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Expression.cs#L908-L944","documentation":"In the second phase of swizzle coalescing, indices produced by combining swizzle1 and swizzle2 are validated against the vector size; if a combined index exceeds the component count, an InvalidOperationException is thrown naming the outer accessor (Accessors[i+1]) and the expression's type.","triggerScenarios":"Chained swizzles where the OUTER swizzle selects beyond the result size of the inner swizzle, e.g. float2 v; v.xy.z — inner yields 2 components but outer index z=2 is out of range.","commonSituations":"Nested/double swizzles ported from other languages; generated shader code composing swizzles programmatically with mismatched lengths.","solutions":["Ensure the outer swizzle letters fit the inner swizzle's component count (e.g. v.xy.yx, not v.xy.z)","Reduce chained swizzles into a single swizzle in the shader source","Validate swizzle letters against component counts before compiling"],"exampleFix":"// before\nfloat2 v; float f = v.xy.z;\n// after\nfloat2 v; float f = v.xy.x;","handlingStrategy":"validation","validationCode":"bool outerValid = outerSwizzle.All(c => \"xyzw\".IndexOf(c) < innerSwizzle.Length);","typeGuard":"bool ChainedSwizzleValid(string inner, string outer) => outer.All(c => \"xyzw\".IndexOf(c) < inner.Length);","tryCatchPattern":"try { Compile(expr); } catch (InvalidOperationException e) when (e.Message.Contains(\"is out of bound\")) { ReportSwizzleError(expr, e.Message); }","preventionTips":["Ensure chained swizzle letters never exceed the inner swizzle's length","Collapse nested swizzles manually in source before compilation","Add unit tests covering chained-swizzle combinations"],"tags":["sdsl","shader","swizzle","out-of-range"],"backgroundTag":"index-out-of-bounds","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"}