zed-industries/zed · error

you must return some state when you pass some element id

Error message

you must return some state when you pass some element id

What it means

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.

Source

Thrown at crates/gpui/src/window.rs:4143

                "reentrant call to with_element_state for the same state type and element id",
            );
            let (result, state) = f(Some(state), self);
            state_box.replace(state);
            self.next_frame.element_states.insert(
                key,
                ElementStateBox {
                    inner: state_box,
                    #[cfg(debug_assertions)]
                    type_name,
                },
            );
            result
        } else {
            let (result, state) = f(None, self);
            self.next_frame.element_states.insert(
                key,
                ElementStateBox {
                    inner: Box::new(Some(state)),
                    #[cfg(debug_assertions)]
                    type_name: std::any::type_name::<S>(),
                },
            );
            result
        }
    }

    /// A variant of `with_element_state` that allows the element's id to be optional. This is a convenience
    /// method for elements where the element id may or may not be assigned. Prefer using `with_element_state`
    /// when the element is guaranteed to have an id.
    ///
    /// The first option means 'no ID provided'
    /// The second option means 'not yet initialized'
    pub fn with_optional_element_state<S, R>(
        &mut self,
        global_id: Option<&GlobalElementId>,
        f: impl FnOnce(Option<Option<S>>, &mut Self) -> (R, Option<S>),

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Ensure the closure passed to with_optional_element_state always returns Some(new_state) when it receives an id
  2. Fix callbacks that return None on 'not yet initialized' instead of initializing
  3. Review the Option<Option<S>> handling: outer None means no id, inner None means initialize
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/gpui/src/window.rs:3889 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@9d272b0363 (2026-08-20). Data as JSON: /api/errors/afd5cc604fc5bac2. Report an issue: GitHub.