Auto merge of #126505 - compiler-errors:no-vtable, r=lcnr

Only compute vtable information during codegen

This PR removes vtable information from the `Object` and `TraitUpcasting` candidate sources in the trait solvers, and defers the computation of relevant information to `Instance::resolve`. This is because vtables really aren't a thing in the trait world -- they're an implementation detail in codegen.

Previously it was just easiest to tangle this information together since we were already doing the work of looking at all the supertraits in the trait solver, and specifically because we use traits to represent when it's possible to call a method via a vtable (`Object` candidate) and do upcasting (`Unsize` candidate). but I am somewhat suspicious we're doing a *lot* of extra work, especially in polymorphic contexts, so let's see what perf says.
This commit is contained in:
bors 2024-06-16 05:33:49 +00:00
commit 5639c21fb3
14 changed files with 141 additions and 195 deletions

View file

@ -213,13 +213,23 @@ fn resolve_associated_item<'tcx>(
Some(ty::Instance::new(leaf_def.item.def_id, args))
}
traits::ImplSource::Builtin(BuiltinImplSource::Object { vtable_base }, _) => {
traits::get_vtable_index_of_object_method(tcx, *vtable_base, trait_item_id).map(
|index| Instance {
def: ty::InstanceDef::Virtual(trait_item_id, index),
traits::ImplSource::Builtin(BuiltinImplSource::Object(_), _) => {
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_args);
if trait_ref.has_non_region_infer() || trait_ref.has_non_region_param() {
// We only resolve totally substituted vtable entries.
None
} else {
let vtable_base = tcx.first_method_vtable_slot(trait_ref);
let offset = tcx
.own_existential_vtable_entries(trait_id)
.iter()
.copied()
.position(|def_id| def_id == trait_item_id);
offset.map(|offset| Instance {
def: ty::InstanceDef::Virtual(trait_item_id, vtable_base + offset),
args: rcvr_args,
},
)
})
}
}
traits::ImplSource::Builtin(BuiltinImplSource::Misc, _) => {
if tcx.is_lang_item(trait_ref.def_id, LangItem::Clone) {