1
Fork 0

Stop allocating vtable entries for non-object-safe methods

This commit is contained in:
Gary Guo 2021-09-01 00:49:14 +01:00
parent 03c775c955
commit 871eb6233e
6 changed files with 54 additions and 12 deletions

View file

@ -289,7 +289,9 @@ pub fn count_own_vtable_entries(tcx: TyCtxt<'tcx>, trait_ref: ty::PolyTraitRef<'
// 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() {
if trait_item.kind == ty::AssocKind::Fn {
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;
}
}
@ -308,13 +310,16 @@ pub fn get_vtable_index_of_object_method<N>(
// 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;
for trait_item in tcx.associated_items(object.upcast_trait_ref.def_id()).in_definition_order() {
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_eq!(trait_item.kind, ty::AssocKind::Fn);
assert!(is_vtable_safe_method);
return entries;
}
if trait_item.kind == ty::AssocKind::Fn {
if is_vtable_safe_method {
entries += 1;
}
}