Skip to content

Commit edc05ae

Browse files
committed
Improve public interface of CValue and CPlace
1 parent 174b73e commit edc05ae

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

src/abi/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ pub(crate) fn codegen_drop<'tcx>(
605605
// | ... |
606606
// \-------/
607607
//
608-
let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
608+
let (ptr, vtable) = drop_place.to_ptr_unsized();
609609
let ptr = ptr.get_addr(fx);
610-
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
610+
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable);
611611

612612
// FIXME(eddyb) perhaps move some of this logic into
613613
// `Instance::resolve_drop_in_place`?

src/base.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,11 @@ fn codegen_stmt<'tcx>(
694694
}
695695
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
696696
let operand = codegen_operand(fx, operand);
697-
operand.unsize_value(fx, lval);
697+
crate::unsize::coerce_unsized_into(fx, operand, lval);
698698
}
699699
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
700700
let operand = codegen_operand(fx, operand);
701-
operand.coerce_dyn_star(fx, lval);
701+
crate::unsize::coerce_dyn_star(fx, operand, lval);
702702
}
703703
Rvalue::Cast(CastKind::Transmute, ref operand, _to_ty) => {
704704
let operand = codegen_operand(fx, operand);
@@ -844,9 +844,7 @@ fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx
844844
let len = fx.monomorphize(len).eval_target_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
845845
fx.bcx.ins().iconst(fx.pointer_type, len)
846846
}
847-
ty::Slice(_elem_ty) => {
848-
place.to_ptr_maybe_unsized().1.expect("Length metadata for slice place")
849-
}
847+
ty::Slice(_elem_ty) => place.to_ptr_unsized().1,
850848
_ => bug!("Rvalue::Len({:?})", place),
851849
}
852850
}
@@ -900,8 +898,7 @@ pub(crate) fn codegen_place<'tcx>(
900898
ty::Slice(elem_ty) => {
901899
assert!(from_end, "slice subslices should be `from_end`");
902900
let elem_layout = fx.layout_of(*elem_ty);
903-
let (ptr, len) = cplace.to_ptr_maybe_unsized();
904-
let len = len.unwrap();
901+
let (ptr, len) = cplace.to_ptr_unsized();
905902
cplace = CPlace::for_ptr_with_extra(
906903
ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
907904
fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),

src/value_and_place.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,6 @@ impl<'tcx> CValue<'tcx> {
258258
}
259259
}
260260

261-
pub(crate) fn unsize_value(self, fx: &mut FunctionCx<'_, '_, 'tcx>, dest: CPlace<'tcx>) {
262-
crate::unsize::coerce_unsized_into(fx, self, dest);
263-
}
264-
265-
pub(crate) fn coerce_dyn_star(self, fx: &mut FunctionCx<'_, '_, 'tcx>, dest: CPlace<'tcx>) {
266-
crate::unsize::coerce_dyn_star(fx, self, dest);
267-
}
268-
269261
/// If `ty` is signed, `const_val` must already be sign extended.
270262
pub(crate) fn const_val(
271263
fx: &mut FunctionCx<'_, '_, 'tcx>,
@@ -454,18 +446,21 @@ impl<'tcx> CPlace<'tcx> {
454446

455447
#[track_caller]
456448
pub(crate) fn to_ptr(self) -> Pointer {
457-
match self.to_ptr_maybe_unsized() {
458-
(ptr, None) => ptr,
459-
(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
449+
match self.inner {
450+
CPlaceInner::Addr(ptr, None) => ptr,
451+
CPlaceInner::Addr(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
452+
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
453+
bug!("Expected CPlace::Addr, found {:?}", self)
454+
}
460455
}
461456
}
462457

463458
#[track_caller]
464-
pub(crate) fn to_ptr_maybe_unsized(self) -> (Pointer, Option<Value>) {
459+
pub(crate) fn to_ptr_unsized(self) -> (Pointer, Value) {
465460
match self.inner {
466-
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
467-
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
468-
bug!("Expected CPlace::Addr, found {:?}", self)
461+
CPlaceInner::Addr(ptr, Some(extra)) => (ptr, extra),
462+
CPlaceInner::Addr(_, None) | CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
463+
bug!("Expected unsized cplace, found {:?}", self)
469464
}
470465
}
471466
}
@@ -498,7 +493,7 @@ impl<'tcx> CPlace<'tcx> {
498493
from: CValue<'tcx>,
499494
method: &'static str,
500495
) {
501-
fn transmute_value<'tcx>(
496+
fn transmute_scalar<'tcx>(
502497
fx: &mut FunctionCx<'_, '_, 'tcx>,
503498
var: Variable,
504499
data: Value,
@@ -569,7 +564,7 @@ impl<'tcx> CPlace<'tcx> {
569564
CPlaceInner::Var(_local, var) => {
570565
let data = CValue(from.0, dst_layout).load_scalar(fx);
571566
let dst_ty = fx.clif_type(self.layout().ty).unwrap();
572-
transmute_value(fx, var, data, dst_ty);
567+
transmute_scalar(fx, var, data, dst_ty);
573568
}
574569
CPlaceInner::VarPair(_local, var1, var2) => {
575570
let (data1, data2) = if from.layout().ty == dst_layout.ty {
@@ -580,8 +575,8 @@ impl<'tcx> CPlace<'tcx> {
580575
CValue(CValueInner::ByRef(ptr, None), dst_layout).load_scalar_pair(fx)
581576
};
582577
let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap();
583-
transmute_value(fx, var1, data1, dst_ty1);
584-
transmute_value(fx, var2, data2, dst_ty2);
578+
transmute_scalar(fx, var1, data1, dst_ty1);
579+
transmute_scalar(fx, var2, data2, dst_ty2);
585580
}
586581
CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
587582
CPlaceInner::Addr(to_ptr, None) => {
@@ -666,7 +661,12 @@ impl<'tcx> CPlace<'tcx> {
666661
_ => {}
667662
}
668663

669-
let (base, extra) = self.to_ptr_maybe_unsized();
664+
let (base, extra) = match self.inner {
665+
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
666+
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => {
667+
bug!("Expected CPlace::Addr, found {:?}", self)
668+
}
669+
};
670670

671671
let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
672672
if field_layout.is_unsized() {
@@ -721,7 +721,7 @@ impl<'tcx> CPlace<'tcx> {
721721
| CPlaceInner::VarPair(_, _, _) => bug!("Can't index into {self:?}"),
722722
}
723723
}
724-
ty::Slice(elem_ty) => (fx.layout_of(*elem_ty), self.to_ptr_maybe_unsized().0),
724+
ty::Slice(elem_ty) => (fx.layout_of(*elem_ty), self.to_ptr_unsized().0),
725725
_ => bug!("place_index({:?})", self.layout().ty),
726726
};
727727

@@ -746,12 +746,8 @@ impl<'tcx> CPlace<'tcx> {
746746
layout: TyAndLayout<'tcx>,
747747
) -> CValue<'tcx> {
748748
if has_ptr_meta(fx.tcx, self.layout().ty) {
749-
let (ptr, extra) = self.to_ptr_maybe_unsized();
750-
CValue::by_val_pair(
751-
ptr.get_addr(fx),
752-
extra.expect("unsized type without metadata"),
753-
layout,
754-
)
749+
let (ptr, extra) = self.to_ptr_unsized();
750+
CValue::by_val_pair(ptr.get_addr(fx), extra, layout)
755751
} else {
756752
CValue::by_val(self.to_ptr().get_addr(fx), layout)
757753
}

0 commit comments

Comments
 (0)