Skip to content

Commit 234e0f2

Browse files
authored
Rollup merge of #100887 - eholk:codegen_call_terminator-cleanup, r=fee1-dead
Refactor part of codegen_call_terminator I was reading through this code and found the chain of `if let` and a nested match on the same value that was matched in the `if let` to be kind of hard to follow. This PR cleans it up by flattening the `if let` chain and nested match into a single `match` expression.
2 parents 8332f65 + b562f95 commit 234e0f2

File tree

1 file changed

+44
-47
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+44
-47
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+44-47
Original file line numberDiff line numberDiff line change
@@ -798,58 +798,55 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
798798
let mut op = self.codegen_operand(&mut bx, arg);
799799

800800
if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) {
801-
if let Pair(..) = op.val {
802-
// In the case of Rc<Self>, we need to explicitly pass a
803-
// *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
804-
// that is understood elsewhere in the compiler as a method on
805-
// `dyn Trait`.
806-
// To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
807-
// we get a value of a built-in pointer type
808-
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
809-
&& !op.layout.ty.is_region_ptr()
810-
{
811-
for i in 0..op.layout.fields.count() {
812-
let field = op.extract_field(&mut bx, i);
813-
if !field.layout.is_zst() {
814-
// we found the one non-zero-sized field that is allowed
815-
// now find *its* non-zero-sized field, or stop if it's a
816-
// pointer
817-
op = field;
818-
continue 'descend_newtypes;
801+
match op.val {
802+
Pair(data_ptr, meta) => {
803+
// In the case of Rc<Self>, we need to explicitly pass a
804+
// *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
805+
// that is understood elsewhere in the compiler as a method on
806+
// `dyn Trait`.
807+
// To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
808+
// we get a value of a built-in pointer type
809+
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
810+
&& !op.layout.ty.is_region_ptr()
811+
{
812+
for i in 0..op.layout.fields.count() {
813+
let field = op.extract_field(&mut bx, i);
814+
if !field.layout.is_zst() {
815+
// we found the one non-zero-sized field that is allowed
816+
// now find *its* non-zero-sized field, or stop if it's a
817+
// pointer
818+
op = field;
819+
continue 'descend_newtypes;
820+
}
819821
}
822+
823+
span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
820824
}
821825

822-
span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
826+
// now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
827+
// data pointer and vtable. Look up the method in the vtable, and pass
828+
// the data pointer as the first argument
829+
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
830+
&mut bx,
831+
meta,
832+
op.layout.ty,
833+
&fn_abi,
834+
));
835+
llargs.push(data_ptr);
836+
continue 'make_args;
823837
}
824-
825-
// now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
826-
// data pointer and vtable. Look up the method in the vtable, and pass
827-
// the data pointer as the first argument
828-
match op.val {
829-
Pair(data_ptr, meta) => {
830-
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
831-
&mut bx,
832-
meta,
833-
op.layout.ty,
834-
&fn_abi,
835-
));
836-
llargs.push(data_ptr);
837-
continue 'make_args;
838-
}
839-
other => bug!("expected a Pair, got {:?}", other),
838+
Ref(data_ptr, Some(meta), _) => {
839+
// by-value dynamic dispatch
840+
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
841+
&mut bx,
842+
meta,
843+
op.layout.ty,
844+
&fn_abi,
845+
));
846+
llargs.push(data_ptr);
847+
continue;
840848
}
841-
} else if let Ref(data_ptr, Some(meta), _) = op.val {
842-
// by-value dynamic dispatch
843-
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
844-
&mut bx,
845-
meta,
846-
op.layout.ty,
847-
&fn_abi,
848-
));
849-
llargs.push(data_ptr);
850-
continue;
851-
} else {
852-
span_bug!(span, "can't codegen a virtual call on {:?}", op);
849+
_ => span_bug!(span, "can't codegen a virtual call on {:?}", op),
853850
}
854851
}
855852

0 commit comments

Comments
 (0)