Closures always implement FnOnce in new solver
This commit is contained in:
parent
8a7ca936e6
commit
177997e383
@ -214,9 +214,20 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
|
|||||||
ty::Closure(_, substs) => {
|
ty::Closure(_, substs) => {
|
||||||
let closure_substs = substs.as_closure();
|
let closure_substs = substs.as_closure();
|
||||||
match closure_substs.kind_ty().to_opt_closure_kind() {
|
match closure_substs.kind_ty().to_opt_closure_kind() {
|
||||||
Some(closure_kind) if closure_kind.extends(goal_kind) => {}
|
// If the closure's kind doesn't extend the goal kind,
|
||||||
None => return Ok(None),
|
// then the closure doesn't implement the trait.
|
||||||
_ => return Err(NoSolution),
|
Some(closure_kind) => {
|
||||||
|
if !closure_kind.extends(goal_kind) {
|
||||||
|
return Err(NoSolution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Closure kind is not yet determined, so we return ambiguity unless
|
||||||
|
// the expected kind is `FnOnce` as that is always implemented.
|
||||||
|
None => {
|
||||||
|
if goal_kind != ty::ClosureKind::FnOnce {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
|
Ok(Some(closure_substs.sig().map_bound(|sig| (sig.inputs()[0], sig.output()))))
|
||||||
}
|
}
|
||||||
|
11
tests/ui/traits/new-solver/closure-inference-guidance.rs
Normal file
11
tests/ui/traits/new-solver/closure-inference-guidance.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// compile-flags: -Ztrait-solver=next
|
||||||
|
// check-pass
|
||||||
|
|
||||||
|
fn foo(i: isize) -> isize { i + 1 }
|
||||||
|
|
||||||
|
fn apply<A, F>(f: F, v: A) -> A where F: FnOnce(A) -> A { f(v) }
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let f = |i| foo(i);
|
||||||
|
assert_eq!(apply(f, 2), 3);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user