Skip to content

Folder experiment: Monomorphize region resolver #139287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions compiler/rustc_type_ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use tracing::{debug, instrument};

use crate::inherent::*;
use crate::visit::{TypeVisitable, TypeVisitableExt as _};
use crate::{self as ty, Interner};
use crate::{self as ty, Interner, TypeFlags};

#[cfg(feature = "nightly")]
type Never = !;
Expand Down Expand Up @@ -438,12 +438,12 @@ where
pub fn fold_regions<I: Interner, T>(
cx: I,
value: T,
mut f: impl FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
f: impl FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
) -> T
where
T: TypeFoldable<I>,
{
value.fold_with(&mut RegionFolder::new(cx, &mut f))
value.fold_with(&mut RegionFolder::new(cx, f))
}

/// Folds over the substructure of a type, visiting its component
Expand All @@ -453,7 +453,7 @@ where
/// new bound regions which are not visited by this visitors as
/// they are not free; only regions that occur free will be
/// visited by `fld_r`.
pub struct RegionFolder<'a, I: Interner> {
pub struct RegionFolder<I, F> {
cx: I,

/// Stores the index of a binder *just outside* the stuff we have
Expand All @@ -464,20 +464,21 @@ pub struct RegionFolder<'a, I: Interner> {
/// Callback invokes for each free region. The `DebruijnIndex`
/// points to the binder *just outside* the ones we have passed
/// through.
fold_region_fn: &'a mut (dyn FnMut(I::Region, ty::DebruijnIndex) -> I::Region + 'a),
fold_region_fn: F,
}

impl<'a, I: Interner> RegionFolder<'a, I> {
impl<I, F> RegionFolder<I, F> {
#[inline]
pub fn new(
cx: I,
fold_region_fn: &'a mut dyn FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
) -> RegionFolder<'a, I> {
pub fn new(cx: I, fold_region_fn: F) -> RegionFolder<I, F> {
RegionFolder { cx, current_index: ty::INNERMOST, fold_region_fn }
}
}

impl<'a, I: Interner> TypeFolder<I> for RegionFolder<'a, I> {
impl<I, F> TypeFolder<I> for RegionFolder<I, F>
where
I: Interner,
F: FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
{
fn cx(&self) -> I {
self.cx
}
Expand All @@ -502,4 +503,34 @@ impl<'a, I: Interner> TypeFolder<I> for RegionFolder<'a, I> {
}
}
}

fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
if t.has_type_flags(
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
) {
t.super_fold_with(self)
} else {
t
}
}

fn fold_const(&mut self, ct: I::Const) -> I::Const {
if ct.has_type_flags(
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
) {
ct.super_fold_with(self)
} else {
ct
}
}

fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
if p.has_type_flags(
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
) {
p.super_fold_with(self)
} else {
p
}
}
}
Loading