slint-ui/slint · error
Expression::ObjectAccess: Cannot find a key in an object
Error message
Expression::ObjectAccess: Cannot find a key in an object
What it means
When generating C++ for `Expression::ObjectAccess`, anonymous structs are compiled to `std::tuple` and field access uses the field's positional index. `struct_field_access` panics if the requested key does not exist in the struct's field list, meaning the type checker let an access to a non-existent key slip through to code generation.
Solutions
- Fix the upstream pass so the Struct type matches the actual fields of the object literal
- Verify the type checker rejects unknown-key accesses before codegen and emits a proper diagnostic
- Re-run the type-checking pass on the failing input to confirm the struct's fields
Example fix
// before
let index = s.fields.keys().position(|k| k == name).expect("...");
// after
let index = s.fields.keys().position(|k| k == name).unwrap_or_else(||
panic!("ObjectAccess: key '{name}' not in struct with fields {:?}", s.fields.keys().collect::<Vec<_>>())); Defensive patterns
Strategy: type-guard
Validate before calling
fn struct_has_field(s: &Struct, name: &str) -> bool {
s.fields.keys().any(|k| k == name)
} Type guard
fn checked_field_index(s: &Struct, name: &str) -> Option<usize> {
s.fields.keys().position(|k| k == name)
} Prevention
- Ensure the type checker validates every ObjectAccess key before codegen
- Run type checking as a hard prerequisite of code generation in tests
- Add regression tests for passes that rewrite anonymous struct fields
- Log the struct's field list in the panic message for diagnosability
When it happens
Trigger: ObjectAccess on an anonymous struct where `name` is not among `s.fields.keys()` — normally prevented by type checking; reaching it indicates a mismatch between the checked type and the generated struct, or a pass that rewrote struct fields without updating accesses.
Common situations: Compiler development: a normalization pass drops or renames struct fields; lookup/type-inference changes that produce an inconsistent Struct for object literals; regression tests with malformed expressions bypassing typecheck.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Expression::StructFieldAccess: Cannot find a key in an…
- model two-way bindings only exist on sub-components
- ShowPopupWindow is invoked on a sub-component
- there should be a popup menu if we want to show it
- Invalid type in public properties
AI-assisted analysis of slint-ui/slint@3a7e700487 (2026-09-16).
Data as JSON: /api/errors/3223511df1a7230d.
Report an issue: GitHub.
Appendix: source
Thrown at internal/compiler/generator/cpp.rs:4955
}
Expression::Closure { arg_name, expression } => {
let arg = ident(arg_name);
let expr = compile_expression(expression, ctx);
format!("[&](auto const &{arg}) -> bool {{ return {expr}; }}")
}
// Generated code has no debug hooks; use the wrapped expression.
Expression::DebugHook { expression, .. } => compile_expression(expression, ctx),
}
}
fn struct_field_access(base: String, s: &crate::langtype::Struct, name: &str) -> String {
if s.name.is_none() {
let index = s
.fields
.keys()
.position(|k| k == name)
.expect("Expression::ObjectAccess: Cannot find a key in an object");
format!("std::get<{}>({})", index, base)
} else {
format!("{}.{}", base, ident(name))
}
}
fn compile_builtin_function_call(
function: BuiltinFunction,
arguments: &[llr::Expression],
ctx: &EvaluationContext,
) -> String {
let mut a = arguments.iter().map(|a| compile_expression(a, ctx));
let pi_180 = std::f64::consts::PI / 180.0;
match function {
BuiltinFunction::GetWindowScaleFactor => {
format!("{}.scale_factor()", access_window_field(ctx))
}View on GitHub (pinned to 3a7e700487)