Skip to content

Commit

Permalink
[MIR] Handle overloaded call expressions during HIR -> HAIR translation.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Jan 5, 2016
1 parent 5253294 commit 04b6c49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -46,6 +46,26 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
}
}

hir::ExprCall(ref fun, ref args) => {
if cx.tcx.is_method_call(self.id) {
// The callee is something implementing Fn, FnMut, or FnOnce.
// Find the actual method implementation being called and
// build the appropriate UFCS call expression with the
// callee-object as self parameter.

let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
let mut argrefs = vec![fun.to_ref()];
argrefs.extend(args.iter().map(|a| a.to_ref()));

ExprKind::Call {
fun: method.to_ref(),
args: argrefs,
}
} else {
ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() }
}
}

hir::ExprAddrOf(mutbl, ref expr) => {
let region = match expr_ty.sty {
ty::TyRef(r, _) => r,
Expand Down Expand Up @@ -328,8 +348,6 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
ExprKind::Vec { fields: fields.to_ref() },
hir::ExprTup(ref fields) =>
ExprKind::Tuple { fields: fields.to_ref() },
hir::ExprCall(ref fun, ref args) =>
ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() },
};

let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
Expand Down
17 changes: 17 additions & 0 deletions src/test/run-pass/mir_trans_calls.rs
Expand Up @@ -93,6 +93,19 @@ fn test8() -> isize {
Two::two()
}

#[rustc_mir]
fn test_fn_impl(f: &&Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
// This call goes through the Fn implementation for &Fn provided in
// core::ops::impls. It expands to a static Fn::call() that calls the
// Fn::call() implemenation of the object shim underneath.
f(x, y)
}

#[rustc_mir]
fn test_fn_object(f: &Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
f(x, y)
}

fn main() {
assert_eq!(test1(1, (2, 3), &[4, 5, 6]), (1, (2, 3), &[4, 5, 6][..]));
assert_eq!(test2(98), 98);
Expand All @@ -103,4 +116,8 @@ fn main() {
// assert_eq!(test6(&Foo, 12367), 12367);
assert_eq!(test7(), 1);
assert_eq!(test8(), 2);

let function_object = (&|x: i32, y: i32| { x + y }) as &Fn(i32, i32) -> i32;
assert_eq!(test_fn_object(function_object, 100, 1), 101);
assert_eq!(test_fn_impl(&function_object, 100, 2), 102);
}

0 comments on commit 04b6c49

Please sign in to comment.