Skip to content

Commit 3c46fd6

Browse files
arora-amanroxelo
andcommitted
traits diagnostics: Don't print closure/generator upvar_tys tuple
Co-authored-by: Roxane Fruytier <[email protected]>
1 parent 08d1ab0 commit 3c46fd6

File tree

11 files changed

+23
-13
lines changed

11 files changed

+23
-13
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,29 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19221922
return;
19231923
}
19241924

1925-
err.note(&format!("required because it appears within the type `{}`", ty));
1925+
// If the obligation for a tuple is set directly by a Generator or Closure,
1926+
// then the tuple must be the one containing capture types.
1927+
let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) {
1928+
false
1929+
} else {
1930+
if let ObligationCauseCode::BuiltinDerivedObligation(ref data) =
1931+
*data.parent_code
1932+
{
1933+
let parent_trait_ref =
1934+
self.resolve_vars_if_possible(&data.parent_trait_ref);
1935+
let ty = parent_trait_ref.skip_binder().self_ty();
1936+
matches!(ty.kind(), ty::Generator(..))
1937+
|| matches!(ty.kind(), ty::Closure(..))
1938+
} else {
1939+
false
1940+
}
1941+
};
1942+
1943+
// Don't print the tuple of capture types
1944+
if !is_upvar_tys_infer_tuple {
1945+
err.note(&format!("required because it appears within the type `{}`", ty));
1946+
}
1947+
19261948
obligated_types.push(ty);
19271949

19281950
let parent_predicate = parent_trait_ref.without_const().to_predicate(tcx);

src/test/ui/async-await/issue-68112.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ LL | require_send(send_fut);
4141
|
4242
= help: the trait `Sync` is not implemented for `RefCell<i32>`
4343
= note: required because of the requirements on the impl of `Send` for `Arc<RefCell<i32>>`
44-
= note: required because it appears within the type `(Arc<RefCell<i32>>,)`
4544
= note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:47:31: 47:36 {}]`
4645
= note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:47:31: 47:36 {}]>`
4746
= note: required because it appears within the type `impl Future`

src/test/ui/closures/closure-move-sync.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | F: Send + 'static,
1111
|
1212
= help: the trait `Sync` is not implemented for `std::sync::mpsc::Receiver<()>`
1313
= note: required because of the requirements on the impl of `Send` for `&std::sync::mpsc::Receiver<()>`
14-
= note: required because it appears within the type `(&std::sync::mpsc::Receiver<()>,)`
1514
= note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:6:27: 9:6]`
1615

1716
error[E0277]: `Sender<()>` cannot be shared between threads safely
@@ -27,7 +26,6 @@ LL | F: Send + 'static,
2726
|
2827
= help: the trait `Sync` is not implemented for `Sender<()>`
2928
= note: required because of the requirements on the impl of `Send` for `&Sender<()>`
30-
= note: required because it appears within the type `(&Sender<()>,)`
3129
= note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:18:19: 18:42]`
3230

3331
error: aborting due to 2 previous errors

src/test/ui/generator/issue-68112.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ LL | require_send(send_gen);
2929
|
3030
= help: the trait `Sync` is not implemented for `RefCell<i32>`
3131
= note: required because of the requirements on the impl of `Send` for `Arc<RefCell<i32>>`
32-
= note: required because it appears within the type `(Arc<RefCell<i32>>,)`
3332
= note: required because it appears within the type `[generator@$DIR/issue-68112.rs:38:5: 41:6 {()}]`
3433
= note: required because it appears within the type `impl Generator`
3534
= note: required because it appears within the type `impl Generator`

