Skip to content

Commit 261b727

Browse files
committed
fix some nits
1 parent b9746ce commit 261b727

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

compiler/rustc_data_structures/src/marker.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,20 @@ cfg_if!(
88
} else {
99
#[rustc_on_unimplemented(
1010
message = "`{Self}` doesn't implement `DynSend`. \
11-
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Send`",
12-
label = "`{Self}` doesn't implement `DynSend`. \
1311
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Send`"
1412
)]
15-
// Ensure data structures is `Send` if `sync::active()` is true.
16-
// `sync::active()` should be checked before using these data structures.
17-
// Note: Ensure that the data structure **will not break**
18-
// thread safety after being created.
19-
//
20-
// `sync::active()` should be checked when downcasting these data structures
21-
// to `Send` via `FromDyn`.
13+
// This is an auto trait for types which can be sent across threads if `sync::active()`
14+
// is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
15+
// `Send` type in `IntoDyn` will create a `DynSend` type.
2216
pub unsafe auto trait DynSend {}
2317

2418
#[rustc_on_unimplemented(
2519
message = "`{Self}` doesn't implement `DynSync`. \
26-
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`",
27-
label = "`{Self}` doesn't implement `DynSync`. \
2820
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`"
2921
)]
30-
// Ensure data structures is `Sync` if `sync::active()` is true.
31-
// Note: Ensure that the data structure **will not break**
32-
// thread safety after being checked.
33-
//
34-
// `sync::active()` should be checked when downcasting these data structures
35-
// to `Send` via `FromDyn`.
22+
// This is an auto trait for types which can be shared across threads if `sync::active()`
23+
// is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
24+
// `Sync` type in `IntoDyn` will create a `DynSync` type.
3625
pub unsafe auto trait DynSync {}
3726

3827
// Same with `Sync` and `Send`.
@@ -110,8 +99,8 @@ cfg_if!(
11099
[thin_vec::ThinVec<T> where T: DynSend]
111100
[smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
112101

113-
// We use `Send` here to omit some extra code, since they are only
114-
// used in `Send` situations now.
102+
// We use `Send` here, since they are only used in `Send` situations now.
103+
// In this case we don't need copy or change the codes in `crate::owning_ref`.
115104
[crate::owning_ref::OwningRef<O, T> where O: Send, T: ?Sized + Send]
116105
[crate::owning_ref::OwningRefMut<O, T> where O: Send, T: ?Sized + Send]
117106
);
@@ -196,8 +185,8 @@ cfg_if!(
196185
[smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
197186
[thin_vec::ThinVec<T> where T: DynSync]
198187

199-
// We use `Sync` here to omit some extra code, since they are only
200-
// used in `Sync` situations now.
188+
// We use `Sync` here, since they are only used in `Sync` situations now.
189+
// In this case we don't need copy or change the codes in `crate::owning_ref`.
201190
[crate::owning_ref::OwningRef<O, T> where O: Sync, T: ?Sized + Sync]
202191
[crate::owning_ref::OwningRefMut<O, T> where O: Sync, T: ?Sized + Sync]
203192
);
@@ -213,11 +202,11 @@ pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {}
213202
pub struct FromDyn<T>(T);
214203

215204
impl<T> FromDyn<T> {
216-
// Check `sync::active()` when creating this structure
217-
// and downcasting to `Send`. So we can ensure it is
218-
// thread-safe.
219205
#[inline(always)]
220206
pub fn from(val: T) -> Self {
207+
// Check that `sync::active()` is true on creation so we can
208+
// implement `Send` and `Sync` for this structure when `T`
209+
// implements `DynSend` and `DynSync` respectively.
221210
#[cfg(parallel_compiler)]
222211
assert!(crate::sync::active());
223212
FromDyn(val)
@@ -229,11 +218,11 @@ impl<T> FromDyn<T> {
229218
}
230219
}
231220

232-
// `FromDyn` is `Send` if `T` is `DynSend`, since it check when created.
221+
// `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::active() is true.
233222
#[cfg(parallel_compiler)]
234223
unsafe impl<T: DynSend> Send for FromDyn<T> {}
235224

236-
// `FromDyn` is `Sync` if `T` is `DynSync`, since it check when created.
225+
// `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::active() is true.
237226
#[cfg(parallel_compiler)]
238227
unsafe impl<T: DynSync> Sync for FromDyn<T> {}
239228

compiler/rustc_data_structures/src/sync.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ cfg_if! {
181181

182182
#[macro_export]
183183
macro_rules! parallel {
184-
($($blocks:block),*) => {{
184+
($($blocks:block),*) => {
185185
// We catch panics here ensuring that all the blocks execute.
186186
// This makes behavior consistent with the parallel compiler.
187187
let mut panic = None;
@@ -197,7 +197,7 @@ cfg_if! {
197197
if let Some(panic) = panic {
198198
::std::panic::resume_unwind(panic);
199199
}
200-
}}
200+
}
201201
}
202202

203203
pub fn par_for_each_in<T: IntoIterator>(t: T, mut for_each: impl FnMut(T::Item) + Sync + Send) {
@@ -368,6 +368,7 @@ cfg_if! {
368368
}
369369
}
370370

371+
// This function only works when `mode::active()`.
371372
pub fn scope<'scope, OP, R>(op: OP) -> R
372373
where
373374
OP: FnOnce(&rayon::Scope<'scope>) -> R + DynSend,
@@ -381,24 +382,22 @@ cfg_if! {
381382
/// the current thread. Use that for the longest running block.
382383
#[macro_export]
383384
macro_rules! parallel {
384-
($fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => {
385-
parallel!($fblock [$block, $($c,)*] [$($rest),*])
385+
(impl $fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => {
386+
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
386387
};
387-
($fblock:block [$($blocks:expr,)*] []) => {
388-
{
389-
::rustc_data_structures::sync::scope(|s| {
390-
$(let block = rustc_data_structures::sync::FromDyn::from(|| $blocks);
391-
s.spawn(move |_| block.into_inner()());)*
392-
(|| $fblock)();
393-
});
394-
}
388+
(impl $fblock:block [$($blocks:expr,)*] []) => {
389+
::rustc_data_structures::sync::scope(|s| {
390+
$(let block = rustc_data_structures::sync::FromDyn::from(|| $blocks);
391+
s.spawn(move |_| block.into_inner()());)*
392+
(|| $fblock)();
393+
});
395394
};
396395
($fblock:block, $($blocks:block),*) => {
397396
if rustc_data_structures::sync::active() {
398397
// Reverse the order of the later blocks since Rayon executes them in reverse order
399398
// when using a single thread. This ensures the execution order matches that
400399
// of a single threaded rustc
401-
parallel!($fblock [] [$($blocks),*]);
400+
parallel!(impl $fblock [] [$($blocks),*]);
402401
} else {
403402
// We catch panics here ensuring that all the blocks execute.
404403
// This makes behavior consistent with the parallel compiler.

0 commit comments

Comments
 (0)