### Godot version

4.0.2

### System information

Windows 11

### Issue description

Changing node type from PopupMenu to ItemList causes Godot to crash

### Steps to reproduce

Change node type from PopupMenu to ItemList

### Minimal reproduction project

n/a
}
			}
		} else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
			Node *n = get_parent();
			if (n) {
				if (Object::cast_to<PopupMenu>(n)) {
					hide();
					set_input_as_handled();
				} else if (Object::cast_to<MenuBar>(n)) {
					Object::cast_to<MenuBar>(n)->gui_input(p_event);
					set_input_as_handled();
					return;
				}
			}
		} else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
			if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && !items[mouse_over].submenu.is_empty() && submenu_over != mouse_over) {
				_activate_submenu(mouse_over, true);
				set_input_as_handled();
			} else {
				Node *n = get_parent();
				if (n && Object::cast_to<MenuBar>(n)) {
					Object::cast_to<MenuBar>(n)->gui_input(p_event);
					set_input_as_handled();
					return;
				}
			}
		} else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
			if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
				if (!items[mouse_over].submenu.is_empty() && submenu_over != mouse_over) {
					_activate_submenu(mouse_over, true);