Skip to content

Commit

Permalink
Add comments explaining how codegen works for dyn Trait methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeyhew committed Nov 1, 2018
1 parent a468da9 commit 192e7c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/librustc_codegen_llvm/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
_ => bug!("receiver type has unsupported layout: {:?}", layout)
}

// In the case of Rc<Self>, we need to explicitly pass a *mut RcBox<Self>
// with a Scalar (not ScalarPair) ABI. This is a hack that is understood
// elsewhere in the compiler as a method on a `dyn Trait`.
// To get the type `*mut RcBox<Self>`, we just keep unwrapping newtypes until we
// get a built-in pointer type
let mut fat_pointer_layout = layout;
'descend_newtypes: while !fat_pointer_layout.ty.is_unsafe_ptr()
&& !fat_pointer_layout.ty.is_region_ptr()
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_codegen_llvm/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,12 @@ impl FunctionCx<'a, 'll, 'tcx> {

if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) {
if let Pair(..) = op.val {
// descend through newtype wrappers until `op` is a builtin pointer to
// `dyn Trait`, e.g. `*const dyn Trait`, `&mut dyn Trait`
// In the case of Rc<Self>, we need to explicitly pass a
// *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
// that is understood elsewhere in the compiler as a method on
// `dyn Trait`.
// To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
// we get a value of a built-in pointer type
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
&& !op.layout.ty.is_region_ptr()
{
Expand Down

0 comments on commit 192e7c4

Please sign in to comment.