Skip to content

Commit f22bb8e

Browse files
committed
Generify traversals of interned types
1 parent 5744209 commit f22bb8e

File tree

5 files changed

+34
-57
lines changed

5 files changed

+34
-57
lines changed

compiler/rustc_data_structures/src/intern.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ mod private {
2424
#[rustc_pass_by_value]
2525
pub struct Interned<'a, T>(pub &'a T, pub private::PrivateZst);
2626

27+
pub trait Internable<'a, I>: Sized {
28+
fn intern(self, interner: I) -> Interned<'a, Self>;
29+
}
30+
2731
impl<'a, T> Interned<'a, T> {
2832
/// Create a new `Interned` value. The value referred to *must* be interned
2933
/// and thus be unique, and it *must* remain unique in the future. This

compiler/rustc_middle/src/traits/solve.rs

+3-51
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use rustc_data_structures::intern::Interned;
33
use crate::infer::canonical::{CanonicalVarValues, QueryRegionConstraints};
44
use crate::traits::query::NoSolution;
55
use crate::traits::{Canonical, DefiningAnchor};
6-
use crate::ty::{
7-
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitor,
8-
};
6+
use crate::ty::{self, ToPredicate, Ty, TyCtxt};
97
use rustc_span::def_id::DefId;
108

119
use super::BuiltinImplSource;
@@ -112,7 +110,7 @@ pub struct PredefinedOpaquesData<'tcx> {
112110
pub opaque_types: Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>,
113111
}
114112

115-
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, HashStable)]
113+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, HashStable, TypeVisitable, TypeFoldable)]
116114
pub struct PredefinedOpaques<'tcx>(pub(crate) Interned<'tcx, PredefinedOpaquesData<'tcx>>);
117115

118116
impl<'tcx> std::ops::Deref for PredefinedOpaques<'tcx> {
@@ -135,7 +133,7 @@ pub type CanonicalResponse<'tcx> = Canonical<'tcx, Response<'tcx>>;
135133
/// solver, merge the two responses again.
136134
pub type QueryResult<'tcx> = Result<CanonicalResponse<'tcx>, NoSolution>;
137135

138-
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, HashStable)]
136+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, HashStable, TypeFoldable, TypeVisitable)]
139137
pub struct ExternalConstraints<'tcx>(pub(crate) Interned<'tcx, ExternalConstraintsData<'tcx>>);
140138

141139
impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
@@ -154,52 +152,6 @@ pub struct ExternalConstraintsData<'tcx> {
154152
pub opaque_types: Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>,
155153
}
156154

157-
// FIXME: Having to clone `region_constraints` for folding feels bad and
158-
// probably isn't great wrt performance.
159-
//
160-
// Not sure how to fix this, maybe we should also intern `opaque_types` and
161-
// `region_constraints` here or something.
162-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
163-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
164-
self,
165-
folder: &mut F,
166-
) -> Result<Self, F::Error> {
167-
Ok(folder.interner().mk_external_constraints((*self).clone().try_fold_with(folder)?))
168-
}
169-
}
170-
171-
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
172-
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(
173-
&self,
174-
visitor: &mut V,
175-
) -> std::ops::ControlFlow<V::BreakTy> {
176-
(**self).visit_with(visitor)
177-
}
178-
}
179-
180-
// FIXME: Having to clone `region_constraints` for folding feels bad and
181-
// probably isn't great wrt performance.
182-
//
183-
// Not sure how to fix this, maybe we should also intern `opaque_types` and
184-
// `region_constraints` here or something.
185-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for PredefinedOpaques<'tcx> {
186-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
187-
self,
188-
folder: &mut F,
189-
) -> Result<Self, F::Error> {
190-
Ok(folder.interner().mk_predefined_opaques_in_body((*self).clone().try_fold_with(folder)?))
191-
}
192-
}
193-
194-
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for PredefinedOpaques<'tcx> {
195-
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(
196-
&self,
197-
visitor: &mut V,
198-
) -> std::ops::ControlFlow<V::BreakTy> {
199-
(**self).visit_with(visitor)
200-
}
201-
}
202-
203155
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable)]
204156
pub enum IsNormalizesToHack {
205157
Yes,

compiler/rustc_middle/src/ty/context.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
3535
use rustc_data_structures::fingerprint::Fingerprint;
3636
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
37-
use rustc_data_structures::intern::Interned;
37+
use rustc_data_structures::intern::{Internable, Interned};
3838
use rustc_data_structures::profiling::SelfProfilerRef;
3939
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
4040
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1549,11 +1549,17 @@ macro_rules! direct_interners {
15491549
}
15501550
}
15511551

1552+
impl<'tcx> Internable<'tcx, TyCtxt<'tcx>> for $ty {
1553+
fn intern(self, tcx: TyCtxt<'tcx>) -> Interned<'tcx, Self> {
1554+
Interned::new_unchecked(tcx.interners.$name.intern(self, |v| {
1555+
InternedInSet(tcx.interners.arena.alloc(v))
1556+
}).0)
1557+
}
1558+
}
1559+
15521560
impl<'tcx> TyCtxt<'tcx> {
15531561
$vis fn $method(self, v: $ty) -> $ret_ty {
1554-
$ret_ctor(Interned::new_unchecked(self.interners.$name.intern(v, |v| {
1555-
InternedInSet(self.interners.arena.alloc(v))
1556-
}).0))
1562+
$ret_ctor(v.intern(self))
15571563
}
15581564
})+
15591565
}

compiler/rustc_type_ir/src/fold.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
//! - u.fold_with(folder)
4646
//! ```
4747
48-
use rustc_data_structures::sync::Lrc;
48+
use rustc_data_structures::{
49+
intern::{Internable, Interned},
50+
sync::Lrc,
51+
};
4952
use rustc_index::{Idx, IndexVec};
5053
use std::marker::PhantomData;
5154
use std::mem;
@@ -403,3 +406,9 @@ impl<I: Interner, T: TypeFoldable<I>, Ix: Idx> TypeFoldable<I> for IndexVec<Ix,
403406
self.raw.try_fold_with(folder).map(IndexVec::from_raw)
404407
}
405408
}
409+
410+
impl<'a, I: Interner, T: Internable<'a, I> + TypeFoldable<I>> TypeFoldable<I> for Interned<'a, T> {
411+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
412+
(*self).clone().try_fold_with(folder).map(|v| v.intern(folder.interner()))
413+
}
414+
}

compiler/rustc_type_ir/src/visit.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! - u.visit_with(visitor)
4242
//! ```
4343
44-
use rustc_data_structures::sync::Lrc;
44+
use rustc_data_structures::{intern::Interned, sync::Lrc};
4545
use rustc_index::{Idx, IndexVec};
4646
use std::fmt;
4747
use std::marker::PhantomData;
@@ -250,3 +250,9 @@ impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix
250250
self.iter().try_for_each(|t| t.visit_with(visitor))
251251
}
252252
}
253+
254+
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Interned<'_, T> {
255+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
256+
(**self).visit_with(visitor)
257+
}
258+
}

0 commit comments

Comments
 (0)