-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #105409 - compiler-errors:closure-infer-cycle, r=jackh726
Don't deduce a signature that makes a closure cyclic Sometimes when elaborating supertrait bounds for closure signature inference, we end up deducing a closure signature that is cyclical because either a parameter or the return type references a projection mentioning `Self` that also has escaping bound vars, which means that it's not eagerly replaced with an inference variable. Interestingly, this is not *just* related to my PR that elaborates supertrait bounds for closure signature deduction. The committed test `supertrait-hint-cycle-3.rs` shows **stable** code that is fixed by this PR: ```rust trait Foo<'a> { type Input; } impl<F: Fn(u32)> Foo<'_> for F { type Input = u32; } fn needs_super<F: for<'a> Fn(<F as Foo<'a>>::Input) + for<'a> Foo<'a>>(_: F) {} fn main() { needs_super(|_: u32| {}); } ``` Fixes #105401 Fixes #105396 r? types
- Loading branch information
Showing
7 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// check-pass | ||
|
||
trait Foo<'a> { | ||
type Input; | ||
} | ||
|
||
impl<F: Fn(u32)> Foo<'_> for F { | ||
type Input = u32; | ||
} | ||
|
||
trait SuperFn: for<'a> Foo<'a> + for<'a> Fn(<Self as Foo<'a>>::Input) {} | ||
impl<T> SuperFn for T where T: for<'a> Fn(<Self as Foo<'a>>::Input) + for<'a> Foo<'a> {} | ||
|
||
fn needs_super(_: impl SuperFn) {} | ||
|
||
fn main() { | ||
needs_super(|_: u32| {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// check-pass | ||
|
||
|
||
trait Foo<'a> { | ||
type Input; | ||
} | ||
|
||
impl<F: Fn(u32)> Foo<'_> for F { | ||
type Input = u32; | ||
} | ||
|
||
fn needs_super<F: for<'a> Fn(<F as Foo<'a>>::Input) + for<'a> Foo<'a>>(_: F) {} | ||
|
||
fn main() { | ||
needs_super(|_: u32| {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// edition:2021 | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
#![feature(closure_lifetime_binder)] | ||
|
||
use std::future::Future; | ||
|
||
trait AsyncFn<I, R>: FnMut(I) -> Self::Fut { | ||
type Fut: Future<Output = R>; | ||
} | ||
|
||
impl<F, I, R, Fut> AsyncFn<I, R> for F | ||
where | ||
Fut: Future<Output = R>, | ||
F: FnMut(I) -> Fut, | ||
{ | ||
type Fut = Fut; | ||
} | ||
|
||
async fn call<C, R, F>(mut ctx: C, mut f: F) -> Result<R, ()> | ||
where | ||
F: for<'a> AsyncFn<&'a mut C, Result<R, ()>>, | ||
{ | ||
loop { | ||
match f(&mut ctx).await { | ||
Ok(val) => return Ok(val), | ||
Err(_) => continue, | ||
} | ||
} | ||
} | ||
|
||
trait Cap<'a> {} | ||
impl<T> Cap<'_> for T {} | ||
|
||
fn works(ctx: &mut usize) { | ||
let mut inner = 0; | ||
|
||
type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>; | ||
|
||
let callback = for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> { | ||
inner += 1; | ||
async move { | ||
let _c = c; | ||
Ok(1usize) | ||
} | ||
}; | ||
call(ctx, callback); | ||
} | ||
|
||
fn doesnt_work_but_should(ctx: &mut usize) { | ||
let mut inner = 0; | ||
|
||
type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>; | ||
|
||
call(ctx, for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> { | ||
inner += 1; | ||
async move { | ||
let _c = c; | ||
Ok(1usize) | ||
} | ||
}); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters