1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use super::MirBorrowckCtxt;
use rustc::hir;
use rustc::ty::{self, TyCtxt};
use rustc::mir::{Mir, Place, ProjectionElem};
pub trait IsPrefixOf<'tcx> {
fn is_prefix_of(&self, other: &Place<'tcx>) -> bool;
}
impl<'tcx> IsPrefixOf<'tcx> for Place<'tcx> {
fn is_prefix_of(&self, other: &Place<'tcx>) -> bool {
let mut cursor = other;
loop {
if self == cursor {
return true;
}
match *cursor {
Place::Local(_) | Place::Static(_) => return false,
Place::Projection(ref proj) => {
cursor = &proj.base;
}
}
}
}
}
pub(super) struct Prefixes<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
mir: &'cx Mir<'tcx>,
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
kind: PrefixSet,
next: Option<&'cx Place<'tcx>>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[allow(dead_code)]
pub(super) enum PrefixSet {
All,
Shallow,
Supporting,
}
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
pub(super) fn prefixes(
&self,
place: &'cx Place<'tcx>,
kind: PrefixSet,
) -> Prefixes<'cx, 'gcx, 'tcx> {
Prefixes {
next: Some(place),
kind,
mir: self.mir,
tcx: self.tcx,
}
}
}
impl<'cx, 'gcx, 'tcx> Iterator for Prefixes<'cx, 'gcx, 'tcx> {
type Item = &'cx Place<'tcx>;
fn next(&mut self) -> Option<Self::Item> {
let mut cursor = match self.next {
None => return None,
Some(place) => place,
};
'cursor: loop {
let proj = match *cursor {
Place::Local(_) |
Place::Static(_) => {
self.next = None;
return Some(cursor);
}
Place::Projection(ref proj) => proj,
};
match proj.elem {
ProjectionElem::Field(_ , _ ) => {
self.next = Some(&proj.base);
return Some(cursor);
}
ProjectionElem::Downcast(..) |
ProjectionElem::Subslice { .. } |
ProjectionElem::ConstantIndex { .. } |
ProjectionElem::Index(_) => {
cursor = &proj.base;
continue 'cursor;
}
ProjectionElem::Deref => {
}
}
assert_eq!(proj.elem, ProjectionElem::Deref);
match self.kind {
PrefixSet::Shallow => {
self.next = None;
return Some(cursor);
}
PrefixSet::All => {
self.next = Some(&proj.base);
return Some(cursor);
}
PrefixSet::Supporting => {
}
}
assert_eq!(self.kind, PrefixSet::Supporting);
let ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);
match ty.sty {
ty::TyRawPtr(_) |
ty::TyRef(
_,
ty::TypeAndMut {
ty: _,
mutbl: hir::MutImmutable,
},
) => {
self.next = None;
return Some(cursor);
}
ty::TyRef(
_,
ty::TypeAndMut {
ty: _,
mutbl: hir::MutMutable,
},
) => {
self.next = Some(&proj.base);
return Some(cursor);
}
ty::TyAdt(..) if ty.is_box() => {
self.next = Some(&proj.base);
return Some(cursor);
}
_ => panic!("unknown type fed to Projection Deref."),
}
}
}
}