diff options
| author | bors <bors@rust-lang.org> | 2023-01-05 03:59:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-05 03:59:31 +0000 |
| commit | 03b9e1d154ecf8e04656fdf0179a28cd8ef333c5 (patch) | |
| tree | 8c1b70fb5f8b03c7d47813924f1f0ca8c09438f0 /src | |
| parent | d26242d35faf50aa9c873fa3c7c15e5564384baa (diff) | |
| parent | fa2f31b97178c3a66d6e5f91fa597711f585fd3b (diff) | |
| download | rust-03b9e1d154ecf8e04656fdf0179a28cd8ef333c5.tar.gz rust-03b9e1d154ecf8e04656fdf0179a28cd8ef333c5.zip | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/closures/supertrait-hint-cycle-2.rs | 18 | ||||
| -rw-r--r-- | src/test/ui/closures/supertrait-hint-cycle-3.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/closures/supertrait-hint-cycle.rs | 65 | ||||
| -rw-r--r-- | src/test/ui/issues/issue-25439.stderr | 7 | ||||
| -rw-r--r-- | src/test/ui/unboxed-closures/unboxed-closure-no-cyclic-sig.stderr | 7 |
5 files changed, 111 insertions, 2 deletions
diff --git a/src/test/ui/closures/supertrait-hint-cycle-2.rs b/src/test/ui/closures/supertrait-hint-cycle-2.rs new file mode 100644 index 00000000000..fda81b18d1e --- /dev/null +++ b/src/test/ui/closures/supertrait-hint-cycle-2.rs @@ -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| {}); +} diff --git a/src/test/ui/closures/supertrait-hint-cycle-3.rs b/src/test/ui/closures/supertrait-hint-cycle-3.rs new file mode 100644 index 00000000000..8149474df19 --- /dev/null +++ b/src/test/ui/closures/supertrait-hint-cycle-3.rs @@ -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| {}); +} diff --git a/src/test/ui/closures/supertrait-hint-cycle.rs b/src/test/ui/closures/supertrait-hint-cycle.rs new file mode 100644 index 00000000000..dbb06b2ef7a --- /dev/null +++ b/src/test/ui/closures/supertrait-hint-cycle.rs @@ -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() {} diff --git a/src/test/ui/issues/issue-25439.stderr b/src/test/ui/issues/issue-25439.stderr index 325c28c15e2..dadae23fdf3 100644 --- a/src/test/ui/issues/issue-25439.stderr +++ b/src/test/ui/issues/issue-25439.stderr @@ -2,12 +2,17 @@ error[E0644]: closure/generator type that references itself --> $DIR/issue-25439.rs:8:9 | LL | fix(|_, x| x); - | ^^^^^^^^ cyclic type of infinite size + | ^^^^^^ cyclic type of infinite size | = note: closures cannot capture themselves or take themselves as argument; this error may be the result of a recent compiler bug-fix, see issue #46062 <https://github.com/rust-lang/rust/issues/46062> for more information +note: required by a bound in `fix` + --> $DIR/issue-25439.rs:3:33 + | +LL | fn fix<F>(f: F) -> i32 where F: Fn(Helper<F>, i32) -> i32 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `fix` error: aborting due to previous error diff --git a/src/test/ui/unboxed-closures/unboxed-closure-no-cyclic-sig.stderr b/src/test/ui/unboxed-closures/unboxed-closure-no-cyclic-sig.stderr index 167479270b5..6d5dbca0558 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-no-cyclic-sig.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closure-no-cyclic-sig.stderr @@ -2,12 +2,17 @@ error[E0644]: closure/generator type that references itself --> $DIR/unboxed-closure-no-cyclic-sig.rs:8:7 | LL | g(|_| { }); - | ^^^^^^^^ cyclic type of infinite size + | ^^^ cyclic type of infinite size | = note: closures cannot capture themselves or take themselves as argument; this error may be the result of a recent compiler bug-fix, see issue #46062 <https://github.com/rust-lang/rust/issues/46062> for more information +note: required by a bound in `g` + --> $DIR/unboxed-closure-no-cyclic-sig.rs:5:24 + | +LL | fn g<F>(_: F) where F: FnOnce(Option<F>) {} + | ^^^^^^^^^^^^^^^^^ required by this bound in `g` error: aborting due to previous error |
