diff options
| author | ouz-a <oguz.agcayazi@gmail.com> | 2022-05-31 21:34:04 +0300 |
|---|---|---|
| committer | ouz-a <oguz.agcayazi@gmail.com> | 2022-06-05 16:35:02 +0300 |
| commit | 8f1fff04a72f65bc991b0c76b7b7300384b0d33d (patch) | |
| tree | d8b93b26305fdd50b3dbd92d543cde14a8b2a9a1 /compiler | |
| parent | d35d972e6974d40d30362344ea619a5b560aae20 (diff) | |
get_vtable returns opt instd of unwrpping
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_trait_selection/src/traits/util.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_ty_utils/src/instance.rs | 14 |
2 files changed, 17 insertions, 11 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index f2e31c068a0..3a00c41d90a 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -304,22 +304,24 @@ pub fn get_vtable_index_of_object_method<'tcx, N>( tcx: TyCtxt<'tcx>, object: &super::ImplSourceObjectData<'tcx, N>, method_def_id: DefId, -) -> usize { +) -> Option<usize> { let existential_trait_ref = object .upcast_trait_ref .map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)); let existential_trait_ref = tcx.erase_regions(existential_trait_ref); + // Count number of methods preceding the one we are selecting and // add them to the total offset. - let index = tcx + if let Some(index) = tcx .own_existential_vtable_entries(existential_trait_ref) .iter() .copied() .position(|def_id| def_id == method_def_id) - .unwrap_or_else(|| { - bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id); - }); - object.vtable_base + index + { + Some(object.vtable_base + index) + } else { + None + } } pub fn closure_trait_ref_and_return_type<'tcx>( diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 17eac2bb2c9..c78a5855edb 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -347,11 +347,15 @@ fn resolve_associated_item<'tcx>( _ => None, }, traits::ImplSource::Object(ref data) => { - let index = traits::get_vtable_index_of_object_method(tcx, data, trait_item_id); - Some(Instance { - def: ty::InstanceDef::Virtual(trait_item_id, index), - substs: rcvr_substs, - }) + if let Some(index) = traits::get_vtable_index_of_object_method(tcx, data, trait_item_id) + { + Some(Instance { + def: ty::InstanceDef::Virtual(trait_item_id, index), + substs: rcvr_substs, + }) + } else { + None + } } traits::ImplSource::Builtin(..) => { if Some(trait_ref.def_id) == tcx.lang_items().clone_trait() { |
