Skip to content

Commit 2f0deec

Browse files
authored
Rollup merge of #41838 - z1mvader:fix_fn_args_coerce_closure, r=nikomatsakis
Fixed argument inference for closures when coercing into 'fn' This fixes #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
2 parents 7f30703 + 82e0736 commit 2f0deec

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

src/librustc_typeck/check/closure.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
126126
(sig, kind)
127127
}
128128
ty::TyInfer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
129+
ty::TyFnPtr(sig) => (Some(sig.skip_binder().clone()), Some(ty::ClosureKind::Fn)),
129130
_ => (None, None),
130131
}
131132
}

src/test/compile-fail/closure-no-fn.rs renamed to src/test/compile-fail/closure-no-fn-1.rs

-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,4 @@ fn main() {
1515
let mut a = 0u8;
1616
let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
1717
//~^ ERROR mismatched types
18-
let b = 0u8;
19-
let bar: fn() -> u8 = || { b };
20-
//~^ ERROR mismatched types
21-
let baz: fn() -> u8 = || { b } as fn() -> u8;
22-
//~^ ERROR mismatched types
23-
//~^^ ERROR non-scalar cast
2418
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Ensure that capturing closures are never coerced to fns
12+
// Especially interesting as non-capturing closures can be.
13+
14+
fn main() {
15+
let b = 0u8;
16+
let bar: fn() -> u8 = || { b };
17+
//~^ ERROR mismatched types
18+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Ensure that capturing closures are never coerced to fns
12+
// Especially interesting as non-capturing closures can be.
13+
14+
fn main() {
15+
let b = 0u8;
16+
let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
17+
//~^ ERROR non-scalar cast
18+
}

src/test/compile-fail/issue-40000.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#![feature(closure_to_fn_coercion)]
1212

1313
fn main() {
14-
let bar: fn(&mut u32) = |_| {}; //~ ERROR mismatched types
15-
//~| expected concrete lifetime, found bound lifetime parameter
14+
let bar: fn(&mut u32) = |_| {};
1615

1716
fn foo(x: Box<Fn(&i32)>) {}
1817
let bar = Box::new(|x: &i32| {}) as Box<Fn(_)>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
// Ensure that we deduce expected argument types when a `fn()` type is expected (#41755)
11+
12+
#![feature(closure_to_fn_coercion)]
13+
fn foo(f: fn(Vec<u32>) -> usize) { }
14+
15+
fn main() {
16+
foo(|x| x.len())
17+
}

0 commit comments

Comments
 (0)