}
}
+ 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,
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);
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);
// 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);
}