Skip to content

Commit ffdb147

Browse files
authored
Rollup merge of rust-lang#122732 - compiler-errors:coroutine-captures-note, r=nnethercote
Remove redundant coroutine captures note This note is redundant, since we'll always be printing this "captures the following types..." between *more* descriptive `BuiltinDerivedObligationCause`s. Please review with whitespace disabled, since I also removed an unnecessary labeled break.
2 parents 2cf93ac + 3d56178 commit ffdb147

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
@@ -3209,71 +3209,69 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
32093209
}
32103210
};
32113211

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

32793277
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)