Skip to content

Commit 82bf372

Browse files
authored
Rollup merge of rust-lang#106944 - Nilstrieb:there-once-was-a-diagnostic, r=WaffleLapkin
Suggest using a lock for `*Cell: Sync` bounds I mostly did this for `OnceCell<T>` at first because users will be confused to see that the `OnceCell<T>` in `std` isn't `Sync` but then extended it to `Cell<T>` and `RefCell<T>` as well.
2 parents f8172e1 + 7be43aa commit 82bf372

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

core/src/cell/once.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@ impl<T> const From<T> for OnceCell<T> {
298298
OnceCell { inner: UnsafeCell::new(Some(value)) }
299299
}
300300
}
301+
302+
// Just like for `Cell<T>` this isn't needed, but results in nicer error messages.
303+
#[unstable(feature = "once_cell", issue = "74465")]
304+
impl<T> !Sync for OnceCell<T> {}

core/src/marker.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,62 @@ pub macro Copy($item:item) {
469469
#[cfg_attr(not(test), rustc_diagnostic_item = "Sync")]
470470
#[lang = "sync"]
471471
#[rustc_on_unimplemented(
472+
on(
473+
_Self = "std::cell::OnceCell<T>",
474+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::OnceLock` instead"
475+
),
476+
on(
477+
_Self = "std::cell::Cell<u8>",
478+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU8` instead",
479+
),
480+
on(
481+
_Self = "std::cell::Cell<u16>",
482+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU16` instead",
483+
),
484+
on(
485+
_Self = "std::cell::Cell<u32>",
486+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU32` instead",
487+
),
488+
on(
489+
_Self = "std::cell::Cell<u64>",
490+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU64` instead",
491+
),
492+
on(
493+
_Self = "std::cell::Cell<usize>",
494+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicUsize` instead",
495+
),
496+
on(
497+
_Self = "std::cell::Cell<i8>",
498+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI8` instead",
499+
),
500+
on(
501+
_Self = "std::cell::Cell<i16>",
502+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI16` instead",
503+
),
504+
on(
505+
_Self = "std::cell::Cell<i32>",
506+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead",
507+
),
508+
on(
509+
_Self = "std::cell::Cell<i64>",
510+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI64` instead",
511+
),
512+
on(
513+
_Self = "std::cell::Cell<isize>",
514+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicIsize` instead",
515+
),
516+
on(
517+
_Self = "std::cell::Cell<bool>",
518+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicBool` instead",
519+
),
520+
on(
521+
_Self = "std::cell::Cell<T>",
522+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock`",
523+
),
524+
on(
525+
_Self = "std::cell::RefCell<T>",
526+
note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` instead",
527+
),
472528
message = "`{Self}` cannot be shared between threads safely",
473529
label = "`{Self}` cannot be shared between threads safely"
474530
)]

0 commit comments

Comments
 (0)