From 3c46fd67f8b4831da9f2e8a536d34f498cf70241 Mon Sep 17 00:00:00 2001 From: Aman Arora Date: Thu, 8 Oct 2020 00:28:53 -0400 Subject: [PATCH] traits diagnostics: Don't print closure/generator upvar_tys tuple Co-authored-by: Roxane Fruytier --- .../src/traits/error_reporting/suggestions.rs | 24 ++++++++++++++++++- src/test/ui/async-await/issue-68112.stderr | 1 - src/test/ui/closures/closure-move-sync.stderr | 2 -- src/test/ui/generator/issue-68112.stderr | 1 - src/test/ui/generator/not-send-sync.stderr | 1 - .../print/generator-print-verbose-1.stderr | 1 - .../print/generator-print-verbose-2.stderr | 1 - .../ui/impl-trait/auto-trait-leak2.stderr | 2 -- .../interior-mutability.stderr | 1 - .../ui/kindck/kindck-nonsendable-1.stderr | 1 - src/test/ui/no-send-res-ports.stderr | 1 - 11 files changed, 23 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 5908e73d4833..7e86548f6ab9 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1922,7 +1922,29 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - err.note(&format!("required because it appears within the type `{}`", ty)); + // If the obligation for a tuple is set directly by a Generator or Closure, + // then the tuple must be the one containing capture types. + let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) { + false + } else { + if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = + *data.parent_code + { + let parent_trait_ref = + self.resolve_vars_if_possible(&data.parent_trait_ref); + let ty = parent_trait_ref.skip_binder().self_ty(); + matches!(ty.kind(), ty::Generator(..)) + || matches!(ty.kind(), ty::Closure(..)) + } else { + false + } + }; + + // Don't print the tuple of capture types + if !is_upvar_tys_infer_tuple { + err.note(&format!("required because it appears within the type `{}`", ty)); + } + obligated_types.push(ty); let parent_predicate = parent_trait_ref.without_const().to_predicate(tcx); diff --git a/src/test/ui/async-await/issue-68112.stderr b/src/test/ui/async-await/issue-68112.stderr index a6c93e91a9eb..e97d088cf3ed 100644 --- a/src/test/ui/async-await/issue-68112.stderr +++ b/src/test/ui/async-await/issue-68112.stderr @@ -41,7 +41,6 @@ LL | require_send(send_fut); | = help: the trait `Sync` is not implemented for `RefCell` = note: required because of the requirements on the impl of `Send` for `Arc>` - = note: required because it appears within the type `(Arc>,)` = note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:47:31: 47:36 {}]` = note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:47:31: 47:36 {}]>` = note: required because it appears within the type `impl Future` diff --git a/src/test/ui/closures/closure-move-sync.stderr b/src/test/ui/closures/closure-move-sync.stderr index 285b6543833a..da5e25c0d18f 100644 --- a/src/test/ui/closures/closure-move-sync.stderr +++ b/src/test/ui/closures/closure-move-sync.stderr @@ -11,7 +11,6 @@ LL | F: Send + 'static, | = help: the trait `Sync` is not implemented for `std::sync::mpsc::Receiver<()>` = note: required because of the requirements on the impl of `Send` for `&std::sync::mpsc::Receiver<()>` - = note: required because it appears within the type `(&std::sync::mpsc::Receiver<()>,)` = note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:6:27: 9:6]` error[E0277]: `Sender<()>` cannot be shared between threads safely @@ -27,7 +26,6 @@ LL | F: Send + 'static, | = help: the trait `Sync` is not implemented for `Sender<()>` = note: required because of the requirements on the impl of `Send` for `&Sender<()>` - = note: required because it appears within the type `(&Sender<()>,)` = note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:18:19: 18:42]` error: aborting due to 2 previous errors diff --git a/src/test/ui/generator/issue-68112.stderr b/src/test/ui/generator/issue-68112.stderr index 5793bbd950ec..96a8d6d70e01 100644 --- a/src/test/ui/generator/issue-68112.stderr +++ b/src/test/ui/generator/issue-68112.stderr @@ -29,7 +29,6 @@ LL | require_send(send_gen); | = help: the trait `Sync` is not implemented for `RefCell` = note: required because of the requirements on the impl of `Send` for `Arc>` - = note: required because it appears within the type `(Arc>,)` = note: required because it appears within the type `[generator@$DIR/issue-68112.rs:38:5: 41:6 {()}]` = note: required because it appears within the type `impl Generator` = note: required because it appears within the type `impl Generator` diff --git a/src/test/ui/generator/not-send-sync.stderr b/src/test/ui/generator/not-send-sync.stderr index f5cfa83f5007..2384ed3d2497 100644 --- a/src/test/ui/generator/not-send-sync.stderr +++ b/src/test/ui/generator/not-send-sync.stderr @@ -9,7 +9,6 @@ LL | assert_send(|| { | = help: the trait `Sync` is not implemented for `Cell` = note: required because of the requirements on the impl of `Send` for `&Cell` - = note: required because it appears within the type `(&Cell,)` = note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:16:17: 20:6 _]` error: generator cannot be shared between threads safely diff --git a/src/test/ui/generator/print/generator-print-verbose-1.stderr b/src/test/ui/generator/print/generator-print-verbose-1.stderr index 9669f4d96b9d..b5c63584c6c1 100644 --- a/src/test/ui/generator/print/generator-print-verbose-1.stderr +++ b/src/test/ui/generator/print/generator-print-verbose-1.stderr @@ -29,7 +29,6 @@ LL | require_send(send_gen); | = help: the trait `Sync` is not implemented for `RefCell` = note: required because of the requirements on the impl of `Send` for `Arc>` - = note: required because it appears within the type `(Arc>,)` = note: required because it appears within the type `[make_gen2>>::{closure#0} upvar_tys=(Arc>) {()}]` = note: required because it appears within the type `Opaque(DefId(0:29 ~ generator_print_verbose_1[317d]::make_gen2::{opaque#0}), [std::sync::Arc>])` = note: required because it appears within the type `Opaque(DefId(0:32 ~ generator_print_verbose_1[317d]::make_non_send_generator2::{opaque#0}), [])` diff --git a/src/test/ui/generator/print/generator-print-verbose-2.stderr b/src/test/ui/generator/print/generator-print-verbose-2.stderr index f363ed6a3b9a..f23949091d91 100644 --- a/src/test/ui/generator/print/generator-print-verbose-2.stderr +++ b/src/test/ui/generator/print/generator-print-verbose-2.stderr @@ -9,7 +9,6 @@ LL | assert_send(|| { | = help: the trait `Sync` is not implemented for `Cell` = note: required because of the requirements on the impl of `Send` for `&'_#3r Cell` - = note: required because it appears within the type `(&'_#3r Cell,)` = note: required because it appears within the type `[main::{closure#1} upvar_tys=(&'_#3r Cell) _#17t]` error: generator cannot be shared between threads safely diff --git a/src/test/ui/impl-trait/auto-trait-leak2.stderr b/src/test/ui/impl-trait/auto-trait-leak2.stderr index fc2bc62f3a78..6b2b8248a4f2 100644 --- a/src/test/ui/impl-trait/auto-trait-leak2.stderr +++ b/src/test/ui/impl-trait/auto-trait-leak2.stderr @@ -11,7 +11,6 @@ LL | send(before()); | ^^^^ `Rc>` cannot be sent between threads safely | = help: within `impl Fn<(i32,)>`, the trait `Send` is not implemented for `Rc>` - = note: required because it appears within the type `(Rc>,)` = note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:7:5: 7:22]` = note: required because it appears within the type `impl Fn<(i32,)>` @@ -28,7 +27,6 @@ LL | fn after() -> impl Fn(i32) { | ------------ within this `impl Fn<(i32,)>` | = help: within `impl Fn<(i32,)>`, the trait `Send` is not implemented for `Rc>` - = note: required because it appears within the type `(Rc>,)` = note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:24:5: 24:22]` = note: required because it appears within the type `impl Fn<(i32,)>` diff --git a/src/test/ui/interior-mutability/interior-mutability.stderr b/src/test/ui/interior-mutability/interior-mutability.stderr index cddfe6c69466..dd43da116644 100644 --- a/src/test/ui/interior-mutability/interior-mutability.stderr +++ b/src/test/ui/interior-mutability/interior-mutability.stderr @@ -12,7 +12,6 @@ LL | pub fn catch_unwind R + UnwindSafe, R>(f: F) -> Result { = help: within `Cell`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell` = note: required because it appears within the type `Cell` = note: required because of the requirements on the impl of `UnwindSafe` for `&Cell` - = note: required because it appears within the type `(&Cell,)` = note: required because it appears within the type `[closure@$DIR/interior-mutability.rs:5:18: 5:35]` error: aborting due to previous error diff --git a/src/test/ui/kindck/kindck-nonsendable-1.stderr b/src/test/ui/kindck/kindck-nonsendable-1.stderr index f942088eb7ec..c7d67a991bff 100644 --- a/src/test/ui/kindck/kindck-nonsendable-1.stderr +++ b/src/test/ui/kindck/kindck-nonsendable-1.stderr @@ -10,7 +10,6 @@ LL | bar(move|| foo(x)); | `Rc` cannot be sent between threads safely | = help: within `[closure@$DIR/kindck-nonsendable-1.rs:9:9: 9:22]`, the trait `Send` is not implemented for `Rc` - = note: required because it appears within the type `(Rc,)` = note: required because it appears within the type `[closure@$DIR/kindck-nonsendable-1.rs:9:9: 9:22]` error: aborting due to previous error diff --git a/src/test/ui/no-send-res-ports.stderr b/src/test/ui/no-send-res-ports.stderr index 8842b1815864..ef7fb4ad7b26 100644 --- a/src/test/ui/no-send-res-ports.stderr +++ b/src/test/ui/no-send-res-ports.stderr @@ -19,7 +19,6 @@ LL | F: Send + 'static, = help: within `[closure@$DIR/no-send-res-ports.rs:25:19: 29:6]`, the trait `Send` is not implemented for `Rc<()>` = note: required because it appears within the type `Port<()>` = note: required because it appears within the type `Foo` - = note: required because it appears within the type `(Foo,)` = note: required because it appears within the type `[closure@$DIR/no-send-res-ports.rs:25:19: 29:6]` error: aborting due to previous error