Skip to content

Commit 3d56178

Browse files
Remove redundant coroutine captures note
1 parent 200e3f7 commit 3d56178

10 files changed

+60
-72
lines changed

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

+58-60
Original file line numberDiff line numberDiff line change
@@ -3199,71 +3199,69 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31993199
}
32003200
};
32013201

3202-
// Don't print the tuple of capture types
3203-
'print: {
3204-
if !is_upvar_tys_infer_tuple {
3205-
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file);
3206-
let msg = format!("required because it appears within the type `{ty_str}`");
3207-
match ty.kind() {
3208-
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
3209-
Some(ident) => err.span_note(ident.span, msg),
3210-
None => err.note(msg),
3211-
},
3212-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
3213-
// If the previous type is async fn, this is the future generated by the body of an async function.
3214-
// Avoid printing it twice (it was already printed in the `ty::Coroutine` arm below).
3215-
let is_future = tcx.ty_is_opaque_future(ty);
3216-
debug!(
3217-
?obligated_types,
3218-
?is_future,
3219-
"note_obligation_cause_code: check for async fn"
3220-
);
3221-
if is_future
3222-
&& obligated_types.last().is_some_and(|ty| match ty.kind() {
3223-
ty::Coroutine(last_def_id, ..) => {
3224-
tcx.coroutine_is_async(*last_def_id)
3225-
}
3226-
_ => false,
3227-
})
3228-
{
3229-
break 'print;
3230-
}
3231-
err.span_note(tcx.def_span(def_id), msg)
3202+
if !is_upvar_tys_infer_tuple {
3203+
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file);
3204+
let msg = format!("required because it appears within the type `{ty_str}`");
3205+
match ty.kind() {
3206+
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
3207+
Some(ident) => {
3208+
err.span_note(ident.span, msg);
32323209
}
3233-
ty::CoroutineWitness(def_id, args) => {
3234-
use std::fmt::Write;
3235-
3236-
// FIXME: this is kind of an unusual format for rustc, can we make it more clear?
3237-
// Maybe we should just remove this note altogether?
3238-
// FIXME: only print types which don't meet the trait requirement
3239-
let mut msg =
3240-
"required because it captures the following types: ".to_owned();
3241-
for bty in tcx.coroutine_hidden_types(*def_id) {
3242-
let ty = bty.instantiate(tcx, args);
3243-
write!(msg, "`{ty}`, ").unwrap();
3244-
}
3245-
err.note(msg.trim_end_matches(", ").to_string())
3210+
None => {
3211+
err.note(msg);
32463212
}
3247-
ty::Coroutine(def_id, _) => {
3248-
let sp = tcx.def_span(def_id);
3249-
3250-
// Special-case this to say "async block" instead of `[static coroutine]`.
3251-
let kind = tcx.coroutine_kind(def_id).unwrap();
3252-
err.span_note(
3253-
sp,
3254-
with_forced_trimmed_paths!(format!(
3255-
"required because it's used within this {kind:#}",
3256-
)),
3257-
)
3213+
},
3214+
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
3215+
// If the previous type is async fn, this is the future generated by the body of an async function.
3216+
// Avoid printing it twice (it was already printed in the `ty::Coroutine` arm below).
3217+
let is_future = tcx.ty_is_opaque_future(ty);
3218+
debug!(
3219+
?obligated_types,
3220+
?is_future,
3221+
"note_obligation_cause_code: check for async fn"
3222+
);
3223+
if is_future
3224+
&& obligated_types.last().is_some_and(|ty| match ty.kind() {
3225+
ty::Coroutine(last_def_id, ..) => {
3226+
tcx.coroutine_is_async(*last_def_id)
3227+
}
3228+
_ => false,
3229+
})
3230+
{
3231+
// See comment above; skip printing twice.
3232+
} else {
3233+
err.span_note(tcx.def_span(def_id), msg);
32583234
}
3259-
ty::Closure(def_id, _) => err.span_note(
3235+
}
3236+
ty::Coroutine(def_id, _) => {
3237+
let sp = tcx.def_span(def_id);
3238+
3239+
// Special-case this to say "async block" instead of `[static coroutine]`.
3240+
let kind = tcx.coroutine_kind(def_id).unwrap();
3241+
err.span_note(
3242+
sp,
3243+
with_forced_trimmed_paths!(format!(
3244+
"required because it's used within this {kind:#}",
3245+
)),
3246+
);
3247+
}
3248+
ty::CoroutineWitness(..) => {
3249+
// Skip printing coroutine-witnesses, since we'll drill into
3250+
// the bad field in another derived obligation cause.
3251+
}
3252+
ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
3253+
err.span_note(
32603254
tcx.def_span(def_id),
32613255
"required because it's used within this closure",
3262-
),
3263-
ty::Str => err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes"),
3264-
_ => err.note(msg),
3265-
};
3266-
}
3256+
);
3257+
}
3258+
ty::Str => {
3259+
err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes");
3260+
}
3261+
_ => {
3262+
err.note(msg);
3263+
}
3264+
};
32673265
}
32683266

32693267
obligated_types.push(ty);

