Skip to content

Commit 1cffe2a

Browse files
committed
Tweak the slice interners.
All the slice interners have a wrapper that handles the empty slice case. We can instead handle this in the `slice_interners!` macro, avoiding the need for most of the wrappers, and allowing the interner functions to be renamed from `_intern_foos` to `intern_foos`. The two exceptions: - intern_predicates: I kept this wrapper because there's a FIXME comment about a possible future change. - intern_poly_existential_predicates: I kept this wrapper because it asserts that the slice is empty and sorted.
1 parent b869e84 commit 1cffe2a

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod tls;
66

77
use crate::arena::Arena;
88
use crate::dep_graph::{DepGraph, DepKindStruct};
9-
use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
9+
use crate::infer::canonical::CanonicalVarInfo;
1010
use crate::lint::struct_lint_level;
1111
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
1212
use crate::middle::resolve_bound_vars;
@@ -1570,24 +1570,28 @@ macro_rules! slice_interners {
15701570
($($field:ident: $method:ident($ty:ty)),+ $(,)?) => (
15711571
impl<'tcx> TyCtxt<'tcx> {
15721572
$(pub fn $method(self, v: &[$ty]) -> &'tcx List<$ty> {
1573-
self.interners.$field.intern_ref(v, || {
1574-
InternedInSet(List::from_arena(&*self.arena, v))
1575-
}).0
1573+
if v.is_empty() {
1574+
List::empty()
1575+
} else {
1576+
self.interners.$field.intern_ref(v, || {
1577+
InternedInSet(List::from_arena(&*self.arena, v))
1578+
}).0
1579+
}
15761580
})+
15771581
}
15781582
);
15791583
}
15801584

15811585
slice_interners!(
1582-
const_lists: _intern_const_list(Const<'tcx>),
1583-
substs: _intern_substs(GenericArg<'tcx>),
1584-
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
1586+
const_lists: intern_const_list(Const<'tcx>),
1587+
substs: intern_substs(GenericArg<'tcx>),
1588+
canonical_var_infos: intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
15851589
poly_existential_predicates:
15861590
_intern_poly_existential_predicates(PolyExistentialPredicate<'tcx>),
15871591
predicates: _intern_predicates(Predicate<'tcx>),
1588-
projs: _intern_projs(ProjectionKind),
1589-
place_elems: _intern_place_elems(PlaceElem<'tcx>),
1590-
bound_variable_kinds: _intern_bound_variable_kinds(ty::BoundVariableKind),
1592+
projs: intern_projs(ProjectionKind),
1593+
place_elems: intern_place_elems(PlaceElem<'tcx>),
1594+
bound_variable_kinds: intern_bound_variable_kinds(ty::BoundVariableKind),
15911595
);
15921596

15931597
impl<'tcx> TyCtxt<'tcx> {
@@ -2157,12 +2161,7 @@ impl<'tcx> TyCtxt<'tcx> {
21572161
// FIXME consider asking the input slice to be sorted to avoid
21582162
// re-interning permutations, in which case that would be asserted
21592163
// here.
2160-
if preds.is_empty() {
2161-
// The macro-generated method below asserts we don't intern an empty slice.
2162-
List::empty()
2163-
} else {
2164-
self._intern_predicates(preds)
2165-
}
2164+
self._intern_predicates(preds)
21662165
}
21672166

21682167
pub fn mk_const_list<I, T>(self, iter: I) -> T::Output
@@ -2173,50 +2172,16 @@ impl<'tcx> TyCtxt<'tcx> {
21732172
T::collect_and_apply(iter, |xs| self.intern_const_list(xs))
21742173
}
21752174

2176-
pub fn intern_const_list(self, cs: &[ty::Const<'tcx>]) -> &'tcx List<ty::Const<'tcx>> {
2177-
if cs.is_empty() { List::empty() } else { self._intern_const_list(cs) }
2178-
}
2179-
21802175
pub fn intern_type_list(self, ts: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>> {
2181-
if ts.is_empty() {
2182-
List::empty()
2183-
} else {
2184-
// Actually intern type lists as lists of `GenericArg`s.
2185-
//
2186-
// Transmuting from `Ty<'tcx>` to `GenericArg<'tcx>` is sound
2187-
// as explained in ty_slice_as_generic_arg`. With this,
2188-
// we guarantee that even when transmuting between `List<Ty<'tcx>>`
2189-
// and `List<GenericArg<'tcx>>`, the uniqueness requirement for
2190-
// lists is upheld.
2191-
let substs = self._intern_substs(ty::subst::ty_slice_as_generic_args(ts));
2192-
substs.try_as_type_list().unwrap()
2193-
}
2194-
}
2195-
2196-
pub fn intern_substs(self, ts: &[GenericArg<'tcx>]) -> &'tcx List<GenericArg<'tcx>> {
2197-
if ts.is_empty() { List::empty() } else { self._intern_substs(ts) }
2198-
}
2199-
2200-
pub fn intern_projs(self, ps: &[ProjectionKind]) -> &'tcx List<ProjectionKind> {
2201-
if ps.is_empty() { List::empty() } else { self._intern_projs(ps) }
2202-
}
2203-
2204-
pub fn intern_place_elems(self, ts: &[PlaceElem<'tcx>]) -> &'tcx List<PlaceElem<'tcx>> {
2205-
if ts.is_empty() { List::empty() } else { self._intern_place_elems(ts) }
2206-
}
2207-
2208-
pub fn intern_canonical_var_infos(
2209-
self,
2210-
ts: &[CanonicalVarInfo<'tcx>],
2211-
) -> CanonicalVarInfos<'tcx> {
2212-
if ts.is_empty() { List::empty() } else { self._intern_canonical_var_infos(ts) }
2213-
}
2214-
2215-
pub fn intern_bound_variable_kinds(
2216-
self,
2217-
ts: &[ty::BoundVariableKind],
2218-
) -> &'tcx List<ty::BoundVariableKind> {
2219-
if ts.is_empty() { List::empty() } else { self._intern_bound_variable_kinds(ts) }
2176+
// Actually intern type lists as lists of `GenericArg`s.
2177+
//
2178+
// Transmuting from `Ty<'tcx>` to `GenericArg<'tcx>` is sound
2179+
// as explained in ty_slice_as_generic_arg`. With this,
2180+
// we guarantee that even when transmuting between `List<Ty<'tcx>>`
2181+
// and `List<GenericArg<'tcx>>`, the uniqueness requirement for
2182+
// lists is upheld.
2183+
let substs = self.intern_substs(ty::subst::ty_slice_as_generic_args(ts));
2184+
substs.try_as_type_list().unwrap()
22202185
}
22212186

22222187
// Unlike various other `mk_*` functions, this one uses `I: IntoIterator`

0 commit comments

Comments
 (0)