{"record":{"id":"2b4d0202e32be45c","repo":"slint-ui/slint","slug":"default-value-requested-for-unknown-struct-field","errorCode":null,"errorMessage":"default value requested for unknown struct field","messagePattern":"default value requested for unknown struct field","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/compiler/langtype.rs","lineNumber":1190,"sourceCode":"            StructName::User { rust_attributes, .. } => rust_attributes,\n            _ => &[],\n        }\n    }\n\n    /// The field names in declaration order for a user-declared struct.\n    pub fn field_order(&self) -> &[SmolStr] {\n        match &self.name {\n            StructName::User { field_order, .. } => field_order,\n            _ => &[],\n        }\n    }\n\n    /// The default value for the given field: the user-declared default if there is one,\n    /// otherwise the default value for the field's type.\n    pub fn default_value_for_field(&self, name: &SmolStr) -> Expression {\n        self.field_defaults.get(name).map(ConstantExpression::to_expression).unwrap_or_else(|| {\n            Expression::default_value_for_type(\n                self.fields.get(name).expect(\"default value requested for unknown struct field\"),\n            )\n        })\n    }\n}\n\n/// A constant expression, used for the default values of struct fields\n/// (see [`Struct::field_defaults`]).\n///\n/// This is deliberately neither [`Expression`] nor an llr expression:\n/// unlike those, it cannot reference any properties, elements, or syntax nodes,\n/// so a [`Struct`] carrying one can safely outlive the object tree.\n/// The variants are the subset that every consumer can materialize.\n/// Keep the matches over this type exhaustive,\n/// so that adding a variant is a compile error in each consumer:\n/// the conversion to an expression tree ([`Self::to_expression`]),\n/// the lowering for the code generators (`lower_constant_expression` in the llr module),\n/// and the interpreter's evaluator (`eval_constant_expression` there).\n#[derive(Debug, Clone)]","sourceCodeStart":1172,"sourceCodeEnd":1208,"githubUrl":"https://github.com/slint-ui/slint/blob/3a7e700487dc74d404cad0726378053ab763d4e0/internal/compiler/langtype.rs#L1172-L1208","documentation":"`default_value_for_field` looks up a field's default: first in `field_defaults`, else via the field's type in `fields`. The panic means the requested field name exists in neither map — an unknown struct field was queried.","triggerScenarios":"Calling `default_value_for_field` with a name not present in the struct's `fields` — e.g. a two-way binding or expression referencing a struct property with a typo'd/renamed field before name-resolution rejects it, or a pass assuming a field exists after a type erasure.","commonSituations":"Compiler developers adding passes that fabricate field accesses without checking `Type::Struct.fields`; users indirectly trigger it via struct property initializers with extra/misspelled fields if upstream validation changed.","solutions":["Check `fields.contains_key(name)` (or use `.get()` returning Option) before requesting the default","Ensure earlier passes reject unknown struct fields so this is never reached with a bad name","If triggered by valid user code, reduce the .slint file and file a compiler bug"],"exampleFix":"// before\nself.fields.get(name).expect(\"default value requested for unknown struct field\")\n// after\nmatch self.fields.get(name) {\n    Some(ty) => Expression::default_value_for_type(ty),\n    None => return Expression::Invalid, // or propagate an error\n}","handlingStrategy":"type-guard","validationCode":"if !struct_ty.fields.contains_key(field_name) {\n    return Err(format!(\"unknown struct field: {field_name}\"));\n}","typeGuard":"fn known_field(s: &Struct, name: &str) -> bool { s.fields.contains_key(name) }","tryCatchPattern":"// unreachable in normal operation; wrap pass invocation to catch panics if running untrusted input\nstd::panic::catch_unwind(|| default_value_for_field(name))","preventionTips":["Validate field names against `fields` before requesting defaults","Keep early passes rejecting unknown struct fields"],"tags":["rust","compiler","panic","struct","type-system"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3a7e700487dc74d404cad0726378053ab763d4e0","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}