{"record":{"id":"7258e21d956e7dea","repo":"stride3d/stride","slug":"nameof-vectorliteral-expecting-totalcount-elements-but-got","errorCode":null,"errorMessage":"{nameof(VectorLiteral)}: Expecting {totalCount} elements but got {elementIndex} for type {Type}","messagePattern":"(.+?): Expecting (.+?) elements but got (.+?) for type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Literals.cs","lineNumber":236,"sourceCode":"                    values[currentElementIndex] = builder.Convert(context, extractedValue, expectedElementType).Id;\n                }\n            }\n            else if (Type is ArrayType arrayType)\n            {\n                values[elementIndex++] = builder.Convert(context, value, expectedElementType).Id;\n            }\n        }\n\n        // Scalar splat: float3(x) means float3(x, x, x)\n        if (elementIndex == 1 && totalCount > 1 && Type is VectorType or MatrixType)\n        {\n            for (int j = 1; j < totalCount; ++j)\n                values[j] = values[0];\n            elementIndex = totalCount;\n        }\n\n        if (elementIndex != totalCount)\n            throw new InvalidOperationException($\"{nameof(VectorLiteral)}: Expecting {totalCount} elements but got {elementIndex} for type {Type}\");\n\n        // Regroup by rows (if necessary, only for Matrix)\n        int compositeSize = totalCount / compositeCount;\n        for (int i = 0; i < compositeCount; ++i)\n        {\n            compositeValues[i] = Type switch\n            {\n                MatrixType m => builder.Insert(new OpCompositeConstruct(context.GetOrRegister(new VectorType(m.BaseType, compositeSize)), context.Bound++, [.. values.Slice(i * compositeSize, compositeSize)])).ResultId,\n                VectorType v => values[i],\n                ArrayType => values[i],\n                _ => throw new NotSupportedException($\"Unsupported composite type {Type} during regrouping\"),\n            };\n        }\n\n        var instruction = builder.Insert(new OpCompositeConstruct(context.GetOrRegister(Type), context.Bound++, [.. compositeValues]));\n        return new(instruction.ResultId, instruction.ResultType);\n    }\n}","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Literals.cs#L218-L254","documentation":"After flattening (including broadcast of a single value), VectorLiteral.CompileImpl verifies the collected element count equals the total count implied by the literal's type (vector size, matrix columns*rows, or array size). A mismatch means the initializer supplied the wrong number of elements, so compilation fails with this InvalidOperationException.","triggerScenarios":"An aggregate literal provides fewer or more scalar values than the target type requires, e.g. 3 values for a float4 or 12 values for a float4x4.","commonSituations":"Hand-written initializer lists with missing trailing elements; size changed in one place (type) but not the other (value list); matrix row/column confusion (float4x1 vs float1x4).","solutions":["Count the initializer elements and match them exactly to the type's total element count","Broadcast explicitly (repeat the value) instead of relying on implicit padding","If the type changed, update the initializer list accordingly"],"exampleFix":"// before\nfloat4 v = { 1.0, 2.0, 3.0 }; // 3 of 4\n// after\nfloat4 v = { 1.0, 2.0, 3.0, 4.0 };","handlingStrategy":"validation","validationCode":"int expected = literal.Type switch {\n    VectorType v => v.Size,\n    MatrixType m => m.Columns * m.Rows,\n    ArrayType a => a.Size,\n    _ => throw new NotSupportedException() };\nif (literal.Elements.Count != expected && literal.Elements.Count != 1)\n    throw new InvalidOperationException($\"Expecting {expected} elements but got {literal.Elements.Count}\");","typeGuard":null,"tryCatchPattern":"try { literal.Compile(context, builder); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Expecting\") && ex.Message.Contains(\"elements\")) { /* fix initializer arity */ }","preventionTips":["Count initializer elements against the type before writing them","Update initializer lists whenever the declared type's size changes","Prefer explicit fills over abbreviating long initializer lists"],"tags":["sdsl","literals","element-count","initializer"],"backgroundTag":"shape-mismatch","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"}