From 0817b1d3ed26898e7d9ed71f6a2218d912ea6de5 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 6 Dec 2022 23:55:26 +0000 Subject: Don't deduce a signature that makes a closure cyclic --- src/test/ui/closures/supertrait-hint-cycle.rs | 65 ++++++++++++++++++++++ src/test/ui/issues/issue-25439.stderr | 21 ++++--- .../unboxed-closure-no-cyclic-sig.stderr | 21 ++++--- 3 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/closures/supertrait-hint-cycle.rs (limited to 'src/test') 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: FnMut(I) -> Self::Fut { + type Fut: Future; +} + +impl AsyncFn for F +where + Fut: Future, + F: FnMut(I) -> Fut, +{ + type Fut = Fut; +} + +async fn call(mut ctx: C, mut f: F) -> Result +where + F: for<'a> AsyncFn<&'a mut C, Result>, +{ + loop { + match f(&mut ctx).await { + Ok(val) => return Ok(val), + Err(_) => continue, + } + } +} + +trait Cap<'a> {} +impl Cap<'_> for T {} + +fn works(ctx: &mut usize) { + let mut inner = 0; + + type Ret<'a, 'b: 'a> = impl Future> + '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> + '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..938c9d9f18c 100644 --- a/src/test/ui/issues/issue-25439.stderr +++ b/src/test/ui/issues/issue-25439.stderr @@ -1,14 +1,19 @@ -error[E0644]: closure/generator type that references itself - --> $DIR/issue-25439.rs:8:9 +error[E0631]: type mismatch in closure arguments + --> $DIR/issue-25439.rs:8:5 | LL | fix(|_, x| x); - | ^^^^^^^^ cyclic type of infinite size + | ^^^ ------ found signature defined here + | | + | expected due to this | - = 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 - for more information + = note: expected closure signature `for<'a> fn(Helper<'a, [closure@$DIR/issue-25439.rs:8:9: 8:15]>, i32) -> _` + found closure signature `fn(_, _) -> _` +note: required by a bound in `fix` + --> $DIR/issue-25439.rs:3:33 + | +LL | fn fix(f: F) -> i32 where F: Fn(Helper, i32) -> i32 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `fix` error: aborting due to previous error -For more information about this error, try `rustc --explain E0644`. +For more information about this error, try `rustc --explain E0631`. 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..cfea13c1127 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 @@ -1,14 +1,19 @@ -error[E0644]: closure/generator type that references itself - --> $DIR/unboxed-closure-no-cyclic-sig.rs:8:7 +error[E0631]: type mismatch in closure arguments + --> $DIR/unboxed-closure-no-cyclic-sig.rs:8:5 | LL | g(|_| { }); - | ^^^^^^^^ cyclic type of infinite size + | ^ --- found signature defined here + | | + | expected due to this | - = 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 - for more information + = note: expected closure signature `fn(Option<[closure@$DIR/unboxed-closure-no-cyclic-sig.rs:8:7: 8:10]>) -> _` + found closure signature `fn(_) -> _` +note: required by a bound in `g` + --> $DIR/unboxed-closure-no-cyclic-sig.rs:5:24 + | +LL | fn g(_: F) where F: FnOnce(Option) {} + | ^^^^^^^^^^^^^^^^^ required by this bound in `g` error: aborting due to previous error -For more information about this error, try `rustc --explain E0644`. +For more information about this error, try `rustc --explain E0631`. -- cgit 1.4.1-3-g733a5 From 9f59ab55e6e5e8825cfc36bd90ed4d5b387ecd70 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 7 Dec 2022 03:03:35 +0000 Subject: Restore cyclic closure message --- compiler/rustc_hir_typeck/src/closure.rs | 2 +- .../src/traits/error_reporting/mod.rs | 21 ++++++++++++++++++++- src/test/ui/issues/issue-25439.stderr | 16 ++++++++-------- .../unboxed-closure-no-cyclic-sig.stderr | 16 ++++++++-------- 4 files changed, 37 insertions(+), 18 deletions(-) (limited to 'src/test') diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index 8167e7e070c..3453da500b9 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -227,7 +227,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); // Make sure that we didn't infer a signature that mentions itself. // This can happen when we elaborate certain supertrait bounds that - // mention projections containing the `Self` type. See + // mention projections containing the `Self` type. See #105401. struct MentionsTy<'tcx> { expected_ty: Ty<'tcx>, } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 8f317beaa77..fb7791879a1 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -33,7 +33,7 @@ use rustc_infer::infer::error_reporting::TypeErrCtxt; use rustc_infer::infer::{InferOk, TypeTrace}; use rustc_middle::traits::select::OverflowError; use rustc_middle::ty::abstract_const::NotConstEvaluatable; -use rustc_middle::ty::error::ExpectedFound; +use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::print::{with_forced_trimmed_paths, FmtPrinter, Print}; use rustc_middle::ty::{ @@ -1215,6 +1215,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } } + OutputTypeParameterMismatch( + found_trait_ref, + expected_trait_ref, + terr @ TypeError::CyclicTy(_), + ) => { + let self_ty = found_trait_ref.self_ty().skip_binder(); + let (cause, terr) = if let ty::Closure(def_id, _) = self_ty.kind() { + ( + ObligationCause::dummy_with_span(tcx.def_span(def_id)), + TypeError::CyclicTy(self_ty), + ) + } else { + (obligation.cause.clone(), terr) + }; + self.report_and_explain_type_error( + TypeTrace::poly_trait_refs(&cause, true, expected_trait_ref, found_trait_ref), + terr, + ) + } OutputTypeParameterMismatch(found_trait_ref, expected_trait_ref, _) => { let found_trait_ref = self.resolve_vars_if_possible(found_trait_ref); let expected_trait_ref = self.resolve_vars_if_possible(expected_trait_ref); diff --git a/src/test/ui/issues/issue-25439.stderr b/src/test/ui/issues/issue-25439.stderr index 938c9d9f18c..dadae23fdf3 100644 --- a/src/test/ui/issues/issue-25439.stderr +++ b/src/test/ui/issues/issue-25439.stderr @@ -1,13 +1,13 @@ -error[E0631]: type mismatch in closure arguments - --> $DIR/issue-25439.rs:8:5 +error[E0644]: closure/generator type that references itself + --> $DIR/issue-25439.rs:8:9 | LL | fix(|_, x| x); - | ^^^ ------ found signature defined here - | | - | expected due to this + | ^^^^^^ cyclic type of infinite size | - = note: expected closure signature `for<'a> fn(Helper<'a, [closure@$DIR/issue-25439.rs:8:9: 8:15]>, i32) -> _` - found closure signature `fn(_, _) -> _` + = 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 + for more information note: required by a bound in `fix` --> $DIR/issue-25439.rs:3:33 | @@ -16,4 +16,4 @@ LL | fn fix(f: F) -> i32 where F: Fn(Helper, i32) -> i32 { error: aborting due to previous error -For more information about this error, try `rustc --explain E0631`. +For more information about this error, try `rustc --explain E0644`. 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 cfea13c1127..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 @@ -1,13 +1,13 @@ -error[E0631]: type mismatch in closure arguments - --> $DIR/unboxed-closure-no-cyclic-sig.rs:8:5 +error[E0644]: closure/generator type that references itself + --> $DIR/unboxed-closure-no-cyclic-sig.rs:8:7 | LL | g(|_| { }); - | ^ --- found signature defined here - | | - | expected due to this + | ^^^ cyclic type of infinite size | - = note: expected closure signature `fn(Option<[closure@$DIR/unboxed-closure-no-cyclic-sig.rs:8:7: 8:10]>) -> _` - found closure signature `fn(_) -> _` + = 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 + for more information note: required by a bound in `g` --> $DIR/unboxed-closure-no-cyclic-sig.rs:5:24 | @@ -16,4 +16,4 @@ LL | fn g(_: F) where F: FnOnce(Option) {} error: aborting due to previous error -For more information about this error, try `rustc --explain E0631`. +For more information about this error, try `rustc --explain E0644`. -- cgit 1.4.1-3-g733a5 From fa2f31b97178c3a66d6e5f91fa597711f585fd3b Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 7 Dec 2022 03:50:35 +0000 Subject: More tests --- src/test/ui/closures/supertrait-hint-cycle-2.rs | 18 ++++++++++++++++++ src/test/ui/closures/supertrait-hint-cycle-3.rs | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/ui/closures/supertrait-hint-cycle-2.rs create mode 100644 src/test/ui/closures/supertrait-hint-cycle-3.rs (limited to 'src/test') 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 Foo<'_> for F { + type Input = u32; +} + +trait SuperFn: for<'a> Foo<'a> + for<'a> Fn(>::Input) {} +impl SuperFn for T where T: for<'a> Fn(>::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 Foo<'_> for F { + type Input = u32; +} + +fn needs_super Fn(>::Input) + for<'a> Foo<'a>>(_: F) {} + +fn main() { + needs_super(|_: u32| {}); +} -- cgit 1.4.1-3-g733a5