Skip to content

Commit a27bdbc

Browse files
committed
incorporate some review feedback
1 parent 88c84b2 commit a27bdbc

File tree

6 files changed

+18
-38
lines changed

6 files changed

+18
-38
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::value::Value;
1010
use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh};
1111
use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
1212
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
13+
use rustc_codegen_ssa::meth;
1314
use rustc_codegen_ssa::mir::operand::OperandRef;
1415
use rustc_codegen_ssa::mir::place::PlaceRef;
1516
use rustc_codegen_ssa::traits::*;
@@ -364,17 +365,13 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
364365
}
365366

366367
sym::vtable_size | sym::vtable_align => {
367-
let ptr = args[0].immediate();
368-
let layout = self.layout_of(self.tcx.types.usize);
369-
let type_ = self.backend_type(layout);
370-
let offset = match name {
371-
sym::vtable_size => 1,
372-
sym::vtable_align => 2,
368+
let vtable = args[0].immediate();
369+
let idx = match name {
370+
sym::vtable_size => ty::COMMON_VTABLE_ENTRIES_SIZE,
371+
sym::vtable_align => ty::COMMON_VTABLE_ENTRIES_ALIGN,
373372
_ => bug!(),
374373
};
375-
let offset = self.const_int(type_, offset);
376-
let vtable_field_ptr = self.inbounds_gep(type_, ptr, &[offset]);
377-
self.load(type_, vtable_field_ptr, layout.align.abi)
374+
meth::VirtualIndex::from_index(idx).get_usize(self, vtable)
378375
}
379376

380377
_ if name.as_str().starts_with("simd_") => {

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -297,32 +297,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
297297
Immediate::new_slice(ptr, length.eval_usize(*self.tcx, self.param_env), self);
298298
self.write_immediate(val, dest)
299299
}
300-
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
301-
let val = self.read_immediate(src)?;
302-
let (old_data, old_vptr) = val.to_scalar_pair()?;
300+
(&ty::Dynamic(ref _data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
301+
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
303302
let old_vptr = self.scalar_to_ptr(old_vptr)?;
304-
if data_a.principal_def_id() == data_b.principal_def_id() {
305-
return self.write_immediate(*val, dest);
306-
}
307-
// trait upcasting coercion
308-
let Some(vptr_entry_idx) = self.tcx.vtable_trait_upcasting_coercion_new_vptr_slot((
309-
src_pointee_ty,
310-
dest_pointee_ty,
311-
)) else {
312-
return self.write_immediate(*val, dest);
313-
};
314-
315303
let (ty, _) = self.get_ptr_vtable(old_vptr)?;
316-
let Some(ty::VtblEntry::TraitVPtr(new_trait)) = self.get_vtable_entries(old_vptr)?.get(vptr_entry_idx) else {
317-
throw_ub_format!(
318-
"upcasting to index {vptr_entry_idx} of vtable {old_vptr} but \
319-
that vtable is too small or does not have an upcast-vtable at that index"
320-
)
321-
};
322-
let new_trait = new_trait.map_bound(|trait_ref| {
323-
ty::ExistentialTraitRef::erase_self_ty(*self.tcx, trait_ref)
324-
});
325-
let new_vptr = self.get_vtable_ptr(ty, Some(new_trait))?;
304+
let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?;
326305
self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest)
327306
}
328307
(_, &ty::Dynamic(ref data, _)) => {

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
871871
write!(fmt, " (vtable: impl {trait_ref} for {ty})")?;
872872
}
873873
Some(GlobalAlloc::Vtable(ty, None)) => {
874-
write!(fmt, " (vtable: impl ? for {ty})")?;
874+
write!(fmt, " (vtable: impl <auto trait> for {ty})")?;
875875
}
876876
Some(GlobalAlloc::Static(did)) => {
877877
write!(fmt, " (static: {})", self.ecx.tcx.def_path_str(did))?;

compiler/rustc_const_eval/src/interpret/traits.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1010
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
1111
/// objects.
1212
///
13-
/// The `trait_ref` encodes the erased self type. Hence, if we are
14-
/// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
15-
/// `trait_ref` would map `T: Trait`.
13+
/// The `trait_ref` encodes the erased self type. Hence, if we are making an object `Foo<Trait>`
14+
/// from a value of type `Foo<T>`, then `trait_ref` would map `T: Trait`. `None` here means that
15+
/// this is an auto trait without any methods, so we only need the basic vtable (drop, size,
16+
/// align).
1617
pub fn get_vtable_ptr(
1718
&self,
1819
ty: Ty<'tcx>,

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ pub fn write_allocations<'tcx>(
728728
Some(GlobalAlloc::Vtable(ty, Some(trait_ref))) => {
729729
write!(w, " (vtable: impl {trait_ref} for {ty})")?
730730
}
731-
Some(GlobalAlloc::Vtable(ty, None)) => write!(w, " (vtable: impl ? for {ty})")?,
731+
Some(GlobalAlloc::Vtable(ty, None)) => write!(w, " (vtable: impl <auto trait> for {ty})")?,
732732
Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
733733
match tcx.eval_static_initializer(did) {
734734
Ok(alloc) => {

library/core/src/ptr/metadata.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
204204
/// Returns the size of the type associated with this vtable.
205205
#[inline]
206206
pub fn size_of(self) -> usize {
207+
// Note that "size stored in vtable" is *not* the same as "result of size_of_val_raw".
208+
// Consider a reference like `&(i32, dyn Send)`: the vtable will only store the size of the
209+
// `Send` part!
207210
#[cfg(bootstrap)]
208211
return self.vtable_ptr.size_of;
209212
#[cfg(not(bootstrap))]

0 commit comments

Comments
 (0)