Skip to content

Commit b51f44e

Browse files
committed
Fix passing self by value for types passed by value
For types that are passed by value, we can't just cast the value to a pointer, but have to use an alloca and copy the value there. This handling is already present for all other arguments, but was missing for "self". Fixes #6682, #4850 and #4878
1 parent c354a0c commit b51f44e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/librustc/middle/trans/base.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,15 @@ pub fn copy_args_to_allocas(fcx: fn_ctxt,
17301730
// We really should do this regardless of whether self is owned, but
17311731
// it doesn't work right with default method impls yet. (FIXME: #2794)
17321732
if slf.is_owned {
1733-
let self_val = PointerCast(bcx, slf.v,
1734-
T_ptr(type_of(bcx.ccx(), slf.t)));
1733+
let self_val = if datum::appropriate_mode(slf.t).is_by_value() {
1734+
let tmp = BitCast(bcx, slf.v, type_of(bcx.ccx(), slf.t));
1735+
let alloc = alloc_ty(bcx, slf.t);
1736+
Store(bcx, tmp, alloc);
1737+
alloc
1738+
} else {
1739+
PointerCast(bcx, slf.v, T_ptr(type_of(bcx.ccx(), slf.t)))
1740+
};
1741+
17351742
fcx.llself = Some(ValSelfData {v: self_val, ..slf});
17361743
add_clean(bcx, self_val, slf.t);
17371744
}

0 commit comments

Comments
 (0)