1
Fork 0

Add query own_existential_vtable_entries

This commit is contained in:
Gary Guo 2021-09-01 23:04:28 +01:00
parent 871eb6233e
commit 97214eecc5
4 changed files with 69 additions and 44 deletions

View file

@ -285,17 +285,10 @@ pub fn upcast_choices(
/// that come from `trait_ref`, excluding its supertraits. Used in
/// computing the vtable base for an upcast trait of a trait object.
pub fn count_own_vtable_entries(tcx: TyCtxt<'tcx>, trait_ref: ty::PolyTraitRef<'tcx>) -> usize {
let mut entries = 0;
// Count number of methods and add them to the total offset.
// Skip over associated types and constants.
for trait_item in tcx.associated_items(trait_ref.def_id()).in_definition_order() {
let is_vtable_safe_method = trait_item.kind == ty::AssocKind::Fn
&& super::is_vtable_safe_method(tcx, trait_ref.def_id(), trait_item);
if is_vtable_safe_method {
entries += 1;
}
}
entries
let existential_trait_ref =
trait_ref.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
let existential_trait_ref = tcx.erase_regions(existential_trait_ref);
tcx.own_existential_vtable_entries(existential_trait_ref).len()
}
/// Given an upcast trait object described by `object`, returns the
@ -306,25 +299,21 @@ pub fn get_vtable_index_of_object_method<N>(
object: &super::ImplSourceObjectData<'tcx, N>,
method_def_id: DefId,
) -> 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.
// Skip over associated types and constants, as those aren't stored in the vtable.
let mut entries = object.vtable_base;
let trait_def_id = object.upcast_trait_ref.def_id();
for trait_item in tcx.associated_items(trait_def_id).in_definition_order() {
let is_vtable_safe_method = trait_item.kind == ty::AssocKind::Fn
&& super::is_vtable_safe_method(tcx, trait_def_id, trait_item);
if trait_item.def_id == method_def_id {
// The item with the ID we were given really ought to be a method.
assert!(is_vtable_safe_method);
return entries;
}
if is_vtable_safe_method {
entries += 1;
}
}
bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id);
let 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
}
pub fn closure_trait_ref_and_return_type(