Skip to content

Commit 90035f1

Browse files
authored
Rollup merge of #105839 - LegionMammal978:mut-upvar-not-send, r=lcnr
Suggest a `T: Send` bound for `&mut T` upvars in `Send` generators Right now, we suggest a `T: Sync` bound for both `&T` and `&mut T` upvars. A user on URLO [found this confusing](https://users.rust-lang.org/t/error-complains-about-missing-sync-but-send-is-whats-missing/86021), so I wrote this quick fix to look at the mutability before making the suggestion.
2 parents f74bcfb + ee53452 commit 90035f1

File tree

3 files changed

+103
-17
lines changed

3 files changed

+103
-17
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -2344,28 +2344,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23442344
}
23452345
}
23462346
GeneratorInteriorOrUpvar::Upvar(upvar_span) => {
2347-
// `Some(ref_ty)` if `target_ty` is `&T` and `T` fails to impl `Sync`
2348-
let refers_to_non_sync = match target_ty.kind() {
2349-
ty::Ref(_, ref_ty, _) => match self.evaluate_obligation(&obligation) {
2350-
Ok(eval) if !eval.may_apply() => Some(ref_ty),
2347+
// `Some((ref_ty, is_mut))` if `target_ty` is `&T` or `&mut T` and fails to impl `Send`
2348+
let non_send = match target_ty.kind() {
2349+
ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(&obligation) {
2350+
Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())),
23512351
_ => None,
23522352
},
23532353
_ => None,
23542354
};
23552355

2356-
let (span_label, span_note) = match refers_to_non_sync {
2357-
// if `target_ty` is `&T` and `T` fails to impl `Sync`,
2358-
// include suggestions to make `T: Sync` so that `&T: Send`
2359-
Some(ref_ty) => (
2360-
format!(
2361-
"has type `{}` which {}, because `{}` is not `Sync`",
2362-
target_ty, trait_explanation, ref_ty
2363-
),
2364-
format!(
2365-
"captured value {} because `&` references cannot be sent unless their referent is `Sync`",
2366-
trait_explanation
2367-
),
2368-
),
2356+
let (span_label, span_note) = match non_send {
2357+
// if `target_ty` is `&T` or `&mut T` and fails to impl `Send`,
2358+
// include suggestions to make `T: Sync` so that `&T: Send`,
2359+
// or to make `T: Send` so that `&mut T: Send`
2360+
Some((ref_ty, is_mut)) => {
2361+
let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
2362+
let ref_kind = if is_mut { "&mut" } else { "&" };
2363+
(
2364+
format!(
2365+
"has type `{}` which {}, because `{}` is not `{}`",
2366+
target_ty, trait_explanation, ref_ty, ref_ty_trait
2367+
),
2368+
format!(
2369+
"captured value {} because `{}` references cannot be sent unless their referent is `{}`",
2370+
trait_explanation, ref_kind, ref_ty_trait
2371+
),
2372+
)
2373+
}
23692374
None => (
23702375
format!("has type `{}` which {}", target_ty, trait_explanation),
23712376
format!("captured value {}", trait_explanation),

Diff for: src/test/ui/generator/ref-upvar-not-send.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// For `Send` generators, suggest a `T: Sync` requirement for `&T` upvars,
2+
// and suggest a `T: Send` requirement for `&mut T` upvars.
3+
4+
#![feature(generators)]
5+
6+
fn assert_send<T: Send>(_: T) {}
7+
//~^ NOTE required by a bound in `assert_send`
8+
//~| NOTE required by this bound in `assert_send`
9+
//~| NOTE required by a bound in `assert_send`
10+
//~| NOTE required by this bound in `assert_send`
11+
12+
fn main() {
13+
let x: &*mut () = &std::ptr::null_mut();
14+
let y: &mut *mut () = &mut std::ptr::null_mut();
15+
assert_send(move || {
16+
//~^ ERROR generator cannot be sent between threads safely
17+
//~| NOTE generator is not `Send`
18+
yield;
19+
let _x = x;
20+
});
21+
//~^^ NOTE captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync`
22+
//~| NOTE has type `&*mut ()` which is not `Send`, because `*mut ()` is not `Sync`
23+
assert_send(move || {
24+
//~^ ERROR generator cannot be sent between threads safely
25+
//~| NOTE generator is not `Send`
26+
yield;
27+
let _y = y;
28+
});
29+
//~^^ NOTE captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send`
30+
//~| NOTE has type `&mut *mut ()` which is not `Send`, because `*mut ()` is not `Send`
31+
}

Diff for: src/test/ui/generator/ref-upvar-not-send.stderr

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: generator cannot be sent between threads safely
2+
--> $DIR/ref-upvar-not-send.rs:15:17
3+
|
4+
LL | assert_send(move || {
5+
| _________________^
6+
LL | |
7+
LL | |
8+
LL | | yield;
9+
LL | | let _x = x;
10+
LL | | });
11+
| |_____^ generator is not `Send`
12+
|
13+
= help: the trait `Sync` is not implemented for `*mut ()`
14+
note: captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync`
15+
--> $DIR/ref-upvar-not-send.rs:19:18
16+
|
17+
LL | let _x = x;
18+
| ^ has type `&*mut ()` which is not `Send`, because `*mut ()` is not `Sync`
19+
note: required by a bound in `assert_send`
20+
--> $DIR/ref-upvar-not-send.rs:6:19
21+
|
22+
LL | fn assert_send<T: Send>(_: T) {}
23+
| ^^^^ required by this bound in `assert_send`
24+
25+
error: generator cannot be sent between threads safely
26+
--> $DIR/ref-upvar-not-send.rs:23:17
27+
|
28+
LL | assert_send(move || {
29+
| _________________^
30+
LL | |
31+
LL | |
32+
LL | | yield;
33+
LL | | let _y = y;
34+
LL | | });
35+
| |_____^ generator is not `Send`
36+
|
37+
= help: within `[generator@$DIR/ref-upvar-not-send.rs:23:17: 23:24]`, the trait `Send` is not implemented for `*mut ()`
38+
note: captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send`
39+
--> $DIR/ref-upvar-not-send.rs:27:18
40+
|
41+
LL | let _y = y;
42+
| ^ has type `&mut *mut ()` which is not `Send`, because `*mut ()` is not `Send`
43+
note: required by a bound in `assert_send`
44+
--> $DIR/ref-upvar-not-send.rs:6:19
45+
|
46+
LL | fn assert_send<T: Send>(_: T) {}
47+
| ^^^^ required by this bound in `assert_send`
48+
49+
error: aborting due to 2 previous errors
50+

0 commit comments

Comments
 (0)