{"record":{"id":"afd5cc604fc5bac2","repo":"zed-industries/zed","slug":"you-must-return-some-state-when-you-pass-some-elem","errorCode":null,"errorMessage":"you must return some state when you pass some element id","messagePattern":"you must return some state when you pass some element id","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui/src/window.rs","lineNumber":4143,"sourceCode":"                \"reentrant call to with_element_state for the same state type and element id\",\n            );\n            let (result, state) = f(Some(state), self);\n            state_box.replace(state);\n            self.next_frame.element_states.insert(\n                key,\n                ElementStateBox {\n                    inner: state_box,\n                    #[cfg(debug_assertions)]\n                    type_name,\n                },\n            );\n            result\n        } else {\n            let (result, state) = f(None, self);\n            self.next_frame.element_states.insert(\n                key,\n                ElementStateBox {\n                    inner: Box::new(Some(state)),\n                    #[cfg(debug_assertions)]\n                    type_name: std::any::type_name::<S>(),\n                },\n            );\n            result\n        }\n    }\n\n    /// A variant of `with_element_state` that allows the element's id to be optional. This is a convenience\n    /// method for elements where the element id may or may not be assigned. Prefer using `with_element_state`\n    /// when the element is guaranteed to have an id.\n    ///\n    /// The first option means 'no ID provided'\n    /// The second option means 'not yet initialized'\n    pub fn with_optional_element_state<S, R>(\n        &mut self,\n        global_id: Option<&GlobalElementId>,\n        f: impl FnOnce(Option<Option<S>>, &mut Self) -> (R, Option<S>),","sourceCodeStart":4125,"sourceCodeEnd":4161,"githubUrl":"https://github.com/zed-industries/zed/blob/9d272b036335401f339d024ea94968fd51016c40/crates/gpui/src/window.rs#L4125-L4161","documentation":"with_optional_element_state delegates to with_element_state when a global id is given; the expect enforces that the callback returned Some(state) — the closure must always produce state when an id was supplied. Panicking means the callback returned None for an existing element id, breaking element-state persistence.","triggerScenarios":"Thrown at crates/gpui/src/window.rs:3889 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the closure passed to with_optional_element_state always returns Some(new_state) when it receives an id","Fix callbacks that return None on 'not yet initialized' instead of initializing","Review the Option<Option<S>> handling: outer None means no id, inner None means initialize"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9d272b036335401f339d024ea94968fd51016c40","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}