{"record":{"id":"55f65867c2cbeb55","repo":"stride3d/stride","slug":"swizzle-accessor-is-out-of-bound-for-expression-tostring-i","errorCode":null,"errorMessage":"Swizzle {accessor} 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":1240,"sourceCode":"                    if (swizzle.Length > 1)\n                    {\n                        if (compiler == null)\n                        {\n                            accessor.Type = new VectorType(v.BaseType, swizzle.Length);\n                            break;\n                        }\n\n                        // Load value\n                        var (builder, context) = compiler;\n                        EmitOpAccessChain(accessChainIds, i - 1);\n                        result = new(builder.InsertData(new OpLoad(context.GetOrRegister(v), context.Bound++, result.Id, null, [])));\n\n                        var swizzleIndices = swizzleBuffer[..swizzle.Length];\n                        for (int j = 0; j < swizzle.Length; ++j)\n                        {\n                            swizzleIndices[j] = ConvertSwizzle(swizzle[j]);\n                            if (swizzleIndices[j] >= v.Size)\n                                throw new InvalidOperationException($\"Swizzle {accessor} is out of bound for expression {ToString(i)} of type {currentValueType}\");\n                        }\n\n                        (result, _) = builder.ApplyVectorSwizzles(context, result, v, swizzleIndices);\n                    }\n                    else\n                    {\n                        // Keep as a pointer\n                        if (compiler == null)\n                        {\n                            accessor.Type = new PointerType(v.BaseType, p.StorageClass);\n                            break;\n                        }\n\n                        var (builder, context) = compiler;\n                        PushAccessChainId(accessChainIds, context.CompileConstant(ConvertSwizzle(swizzle[0])).Id);\n                    }\n                    break;\n                case (VectorType v, Identifier { Name: var swizzle } id) when id.IsVectorSwizzle():","sourceCodeStart":1222,"sourceCodeEnd":1258,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Expression.cs#L1222-L1258","documentation":"During compilation of a swizzle applied to a vector value, each swizzle character is converted to an index and checked against the vector's Size. If any swizzle index is >= the vector size (e.g. .zw on a float2), the compiler throws this error because the swizzle reads components that don't exist on the value's type.","triggerScenarios":"Applying a vector swizzle (compiler != null path) where a converted swizzle index is greater than or equal to v.Size of the operand vector type — e.g. float2.xxx is fine but float2.z is not.","commonSituations":"Swizzling a float2/float3 with out-of-range components (.z on float2, .w on float3), copy-pasting swizzles between variables of different vector sizes, or generic/template shader code that assumes a larger vector.","solutions":["Reduce the swizzle to components that exist for the operand's vector size (float2 has x/y only, float3 x/y/z).","Change the variable's declared type to a larger vector if more components are genuinely needed.","Refactor swizzles that were copied from differently-sized vectors."],"exampleFix":"// before\nfloat2 uv = ...;\nfloat x = uv.z;                 // z out of bounds for float2\n\n// after\nfloat x = uv.y;                 // or declare uv as float4 to use .z/.w","handlingStrategy":"validation","validationCode":"// validate swizzle length against vector size before applying\nif (swizzle.Length > v.Size || swizzle.Any(c => ConvertSwizzle(c) >= v.Size))\n    throw new InvalidOperationException(\"Swizzle exceeds vector component count\");","typeGuard":"static bool IsSwizzleInBounds(string swizzle, int vectorSize) => swizzle.All(c => \"xyzw\".IndexOf(c) < vectorSize);","tryCatchPattern":"try { CompileShader(source); }\ncatch (InvalidOperationException e) when (e.Message.StartsWith(\"Swizzle\") && e.Message.Contains(\"out of bound\"))\n{\n    // fix the swizzle or widen the vector type\n}","preventionTips":["Keep swizzle components within the operand's vector size (float2: x/y, float3: x/y/z).","Re-check swizzles after changing a variable's vector width.","Avoid copy-pasting swizzles between differently sized vectors."],"tags":["shader","sdsl","swizzle","out-of-range"],"backgroundTag":"index-out-of-range","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"}