Skip to content

Commit 3094cc1

Browse files
authored
Rollup merge of rust-lang#101141 - compiler-errors:get-trait-ref-is-a-misleading-name, r=oli-obk
Simplify `get_trait_ref` fn used for `virtual_function_elimination` 1. The name `get_trait_ref` is misleading, so I renamed it to something more like `expect_...` because it ICEs if used incorrectly. 2. No need to manually go through the existential trait refs, we already have `.principal()` for that.
2 parents cd53b4d + 72fe792 commit 3094cc1

File tree

1 file changed

+7
-11
lines changed
  • compiler/rustc_codegen_ssa/src

1 file changed

+7
-11
lines changed

compiler/rustc_codegen_ssa/src/meth.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::traits::*;
22

3-
use rustc_middle::ty::{self, subst::GenericArgKind, ExistentialPredicate, Ty, TyCtxt};
3+
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
44
use rustc_session::config::Lto;
55
use rustc_symbol_mangling::typeid_for_trait_ref;
66
use rustc_target::abi::call::FnAbi;
@@ -29,7 +29,7 @@ impl<'a, 'tcx> VirtualIndex {
2929
&& bx.cx().sess().lto() == Lto::Fat
3030
{
3131
let typeid =
32-
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), get_trait_ref(bx.tcx(), ty)));
32+
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)));
3333
let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
3434
let type_checked_load = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
3535
let func = bx.extract_value(type_checked_load, 0);
@@ -64,17 +64,13 @@ impl<'a, 'tcx> VirtualIndex {
6464
}
6565
}
6666

67-
fn get_trait_ref<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
67+
/// This takes a valid `self` receiver type and extracts the principal trait
68+
/// ref of the type.
69+
fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
6870
for arg in ty.peel_refs().walk() {
6971
if let GenericArgKind::Type(ty) = arg.unpack() {
70-
if let ty::Dynamic(trait_refs, _) = ty.kind() {
71-
return trait_refs[0].map_bound(|trait_ref| match trait_ref {
72-
ExistentialPredicate::Trait(tr) => tr,
73-
ExistentialPredicate::Projection(proj) => proj.trait_ref(tcx),
74-
ExistentialPredicate::AutoTrait(_) => {
75-
bug!("auto traits don't have functions")
76-
}
77-
});
72+
if let ty::Dynamic(data, _) = ty.kind() {
73+
return data.principal().expect("expected principal trait object");
7874
}
7975
}
8076
}

0 commit comments

Comments
 (0)