]> git.lizzy.rs Git - rust.git/commitdiff
[MIR] Handle overloaded call expressions during HIR -> HAIR translation.
authorMichael Woerister <michaelwoerister@posteo.net>
Mon, 4 Jan 2016 11:08:11 +0000 (06:08 -0500)
committerMichael Woerister <michaelwoerister@posteo.net>
Tue, 5 Jan 2016 17:40:35 +0000 (12:40 -0500)
src/librustc_mir/hair/cx/expr.rs
src/test/run-pass/mir_trans_calls.rs

index ce78a39d599a518d6bd4cf6d2600f249bc61f11b..aa6257345fe445a31a147ce12cc2619315306826 100644 (file)
@@ -46,6 +46,26 @@ fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
                 }
             }
 
+            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,
@@ -328,8 +348,6 @@ fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
                 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);
index cf3d3d0720bcc4a6e7e157d569cf071818ab7ff4..1ce0618daf36ab1258c5c25b31ebe91ac9a5c124 100644 (file)
@@ -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);
@@ -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);
 }