tauri-apps/tauri · error

Not a Submenu

Error message

Not a Submenu

What it means

`as_submenu_unchecked` downcasts a `MenuItemKind` to `Submenu<R>` and panics with 'Not a Submenu' when the variant is a plain MenuItem, Predefined, Check, or Icon item. The safe counterpart `as_submenu` returns Option.

Solutions

  1. Use the checked `as_submenu()` returning `Option<&Submenu<R>>` and handle None.
  2. Pattern-match on `MenuItemKind::Submenu(s)` explicitly before using submenu APIs.
  3. Keep concrete `Submenu<R>` handles where submenu APIs are needed instead of round-tripping through MenuItemKind.
  4. Guard with `matches!(kind, MenuItemKind::Submenu(_))` if the unchecked variant must stay.

Example fix

// before
let sub: &Submenu<R> = kind.as_submenu_unchecked();
sub.append(&item)?;
// after
if let Some(sub) = kind.as_submenu() {
  sub.append(&item)?;
} else { eprintln!("item is not a submenu"); }
Defensive patterns

Strategy: type-guard

Validate before calling

if matches!(kind, tauri::menu::MenuItemKind::Submenu(_)) {
  let sub = kind.as_submenu_unchecked();
}

Type guard

fn as_submenu<R: tauri::Runtime>(kind: &tauri::menu::MenuItemKind<R>) -> Option<&tauri::menu::Submenu<R>> {
  kind.as_submenu()
}

Prevention

When it happens

Trigger: Calling `as_submenu_unchecked()` on a MenuItemKind that is not `MenuItemKind::Submenu` — typically when iterating menu items and assuming each is a submenu to add children to it.

Common situations: Building nested menus dynamically where some items are plain MenuItems but the code casts all to Submenu; retrieving an item by id that was changed to a MenuItem in a refactor; handling menu events with the wrong stored kind.

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/b1a502ffd503c990. Report an issue: GitHub.

Appendix: source

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

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

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

  /// Casts this item to a [`Submenu`], and panics if it wasn't.
  pub fn as_submenu_unchecked(&self) -> &Submenu<R> {
    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"),
    }
  }

View on GitHub (pinned to 460ec35447)