about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-05-08 22:34:52 -0400
committerGitHub <noreply@github.com>2017-05-08 22:34:52 -0400
commit2f0deec98bf360af18bead78b2d8092ba4aa6c50 (patch)
treee6a811679fe233fe1d9d51d533f60b2878294ff2
parent7f3070315597917c65f511a055c2b56451283eb1 (diff)
parent82e07363916146f0d6e01199ef30544e7fdd6248 (diff)
Rollup merge of #41838 - z1mvader:fix_fn_args_coerce_closure, r=nikomatsakis
Fixed argument inference for closures when coercing into 'fn'

This fixes https://github.com/rust-lang/rust/issues/41755. The tests  `compile-fail/closure-no-fn.rs` and `compile-fail/issue-40000.rs` were modified. A new test `run-pass/closure_to_fn_coercion-expected-types.rs` was added

r? @nikomatsakis
-rw-r--r--src/librustc_typeck/check/closure.rs1
-rw-r--r--src/test/compile-fail/closure-no-fn-1.rs (renamed from src/test/compile-fail/closure-no-fn.rs)6
-rw-r--r--src/test/compile-fail/closure-no-fn-2.rs18
-rw-r--r--src/test/compile-fail/closure-no-fn-3.rs18
-rw-r--r--src/test/compile-fail/issue-40000.rs3
-rw-r--r--src/test/run-pass/closure_to_fn_coercion-expected-types.rs17
6 files changed, 55 insertions, 8 deletions
diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs
index 45b0a571bd0..fb3be849319 100644
--- a/src/librustc_typeck/check/closure.rs
+++ b/src/librustc_typeck/check/closure.rs
@@ -126,6 +126,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                 (sig, kind)
             }
             ty::TyInfer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
+            ty::TyFnPtr(sig) => (Some(sig.skip_binder().clone()), Some(ty::ClosureKind::Fn)),
             _ => (None, None),
         }
     }
diff --git a/src/test/compile-fail/closure-no-fn.rs b/src/test/compile-fail/closure-no-fn-1.rs
index fe179e8a48f..10c99703a97 100644
--- a/src/test/compile-fail/closure-no-fn.rs
+++ b/src/test/compile-fail/closure-no-fn-1.rs
@@ -15,10 +15,4 @@ fn main() {
     let mut a = 0u8;
     let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
     //~^ ERROR mismatched types
-    let b = 0u8;
-    let bar: fn() -> u8 = || { b };
-    //~^ ERROR mismatched types
-    let baz: fn() -> u8 = || { b } as fn() -> u8;
-    //~^ ERROR mismatched types
-    //~^^ ERROR non-scalar cast
 }
diff --git a/src/test/compile-fail/closure-no-fn-2.rs b/src/test/compile-fail/closure-no-fn-2.rs
new file mode 100644
index 00000000000..a6438bb5f24
--- /dev/null
+++ b/src/test/compile-fail/closure-no-fn-2.rs
@@ -0,0 +1,18 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Ensure that capturing closures are never coerced to fns
+// Especially interesting as non-capturing closures can be.
+
+fn main() {
+    let b = 0u8;
+    let bar: fn() -> u8 = || { b };
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/compile-fail/closure-no-fn-3.rs b/src/test/compile-fail/closure-no-fn-3.rs
new file mode 100644
index 00000000000..85dbc899208
--- /dev/null
+++ b/src/test/compile-fail/closure-no-fn-3.rs
@@ -0,0 +1,18 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Ensure that capturing closures are never coerced to fns
+// Especially interesting as non-capturing closures can be.
+
+fn main() {
+    let b = 0u8;
+    let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
+    //~^ ERROR non-scalar cast
+}
diff --git a/src/test/compile-fail/issue-40000.rs b/src/test/compile-fail/issue-40000.rs
index 9be114ebcb6..3ccee0f12be 100644
--- a/src/test/compile-fail/issue-40000.rs
+++ b/src/test/compile-fail/issue-40000.rs
@@ -11,8 +11,7 @@
 #![feature(closure_to_fn_coercion)]
 
 fn main() {
-    let bar: fn(&mut u32) = |_| {}; //~ ERROR mismatched types
-    //~| expected concrete lifetime, found bound lifetime parameter
+    let bar: fn(&mut u32) = |_| {};
 
     fn foo(x: Box<Fn(&i32)>) {}
     let bar = Box::new(|x: &i32| {}) as Box<Fn(_)>;
diff --git a/src/test/run-pass/closure_to_fn_coercion-expected-types.rs b/src/test/run-pass/closure_to_fn_coercion-expected-types.rs
new file mode 100644
index 00000000000..7214ebfaf07
--- /dev/null
+++ b/src/test/run-pass/closure_to_fn_coercion-expected-types.rs
@@ -0,0 +1,17 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+// Ensure that we deduce expected argument types when a `fn()` type is expected (#41755)
+
+#![feature(closure_to_fn_coercion)]
+fn foo(f: fn(Vec<u32>) -> usize) { }
+
+fn main() {
+    foo(|x| x.len())
+}