{"record":{"id":"582f3d3467938239","repo":"slint-ui/slint","slug":"unsupported-builtin-struct-field-default-tokens","errorCode":null,"errorMessage":"unsupported builtin struct field default `{tokens}`","messagePattern":"unsupported builtin struct field default `(.+?)`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/python/slint/language.rs","lineNumber":50,"sourceCode":"/// so `register_structs` must run after `register_enums`.\nfn declared_default_value<'py>(\n    py: Python<'py>,\n    m: &Bound<'py, PyModule>,\n    tokens: &str,\n    rust_ty: &str,\n) -> PyResult<Bound<'py, PyAny>> {\n    let text: String =\n        tokens.chars().filter(|c| !c.is_whitespace() && *c != '(' && *c != ')').collect();\n    if let Some((enum_name, variant)) = text.split_once(\"::\") {\n        let member = to_kebab_case(variant.trim_start_matches(\"r#\")).replace('-', \"_\");\n        return language_submodule(py, m)?.getattr(enum_name)?.getattr(member.as_str());\n    }\n    if let Ok(b) = text.parse::<bool>() {\n        return b.into_bound_py_any(py);\n    }\n    let value = text\n        .parse::<f64>()\n        .unwrap_or_else(|_| panic!(\"unsupported builtin struct field default `{tokens}`\"));\n    if rust_ty == \"i32\" {\n        (value as i32).into_bound_py_any(py)\n    } else {\n        value.into_bound_py_any(py)\n    }\n}\n\n/// Returns the `slint.language` submodule, creating and registering it on the parent\n/// module + `sys.modules` on first call so `from slint.language import …` works.\nfn language_submodule<'py>(\n    py: Python<'py>,\n    m: &Bound<'py, PyModule>,\n) -> PyResult<Bound<'py, PyModule>> {\n    match m.getattr(\"language\") {\n        Ok(existing) => Ok(existing.cast_into::<PyModule>()?),\n        Err(_) => {\n            let sub = PyModule::new(py, \"slint.language\")?;\n            m.add(\"language\", &sub)?;","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/api/python/slint/language.rs#L32-L68","documentation":"The Python binding's `declared_default_value` converts a builtin struct field's declared default (a token string from .slint markup) into a Python value. It accepts booleans and numbers; any other non-numeric token panics with this message. It means a default value in a builtin struct definition is of a type the Python codegen cannot express.","triggerScenarios":"A builtin struct field in the Slint compiler's builtins declares a textual default value that is neither `true`/`false` nor parses as f64 — e.g. a color, string, length-with-unit (\"4px\"), or enum default.","commonSituations":"Developers extending builtin structs in the compiler (builtins.slint) adding fields with defaults of unsupported types (lengths, strings, colors) while testing the Python backend; unit-suffixed literals like `4px` fail f64 parsing.","solutions":["Change the field's declared default in the builtin definition to a plain boolean or numeric literal","Add a parsing branch in declared_default_value for the new default type (e.g. strip unit or map colors) before the f64 fallback","If the default is inherently non-numeric, skip Python default generation for that field instead of panicking"],"exampleFix":"// before (builtins):\nproperty <length> offset: 4px;\n// after\nproperty <length> offset: 4.0; // or handle unit parsing in declared_default_value","handlingStrategy":"validation","validationCode":"def ensure_supported_default(value: str) -> None:\n    if value in (\"true\", \"false\"):\n        return\n    try:\n        float(value)\n    except ValueError:\n        raise ValueError(f\"default {value!r} must be bool or numeric for Python codegen\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep builtin struct defaults to plain bool/numeric literals","Add unit-stripping support when introducing length defaults","Test Python codegen after editing builtins"],"tags":["python","codegen","slint-compiler","default-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}