|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -use std::mem; |
12 | | -use rustc_data_structures::small_vec::SmallVec; |
13 | | -use syntax::ast::CRATE_NODE_ID; |
14 | 11 | use util::nodemap::FxHashSet; |
15 | 12 | use ty::context::TyCtxt; |
16 | 13 | use ty::{AdtDef, VariantDef, FieldDef, TyS}; |
17 | 14 | use ty::{DefId, Substs}; |
18 | | -use ty::{AdtKind, Visibility, DefIdTree}; |
| 15 | +use ty::{AdtKind, Visibility}; |
19 | 16 | use ty::TypeVariants::*; |
20 | 17 |
|
21 | | -/// Represents a set of DefIds closed under the ancestor relation. That is, if |
22 | | -/// a DefId is in this set then so are all its descendants. |
23 | | -#[derive(Clone)] |
24 | | -pub struct DefIdForest { |
25 | | - /// The minimal set of DefIds required to represent the whole set. |
26 | | - /// If A and B are DefIds in the DefIdForest, and A is a desecendant |
27 | | - /// of B, then only B will be in root_ids. |
28 | | - /// We use a SmallVec here because (for its use in this module) its rare |
29 | | - /// that this will contain even two ids. |
30 | | - root_ids: SmallVec<[DefId; 1]>, |
31 | | -} |
32 | | - |
33 | | -impl<'a, 'gcx, 'tcx> DefIdForest { |
34 | | - /// Create an empty forest. |
35 | | - pub fn empty() -> DefIdForest { |
36 | | - DefIdForest { |
37 | | - root_ids: SmallVec::new(), |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - /// Create a forest consisting of a single tree representing the entire |
42 | | - /// crate. |
43 | | - #[inline] |
44 | | - pub fn full(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest { |
45 | | - let crate_id = tcx.map.local_def_id(CRATE_NODE_ID); |
46 | | - DefIdForest::from_id(crate_id) |
47 | | - } |
48 | | - |
49 | | - /// Create a forest containing a DefId and all its descendants. |
50 | | - pub fn from_id(id: DefId) -> DefIdForest { |
51 | | - let mut root_ids = SmallVec::new(); |
52 | | - root_ids.push(id); |
53 | | - DefIdForest { |
54 | | - root_ids: root_ids, |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - /// Test whether the forest is empty. |
59 | | - pub fn is_empty(&self) -> bool { |
60 | | - self.root_ids.is_empty() |
61 | | - } |
62 | | - |
63 | | - /// Test whether the forest conains a given DefId. |
64 | | - pub fn contains(&self, |
65 | | - tcx: TyCtxt<'a, 'gcx, 'tcx>, |
66 | | - id: DefId) -> bool |
67 | | - { |
68 | | - for root_id in self.root_ids.iter() { |
69 | | - if tcx.is_descendant_of(id, *root_id) { |
70 | | - return true; |
71 | | - } |
72 | | - } |
73 | | - false |
74 | | - } |
75 | | - |
76 | | - /// Calculate the intersection of a collection of forests. |
77 | | - pub fn intersection<I>(tcx: TyCtxt<'a, 'gcx, 'tcx>, |
78 | | - iter: I) -> DefIdForest |
79 | | - where I: IntoIterator<Item=DefIdForest> |
80 | | - { |
81 | | - let mut ret = DefIdForest::full(tcx); |
82 | | - let mut next_ret = SmallVec::new(); |
83 | | - let mut old_ret: SmallVec<[DefId; 1]> = SmallVec::new(); |
84 | | - for next_forest in iter { |
85 | | - for id in ret.root_ids.drain(..) { |
86 | | - if next_forest.contains(tcx, id) { |
87 | | - next_ret.push(id); |
88 | | - } else { |
89 | | - old_ret.push(id); |
90 | | - } |
91 | | - } |
92 | | - ret.root_ids.extend(old_ret.drain(..)); |
| 18 | +pub use self::def_id_forest::DefIdForest; |
93 | 19 |
|
94 | | - for id in next_forest.root_ids { |
95 | | - if ret.contains(tcx, id) { |
96 | | - next_ret.push(id); |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - mem::swap(&mut next_ret, &mut ret.root_ids); |
101 | | - next_ret.drain(..); |
102 | | - } |
103 | | - ret |
104 | | - } |
| 20 | +mod def_id_forest; |
105 | 21 |
|
106 | | - /// Calculate the union of a collection of forests. |
107 | | - pub fn union<I>(tcx: TyCtxt<'a, 'gcx, 'tcx>, |
108 | | - iter: I) -> DefIdForest |
109 | | - where I: IntoIterator<Item=DefIdForest> |
110 | | - { |
111 | | - let mut ret = DefIdForest::empty(); |
112 | | - let mut next_ret = SmallVec::new(); |
113 | | - for next_forest in iter { |
114 | | - for id in ret.root_ids.drain(..) { |
115 | | - if !next_forest.contains(tcx, id) { |
116 | | - next_ret.push(id); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - for id in next_forest.root_ids { |
121 | | - if !next_ret.contains(&id) { |
122 | | - next_ret.push(id); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - mem::swap(&mut next_ret, &mut ret.root_ids); |
127 | | - next_ret.drain(..); |
128 | | - } |
129 | | - ret |
130 | | - } |
131 | | -} |
| 22 | +// The methods in this module calculate DefIdForests of modules in which a |
| 23 | +// AdtDef/VariantDef/FieldDef is visibly uninhabited. |
| 24 | +// |
| 25 | +// # Example |
| 26 | +// ```rust |
| 27 | +// enum Void {} |
| 28 | +// mod a { |
| 29 | +// pub mod b { |
| 30 | +// pub struct SecretlyUninhabited { |
| 31 | +// _priv: !, |
| 32 | +// } |
| 33 | +// } |
| 34 | +// } |
| 35 | +// |
| 36 | +// mod c { |
| 37 | +// pub struct AlsoSecretlyUninhabited { |
| 38 | +// _priv: Void, |
| 39 | +// } |
| 40 | +// mod d { |
| 41 | +// } |
| 42 | +// } |
| 43 | +// |
| 44 | +// struct Foo { |
| 45 | +// x: a::b::SecretlyUninhabited, |
| 46 | +// y: c::AlsoSecretlyUninhabited, |
| 47 | +// } |
| 48 | +// ``` |
| 49 | +// In this code, the type Foo will only be visibly uninhabited inside the |
| 50 | +// modules b, c and d. Calling uninhabited_from on Foo or its AdtDef will |
| 51 | +// return the forest of modules {b, c->d} (represented in a DefIdForest by the |
| 52 | +// set {b, c}) |
| 53 | +// |
| 54 | +// We need this information for pattern-matching on Foo or types that contain |
| 55 | +// Foo. |
| 56 | +// |
| 57 | +// # Example |
| 58 | +// ```rust |
| 59 | +// let foo_result: Result<T, Foo> = ... ; |
| 60 | +// let Ok(t) = foo_result; |
| 61 | +// ``` |
| 62 | +// This code should only compile in modules where the uninhabitedness of Foo is |
| 63 | +// visible. |
132 | 64 |
|
133 | 65 | impl<'a, 'gcx, 'tcx> AdtDef { |
134 | 66 | /// Calculate the forest of DefIds from which this adt is visibly uninhabited. |
@@ -189,6 +121,9 @@ impl<'a, 'gcx, 'tcx> FieldDef { |
189 | 121 | is_enum: bool) -> DefIdForest |
190 | 122 | { |
191 | 123 | let mut data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(visited, tcx); |
| 124 | + // FIXME(canndrew): Currently enum fields are (incorrectly) stored with |
| 125 | + // Visibility::Invisible so we need to override self.vis if we're |
| 126 | + // dealing with an enum. |
192 | 127 | if is_enum { |
193 | 128 | data_uninhabitedness() |
194 | 129 | } else { |
|
0 commit comments