{"record":{"id":"e12e10918b2265fb","repo":"stride3d/stride","slug":"unsupported-constant-type-value-gettype","errorCode":null,"errorMessage":"Unsupported constant type: {value.GetType()}","messagePattern":"Unsupported constant type: (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Core/ConstantExpression.cs","lineNumber":69,"sourceCode":"        var tempContext = new SpirvContext();\n        var resultId = Emit(tempContext);\n        return SpirvContext.ExtractConstantFromBuffer(resultId, tempContext.GetBuffer());\n    }\n\n    /// <summary>\n    /// Create a ConstantExpression from a concrete runtime value.\n    /// </summary>\n    public static ConstantExpression FromValue(object value) => value switch\n    {\n        int i => new IntConstExpr(i),\n        uint u => new IntConstExpr(u),\n        long l => new IntConstExpr(l),\n        ulong u => new IntConstExpr((long)u),\n        float f => new FloatConstExpr(f),\n        double d => new FloatConstExpr(d),\n        bool b => new BoolConstExpr(b),\n        string s => new StringConstExpr(s),\n        _ => throw new NotSupportedException($\"Unsupported constant type: {value.GetType()}\")\n    };\n\n    /// <summary>\n    /// Parse a SPIR-V constant ID into a ConstantExpression tree.\n    /// Replaces ExtractConstantFromBuffer for array sizes and generic arguments.\n    /// </summary>\n    public static ConstantExpression ParseFromBuffer(int constantId, SpirvBuffer buffer, SpirvContext context)\n    {\n        if (!buffer.TryGetInstructionById(constantId, out var inst))\n            throw new InvalidOperationException($\"Cannot find instruction for id {constantId}\");\n\n        return ParseInstruction(inst, buffer, context);\n    }\n\n    private static ConstantExpression ParseInstruction(OpDataIndex inst, SpirvBuffer buffer, SpirvContext context)\n    {\n        switch (inst.Op)\n        {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Core/ConstantExpression.cs#L51-L87","documentation":"ConstantExpression.FromValue throws NotSupportedException when given a .NET value whose type has no constant-expression representation. Supported types are int/long/uint/ulong (ints), float/double, bool, and string.","triggerScenarios":"Calling FromValue with a value of any other type (e.g. char, decimal, Guid, enum boxed as its underlying-but-unsupported shape, user struct) — often from generic constants in SDSL/SPIR-V codegen.","commonSituations":"Passing C# enum or decimal constant into shader constant emission; accidentally passing a boxed object; switching compiler versions where narrower types (byte/short) are no longer implicitly matched.","solutions":["Convert the value to a supported type before calling: e.g. cast enum to int, decimal to double, byte/short to int.","Extend FromValue to add a pattern arm for the type if you own the code.","Avoid passing unsupported runtime types as shader constants."],"exampleFix":"// before\nvar expr = ConstantExpression.FromValue(myEnumValue); // NotSupportedException\n// after\nvar expr = ConstantExpression.FromValue(Convert.ToInt32(myEnumValue));","handlingStrategy":"type-guard","validationCode":"static bool IsSupportedConstantType(object v) => v is int or long or uint or ulong or float or double or bool or string;","typeGuard":"bool IsSupported(object? v) => v is int or long or uint or ulong or float or double or bool or string;","tryCatchPattern":"try { expr = ConstantExpression.FromValue(value); }\ncatch (NotSupportedException ex) { expr = ConstantExpression.FromValue(Convert.ToDouble(value)); }","preventionTips":["Normalize enum/decimal/byte/short values to int/double before emitting","Document allowed constant types at the API boundary","Add unit tests for each constant type you use"],"tags":["spirv","unsupported-type","codegen"],"backgroundTag":"unsupported-operation","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"}