1 use crate::ty::context::TyCtxt;
2 use crate::ty::{DefId, DefIdTree};
3 use rustc_span::def_id::CRATE_DEF_ID;
4 use smallvec::SmallVec;
9 /// Represents a forest of `DefId`s closed under the ancestor relation. That is,
10 /// if a `DefId` representing a module is contained in the forest then all
11 /// `DefId`s defined in that module or submodules are also implicitly contained
14 /// This is used to represent a set of modules in which a type is visibly
17 /// We store the minimal set of `DefId`s required to represent the whole set. If A and B are
18 /// `DefId`s in the `DefIdForest`, and A is a parent of B, then only A will be stored. When this is
19 /// used with `type_uninhabited_from`, there will very rarely be more than one `DefId` stored.
20 #[derive(Copy, Clone, HashStable, Debug)]
21 pub enum DefIdForest<'a> {
24 /// This variant is very rare.
25 /// Invariant: >1 elements
26 Multiple(&'a [DefId]),
29 /// Tests whether a slice of roots contains a given DefId.
31 fn slice_contains<'tcx>(tcx: TyCtxt<'tcx>, slice: &[DefId], id: DefId) -> bool {
32 slice.iter().any(|root_id| tcx.is_descendant_of(id, *root_id))
35 impl<'tcx> DefIdForest<'tcx> {
36 /// Creates an empty forest.
37 pub fn empty() -> DefIdForest<'tcx> {
41 /// Creates a forest consisting of a single tree representing the entire
44 pub fn full() -> DefIdForest<'tcx> {
45 DefIdForest::from_id(CRATE_DEF_ID.to_def_id())
48 /// Creates a forest containing a `DefId` and all its descendants.
49 pub fn from_id(id: DefId) -> DefIdForest<'tcx> {
50 DefIdForest::Single(id)
53 fn as_slice(&self) -> &[DefId] {
56 Single(id) => std::slice::from_ref(id),
57 Multiple(root_ids) => root_ids,
61 // Only allocates in the rare `Multiple` case.
62 fn from_vec(tcx: TyCtxt<'tcx>, root_ids: SmallVec<[DefId; 1]>) -> DefIdForest<'tcx> {
66 _ => DefIdForest::Multiple(tcx.arena.alloc_from_iter(root_ids)),
70 /// Tests whether the forest is empty.
71 pub fn is_empty(&self) -> bool {
74 Single(..) | Multiple(..) => false,
78 /// Iterate over the set of roots.
79 fn iter(&self) -> impl Iterator<Item = DefId> + '_ {
80 self.as_slice().iter().copied()
83 /// Tests whether the forest contains a given DefId.
84 pub fn contains(&self, tcx: TyCtxt<'tcx>, id: DefId) -> bool {
85 slice_contains(tcx, self.as_slice(), id)
88 /// Calculate the intersection of a collection of forests.
89 pub fn intersection<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest<'tcx>
91 I: IntoIterator<Item = DefIdForest<'tcx>>,
93 let mut iter = iter.into_iter();
94 let mut ret: SmallVec<[_; 1]> = if let Some(first) = iter.next() {
95 SmallVec::from_slice(first.as_slice())
97 return DefIdForest::full();
100 let mut next_ret: SmallVec<[_; 1]> = SmallVec::new();
101 for next_forest in iter {
102 // No need to continue if the intersection is already empty.
103 if ret.is_empty() || next_forest.is_empty() {
104 return DefIdForest::empty();
107 // We keep the elements in `ret` that are also in `next_forest`.
108 next_ret.extend(ret.iter().copied().filter(|&id| next_forest.contains(tcx, id)));
109 // We keep the elements in `next_forest` that are also in `ret`.
110 next_ret.extend(next_forest.iter().filter(|&id| slice_contains(tcx, &ret, id)));
112 mem::swap(&mut next_ret, &mut ret);
115 DefIdForest::from_vec(tcx, ret)
118 /// Calculate the union of a collection of forests.
119 pub fn union<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest<'tcx>
121 I: IntoIterator<Item = DefIdForest<'tcx>>,
123 let mut ret: SmallVec<[_; 1]> = SmallVec::new();
124 let mut next_ret: SmallVec<[_; 1]> = SmallVec::new();
125 for next_forest in iter {
126 // Union with the empty set is a no-op.
127 if next_forest.is_empty() {
131 // We add everything in `ret` that is not in `next_forest`.
132 next_ret.extend(ret.iter().copied().filter(|&id| !next_forest.contains(tcx, id)));
133 // We add everything in `next_forest` that we haven't added yet.
134 for id in next_forest.iter() {
135 if !slice_contains(tcx, &next_ret, id) {
140 mem::swap(&mut next_ret, &mut ret);
143 DefIdForest::from_vec(tcx, ret)