Skip to content

Commit

Permalink
Auto merge of #116830 - nnethercote:rustc_type_ir, r=compiler-errors
Browse files Browse the repository at this point in the history
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.

r? `@BoxyUwU`
  • Loading branch information
bors committed Oct 17, 2023
2 parents 09df610 + 178ba0e commit b534e74
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 128 deletions.
116 changes: 0 additions & 116 deletions compiler/rustc_data_structures/src/functor.rs

This file was deleted.

5 changes: 0 additions & 5 deletions compiler/rustc_data_structures/src/lib.rs
Expand Up @@ -10,7 +10,6 @@
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
#![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)]
Expand All @@ -21,15 +20,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(vec_into_raw_parts)]
#![feature(allocator_api)]
#![feature(get_mut_unchecked)]
#![feature(lint_reasons)]
#![feature(unwrap_infallible)]
#![feature(strict_provenance)]
Expand Down Expand Up @@ -65,7 +61,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;
Expand Down
2 changes: 2 additions & 0 deletions 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)]
Expand Down
46 changes: 39 additions & 7 deletions compiler/rustc_type_ir/src/structural_impls.rs
Expand Up @@ -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;

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -108,8 +108,39 @@ impl<I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>> TypeVisitable<I> for
}

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Lrc<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
self.try_map_id(|value| value.try_fold_with(folder))
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
// 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<ManuallyDrop<T>>` so that
// panicking during `make_mut` does not leak the `T`.
Lrc::make_mut(&mut self);

// Casting to `Lrc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
// is `repr(transparent)`.
let ptr = Lrc::into_raw(self).cast::<mem::ManuallyDrop<T>>();
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<T>`.
Ok(Lrc::from_raw(Lrc::into_raw(unique).cast()))
}
}
}

Expand All @@ -120,8 +151,9 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Lrc<T> {
}

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
self.try_map_id(|value| value.try_fold_with(folder))
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
*self = (*self).try_fold_with(folder)?;
Ok(self)
}
}

Expand All @@ -133,7 +165,7 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Box<T> {

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Vec<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
self.try_map_id(|t| t.try_fold_with(folder))
self.into_iter().map(|t| t.try_fold_with(folder)).collect()
}
}

Expand Down Expand Up @@ -161,7 +193,7 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Box<[T]> {

impl<I: Interner, T: TypeFoldable<I>, Ix: Idx> TypeFoldable<I> for IndexVec<Ix, T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
self.try_map_id(|x| x.try_fold_with(folder))
self.raw.try_fold_with(folder).map(IndexVec::from_raw)
}
}

Expand Down

0 comments on commit b534e74

Please sign in to comment.