src/test/ui/generator/not-send-sync.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | assert_send(|| {
99
|
1010
= help: the trait `Sync` is not implemented for `Cell<i32>`
1111
= note: required because of the requirements on the impl of `Send` for `&Cell<i32>`
12-
= note: required because it appears within the type `(&Cell<i32>,)`
1312
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:16:17: 20:6 _]`
1413

1514
error: generator cannot be shared between threads safely

src/test/ui/generator/print/generator-print-verbose-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ LL | require_send(send_gen);
2929
|
3030
= help: the trait `Sync` is not implemented for `RefCell<i32>`
3131
= note: required because of the requirements on the impl of `Send` for `Arc<RefCell<i32>>`
32-
= note: required because it appears within the type `(Arc<RefCell<i32>>,)`
3332
= note: required because it appears within the type `[make_gen2<Arc<RefCell<i32>>>::{closure#0} upvar_tys=(Arc<RefCell<i32>>) {()}]`
3433
= note: required because it appears within the type `Opaque(DefId(0:29 ~ generator_print_verbose_1[317d]::make_gen2::{opaque#0}), [std::sync::Arc<std::cell::RefCell<i32>>])`
3534
= note: required because it appears within the type `Opaque(DefId(0:32 ~ generator_print_verbose_1[317d]::make_non_send_generator2::{opaque#0}), [])`

src/test/ui/generator/print/generator-print-verbose-2.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | assert_send(|| {
99
|
1010
= help: the trait `Sync` is not implemented for `Cell<i32>`
1111
= note: required because of the requirements on the impl of `Send` for `&'_#3r Cell<i32>`
12-
= note: required because it appears within the type `(&'_#3r Cell<i32>,)`
1312
= note: required because it appears within the type `[main::{closure#1} upvar_tys=(&'_#3r Cell<i32>) _#17t]`
1413

1514
error: generator cannot be shared between threads safely

src/test/ui/impl-trait/auto-trait-leak2.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | send(before());
1111
| ^^^^ `Rc<Cell<i32>>` cannot be sent between threads safely
1212
|
1313
= help: within `impl Fn<(i32,)>`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
14-
= note: required because it appears within the type `(Rc<Cell<i32>>,)`
1514
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:7:5: 7:22]`
1615
= note: required because it appears within the type `impl Fn<(i32,)>`
1716

@@ -28,7 +27,6 @@ LL | fn after() -> impl Fn(i32) {
2827
| ------------ within this `impl Fn<(i32,)>`
2928
|
3029
= help: within `impl Fn<(i32,)>`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
31-
= note: required because it appears within the type `(Rc<Cell<i32>>,)`
3230
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:24:5: 24:22]`
3331
= note: required because it appears within the type `impl Fn<(i32,)>`
3432

src/test/ui/interior-mutability/interior-mutability.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
1212
= help: within `Cell<i32>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<i32>`
1313
= note: required because it appears within the type `Cell<i32>`
1414
= note: required because of the requirements on the impl of `UnwindSafe` for `&Cell<i32>`
15-
= note: required because it appears within the type `(&Cell<i32>,)`
1615
= note: required because it appears within the type `[closure@$DIR/interior-mutability.rs:5:18: 5:35]`
1716

1817
error: aborting due to previous error

src/test/ui/kindck/kindck-nonsendable-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | bar(move|| foo(x));
1010
| `Rc<usize>` cannot be sent between threads safely
1111
|
1212
= help: within `[closure@$DIR/kindck-nonsendable-1.rs:9:9: 9:22]`, the trait `Send` is not implemented for `Rc<usize>`
13-
= note: required because it appears within the type `(Rc<usize>,)`
1413
= note: required because it appears within the type `[closure@$DIR/kindck-nonsendable-1.rs:9:9: 9:22]`
1514

1615
error: aborting due to previous error

src/test/ui/no-send-res-ports.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LL | F: Send + 'static,
1919
= help: within `[closure@$DIR/no-send-res-ports.rs:25:19: 29:6]`, the trait `Send` is not implemented for `Rc<()>`
2020
= note: required because it appears within the type `Port<()>`
2121
= note: required because it appears within the type `Foo`
22-
= note: required because it appears within the type `(Foo,)`
2322
= note: required because it appears within the type `[closure@$DIR/no-send-res-ports.rs:25:19: 29:6]`
2423

2524
error: aborting due to previous error

0 commit comments

Comments
 (0)