Skip to content

Commit c98814b

Browse files
nikomatsakisJorge Aparicio
authored and
Jorge Aparicio
committed
Correctly "detuple" arguments when creating trait object shims for a trait method with rust-call ABI.
1 parent f97b124 commit c98814b

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

src/librustc_trans/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
100100

101101
fn datum_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
102102
-> Callee<'blk, 'tcx> {
103-
let DatumBlock { mut bcx, datum, .. } = expr::trans(bcx, expr);
103+
let DatumBlock { bcx, datum, .. } = expr::trans(bcx, expr);
104104
match datum.ty.sty {
105105
ty::ty_bare_fn(..) => {
106106
let llval = datum.to_llscalarish(bcx);

src/librustc_trans/trans/meth.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,27 @@ pub fn trans_object_shim<'a, 'tcx>(
599599
bcx.val_to_string(llobject));
600600

601601
// the remaining arguments will be, well, whatever they are
602+
let input_tys =
603+
match fty.abi {
604+
RustCall => {
605+
// unpack the tuple to extract the input type arguments:
606+
match fty.sig.0.inputs[1].sty {
607+
ty::ty_tup(ref tys) => tys.as_slice(),
608+
_ => {
609+
bcx.sess().bug(
610+
format!("rust-call expects a tuple not {}",
611+
fty.sig.0.inputs[1].repr(tcx)).as_slice());
612+
}
613+
}
614+
}
615+
_ => {
616+
// skip the self parameter:
617+
fty.sig.0.inputs.slice_from(1)
618+
}
619+
};
620+
602621
let llargs: Vec<_> =
603-
fty.sig.0.inputs[1..].iter()
622+
input_tys.iter()
604623
.enumerate()
605624
.map(|(i, _)| {
606625
let llarg = get_param(fcx.llfn, fcx.arg_pos(i+1) as u32);
@@ -609,6 +628,7 @@ pub fn trans_object_shim<'a, 'tcx>(
609628
llarg
610629
})
611630
.collect();
631+
612632
assert!(!fcx.needs_ret_allocas);
613633

614634
let dest =
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests calls to closure arguments where the closure takes 1 argument.
12+
// This is a bit tricky due to rust-call ABI.
13+
14+
fn foo(f: &mut FnMut(int) -> int) -> int {
15+
f(22)
16+
}
17+
18+
fn main() {
19+
let z = foo(&mut |x| x *100);
20+
assert_eq!(z, 2200);
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests calls to closure arguments where the closure takes 2 arguments.
12+
// This is a bit tricky due to rust-call ABI.
13+
14+
fn foo(f: &mut FnMut(int, int) -> int) -> int {
15+
f(1, 2)
16+
}
17+
18+
fn main() {
19+
let z = foo(&mut |x, y| x * 10 + y);
20+
assert_eq!(z, 12);
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests calls to closure arguments where the closure takes 0 arguments.
12+
// This is a bit tricky due to rust-call ABI.
13+
14+
fn foo(f: &mut FnMut()) -> int {
15+
f()
16+
}
17+
18+
fn main() {
19+
let z = foo(|| 22);
20+
assert_eq!(z, 22);
21+
}

0 commit comments

Comments
 (0)