Skip to content

Commit 11296fa

Browse files
committed
Generify traversals of interned types
1 parent b0ccdac commit 11296fa

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

compiler/rustc_data_structures/src/intern.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use rustc_query_system::cache::Cache;
44
use crate::infer::canonical::{CanonicalVarValues, QueryRegionConstraints};
55
use crate::traits::query::NoSolution;
66
use crate::traits::Canonical;
7-
use crate::ty::{
8-
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitor,
9-
};
7+
use crate::ty::{self, ToPredicate, Ty, TyCtxt};
108

119
pub type EvaluationCache<'tcx> = Cache<CanonicalGoal<'tcx>, QueryResult<'tcx>>;
1210

@@ -96,7 +94,7 @@ pub type CanonicalResponse<'tcx> = Canonical<'tcx, Response<'tcx>>;
9694
/// solver, merge the two responses again.
9795
pub type QueryResult<'tcx> = Result<CanonicalResponse<'tcx>, NoSolution>;
9896

99-
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
97+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, TypeFoldable, TypeVisitable)]
10098
pub struct ExternalConstraints<'tcx>(pub(crate) Interned<'tcx, ExternalConstraintsData<'tcx>>);
10199

102100
impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
@@ -114,26 +112,3 @@ pub struct ExternalConstraintsData<'tcx> {
114112
pub region_constraints: QueryRegionConstraints<'tcx>,
115113
pub opaque_types: Vec<(Ty<'tcx>, Ty<'tcx>)>,
116114
}
117-
118-
// FIXME: Having to clone `region_constraints` for folding feels bad and
119-
// probably isn't great wrt performance.
120-
//
121-
// Not sure how to fix this, maybe we should also intern `opaque_types` and
122-
// `region_constraints` here or something.
123-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
124-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
125-
self,
126-
folder: &mut F,
127-
) -> Result<Self, F::Error> {
128-
Ok(folder.interner().mk_external_constraints((*self).clone().try_fold_with(folder)?))
129-
}
130-
}
131-
132-
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
133-
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(
134-
&self,
135-
visitor: &mut V,
136-
) -> std::ops::ControlFlow<V::BreakTy> {
137-
(**self).visit_with(visitor)
138-
}
139-
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::ty::{GenericArg, InternalSubsts, SubstsRef};
3030
use rustc_ast as ast;
3131
use rustc_data_structures::fingerprint::Fingerprint;
3232
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33-
use rustc_data_structures::intern::Interned;
33+
use rustc_data_structures::intern::{Internable, Interned};
3434
use rustc_data_structures::memmap::Mmap;
3535
use rustc_data_structures::profiling::SelfProfilerRef;
3636
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
@@ -1555,11 +1555,17 @@ macro_rules! direct_interners {
15551555
}
15561556
}
15571557

1558+
impl<'tcx> Internable<'tcx, TyCtxt<'tcx>> for $ty {
1559+
fn intern(self, tcx: TyCtxt<'tcx>) -> Interned<'tcx, Self> {
1560+
Interned::new_unchecked(tcx.interners.$name.intern(self, |v| {
1561+
InternedInSet(tcx.interners.arena.alloc(v))
1562+
}).0)
1563+
}
1564+
}
1565+
15581566
impl<'tcx> TyCtxt<'tcx> {
15591567
$vis fn $method(self, v: $ty) -> $ret_ty {
1560-
$ret_ctor(Interned::new_unchecked(self.interners.$name.intern(v, |v| {
1561-
InternedInSet(self.interners.arena.alloc(v))
1562-
}).0))
1568+
$ret_ctor(v.intern(self))
15631569
}
15641570
})+
15651571
}

compiler/rustc_type_ir/src/structural_impls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use crate::fold::{FallibleTypeFolder, TypeFoldable};
66
use crate::visit::{TypeVisitable, TypeVisitor};
77
use crate::Interner;
8-
use rustc_data_structures::functor::IdFunctor;
8+
use rustc_data_structures::{
9+
functor::IdFunctor,
10+
intern::{Internable, Interned},
11+
};
912
use rustc_index::vec::{Idx, IndexVec};
1013

1114
use std::ops::ControlFlow;
@@ -189,3 +192,15 @@ impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix
189192
self.iter().try_for_each(|t| t.visit_with(visitor))
190193
}
191194
}
195+
196+
impl<'a, I: Interner, T: Internable<'a, I> + TypeFoldable<I>> TypeFoldable<I> for Interned<'a, T> {
197+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
198+
(*self).clone().try_fold_with(folder).map(|v| v.intern(folder.interner()))
199+
}
200+
}
201+
202+
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Interned<'_, T> {
203+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
204+
(**self).visit_with(visitor)
205+
}
206+
}

0 commit comments

Comments
 (0)