Skip to content

Commit 76a1780

Browse files
committed
Refactor drop codegen
1 parent f8c5e10 commit 76a1780

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

src/abi.rs

+42-13
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ pub fn codegen_terminator_call<'a, 'tcx: 'a>(
679679
}
680680
}
681681

682-
pub fn codegen_call_inner<'a, 'tcx: 'a>(
682+
fn codegen_call_inner<'a, 'tcx: 'a>(
683683
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
684684
func: Option<&Operand<'tcx>>,
685685
fn_ty: Ty<'tcx>,
@@ -811,22 +811,51 @@ pub fn codegen_call_inner<'a, 'tcx: 'a>(
811811
pub fn codegen_drop<'a, 'tcx: 'a>(
812812
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
813813
drop_place: CPlace<'tcx>,
814-
drop_fn_ty: Ty<'tcx>,
815814
) {
816-
let (ptr, vtable) = drop_place.to_addr_maybe_unsized(fx);
817-
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
815+
let ty = drop_place.layout().ty;
816+
let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty);
818817

819-
let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &drop_fn_ty.fn_sig(fx.tcx));
818+
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
819+
// we don't actually need to drop anything
820+
} else {
821+
let drop_fn_ty = drop_fn.ty(fx.tcx);
822+
match ty.sty {
823+
ty::Dynamic(..) => {
824+
let (ptr, vtable) = drop_place.to_addr_maybe_unsized(fx);
825+
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
820826

821-
match get_pass_mode(fx.tcx, fx.layout_of(fn_sig.output())) {
822-
PassMode::NoPass => {}
823-
_ => unreachable!(),
824-
};
827+
let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &drop_fn_ty.fn_sig(fx.tcx));
825828

826-
let sig = fx
827-
.bcx
828-
.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig, true));
829-
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
829+
assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
830+
831+
let sig = fx
832+
.bcx
833+
.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig, true));
834+
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
835+
}
836+
_ => {
837+
let arg_place = CPlace::new_stack_slot(
838+
fx,
839+
fx.tcx.mk_ref(
840+
&ty::RegionKind::ReErased,
841+
TypeAndMut {
842+
ty,
843+
mutbl: crate::rustc::hir::Mutability::MutMutable,
844+
},
845+
),
846+
);
847+
drop_place.write_place_ref(fx, arg_place);
848+
let arg_value = arg_place.to_cvalue(fx);
849+
crate::abi::codegen_call_inner(
850+
fx,
851+
None,
852+
drop_fn_ty,
853+
vec![arg_value],
854+
None,
855+
);
856+
}
857+
}
858+
}
830859
}
831860

832861
pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {

src/base.rs

+2-36
Original file line numberDiff line numberDiff line change
@@ -286,42 +286,8 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
286286
target,
287287
unwind: _,
288288
} => {
289-
let ty = location.ty(fx.mir, fx.tcx).ty;
290-
let ty = fx.monomorphize(&ty);
291-
let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty);
292-
293-
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
294-
// we don't actually need to drop anything
295-
} else {
296-
let drop_place = trans_place(fx, location);
297-
let drop_fn_ty = drop_fn.ty(fx.tcx);
298-
match ty.sty {
299-
ty::Dynamic(..) => {
300-
crate::abi::codegen_drop(fx, drop_place, drop_fn_ty);
301-
}
302-
_ => {
303-
let arg_place = CPlace::new_stack_slot(
304-
fx,
305-
fx.tcx.mk_ref(
306-
&ty::RegionKind::ReErased,
307-
TypeAndMut {
308-
ty,
309-
mutbl: crate::rustc::hir::Mutability::MutMutable,
310-
},
311-
),
312-
);
313-
drop_place.write_place_ref(fx, arg_place);
314-
let arg_value = arg_place.to_cvalue(fx);
315-
crate::abi::codegen_call_inner(
316-
fx,
317-
None,
318-
drop_fn_ty,
319-
vec![arg_value],
320-
None,
321-
);
322-
}
323-
}
324-
}
289+
let drop_place = trans_place(fx, location);
290+
crate::abi::codegen_drop(fx, drop_place);
325291

326292
let target_ebb = fx.get_ebb(*target);
327293
fx.bcx.ins().jump(target_ebb, &[]);

0 commit comments

Comments
 (0)