tauri-apps/tauri · error

Not a PredefinedMenuItem

Error message

Not a PredefinedMenuItem

What it means

`as_predefined_menuitem_unchecked` downcasts a `MenuItemKind` to `PredefinedMenuItem<R>` (separator, copy, paste, etc.) and panics with 'Not a PredefinedMenuItem' when the item is any other kind. The safe `as_predefined_menuitem` returns Option.

Solutions

  1. Use `as_predefined_menuitem()` returning `Option<&PredefinedMenuItem<R>>` instead of the unchecked cast.
  2. Match explicitly on `MenuItemKind::Predefined(p)` and skip/handle other variants.
  3. Store concrete `PredefinedMenuItem<R>` handles when you need predefined-item APIs later.
  4. Filter the item collection by kind before performing predefined-specific operations.

Example fix

// before
let pre: &PredefinedMenuItem<R> = kind.as_predefined_menuitem_unchecked();
// after
if let Some(pre) = kind.as_predefined_menuitem() {
  // predefined-only API
}
Defensive patterns

Strategy: type-guard

Validate before calling

if matches!(kind, tauri::menu::MenuItemKind::Predefined(_)) {
  let pre = kind.as_predefined_menuitem_unchecked();
}

Type guard

fn as_predefined<R: tauri::Runtime>(kind: &tauri::menu::MenuItemKind<R>) -> Option<&tauri::menu::PredefinedMenuItem<R>> {
  kind.as_predefined_menuitem()
}

Prevention

When it happens

Trigger: Calling `as_predefined_menuitem_unchecked()` on a kind that is not `MenuItemKind::Predefined` — e.g. scanning menu items looking for separators while the collection contains plain menu items or submenus.

Common situations: Menu inspection/migration code that assumes uniform kinds; toggling predefined items by casting stored kinds that were actually regular MenuItems; automated menu tests iterating heterogeneous item lists.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/ff15c3b317194c2b. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri/src/menu/mod.rs:641

    match self {
      MenuItemKind::Submenu(i) => i,
      _ => panic!("Not a Submenu"),
    }
  }

  /// Casts this item to a [`PredefinedMenuItem`], and returns `None` if it wasn't.
  pub fn as_predefined_menuitem(&self) -> Option<&PredefinedMenuItem<R>> {
    match self {
      MenuItemKind::Predefined(i) => Some(i),
      _ => None,
    }
  }

  /// Casts this item to a [`PredefinedMenuItem`], and panics if it wasn't.
  pub fn as_predefined_menuitem_unchecked(&self) -> &PredefinedMenuItem<R> {
    match self {
      MenuItemKind::Predefined(i) => i,
      _ => panic!("Not a PredefinedMenuItem"),
    }
  }

  /// Casts this item to a [`CheckMenuItem`], and returns `None` if it wasn't.
  pub fn as_check_menuitem(&self) -> Option<&CheckMenuItem<R>> {
    match self {
      MenuItemKind::Check(i) => Some(i),
      _ => None,
    }
  }

  /// Casts this item to a [`CheckMenuItem`], and panics if it wasn't.
  pub fn as_check_menuitem_unchecked(&self) -> &CheckMenuItem<R> {
    match self {
      MenuItemKind::Check(i) => i,
      _ => panic!("Not a CheckMenuItem"),
    }
  }

View on GitHub (pinned to 460ec35447)