Skip to content

Commit 29b51cd

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 07c993e commit 29b51cd

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

compiler/rustc_middle/src/ty/context.rs

+24-59
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;
@@ -1565,24 +1565,28 @@ macro_rules! slice_interners {
15651565
($($field:ident: $method:ident($ty:ty)),+ $(,)?) => (
15661566
impl<'tcx> TyCtxt<'tcx> {
15671567
$(pub fn $method(self, v: &[$ty]) -> &'tcx List<$ty> {
1568-
self.interners.$field.intern_ref(v, || {
1569-
InternedInSet(List::from_arena(&*self.arena, v))
1570-
}).0
1568+
if v.is_empty() {
1569+
List::empty()
1570+
} else {
1571+
self.interners.$field.intern_ref(v, || {
1572+
InternedInSet(List::from_arena(&*self.arena, v))
1573+
}).0
1574+
}
15711575
})+
15721576
}
15731577
);
15741578
}
15751579

15761580
slice_interners!(
1577-
const_lists: _intern_const_list(Const<'tcx>),
1578-
substs: _intern_substs(GenericArg<'tcx>),
1579-
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
1581+
const_lists: intern_const_list(Const<'tcx>),
1582+
substs: intern_substs(GenericArg<'tcx>),
1583+
canonical_var_infos: intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
15801584
poly_existential_predicates:
15811585
_intern_poly_existential_predicates(PolyExistentialPredicate<'tcx>),
15821586
predicates: _intern_predicates(Predicate<'tcx>),
1583-
projs: _intern_projs(ProjectionKind),
1584-
place_elems: _intern_place_elems(PlaceElem<'tcx>),
1585-
bound_variable_kinds: _intern_bound_variable_kinds(ty::BoundVariableKind),
1587+
projs: intern_projs(ProjectionKind),
1588+
place_elems: intern_place_elems(PlaceElem<'tcx>),
1589+
bound_variable_kinds: intern_bound_variable_kinds(ty::BoundVariableKind),
15861590
);
15871591

15881592
impl<'tcx> TyCtxt<'tcx> {
@@ -2152,12 +2156,7 @@ impl<'tcx> TyCtxt<'tcx> {
21522156
// FIXME consider asking the input slice to be sorted to avoid
21532157
// re-interning permutations, in which case that would be asserted
21542158
// here.
2155-
if preds.is_empty() {
2156-
// The macro-generated method below asserts we don't intern an empty slice.
2157-
List::empty()
2158-
} else {
2159-
self._intern_predicates(preds)
2160-
}
2159+
self._intern_predicates(preds)
21612160
}
21622161

21632162
pub fn mk_const_list<I, T>(self, iter: I) -> T::Output
@@ -2168,50 +2167,16 @@ impl<'tcx> TyCtxt<'tcx> {
21682167
T::collect_and_apply(iter, |xs| self.intern_const_list(xs))
21692168
}
21702169

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

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

0 commit comments

Comments
 (0)