Skip to content

Commit f72f15c

Browse files
committed
Use a slice in DefIdForest.
1 parent 6c2ee88 commit f72f15c

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,8 +1515,7 @@ rustc_queries! {
15151515
/// check whether the forest is empty.
15161516
query type_uninhabited_from(
15171517
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>
1518-
) -> ty::inhabitedness::DefIdForest {
1519-
storage(ArenaCacheSelector<'tcx>)
1518+
) -> ty::inhabitedness::DefIdForest<'tcx> {
15201519
desc { "computing the inhabitedness of `{:?}`", key }
15211520
remap_env_constness
15221521
}

compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::ty::{DefId, DefIdTree};
33
use rustc_span::def_id::CRATE_DEF_ID;
44
use smallvec::SmallVec;
55
use std::mem;
6-
use std::sync::Arc;
76

87
use DefIdForest::*;
98

@@ -18,14 +17,13 @@ use DefIdForest::*;
1817
/// We store the minimal set of `DefId`s required to represent the whole set. If A and B are
1918
/// `DefId`s in the `DefIdForest`, and A is a parent of B, then only A will be stored. When this is
2019
/// used with `type_uninhabited_from`, there will very rarely be more than one `DefId` stored.
21-
#[derive(Clone, HashStable, Debug)]
22-
pub enum DefIdForest {
20+
#[derive(Copy, Clone, HashStable, Debug)]
21+
pub enum DefIdForest<'a> {
2322
Empty,
2423
Single(DefId),
2524
/// This variant is very rare.
2625
/// Invariant: >1 elements
27-
/// We use `Arc` because this is used in the output of a query.
28-
Multiple(Arc<[DefId]>),
26+
Multiple(&'a [DefId]),
2927
}
3028

3129
/// Tests whether a slice of roots contains a given DefId.
@@ -34,21 +32,21 @@ fn slice_contains<'tcx>(tcx: TyCtxt<'tcx>, slice: &[DefId], id: DefId) -> bool {
3432
slice.iter().any(|root_id| tcx.is_descendant_of(id, *root_id))
3533
}
3634

37-
impl<'tcx> DefIdForest {
35+
impl<'tcx> DefIdForest<'tcx> {
3836
/// Creates an empty forest.
39-
pub fn empty() -> DefIdForest {
37+
pub fn empty() -> DefIdForest<'tcx> {
4038
DefIdForest::Empty
4139
}
4240

4341
/// Creates a forest consisting of a single tree representing the entire
4442
/// crate.
4543
#[inline]
46-
pub fn full() -> DefIdForest {
44+
pub fn full() -> DefIdForest<'tcx> {
4745
DefIdForest::from_id(CRATE_DEF_ID.to_def_id())
4846
}
4947

5048
/// Creates a forest containing a `DefId` and all its descendants.
51-
pub fn from_id(id: DefId) -> DefIdForest {
49+
pub fn from_id(id: DefId) -> DefIdForest<'tcx> {
5250
DefIdForest::Single(id)
5351
}
5452

@@ -61,11 +59,11 @@ impl<'tcx> DefIdForest {
6159
}
6260

6361
// Only allocates in the rare `Multiple` case.
64-
fn from_slice(root_ids: &[DefId]) -> DefIdForest {
65-
match root_ids {
62+
fn from_vec(tcx: TyCtxt<'tcx>, root_ids: SmallVec<[DefId; 1]>) -> DefIdForest<'tcx> {
63+
match &root_ids[..] {
6664
[] => Empty,
6765
[id] => Single(*id),
68-
_ => DefIdForest::Multiple(root_ids.into()),
66+
_ => DefIdForest::Multiple(tcx.arena.alloc_from_iter(root_ids)),
6967
}
7068
}
7169

@@ -88,9 +86,9 @@ impl<'tcx> DefIdForest {
8886
}
8987

9088
/// Calculate the intersection of a collection of forests.
91-
pub fn intersection<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest
89+
pub fn intersection<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest<'tcx>
9290
where
93-
I: IntoIterator<Item = DefIdForest>,
91+
I: IntoIterator<Item = DefIdForest<'tcx>>,
9492
{
9593
let mut iter = iter.into_iter();
9694
let mut ret: SmallVec<[_; 1]> = if let Some(first) = iter.next() {
@@ -114,13 +112,13 @@ impl<'tcx> DefIdForest {
114112
mem::swap(&mut next_ret, &mut ret);
115113
next_ret.clear();
116114
}
117-
DefIdForest::from_slice(&ret)
115+
DefIdForest::from_vec(tcx, ret)
118116
}
119117

120118
/// Calculate the union of a collection of forests.
121-
pub fn union<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest
119+
pub fn union<I>(tcx: TyCtxt<'tcx>, iter: I) -> DefIdForest<'tcx>
122120
where
123-
I: IntoIterator<Item = DefIdForest>,
121+
I: IntoIterator<Item = DefIdForest<'tcx>>,
124122
{
125123
let mut ret: SmallVec<[_; 1]> = SmallVec::new();
126124
let mut next_ret: SmallVec<[_; 1]> = SmallVec::new();
@@ -142,6 +140,6 @@ impl<'tcx> DefIdForest {
142140
mem::swap(&mut next_ret, &mut ret);
143141
next_ret.clear();
144142
}
145-
DefIdForest::from_slice(&ret)
143+
DefIdForest::from_vec(tcx, ret)
146144
}
147145
}

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'tcx> AdtDef {
112112
tcx: TyCtxt<'tcx>,
113113
substs: SubstsRef<'tcx>,
114114
param_env: ty::ParamEnv<'tcx>,
115-
) -> DefIdForest {
115+
) -> DefIdForest<'tcx> {
116116
// Non-exhaustive ADTs from other crates are always considered inhabited.
117117
if self.is_variant_list_non_exhaustive() && !self.did.is_local() {
118118
DefIdForest::empty()
@@ -135,7 +135,7 @@ impl<'tcx> VariantDef {
135135
substs: SubstsRef<'tcx>,
136136
adt_kind: AdtKind,
137137
param_env: ty::ParamEnv<'tcx>,
138-
) -> DefIdForest {
138+
) -> DefIdForest<'tcx> {
139139
let is_enum = match adt_kind {
140140
// For now, `union`s are never considered uninhabited.
141141
// The precise semantics of inhabitedness with respect to unions is currently undecided.
@@ -163,7 +163,7 @@ impl<'tcx> FieldDef {
163163
substs: SubstsRef<'tcx>,
164164
is_enum: bool,
165165
param_env: ty::ParamEnv<'tcx>,
166-
) -> DefIdForest {
166+
) -> DefIdForest<'tcx> {
167167
let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
168168
// FIXME(canndrew): Currently enum fields are (incorrectly) stored with
169169
// `Visibility::Invisible` so we need to override `self.vis` if we're
@@ -190,7 +190,7 @@ impl<'tcx> TyS<'tcx> {
190190
&'tcx self,
191191
tcx: TyCtxt<'tcx>,
192192
param_env: ty::ParamEnv<'tcx>,
193-
) -> DefIdForest {
193+
) -> DefIdForest<'tcx> {
194194
tcx.type_uninhabited_from(param_env.and(self)).clone()
195195
}
196196
}
@@ -199,7 +199,7 @@ impl<'tcx> TyS<'tcx> {
199199
pub(crate) fn type_uninhabited_from<'tcx>(
200200
tcx: TyCtxt<'tcx>,
201201
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
202-
) -> DefIdForest {
202+
) -> DefIdForest<'tcx> {
203203
let ty = key.value;
204204
let param_env = key.param_env;
205205
match *ty.kind() {

0 commit comments

Comments
 (0)