### 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
ERR_FAIL_INDEX(p_idx, items.size());
	ERR_FAIL_COND(items[p_idx].separator);
	int id = items[p_idx].id >= 0 ? items[p_idx].id : p_idx;
	
	Node *next = get_parent();
	PopupMenu *pop = Object::cast_to<PopupMenu>(next);
	while (pop) {
		
		
		if (items[p_idx].checkable_type) {
			if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
				break;
			}
		} else if (0 < items[p_idx].max_states) {
			if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {