new rules for merging expected/supplied types in closure signatures

Also, fix numbering in mir-opt tests. We are now anonymizing more
consistently, I think, and hence some of the `TyAnon` indices shifted.
This commit is contained in:
Niko Matsakis 2017-10-06 06:06:38 -04:00
parent ea4db3521e
commit 053383dbef
18 changed files with 649 additions and 71 deletions

View file

@ -0,0 +1 @@
See `src/test/run-pass/closure-expected-type`.

View file

@ -0,0 +1,49 @@
// Copyright 2016 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.
// must-compile-successfully
#![feature(underscore_lifetimes)]
#![allow(warnings)]
type Different<'a, 'b> = &'a mut (&'a (), &'b ());
type Same<'a> = Different<'a, 'a>;
fn with_closure_expecting_different<F>(_: F)
where F: for<'a, 'b> FnOnce(Different<'a, 'b>)
{
}
fn with_closure_expecting_different_anon<F>(_: F)
where F: FnOnce(Different<'_, '_>)
{
}
fn supplying_nothing_expecting_anon() {
with_closure_expecting_different_anon(|x: Different| {
})
}
fn supplying_nothing_expecting_named() {
with_closure_expecting_different(|x: Different| {
})
}
fn supplying_underscore_expecting_anon() {
with_closure_expecting_different_anon(|x: Different<'_, '_>| {
})
}
fn supplying_underscore_expecting_named() {
with_closure_expecting_different(|x: Different<'_, '_>| {
})
}
fn main() { }

View file

@ -0,0 +1,70 @@
// Copyright 2016 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.
#![feature(underscore_lifetimes)]
fn with_closure_expecting_fn_with_free_region<F>(_: F)
where F: for<'a> FnOnce(fn(&'a u32), &i32)
{
}
fn with_closure_expecting_fn_with_bound_region<F>(_: F)
where F: FnOnce(fn(&u32), &i32)
{
}
fn expect_free_supply_free_from_fn<'x>(x: &'x u32) {
// Here, the type given for `'x` "obscures" a region from the
// expected signature that is bound at closure level.
with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
//~^ ERROR mismatched types
//~| ERROR mismatched types
}
fn expect_free_supply_free_from_closure() {
// A variant on the previous test. Here, the region `'a` will be
// bound at the closure level, just as is expected, so no error
// results.
type Foo<'a> = fn(&'a u32);
with_closure_expecting_fn_with_free_region(|_x: Foo<'_>, y| {});
}
fn expect_free_supply_bound() {
// Here, we are given a function whose region is bound at closure level,
// but we expect one bound in the argument. Error results.
with_closure_expecting_fn_with_free_region(|x: fn(&u32), y| {});
//~^ ERROR mismatched types
}
fn expect_bound_supply_free_from_fn<'x>(x: &'x u32) {
// Here, we are given a `fn(&u32)` but we expect a `fn(&'x
// u32)`. In principle, this could be ok, but we demand equality.
with_closure_expecting_fn_with_bound_region(|x: fn(&'x u32), y| {});
//~^ ERROR mismatched types
}
fn expect_bound_supply_free_from_closure() {
// A variant on the previous test. Here, the region `'a` will be
// bound at the closure level, but we expect something bound at
// the argument level.
type Foo<'a> = fn(&'a u32);
with_closure_expecting_fn_with_bound_region(|_x: Foo<'_>, y| {});
//~^ ERROR mismatched types
}
fn expect_bound_supply_bound<'x>(x: &'x u32) {
// No error in this case. The supplied type supplies the bound
// regions, and hence we are able to figure out the type of `y`
// from the expected type
with_closure_expecting_fn_with_bound_region(|x: for<'z> fn(&'z u32), y| {
});
}
fn main() { }

View file

@ -0,0 +1,35 @@
// Copyright 2016 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.
fn with_closure<F, A>(_: F)
where F: FnOnce(A, A)
{
}
fn a() {
with_closure(|x: u32, y| {
// We deduce type of `y` from `x`.
});
}
fn b() {
// Here we take the supplied types, resulting in an error later on.
with_closure(|x: u32, y: i32| {
//~^ ERROR mismatched types
});
}
fn c() {
with_closure(|x, y: i32| {
// We deduce type of `x` from `y`.
});
}
fn main() { }

View file

@ -0,0 +1,29 @@
// Copyright 2016 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.
// must-compile-successfully
fn with_closure<F, A>(_: F)
where F: FnOnce(A, &u32)
{
}
fn foo() {
// This version works; we infer `A` to be `u32`, and take the type
// of `y` to be `&u32`.
with_closure(|x: u32, y| {});
}
fn bar() {
// This version also works.
with_closure(|x: &u32, y| {});
}
fn main() { }

View file

@ -0,0 +1,29 @@
// Copyright 2016 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.
// must-compile-successfully
fn with_closure<F, A>(_: F)
where F: FnOnce(A, &u32)
{
}
fn foo() {
// This version works; we infer `A` to be `u32`, and take the type
// of `y` to be `&u32`.
with_closure(|x: u32, y| {});
}
fn bar<'x>(x: &'x u32) {
// Same.
with_closure(|x: &'x u32, y| {});
}
fn main() { }

View file

@ -0,0 +1,80 @@
// Copyright 2016 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.
#![allow(warnings)]
fn closure_expecting_bound<F>(_: F)
where F: FnOnce(&u32)
{
}
fn closure_expecting_free<'a, F>(_: F)
where F: FnOnce(&'a u32)
{
}
fn expect_bound_supply_nothing() {
// Because `x` is inferred to have a bound region, we cannot allow
// it to escape into `f`:
let mut f: Option<&u32> = None;
closure_expecting_bound(|x| {
f = Some(x); //~ ERROR E0495
});
}
fn expect_bound_supply_bound() {
// Because `x` is inferred to have a bound region, we cannot allow
// it to escape into `f`, even with an explicit type annotation on
// closure:
let mut f: Option<&u32> = None;
closure_expecting_bound(|x: &u32| {
f = Some(x); //~ ERROR E0495
});
}
fn expect_bound_supply_named<'x>() {
let mut f: Option<&u32> = None;
// Here we give a type annotation that `x` should be free. We get
// an error because of that.
closure_expecting_bound(|x: &'x u32| {
//~^ ERROR mismatched types
//~| ERROR mismatched types
// And we still cannot let `x` escape into `f`.
f = Some(x);
//~^ ERROR cannot infer
});
}
fn expect_free_supply_nothing() {
let mut f: Option<&u32> = None;
closure_expecting_free(|x| f = Some(x)); // OK
}
fn expect_free_supply_bound() {
let mut f: Option<&u32> = None;
// Here, even though the annotation `&u32` could be seen as being
// bound in the closure, we permit it to be defined as a free
// region (which is inferred to something in the fn body).
closure_expecting_free(|x: &u32| f = Some(x)); // OK
}
fn expect_free_supply_named<'x>() {
let mut f: Option<&u32> = None;
// Here, even though the annotation `&u32` could be seen as being
// bound in the closure, we permit it to be defined as a free
// region (which is inferred to something in the fn body).
closure_expecting_free(|x: &'x u32| f = Some(x)); // OK
}
fn main() { }

View file

@ -0,0 +1,29 @@
// Copyright 2016 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.
fn with_closure<F, A, B>(_: F)
where F: FnOnce(A, B)
{
}
fn a() {
// Type of `y` is unconstrained.
with_closure(|x: u32, y| {}); //~ ERROR E0282
}
fn b() {
with_closure(|x: u32, y: u32| {}); // OK
}
fn c() {
with_closure(|x: u32, y: u32| {}); // OK
}
fn main() { }

View file

@ -62,7 +62,7 @@ fn main() {
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(50)], _2: &ReErased mut i32) -> i32 {
// ...
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:11) => validate_1[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(50)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:11) => validate_1[317d]::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:11) => validate_1[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(50)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:11) => validate_1[317d]::main[0]::{{closure}}[0] }, BrAnon(0)) mut i32]);
// StorageLive(_3);
// Validate(Suspend(ReScope(Remainder(BlockRemainder { block: ItemLocalId(22), first_statement_index: 0 }))), [(*_2): i32]);
// _3 = &ReErased (*_2);

View file

@ -78,8 +78,8 @@ fn main() {
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(60)], _2: &ReErased mut i32) -> bool {
// ...
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// Validate(Release, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrAnon(0)) mut i32]);
// Validate(Release, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:10) => validate_4[317d]::main[0]::{{closure}}[0] }, BrAnon(0)) mut i32]);
// StorageLive(_3);
// ...
// _0 = const write_42(_3) -> bb1;

View file

@ -49,7 +49,7 @@ fn main() {
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(46)], _2: &ReErased mut i32) -> bool {
// ...
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:9) => validate_5[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(46)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:9) => validate_5[317d]::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:9) => validate_5[317d]::main[0]::{{closure}}[0] }, BrEnv) [closure@NodeId(46)], _2: &ReFree(DefId { krate: CrateNum(0), index: DefIndex(1:9) => validate_5[317d]::main[0]::{{closure}}[0] }, BrAnon(0)) mut i32]);
// StorageLive(_3);
// StorageLive(_4);
// Validate(Suspend(ReScope(Node(ItemLocalId(9)))), [(*_2): i32]);

View file

@ -0,0 +1,8 @@
Some tests targeted at how we deduce the types of closure arguments.
This process is a result of some heuristics aimed at combining the
*expected type* we have with the *actual types* that we get from
inputs. This investigation was kicked off by #38714, which revealed
some pretty deep flaws in the ad-hoc way that we were doing things
before.
See also `src/test/compile-fail/closure-expected-type`.

View file

@ -0,0 +1,26 @@
// Copyright 2016 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.
fn with_closure<A, F>(_: F)
where F: FnOnce(Vec<A>, A)
{
}
fn expect_free_supply_free<'x>(x: &'x u32) {
with_closure(|mut x: Vec<_>, y| {
// Shows that the call to `x.push()` is influencing type of `y`...
x.push(22_u32);
// ...since we now know the type of `y` and can resolve the method call.
y.wrapping_add(1);
});
}
fn main() { }

View file

@ -0,0 +1,26 @@
// Copyright 2016 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.
struct UsizeRef<'a> {
a: &'a usize
}
type RefTo = Box<for<'r> Fn(&'r Vec<usize>) -> UsizeRef<'r>>;
fn ref_to<'a>(vec: &'a Vec<usize>) -> UsizeRef<'a> {
UsizeRef{ a: &vec[0]}
}
fn main() {
// Regression test: this was causing ICEs; it should compile.
let a: RefTo = Box::new(|vec: &Vec<usize>| {
UsizeRef{ a: &vec[0] }
});
}

View file

@ -0,0 +1,35 @@
// Copyright 2016 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.
fn with_closure<F, R>(f: F) -> Result<char, R>
where F: FnOnce(&char) -> Result<char, R>,
{
f(&'a')
}
fn main() {
// Test that supplying the `-> Result<char, ()>` manually here
// (which is needed to constrain `R`) still allows us to figure
// out that the type of `x` is `&'a char` where `'a` is bound in
// the closure (if we didn't, we'd get a type-error because
// `with_closure` requires a bound region).
//
// This pattern was found in the wild.
let z = with_closure(|x| -> Result<char, ()> { Ok(*x) });
assert_eq!(z.unwrap(), 'a');
// It also works with `_`:
let z = with_closure(|x: _| -> Result<char, ()> { Ok(*x) });
assert_eq!(z.unwrap(), 'a');
// It also works with `&_`:
let z = with_closure(|x: &_| -> Result<char, ()> { Ok(*x) });
assert_eq!(z.unwrap(), 'a');
}

View file

@ -0,0 +1,20 @@
// Copyright 2016 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.
fn with_closure<F>(f: F) -> u32
where F: FnOnce(&u32, &u32) -> u32
{
f(&22, &44)
}
fn main() {
let z = with_closure(|x, y| x + y).wrapping_add(1);
assert_eq!(z, 22 + 44 + 1);
}