Skip to content

Commit

Permalink
Rollup merge of rust-lang#41838 - z1mvader:fix_fn_args_coerce_closure…
Browse files Browse the repository at this point in the history
…, r=nikomatsakis

Fixed argument inference for closures when coercing into 'fn'

This fixes rust-lang#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
  • Loading branch information
frewsxcv authored May 9, 2017
2 parents 7f30703 + 82e0736 commit 2f0deec
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/librustc_typeck/check/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions src/test/compile-fail/closure-no-fn-2.rs
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions src/test/compile-fail/closure-no-fn-3.rs
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions src/test/compile-fail/issue-40000.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)>;
Expand Down
17 changes: 17 additions & 0 deletions src/test/run-pass/closure_to_fn_coercion-expected-types.rs
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 2f0deec

Please sign in to comment.