Skip to content

Commit 7f798c2

Browse files
Emit the right types for vtable pointers when dropping dyn*
1 parent e82cc65 commit 7f798c2

File tree

1 file changed

+78
-80
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+78
-80
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+78-80
Original file line numberDiff line numberDiff line change
@@ -452,86 +452,84 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
452452
args1 = [place.llval];
453453
&args1[..]
454454
};
455-
let (drop_fn, fn_abi) = match ty.kind() {
456-
// FIXME(eddyb) perhaps move some of this logic into
457-
// `Instance::resolve_drop_in_place`?
458-
ty::Dynamic(_, _, ty::Dyn) => {
459-
// IN THIS ARM, WE HAVE:
460-
// ty = *mut (dyn Trait)
461-
// which is: exists<T> ( *mut T, Vtable<T: Trait> )
462-
// args[0] args[1]
463-
//
464-
// args = ( Data, Vtable )
465-
// |
466-
// v
467-
// /-------\
468-
// | ... |
469-
// \-------/
470-
//
471-
let virtual_drop = Instance {
472-
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
473-
substs: drop_fn.substs,
474-
};
475-
debug!("ty = {:?}", ty);
476-
debug!("drop_fn = {:?}", drop_fn);
477-
debug!("args = {:?}", args);
478-
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
479-
let vtable = args[1];
480-
// Truncate vtable off of args list
481-
args = &args[..1];
482-
(
483-
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
484-
.get_fn(bx, vtable, ty, &fn_abi),
485-
fn_abi,
486-
)
487-
}
488-
ty::Dynamic(_, _, ty::DynStar) => {
489-
// IN THIS ARM, WE HAVE:
490-
// ty = *mut (dyn* Trait)
491-
// which is: *mut exists<T: sizeof(T) == sizeof(usize)> (T, Vtable<T: Trait>)
492-
//
493-
// args = [ * ]
494-
// |
495-
// v
496-
// ( Data, Vtable )
497-
// |
498-
// v
499-
// /-------\
500-
// | ... |
501-
// \-------/
502-
//
503-
//
504-
// WE CAN CONVERT THIS INTO THE ABOVE LOGIC BY DOING
505-
//
506-
// data = &(*args[0]).0 // gives a pointer to Data above (really the same pointer)
507-
// vtable = (*args[0]).1 // loads the vtable out
508-
// (data, vtable) // an equivalent Rust `*mut dyn Trait`
509-
//
510-
// SO THEN WE CAN USE THE ABOVE CODE.
511-
let virtual_drop = Instance {
512-
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
513-
substs: drop_fn.substs,
514-
};
515-
debug!("ty = {:?}", ty);
516-
debug!("drop_fn = {:?}", drop_fn);
517-
debug!("args = {:?}", args);
518-
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
519-
let data = args[0];
520-
let data_ty = bx.cx().backend_type(place.layout);
521-
let vtable_ptr =
522-
bx.gep(data_ty, data, &[bx.cx().const_i32(0), bx.cx().const_i32(1)]);
523-
let vtable = bx.load(bx.type_i8p(), vtable_ptr, abi::Align::ONE);
524-
// Truncate vtable off of args list
525-
args = &args[..1];
526-
debug!("args' = {:?}", args);
527-
(
528-
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
529-
.get_fn(bx, vtable, ty, &fn_abi),
530-
fn_abi,
531-
)
532-
}
533-
_ => (bx.get_fn_addr(drop_fn), bx.fn_abi_of_instance(drop_fn, ty::List::empty())),
534-
};
455+
let (drop_fn, fn_abi) =
456+
match ty.kind() {
457+
// FIXME(eddyb) perhaps move some of this logic into
458+
// `Instance::resolve_drop_in_place`?
459+
ty::Dynamic(_, _, ty::Dyn) => {
460+
// IN THIS ARM, WE HAVE:
461+
// ty = *mut (dyn Trait)
462+
// which is: exists<T> ( *mut T, Vtable<T: Trait> )
463+
// args[0] args[1]
464+
//
465+
// args = ( Data, Vtable )
466+
// |
467+
// v
468+
// /-------\
469+
// | ... |
470+
// \-------/
471+
//
472+
let virtual_drop = Instance {
473+
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
474+
substs: drop_fn.substs,
475+
};
476+
debug!("ty = {:?}", ty);
477+
debug!("drop_fn = {:?}", drop_fn);
478+
debug!("args = {:?}", args);
479+
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
480+
let vtable = args[1];
481+
// Truncate vtable off of args list
482+
args = &args[..1];
483+
(
484+
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
485+
.get_fn(bx, vtable, ty, &fn_abi),
486+
fn_abi,
487+
)
488+
}
489+
ty::Dynamic(_, _, ty::DynStar) => {
490+
// IN THIS ARM, WE HAVE:
491+
// ty = *mut (dyn* Trait)
492+
// which is: *mut exists<T: sizeof(T) == sizeof(usize)> (T, Vtable<T: Trait>)
493+
//
494+
// args = [ * ]
495+
// |
496+
// v
497+
// ( Data, Vtable )
498+
// |
499+
// v
500+
// /-------\
501+
// | ... |
502+
// \-------/
503+
//
504+
//
505+
// WE CAN CONVERT THIS INTO THE ABOVE LOGIC BY DOING
506+
//
507+
// data = &(*args[0]).0 // gives a pointer to Data above (really the same pointer)
508+
// vtable = (*args[0]).1 // loads the vtable out
509+
// (data, vtable) // an equivalent Rust `*mut dyn Trait`
510+
//
511+
// SO THEN WE CAN USE THE ABOVE CODE.
512+
let virtual_drop = Instance {
513+
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
514+
substs: drop_fn.substs,
515+
};
516+
debug!("ty = {:?}", ty);
517+
debug!("drop_fn = {:?}", drop_fn);
518+
debug!("args = {:?}", args);
519+
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
520+
let meta_ptr = place.project_field(bx, 1);
521+
let meta = bx.load_operand(meta_ptr);
522+
// Truncate vtable off of args list
523+
args = &args[..1];
524+
debug!("args' = {:?}", args);
525+
(
526+
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
527+
.get_fn(bx, meta.immediate(), ty, &fn_abi),
528+
fn_abi,
529+
)
530+
}
531+
_ => (bx.get_fn_addr(drop_fn), bx.fn_abi_of_instance(drop_fn, ty::List::empty())),
532+
};
535533
helper.do_call(
536534
self,
537535
bx,

0 commit comments

Comments
 (0)