Skip to content

Commit 089a388

Browse files
committed
correct literals for dyn thread safe
1 parent 9f8ab2a commit 089a388

File tree

11 files changed

+48
-41
lines changed

11 files changed

+48
-41
lines changed

compiler/rustc_ast/src/tokenstream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum TokenTree {
4848
Delimited(DelimSpan, Delimiter, TokenStream),
4949
}
5050

51-
// Ensure all fields of `TokenTree` is `DynSend` and `DynSync`.
51+
// Ensure all fields of `TokenTree` are `DynSend` and `DynSync`.
5252
#[cfg(parallel_compiler)]
5353
fn _dummy()
5454
where

compiler/rustc_data_structures/src/marker.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,20 +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`"
11+
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`"
1212
)]
1313
// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
1414
// 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.
15+
// `Send` type in `IntoDynSyncSend` will create a `DynSend` type.
1616
pub unsafe auto trait DynSend {}
1717

1818
#[rustc_on_unimplemented(
1919
message = "`{Self}` doesn't implement `DynSync`. \
20-
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`"
20+
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Sync`"
2121
)]
2222
// This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()`
2323
// 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.
24+
// `Sync` type in `IntoDynSyncSend` will create a `DynSync` type.
2525
pub unsafe auto trait DynSync {}
2626

2727
// Same with `Sync` and `Send`.
@@ -234,23 +234,26 @@ impl<T> const std::ops::Deref for FromDyn<T> {
234234
}
235235
}
236236

237+
// A wrapper to convert a struct that is already a `Send` or `Sync` into
238+
// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
239+
// it automatically in some cases. (e.g. Box<dyn Send / Sync>)
237240
#[derive(Copy, Clone)]
238-
pub struct IntoDyn<T: ?Sized>(pub T);
241+
pub struct IntoDynSyncSend<T: ?Sized>(pub T);
239242

240243
#[cfg(parallel_compiler)]
241-
unsafe impl<T: ?Sized + Send> DynSend for IntoDyn<T> {}
244+
unsafe impl<T: ?Sized + Send> DynSend for IntoDynSyncSend<T> {}
242245
#[cfg(parallel_compiler)]
243-
unsafe impl<T: ?Sized + Sync> DynSync for IntoDyn<T> {}
246+
unsafe impl<T: ?Sized + Sync> DynSync for IntoDynSyncSend<T> {}
244247

