{"record":{"id":"a8518c18b1e3ac26","repo":"slint-ui/slint","slug":"expression-structfieldaccess-cannot-find-a-key-in-an-object","errorCode":null,"errorMessage":"Expression::StructFieldAccess: Cannot find a key in an object","messagePattern":"Expression::StructFieldAccess: Cannot find a key in an object","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/compiler/generator/rust.rs","lineNumber":4522,"sourceCode":"                &self::_SLINT_TRANSLATED_STRINGS_PLURALS[#string_index],\n                &self::_SLINT_TRANSLATED_PLURAL_RULES,\n                sp::Slice::<sp::SharedString>::from(#args).as_slice(),\n                #plural as _\n            ))\n        }\n        None => {\n            quote!(sp::translate_from_bundle(&self::_SLINT_TRANSLATED_STRINGS[#string_index], sp::Slice::<sp::SharedString>::from(#args).as_slice()))\n        }\n    }\n}\n\nfn struct_field_access(s: &Struct, name: &str) -> proc_macro2::TokenTree {\n    if s.name.is_none() {\n        let index = s\n            .fields\n            .keys()\n            .position(|k| k == name)\n            .expect(\"Expression::StructFieldAccess: Cannot find a key in an object\");\n        proc_macro2::Literal::usize_unsuffixed(index).into()\n    } else {\n        ident(name).into()\n    }\n}\n\nfn compile_builtin_function_call(\n    function: BuiltinFunction,\n    arguments: &[Expression],\n    ctx: &EvaluationContext,\n) -> TokenStream {\n    let mut a = arguments.iter().map(|a| compile_expression_to_value(a, ctx));\n    match function {\n        BuiltinFunction::SetFocusItem => {\n            if let [Expression::PropertyReference(pr)] = arguments {\n                let window_tokens = access_window_adapter_field(ctx);\n                item_owner(pr).then(|owner| {\n                    let (_, focus_item) = native_item_from_owner(pr, ctx, &owner);","sourceCodeStart":4504,"sourceCodeEnd":4540,"githubUrl":"https://github.com/slint-ui/slint/blob/3a7e700487dc74d404cad0726378053ab763d4e0/internal/compiler/generator/rust.rs#L4504-L4540","documentation":"Rust generator counterpart of the C++ struct-key panic: anonymous structs become tuples, and `struct_field_access` computes the tuple element index from the field name. Panics when the requested field name is absent from the struct's field map — an expression accessing a key that does not exist reached codegen despite type checking.","triggerScenarios":"`lower_field_access_chain` or `compile_expression` lowering a `StructFieldAccess` whose `name` is missing from `s.fields` of an anonymous struct — caused by a pass rewriting struct shapes or a typecheck/codegen inconsistency.","commonSituations":"Compiler development: field-renaming or optimization passes that change anonymous struct fields without tracking dependent accesses; lookup/type-inference regressions; fuzzed inputs that bypass checker invariants.","solutions":["Fix the pass that desynchronizes the struct type from the field accesses","Ensure the type checker rejects unknown field accesses with a user-facing diagnostic before codegen","Dump the Struct's fields at the panic site to identify which pass dropped the key"],"exampleFix":"// before\nlet index = s.fields.keys().position(|k| k == name).expect(\"...\");\n// after\nlet index = s.fields.keys().position(|k| k == name).unwrap_or_else(||\n    panic!(\"StructFieldAccess: key '{name}' not found in fields {:?}\", s.fields.keys().collect::<Vec<_>>()));","handlingStrategy":"type-guard","validationCode":"fn struct_has_field(s: &Struct, name: &str) -> bool {\n    s.fields.keys().any(|k| k == name)\n}","typeGuard":"fn field_index(s: &Struct, name: &str) -> Option<usize> {\n    s.fields.keys().position(|k| k == name)\n}","tryCatchPattern":null,"preventionTips":["Guarantee type checking runs and passes before rust codegen","Add tests for passes that mutate anonymous struct shapes","Keep struct_field_access behavior aligned with the C++ generator","Include field names in panic messages to speed up debugging"],"tags":["compiler","rust-codegen","structs","internal-invariant"],"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"}