tests/ui/async-await/async-await-let-else.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ LL | async fn bar2<T>(_: T) -> ! {
3838
LL | | panic!()
3939
LL | | }
4040
| |_^
41-
= note: required because it captures the following types: `impl Future<Output = !>`
4241
note: required because it's used within this `async` fn body
4342
--> $DIR/async-await-let-else.rs:18:32
4443
|

tests/ui/async-await/issue-68112.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ note: required because it appears within the type `impl Future<Output = Arc<RefC
5858
|
5959
LL | fn make_non_send_future2() -> impl Future<Output = Arc<RefCell<i32>>> {
6060
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
61-
= note: required because it captures the following types: `impl Future<Output = Arc<RefCell<i32>>>`, `Ready<i32>`
6261
note: required because it's used within this `async` block
6362
--> $DIR/issue-68112.rs:57:20
6463
|

tests/ui/async-await/issue-70935-complex-spans.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ LL | async fn baz<T>(_c: impl FnMut() -> T) where T: Future<Output=()> {
2525
| ___________________________________________________________________^
2626
LL | | }
2727
| |_^
28-
= note: required because it captures the following types: `impl Future<Output = ()>`
2928
note: required because it's used within this `async` block
3029
--> $DIR/issue-70935-complex-spans.rs:18:5
3130
|
@@ -63,7 +62,6 @@ LL | async fn baz<T>(_c: impl FnMut() -> T) where T: Future<Output=()> {
6362
| ___________________________________________________________________^
6463
LL | | }
6564
| |_^
66-
= note: required because it captures the following types: `impl Future<Output = ()>`
6765
note: required because it's used within this `async` block
6866
--> $DIR/issue-70935-complex-spans.rs:18:5
6967
|

tests/ui/async-await/issues/issue-67893.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | pub async fn run() {
1212
| ------------------ within this `impl Future<Output = ()>`
1313
|
1414
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `MutexGuard<'_, ()>`, which is required by `impl Future<Output = ()>: Send`
15-
= note: required because it captures the following types: `Arc<Mutex<()>>`, `MutexGuard<'_, ()>`, `impl Future<Output = ()>`
1615
note: required because it's used within this `async` fn body
1716
--> $DIR/auxiliary/issue_67893.rs:9:20
1817
|

tests/ui/async-await/partial-drop-partial-reinit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() {
88
//~| NOTE cannot be sent
99
//~| NOTE bound introduced by
1010
//~| NOTE appears within the type
11-
//~| NOTE captures the following types
1211
}
1312

1413
fn gimme_send<T: Send>(t: T) {

tests/ui/async-await/partial-drop-partial-reinit.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ LL | async fn foo() {
1111
|
1212
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `NotSend`, which is required by `impl Future<Output = ()>: Send`
1313
= note: required because it appears within the type `(NotSend,)`
14-
= note: required because it captures the following types: `(NotSend,)`, `impl Future<Output = ()>`
1514
note: required because it's used within this `async` fn body
16-
--> $DIR/partial-drop-partial-reinit.rs:28:16
15+
--> $DIR/partial-drop-partial-reinit.rs:27:16
1716
|
1817
LL | async fn foo() {
1918
| ________________^
@@ -25,7 +24,7 @@ LL | | bar().await;
2524
LL | | }
2625
| |_^
2726
note: required by a bound in `gimme_send`
28-
--> $DIR/partial-drop-partial-reinit.rs:14:18
27+
--> $DIR/partial-drop-partial-reinit.rs:13:18
2928
|
3029
LL | fn gimme_send<T: Send>(t: T) {
3130
| ^^^^ required by this bound in `gimme_send`

tests/ui/coroutine/issue-68112.rs

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn test2() {
6565
//~^ ERROR `RefCell<i32>` cannot be shared between threads safely
6666
//~| NOTE `RefCell<i32>` cannot be shared between threads safely
6767
//~| NOTE required for
68-
//~| NOTE captures the following types
6968
//~| NOTE use `std::sync::RwLock` instead
7069
}
7170

tests/ui/coroutine/issue-68112.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ note: required because it appears within the type `impl Coroutine<Return = Arc<R
4444
|
4545
LL | fn make_non_send_coroutine2() -> impl Coroutine<Return = Arc<RefCell<i32>>> {
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47-
= note: required because it captures the following types: `impl Coroutine<Return = Arc<RefCell<i32>>>`
4847
note: required because it's used within this coroutine
4948
--> $DIR/issue-68112.rs:60:20
5049
|

tests/ui/coroutine/print/coroutine-print-verbose-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ note: required because it appears within the type `Opaque(DefId(0:36 ~ coroutine
4343
|
4444
LL | fn make_non_send_coroutine2() -> impl Coroutine<Return = Arc<RefCell<i32>>> {
4545
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46-
= note: required because it captures the following types: `Opaque(DefId(0:36 ~ coroutine_print_verbose_1[75fb]::make_non_send_coroutine2::{opaque#0}), [])`
4746
note: required because it's used within this coroutine
4847
--> $DIR/coroutine-print-verbose-1.rs:52:20
4948
|

0 commit comments

Comments
 (0)