245-
impl<T> const std::ops::Deref for IntoDyn<T> {
248+
impl<T> const std::ops::Deref for IntoDynSyncSend<T> {
246249
type Target = T;
247250

248251
fn deref(&self) -> &T {
249252
&self.0
250253
}
251254
}
252255

253-
impl<T> const std::ops::DerefMut for IntoDyn<T> {
256+
impl<T> const std::ops::DerefMut for IntoDynSyncSend<T> {
254257
fn deref_mut(&mut self) -> &mut T {
255258
&mut self.0
256259
}

compiler/rustc_data_structures/src/sync.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,25 @@ mod mode {
6161
use std::sync::atomic::AtomicU8;
6262

6363
const UNINITIALIZED: u8 = 0;
64-
const DYN_NOT_SYNC: u8 = 1;
65-
const DYN_SYNC: u8 = 2;
64+
const DYN_NOT_THREAD_SAFE: u8 = 1;
65+
const DYN_THREAD_SAFE: u8 = 2;
6666

67-
static DYN_SYNC_MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
67+
static DYN_THREAD_SAFE_MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
6868

69-
// Weather control thread safety dynamically
69+
// Whether thread safety is enabled (due to running under multiple threads).
7070
#[inline]
7171
pub fn is_dyn_thread_safe() -> bool {
72-
match DYN_SYNC_MODE.load(Ordering::Relaxed) {
73-
DYN_NOT_SYNC => false,
74-
DYN_SYNC => true,
75-
_ => panic!("uninitialized parallel mode!"),
72+
match DYN_THREAD_SAFE_MODE.load(Ordering::Relaxed) {
73+
DYN_NOT_THREAD_SAFE => false,
74+
DYN_THREAD_SAFE => true,
75+
_ => panic!("uninitialized dyn_thread_safe mode!"),
7676
}
7777
}
7878

7979
// Only set by the `-Z threads` compile option
80-
pub fn set_dyn_thread_safe_mode(parallel: bool) {
81-
let set: u8 = if parallel { DYN_SYNC } else { DYN_NOT_SYNC };
82-
let previous = DYN_SYNC_MODE.compare_exchange(
80+
pub fn set_dyn_thread_safe_mode(mode: bool) {
81+
let set: u8 = if mode { DYN_THREAD_SAFE } else { DYN_NOT_THREAD_SAFE };
82+
let previous = DYN_THREAD_SAFE_MODE.compare_exchange(
8383
UNINITIALIZED,
8484
set,
8585
Ordering::Relaxed,
@@ -401,7 +401,7 @@ cfg_if! {
401401
if rustc_data_structures::sync::is_dyn_thread_safe() {
402402
// Reverse the order of the later blocks since Rayon executes them in reverse order
403403
// when using a single thread. This ensures the execution order matches that
404-
// of a single threaded rustc
404+
// of a single threaded rustc.
405405
parallel!(impl $fblock [] [$($blocks),*]);
406406
} else {
407407
// We catch panics here ensuring that all the blocks execute.

compiler/rustc_driver_impl/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ fn run_compiler(
255255

256256
let sopts = config::build_session_options(&matches);
257257

258-
// Set parallel mode before thread pool creation as the session will already create locks.
259-
interface::set_parallel_mode(&sopts.unstable_opts);
258+
// Set parallel mode before thread pool creation, which will create `Lock`s.
259+
interface::set_thread_safe_mode(&sopts.unstable_opts);
260260

261261
if let Some(ref code) = matches.opt_str("explain") {
262262
handle_explain(diagnostics_registry(), code, sopts.error_format);

compiler/rustc_error_messages/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate tracing;
1111
use fluent_bundle::FluentResource;
1212
use fluent_syntax::parser::ParserError;
1313
use icu_provider_adapters::fallback::{LocaleFallbackProvider, LocaleFallbacker};
14-
use rustc_data_structures::sync::{IntoDyn, Lrc};
14+
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
1515
use rustc_fluent_macro::fluent_messages;
1616
use rustc_macros::{Decodable, Encodable};
1717
use rustc_span::Span;
@@ -38,16 +38,16 @@ pub use unic_langid::{langid, LanguageIdentifier};
3838
fluent_messages! { "../messages.ftl" }
3939

4040
pub type FluentBundle =
41-
IntoDyn<fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>>;
41+
IntoDynSyncSend<fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>>;
4242

4343
#[cfg(not(parallel_compiler))]
4444
fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
45-
IntoDyn(fluent_bundle::bundle::FluentBundle::new(locales))
45+
IntoDynSyncSend(fluent_bundle::bundle::FluentBundle::new(locales))
4646
}
4747

4848
#[cfg(parallel_compiler)]
4949
fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
50-
IntoDyn(fluent_bundle::bundle::FluentBundle::new_concurrent(locales))
50+
IntoDynSyncSend(fluent_bundle::bundle::FluentBundle::new_concurrent(locales))
5151
}
5252

5353
#[derive(Debug)]

compiler/rustc_errors/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use emitter::{is_case_difference, Emitter, EmitterWriter};
3232
use registry::Registry;
3333
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3434
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
35-
use rustc_data_structures::sync::{self, IntoDyn, Lock, Lrc};
35+
use rustc_data_structures::sync::{self, IntoDynSyncSend, Lock, Lrc};
3636
use rustc_data_structures::AtomicRef;
3737
pub use rustc_error_messages::{
3838
fallback_fluent_bundle, fluent_bundle, DelayDm, DiagnosticMessage, FluentBundle,
@@ -409,7 +409,7 @@ struct HandlerInner {
409409
err_count: usize,
410410
warn_count: usize,
411411
deduplicated_err_count: usize,
412-
emitter: IntoDyn<Box<dyn Emitter + sync::Send>>,
412+
emitter: IntoDynSyncSend<Box<dyn Emitter + sync::Send>>,
413413
delayed_span_bugs: Vec<DelayedDiagnostic>,
414414
delayed_good_path_bugs: Vec<DelayedDiagnostic>,
415415
/// This flag indicates that an expected diagnostic was emitted and suppressed.
@@ -605,7 +605,7 @@ impl Handler {
605605
warn_count: 0,
606606
deduplicated_err_count: 0,
607607
deduplicated_warn_count: 0,
608-
emitter: IntoDyn(emitter),
608+
emitter: IntoDynSyncSend(emitter),
609609
delayed_span_bugs: Vec::new(),
610610
delayed_good_path_bugs: Vec::new(),
611611
suppressed_expected_diag: false,

compiler/rustc_errors/src/tests.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::{TranslateError, TranslateErrorKind};
22
use crate::fluent_bundle::*;
33
use crate::translation::Translate;
44
use crate::FluentBundle;
5-
use rustc_data_structures::sync::{IntoDyn, Lrc};
5+
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
66
use rustc_error_messages::fluent_bundle::resolver::errors::{ReferenceKind, ResolverError};
77
use rustc_error_messages::langid;
88
use rustc_error_messages::DiagnosticMessage;
@@ -28,11 +28,13 @@ fn make_dummy(ftl: &'static str) -> Dummy {
2828

2929
#[cfg(parallel_compiler)]
3030
let mut bundle: FluentBundle =
31-
IntoDyn(crate::fluent_bundle::bundle::FluentBundle::new_concurrent(vec![langid_en]));
31+
IntoDynSyncSend(crate::fluent_bundle::bundle::FluentBundle::new_concurrent(vec![
32+
langid_en,
33+
]));
3234

3335
#[cfg(not(parallel_compiler))]
3436
let mut bundle: FluentBundle =
35-
IntoDyn(crate::fluent_bundle::bundle::FluentBundle::new(vec![langid_en]));
37+
IntoDynSyncSend(crate::fluent_bundle::bundle::FluentBundle::new(vec![langid_en]));
3638

3739
bundle.add_resource(resource).expect("Failed to add FTL resources to the bundle.");
3840

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Compiler {
6161
}
6262

6363
#[allow(rustc::bad_opt_access)]
64-
pub fn set_parallel_mode(sopts: &config::UnstableOptions) {
64+
pub fn set_thread_safe_mode(sopts: &config::UnstableOptions) {
6565
rustc_data_structures::sync::set_dyn_thread_safe_mode(sopts.threads > 1);
6666
}
6767

compiler/rustc_middle/src/ty/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'a, T: Copy> IntoIterator for &'a List<T> {
199199

200200
unsafe impl<T: Sync> Sync for List<T> {}
201201

202-
// We need this since `List` uses extern type `OpaqueListContents`
202+
// We need this since `List` uses extern type `OpaqueListContents`.
203203
#[cfg(parallel_compiler)]
204204
use rustc_data_structures::sync::DynSync;
205205
#[cfg(parallel_compiler)]

compiler/rustc_span/src/source_map.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub use crate::*;
1414

1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::stable_hasher::{Hash128, Hash64, StableHasher};
17-
use rustc_data_structures::sync::{AtomicU32, IntoDyn, Lrc, MappedReadGuard, ReadGuard, RwLock};
17+
use rustc_data_structures::sync::{
18+
AtomicU32, IntoDynSyncSend, Lrc, MappedReadGuard, ReadGuard, RwLock,
19+
};
1820
use std::cmp;
1921
use std::hash::Hash;
2022
use std::path::{self, Path, PathBuf};
@@ -176,7 +178,7 @@ pub struct SourceMap {
176178
used_address_space: AtomicU32,
177179

178180
files: RwLock<SourceMapFiles>,
179-
file_loader: IntoDyn<Box<dyn FileLoader + Sync + Send>>,
181+
file_loader: IntoDynSyncSend<Box<dyn FileLoader + Sync + Send>>,
180182
// This is used to apply the file path remapping as specified via
181183
// `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
182184
path_mapping: FilePathMapping,
@@ -202,7 +204,7 @@ impl SourceMap {
202204
SourceMap {
203205
used_address_space: AtomicU32::new(0),
204206
files: Default::default(),
205-
file_loader: IntoDyn(file_loader),
207+
file_loader: IntoDynSyncSend(file_loader),
206208
path_mapping,
207209
hash_kind,
208210
}

src/librustdoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ fn main_args(at_args: &[String]) -> MainResult {
730730
}
731731
};
732732

733-
// Set parallel mode early as the error handler will already create locks.
734-
interface::set_parallel_mode(&options.unstable_opts);
733+
// Set parallel mode before error handler creation, which will create `Lock`s.
734+
interface::set_thread_safe_mode(&options.unstable_opts);
735735

736736
let diag = core::new_handler(
737737
options.error_format,

0 commit comments

Comments
 (0)