Skip to content

Commit 2a40c5d

Browse files
committed
Avoid extra casts for "self" arguments
"self" is always passed as an opaque box, so there's no point in using the concrete self type when translating the argument. All it does it causing the value to be casted back to an opaque box right away.
1 parent 08a5278 commit 2a40c5d

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

src/librustc/middle/trans/callee.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,7 @@ pub fn trans_call_inner(in_cx: block,
615615
}
616616
Method(d) => {
617617
// Weird but true: we pass self in the *environment* slot!
618-
let llself = PointerCast(bcx,
619-
d.llself,
620-
Type::opaque_box(ccx).ptr_to());
621-
(d.llfn, llself)
618+
(d.llfn, d.llself)
622619
}
623620
Closure(d) => {
624621
// Closures are represented as (llfn, llclosure) pair:
@@ -944,10 +941,6 @@ pub fn trans_arg_expr(bcx: block,
944941
if formal_arg_ty != arg_datum.ty {
945942
// this could happen due to e.g. subtyping
946943
let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, &formal_arg_ty);
947-
let llformal_arg_ty = match self_mode {
948-
ty::ByRef => llformal_arg_ty.ptr_to(),
949-
ty::ByCopy => llformal_arg_ty,
950-
};
951944
debug!("casting actual type (%s) to match formal (%s)",
952945
bcx.val_to_str(val), bcx.llty_str(llformal_arg_ty));
953946
val = PointerCast(bcx, val, llformal_arg_ty);

src/librustc/middle/trans/meth.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ pub fn trans_self_arg(bcx: block,
133133
let _icx = push_ctxt("impl::trans_self_arg");
134134
let mut temp_cleanups = ~[];
135135

136-
// Compute the type of self.
137-
let self_ty = monomorphize_type(bcx, mentry.self_ty);
136+
// self is passed as an opaque box in the environment slot
137+
let self_ty = ty::mk_opaque_box(bcx.tcx());
138138
let result = trans_arg_expr(bcx,
139139
self_ty,
140140
mentry.self_mode,
@@ -576,7 +576,6 @@ pub fn trans_trait_callee_from_llval(bcx: block,
576576
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, abi::trt_field_box]));
577577

578578
// Munge `llself` appropriately for the type of `self` in the method.
579-
let self_mode;
580579
match explicit_self {
581580
ast::sty_static => {
582581
bcx.tcx().sess.bug("shouldn't see static method here");
@@ -597,12 +596,6 @@ pub fn trans_trait_callee_from_llval(bcx: block,
597596
llself = llbox;
598597
}
599598
}
600-
601-
let llscratch = alloca(bcx, val_ty(llself));
602-
Store(bcx, llself, llscratch);
603-
llself = llscratch;
604-
605-
self_mode = ty::ByRef;
606599
}
607600
ast::sty_box(_) => {
608601
// Bump the reference count on the box.
@@ -615,28 +608,20 @@ pub fn trans_trait_callee_from_llval(bcx: block,
615608
ty::BoxTraitStore => llself = llbox,
616609
_ => bcx.tcx().sess.bug("@self receiver with non-@Trait")
617610
}
618-
619-
let llscratch = alloca(bcx, val_ty(llself));
620-
Store(bcx, llself, llscratch);
621-
llself = llscratch;
622-
623-
self_mode = ty::ByRef;
624611
}
625612
ast::sty_uniq(_) => {
626613
// Pass the unique pointer.
627614
match store {
628615
ty::UniqTraitStore => llself = llbox,
629616
_ => bcx.tcx().sess.bug("~self receiver with non-~Trait")
630617
}
631-
632-
let llscratch = alloca(bcx, val_ty(llself));
633-
Store(bcx, llself, llscratch);
634-
llself = llscratch;
635-
636-
self_mode = ty::ByRef;
637618
}
638619
}
639620

621+
let llscratch = alloca(bcx, val_ty(llself));
622+
Store(bcx, llself, llscratch);
623+
llself = PointerCast(bcx, llscratch, Type::opaque_box(ccx).ptr_to());
624+
640625
// Load the function from the vtable and cast it to the expected type.
641626
debug!("(translating trait callee) loading method");
642627
let llcallee_ty = type_of_fn_from_ty(ccx, callee_ty);
@@ -652,7 +637,7 @@ pub fn trans_trait_callee_from_llval(bcx: block,
652637
llfn: mptr,
653638
llself: llself,
654639
self_ty: ty::mk_opaque_box(bcx.tcx()),
655-
self_mode: self_mode,
640+
self_mode: ty::ByRef,
656641
explicit_self: explicit_self
657642
/* XXX: Some(llbox) */
658643
})

0 commit comments

Comments
 (0)