From 4175c9b59546b78d7bab2d17a5e8c87fc633b4a5 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Oct 2023 17:08:14 +1100 Subject: [PATCH 1/3] Remove unused features from `rustc_data_structures`. --- compiler/rustc_data_structures/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 461ec3a90ed97..1d1a5b4d5ee45 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -8,7 +8,6 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![feature(array_windows)] -#![feature(associated_type_bounds)] #![feature(auto_traits)] #![feature(cell_leak)] #![feature(core_intrinsics)] @@ -25,7 +24,6 @@ #![feature(negative_impls)] #![feature(test)] #![feature(thread_id_value)] -#![feature(vec_into_raw_parts)] #![feature(allocator_api)] #![feature(get_mut_unchecked)] #![feature(lint_reasons)] From 847c8ba70df6fd5772ad7e7f0df8cc3617812ea3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Oct 2023 16:50:25 +1100 Subject: [PATCH 2/3] Remove `IdFunctor` trait. It's defined in `rustc_data_structures` but is only used in `rustc_type_ir`. The code is shorter and easier to read if we remove this layer of abstraction and just do the things directly where they are needed. --- compiler/rustc_data_structures/src/functor.rs | 116 ------------------ compiler/rustc_data_structures/src/lib.rs | 3 - compiler/rustc_type_ir/src/lib.rs | 2 + .../rustc_type_ir/src/structural_impls.rs | 52 +++++++- 4 files changed, 48 insertions(+), 125 deletions(-) delete mode 100644 compiler/rustc_data_structures/src/functor.rs diff --git a/compiler/rustc_data_structures/src/functor.rs b/compiler/rustc_data_structures/src/functor.rs deleted file mode 100644 index e3fcaccb1bd5f..0000000000000 --- a/compiler/rustc_data_structures/src/functor.rs +++ /dev/null @@ -1,116 +0,0 @@ -use rustc_index::{Idx, IndexVec}; -use std::{mem, rc::Rc, sync::Arc}; - -pub trait IdFunctor: Sized { - type Inner; - - fn try_map_id(self, f: F) -> Result - where - F: FnMut(Self::Inner) -> Result; -} - -impl IdFunctor for Box { - type Inner = T; - - #[inline] - fn try_map_id(self, mut f: F) -> Result - where - F: FnMut(Self::Inner) -> Result, - { - let raw = Box::into_raw(self); - Ok(unsafe { - // SAFETY: The raw pointer points to a valid value of type `T`. - let value = raw.read(); - // SAFETY: Converts `Box` to `Box>` which is the - // inverse of `Box::assume_init()` and should be safe. - let raw: Box> = Box::from_raw(raw.cast()); - // SAFETY: Write the mapped value back into the `Box`. - Box::write(raw, f(value)?) - }) - } -} - -impl IdFunctor for Vec { - type Inner = T; - - #[inline] - fn try_map_id(self, f: F) -> Result - where - F: FnMut(Self::Inner) -> Result, - { - self.into_iter().map(f).collect() - } -} - -impl IdFunctor for Box<[T]> { - type Inner = T; - - #[inline] - fn try_map_id(self, f: F) -> Result - where - F: FnMut(Self::Inner) -> Result, - { - Vec::from(self).try_map_id(f).map(Into::into) - } -} - -impl IdFunctor for IndexVec { - type Inner = T; - - #[inline] - fn try_map_id(self, f: F) -> Result - where - F: FnMut(Self::Inner) -> Result, - { - self.raw.try_map_id(f).map(IndexVec::from_raw) - } -} - -macro_rules! rc { - ($($rc:ident),+) => {$( - impl IdFunctor for $rc { - type Inner = T; - - #[inline] - fn try_map_id(mut self, mut f: F) -> Result - where - F: FnMut(Self::Inner) -> Result, - { - // We merely want to replace the contained `T`, if at all possible, - // so that we don't needlessly allocate a new `$rc` or indeed clone - // the contained type. - unsafe { - // First step is to ensure that we have a unique reference to - // the contained type, which `$rc::make_mut` will accomplish (by - // allocating a new `$rc` and cloning the `T` only if required). - // This is done *before* casting to `$rc>` so that - // panicking during `make_mut` does not leak the `T`. - $rc::make_mut(&mut self); - - // Casting to `$rc>` is safe because `ManuallyDrop` - // is `repr(transparent)`. - let ptr = $rc::into_raw(self).cast::>(); - let mut unique = $rc::from_raw(ptr); - - // Call to `$rc::make_mut` above guarantees that `unique` is the - // sole reference to the contained value, so we can avoid doing - // a checked `get_mut` here. - let slot = $rc::get_mut_unchecked(&mut unique); - - // Semantically move the contained type out from `unique`, fold - // it, then move the folded value back into `unique`. Should - // folding fail, `ManuallyDrop` ensures that the "moved-out" - // value is not re-dropped. - let owned = mem::ManuallyDrop::take(slot); - let folded = f(owned)?; - *slot = mem::ManuallyDrop::new(folded); - - // Cast back to `$rc`. - Ok($rc::from_raw($rc::into_raw(unique).cast())) - } - } - } - )+}; -} - -rc! { Rc, Arc } diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 1d1a5b4d5ee45..4dd4ade4e6bef 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -18,14 +18,12 @@ #![feature(min_specialization)] #![feature(never_type)] #![feature(type_alias_impl_trait)] -#![feature(new_uninit)] #![feature(lazy_cell)] #![feature(rustc_attrs)] #![feature(negative_impls)] #![feature(test)] #![feature(thread_id_value)] #![feature(allocator_api)] -#![feature(get_mut_unchecked)] #![feature(lint_reasons)] #![feature(unwrap_infallible)] #![feature(strict_provenance)] @@ -61,7 +59,6 @@ pub mod binary_search_util; pub mod captures; pub mod flat_map_in_place; pub mod flock; -pub mod functor; pub mod fx; pub mod graph; pub mod intern; diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 5df068de1f849..9f8d9f02ec24c 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -1,7 +1,9 @@ #![feature(associated_type_defaults)] #![feature(fmt_helpers_for_derive)] +#![feature(get_mut_unchecked)] #![feature(min_specialization)] #![feature(never_type)] +#![feature(new_uninit)] #![feature(rustc_attrs)] #![feature(unwrap_infallible)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_type_ir/src/structural_impls.rs b/compiler/rustc_type_ir/src/structural_impls.rs index f1037fe0bafa5..18ff33f6997f3 100644 --- a/compiler/rustc_type_ir/src/structural_impls.rs +++ b/compiler/rustc_type_ir/src/structural_impls.rs @@ -5,12 +5,12 @@ use crate::fold::{FallibleTypeFolder, TypeFoldable}; use crate::visit::{TypeVisitable, TypeVisitor}; use crate::{ConstKind, FloatTy, InferTy, IntTy, Interner, UintTy, UniverseIndex}; -use rustc_data_structures::functor::IdFunctor; use rustc_data_structures::sync::Lrc; use rustc_index::{Idx, IndexVec}; use core::fmt; use std::marker::PhantomData; +use std::mem; use std::ops::ControlFlow; /////////////////////////////////////////////////////////////////////////// @@ -108,8 +108,39 @@ impl, E: TypeVisitable> TypeVisitable for } impl> TypeFoldable for Lrc { - fn try_fold_with>(self, folder: &mut F) -> Result { - self.try_map_id(|value| value.try_fold_with(folder)) + fn try_fold_with>(mut self, folder: &mut F) -> Result { + // We merely want to replace the contained `T`, if at all possible, + // so that we don't needlessly allocate a new `Lrc` or indeed clone + // the contained type. + unsafe { + // First step is to ensure that we have a unique reference to + // the contained type, which `Lrc::make_mut` will accomplish (by + // allocating a new `Lrc` and cloning the `T` only if required). + // This is done *before* casting to `Lrc>` so that + // panicking during `make_mut` does not leak the `T`. + Lrc::make_mut(&mut self); + + // Casting to `Lrc>` is safe because `ManuallyDrop` + // is `repr(transparent)`. + let ptr = Lrc::into_raw(self).cast::>(); + let mut unique = Lrc::from_raw(ptr); + + // Call to `Lrc::make_mut` above guarantees that `unique` is the + // sole reference to the contained value, so we can avoid doing + // a checked `get_mut` here. + let slot = Lrc::get_mut_unchecked(&mut unique); + + // Semantically move the contained type out from `unique`, fold + // it, then move the folded value back into `unique`. Should + // folding fail, `ManuallyDrop` ensures that the "moved-out" + // value is not re-dropped. + let owned = mem::ManuallyDrop::take(slot); + let folded = owned.try_fold_with(folder)?; + *slot = mem::ManuallyDrop::new(folded); + + // Cast back to `Lrc`. + Ok(Lrc::from_raw(Lrc::into_raw(unique).cast())) + } } } @@ -121,7 +152,16 @@ impl> TypeVisitable for Lrc { impl> TypeFoldable for Box { fn try_fold_with>(self, folder: &mut F) -> Result { - self.try_map_id(|value| value.try_fold_with(folder)) + let raw = Box::into_raw(self); + Ok(unsafe { + // SAFETY: The raw pointer points to a valid value of type `T`. + let value = raw.read(); + // SAFETY: Converts `Box` to `Box>` which is the + // inverse of `Box::assume_init()` and should be safe. + let raw: Box> = Box::from_raw(raw.cast()); + // SAFETY: Write the mapped value back into the `Box`. + Box::write(raw, value.try_fold_with(folder)?) + }) } } @@ -133,7 +173,7 @@ impl> TypeVisitable for Box { impl> TypeFoldable for Vec { fn try_fold_with>(self, folder: &mut F) -> Result { - self.try_map_id(|t| t.try_fold_with(folder)) + self.into_iter().map(|t| t.try_fold_with(folder)).collect() } } @@ -161,7 +201,7 @@ impl> TypeVisitable for Box<[T]> { impl, Ix: Idx> TypeFoldable for IndexVec { fn try_fold_with>(self, folder: &mut F) -> Result { - self.try_map_id(|x| x.try_fold_with(folder)) + self.raw.try_fold_with(folder).map(IndexVec::from_raw) } } From 178ba0e85cc07995d9ac420875995efa26585b59 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 17 Oct 2023 13:44:25 +1100 Subject: [PATCH 3/3] Rewrite `Box::try_fold_with`. It can be written more simply, without needing `unsafe`. --- compiler/rustc_type_ir/src/structural_impls.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_type_ir/src/structural_impls.rs b/compiler/rustc_type_ir/src/structural_impls.rs index 18ff33f6997f3..08af96ea15f0f 100644 --- a/compiler/rustc_type_ir/src/structural_impls.rs +++ b/compiler/rustc_type_ir/src/structural_impls.rs @@ -151,17 +151,9 @@ impl> TypeVisitable for Lrc { } impl> TypeFoldable for Box { - fn try_fold_with>(self, folder: &mut F) -> Result { - let raw = Box::into_raw(self); - Ok(unsafe { - // SAFETY: The raw pointer points to a valid value of type `T`. - let value = raw.read(); - // SAFETY: Converts `Box` to `Box>` which is the - // inverse of `Box::assume_init()` and should be safe. - let raw: Box> = Box::from_raw(raw.cast()); - // SAFETY: Write the mapped value back into the `Box`. - Box::write(raw, value.try_fold_with(folder)?) - }) + fn try_fold_with>(mut self, folder: &mut F) -> Result { + *self = (*self).try_fold_with(folder)?; + Ok(self) } }