Skip to content

Commit 04b6c49

Browse files
[MIR] Handle overloaded call expressions during HIR -> HAIR translation.
1 parent 5253294 commit 04b6c49

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
4646
}
4747
}
4848

49+
hir::ExprCall(ref fun, ref args) => {
50+
if cx.tcx.is_method_call(self.id) {
51+
// The callee is something implementing Fn, FnMut, or FnOnce.
52+
// Find the actual method implementation being called and
53+
// build the appropriate UFCS call expression with the
54+
// callee-object as self parameter.
55+
56+
let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
57+
let mut argrefs = vec![fun.to_ref()];
58+
argrefs.extend(args.iter().map(|a| a.to_ref()));
59+
60+
ExprKind::Call {
61+
fun: method.to_ref(),
62+
args: argrefs,
63+
}
64+
} else {
65+
ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() }
66+
}
67+
}
68+
4969
hir::ExprAddrOf(mutbl, ref expr) => {
5070
let region = match expr_ty.sty {
5171
ty::TyRef(r, _) => r,
@@ -328,8 +348,6 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
328348
ExprKind::Vec { fields: fields.to_ref() },
329349
hir::ExprTup(ref fields) =>
330350
ExprKind::Tuple { fields: fields.to_ref() },
331-
hir::ExprCall(ref fun, ref args) =>
332-
ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() },
333351
};
334352

335353
let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);

src/test/run-pass/mir_trans_calls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ fn test8() -> isize {
9393
Two::two()
9494
}
9595

96+
#[rustc_mir]
97+
fn test_fn_impl(f: &&Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
98+
// This call goes through the Fn implementation for &Fn provided in
99+
// core::ops::impls. It expands to a static Fn::call() that calls the
100+
// Fn::call() implemenation of the object shim underneath.
101+
f(x, y)
102+
}
103+
104+
#[rustc_mir]
105+
fn test_fn_object(f: &Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
106+
f(x, y)
107+
}
108+
96109
fn main() {
97110
assert_eq!(test1(1, (2, 3), &[4, 5, 6]), (1, (2, 3), &[4, 5, 6][..]));
98111
assert_eq!(test2(98), 98);
@@ -103,4 +116,8 @@ fn main() {
103116
// assert_eq!(test6(&Foo, 12367), 12367);
104117
assert_eq!(test7(), 1);
105118
assert_eq!(test8(), 2);
119+
120+
let function_object = (&|x: i32, y: i32| { x + y }) as &Fn(i32, i32) -> i32;
121+
assert_eq!(test_fn_object(function_object, 100, 1), 101);
122+
assert_eq!(test_fn_impl(&function_object, 100, 2), 102);
106123
}

0 commit comments

Comments
 (0)