diff --git a/Cargo.lock b/Cargo.lock index af7c24abd132c..27eef7c9cdcd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3687,6 +3687,7 @@ dependencies = [ "tempfile", "thin-vec", "tracing", + "triomphe", "windows 0.61.3", ] @@ -4567,6 +4568,7 @@ dependencies = [ "smallvec", "tempfile", "thin-vec", + "triomphe", ] [[package]] @@ -5684,6 +5686,16 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "triomphe" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8f7726da4807b58ea5c96fdc122f80702030edc33b35aff9190a51148ccc85" +dependencies = [ + "serde", + "stable_deref_trait", +] + [[package]] name = "twox-hash" version = "1.6.3" diff --git a/Cargo.toml b/Cargo.toml index 5003e107cc8a4..8c51bbed76358 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ memchr = "2.7.5" rustc-literal-escaper = "0.0.5" thin-vec = "0.2.14" tracing = "0.1.37" +triomphe = "0.1.14" # tidy-alphabetical-end [profile.release.package.rustc_thread_pool] diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index f4f35a4d2ee07..38bf694cf1dc9 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -19,7 +19,7 @@ use std::sync::Arc; use std::{cmp, fmt, iter, mem}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_data_structures::sync; +use rustc_data_structures::sync::{self, TrArc}; use rustc_macros::{Decodable, Encodable, HashStable_Generic, Walkable}; use rustc_serialize::{Decodable, Encodable}; use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym}; @@ -558,7 +558,7 @@ pub struct AttrsTarget { /// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s. #[derive(Clone, Debug, Default, Encodable, Decodable)] -pub struct TokenStream(pub(crate) Arc>); +pub struct TokenStream(pub(crate) TrArc>); /// Indicates whether a token can join with the following token to form a /// compound token. Used for conversions to `proc_macro::Spacing`. Also used to @@ -675,7 +675,7 @@ impl PartialEq for TokenStream { impl TokenStream { pub fn new(tts: Vec) -> TokenStream { - TokenStream(Arc::new(tts)) + TokenStream(TrArc::new(tts)) } pub fn is_empty(&self) -> bool { @@ -728,7 +728,7 @@ impl TokenStream { /// Push `tt` onto the end of the stream, possibly gluing it to the last /// token. Uses `make_mut` to maximize efficiency. pub fn push_tree(&mut self, tt: TokenTree) { - let vec_mut = Arc::make_mut(&mut self.0); + let vec_mut = TrArc::make_mut(&mut self.0); if Self::try_glue_to_last(vec_mut, &tt) { // nothing else to do @@ -741,7 +741,7 @@ impl TokenStream { /// token tree to the last token. (No other token trees will be glued.) /// Uses `make_mut` to maximize efficiency. pub fn push_stream(&mut self, stream: TokenStream) { - let vec_mut = Arc::make_mut(&mut self.0); + let vec_mut = TrArc::make_mut(&mut self.0); let stream_iter = stream.0.iter().cloned(); @@ -761,7 +761,7 @@ impl TokenStream { } /// Desugar doc comments like `/// foo` in the stream into `#[doc = - /// r"foo"]`. Modifies the `TokenStream` via `Arc::make_mut`, but as little + /// r"foo"]`. Modifies the `TokenStream` via `TrArc::make_mut`, but as little /// as possible. pub fn desugar_doc_comments(&mut self) { if let Some(desugared_stream) = desugar_inner(self.clone()) { @@ -780,7 +780,7 @@ impl TokenStream { ) => { let desugared = desugared_tts(attr_style, data, span); let desugared_len = desugared.len(); - Arc::make_mut(&mut stream.0).splice(i..i + 1, desugared); + TrArc::make_mut(&mut stream.0).splice(i..i + 1, desugared); modified = true; i += desugared_len; } @@ -791,7 +791,7 @@ impl TokenStream { if let Some(desugared_delim_stream) = desugar_inner(delim_stream.clone()) { let new_tt = TokenTree::Delimited(sp, spacing, delim, desugared_delim_stream); - Arc::make_mut(&mut stream.0)[i] = new_tt; + TrArc::make_mut(&mut stream.0)[i] = new_tt; modified = true; } i += 1; diff --git a/compiler/rustc_data_structures/Cargo.toml b/compiler/rustc_data_structures/Cargo.toml index aa964806a870b..c3a7414f1068a 100644 --- a/compiler/rustc_data_structures/Cargo.toml +++ b/compiler/rustc_data_structures/Cargo.toml @@ -27,6 +27,7 @@ stacker = "0.1.17" tempfile = "3.2" thin-vec.workspace = true tracing.workspace = true +triomphe.workspace = true # tidy-alphabetical-end [dependencies.hashbrown] diff --git a/compiler/rustc_data_structures/src/marker.rs b/compiler/rustc_data_structures/src/marker.rs index 2be9ba292f976..e56e9a05d42a4 100644 --- a/compiler/rustc_data_structures/src/marker.rs +++ b/compiler/rustc_data_structures/src/marker.rs @@ -75,6 +75,9 @@ impl_dyn_send!( [std::sync::Mutex where T: ?Sized+ DynSend] [std::sync::mpsc::Sender where T: DynSend] [std::sync::Arc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrArc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrUniqueArc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrThinArc where H: DynSync + DynSend, T: DynSync + DynSend] [std::sync::LazyLock where T: DynSend, F: DynSend] [std::collections::HashSet where K: DynSend, S: DynSend] [std::collections::HashMap where K: DynSend, V: DynSend, S: DynSend] @@ -157,6 +160,9 @@ impl_dyn_sync!( [std::sync::OnceLock where T: DynSend + DynSync] [std::sync::Mutex where T: ?Sized + DynSend] [std::sync::Arc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrArc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrUniqueArc where T: ?Sized + DynSync + DynSend] + [crate::sync::TrThinArc where H: DynSync + DynSend, T: DynSync + DynSend] [std::sync::LazyLock where T: DynSend + DynSync, F: DynSend] [std::collections::HashSet where K: DynSync, S: DynSync] [std::collections::HashMap where K: DynSync, V: DynSync, S: DynSync] diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs index 3881f3c2aa841..47e24f331abba 100644 --- a/compiler/rustc_data_structures/src/sync.rs +++ b/compiler/rustc_data_structures/src/sync.rs @@ -105,6 +105,15 @@ mod mode { } } +/// Alternative form of `Arc` that lacks a weak ref count. +pub type TrArc = triomphe::Arc; + +/// Alternative form of `UniqueArc` that lacks a weak ref count. +pub type TrUniqueArc = triomphe::UniqueArc; + +/// Like `TrArc<(H, [T])>`, but with a thin pointer. +pub type TrThinArc = triomphe::ThinArc; + // FIXME(parallel_compiler): Get rid of these aliases across the compiler. #[derive(Debug, Default)] diff --git a/compiler/rustc_serialize/Cargo.toml b/compiler/rustc_serialize/Cargo.toml index e9959735f9f2e..e66846cc9840f 100644 --- a/compiler/rustc_serialize/Cargo.toml +++ b/compiler/rustc_serialize/Cargo.toml @@ -9,6 +9,7 @@ indexmap = "2.0.0" rustc_hashes = { path = "../rustc_hashes" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } thin-vec.workspace = true +triomphe.workspace = true # tidy-alphabetical-end [dev-dependencies] diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 1cb09e8a1ee2e..1fd92461fcb63 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -509,6 +509,18 @@ impl> Decodable for Arc { } } +impl> Encodable for triomphe::Arc { + fn encode(&self, s: &mut S) { + (**self).encode(s); + } +} + +impl> Decodable for triomphe::Arc { + fn decode(d: &mut D) -> triomphe::Arc { + triomphe::Arc::new(Decodable::decode(d)) + } +} + impl> Encodable for Box { fn encode(&self, s: &mut S) { (**self).encode(s) diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index 18d97a748ba9f..6066d97180aaf 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -394,6 +394,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "tracing-log", "tracing-subscriber", "tracing-tree", + "triomphe", "twox-hash", "type-map", "typenum",