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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use self::Mode::*;
use middle::ty;
use middle::def;
use middle::infer;
use middle::traits;
use middle::mem_categorization as mc;
use middle::expr_use_visitor as euv;
use util::nodemap::NodeSet;
use syntax::ast;
use syntax::print::pprust;
use syntax::visit::Visitor;
use syntax::codemap::Span;
use syntax::visit;
#[deriving(Copy, Eq, PartialEq)]
enum Mode {
InConstant,
InStatic,
InStaticMut,
InNothing,
}
struct CheckStaticVisitor<'a, 'tcx: 'a> {
tcx: &'a ty::ctxt<'tcx>,
mode: Mode,
checker: &'a mut GlobalChecker,
}
struct GlobalVisitor<'a,'b,'tcx:'a+'b>(
euv::ExprUseVisitor<'a,'b,'tcx,ty::ctxt<'tcx>>);
struct GlobalChecker {
static_consumptions: NodeSet,
const_borrows: NodeSet,
static_interior_borrows: NodeSet,
static_local_borrows: NodeSet,
}
pub fn check_crate(tcx: &ty::ctxt) {
let mut checker = GlobalChecker {
static_consumptions: NodeSet::new(),
const_borrows: NodeSet::new(),
static_interior_borrows: NodeSet::new(),
static_local_borrows: NodeSet::new(),
};
{
let param_env = ty::empty_parameter_environment();
let visitor = euv::ExprUseVisitor::new(&mut checker, tcx, ¶m_env);
visit::walk_crate(&mut GlobalVisitor(visitor), tcx.map.krate());
}
visit::walk_crate(&mut CheckStaticVisitor {
tcx: tcx,
mode: InNothing,
checker: &mut checker,
}, tcx.map.krate());
}
impl<'a, 'tcx> CheckStaticVisitor<'a, 'tcx> {
fn with_mode<F>(&mut self, mode: Mode, f: F) where
F: FnOnce(&mut CheckStaticVisitor<'a, 'tcx>),
{
let old = self.mode;
self.mode = mode;
f(self);
self.mode = old;
}
fn msg(&self) -> &'static str {
match self.mode {
InConstant => "constants",
InStaticMut | InStatic => "statics",
InNothing => unreachable!(),
}
}
fn check_static_mut_type(&self, e: &ast::Expr) {
let node_ty = ty::node_id_to_type(self.tcx, e.id);
let tcontents = ty::type_contents(self.tcx, node_ty);
let suffix = if tcontents.has_dtor() {
"destructors"
} else if tcontents.owns_owned() {
"owned pointers"
} else {
return
};
self.tcx.sess.span_err(e.span, format!("mutable statics are not allowed \
to have {}", suffix)[]);
}
fn check_static_type(&self, e: &ast::Expr) {
let ty = ty::node_id_to_type(self.tcx, e.id);
let infcx = infer::new_infer_ctxt(self.tcx);
let mut fulfill_cx = traits::FulfillmentContext::new();
let cause = traits::ObligationCause::new(e.span, e.id, traits::SharedStatic);
fulfill_cx.register_builtin_bound(&infcx, ty, ty::BoundSync, cause);
let env = ty::empty_parameter_environment();
match fulfill_cx.select_all_or_error(&infcx, &env, self.tcx) {
Ok(()) => { },
Err(ref errors) => {
traits::report_fulfillment_errors(&infcx, errors);
}
}
}
}
impl<'a, 'tcx, 'v> Visitor<'v> for CheckStaticVisitor<'a, 'tcx> {
fn visit_item(&mut self, i: &ast::Item) {
debug!("visit_item(item={})", pprust::item_to_string(i));
match i.node {
ast::ItemStatic(_, ast::MutImmutable, ref expr) => {
self.check_static_type(&**expr);
self.with_mode(InStatic, |v| v.visit_expr(&**expr));
}
ast::ItemStatic(_, ast::MutMutable, ref expr) => {
self.check_static_mut_type(&**expr);
self.with_mode(InStaticMut, |v| v.visit_expr(&**expr));
}
ast::ItemConst(_, ref expr) => {
self.with_mode(InConstant, |v| v.visit_expr(&**expr));
}
_ => {
self.with_mode(InNothing, |v| visit::walk_item(v, i));
}
}
}
fn visit_expr(&mut self, e: &ast::Expr) {
if self.mode == InNothing {
return visit::walk_expr(self, e);
}
let node_ty = ty::node_id_to_type(self.tcx, e.id);
match node_ty.sty {
ty::ty_struct(did, _) |
ty::ty_enum(did, _) if ty::has_dtor(self.tcx, did) => {
self.tcx.sess.span_err(e.span,
format!("{} are not allowed to have \
destructors", self.msg())[])
}
_ => {}
}
if self.checker.static_consumptions.remove(&e.id) {
self.tcx.sess.span_err(e.span, "cannot refer to other statics by \
value, use the address-of operator \
or a constant instead");
}
if self.checker.static_interior_borrows.remove(&e.id) {
self.tcx.sess.span_err(e.span, "cannot refer to the interior of \
another static, use a constant \
instead");
}
if self.checker.const_borrows.remove(&e.id) {
let node_ty = ty::node_id_to_type(self.tcx, e.id);
let tcontents = ty::type_contents(self.tcx, node_ty);
if tcontents.interior_unsafe() {
self.tcx.sess.span_err(e.span, "cannot borrow a constant which \
contains interior mutability, \
create a static instead");
}
}
if self.checker.static_local_borrows.remove(&e.id) {
self.tcx.sess.span_err(e.span, "cannot borrow a local variable inside \
a static block, define a separate static \
instead");
}
match e.node {
ast::ExprAddrOf(ast::MutMutable, _) => {
if self.mode != InStaticMut {
span_err!(self.tcx.sess, e.span, E0020,
"{} are not allowed to have mutable references",
self.msg());
}
},
ast::ExprBox(..) |
ast::ExprUnary(ast::UnUniq, _) => {
span_err!(self.tcx.sess, e.span, E0022,
"{} are not allowed to have custom pointers",
self.msg());
}
ast::ExprPath(..) => {
match ty::resolve_expr(self.tcx, e) {
def::DefStatic(..) if self.mode == InConstant => {
let msg = "constants cannot refer to other statics, \
insert an intermediate constant \
instead";
self.tcx.sess.span_err(e.span, msg[]);
}
_ => {}
}
}
_ => {}
}
visit::walk_expr(self, e);
}
}
impl<'a,'b,'t,'v> Visitor<'v> for GlobalVisitor<'a,'b,'t> {
fn visit_item(&mut self, item: &ast::Item) {
match item.node {
ast::ItemConst(_, ref e) |
ast::ItemStatic(_, _, ref e) => {
let GlobalVisitor(ref mut v) = *self;
v.consume_expr(&**e);
}
_ => {}
}
visit::walk_item(self, item);
}
}
impl<'tcx> euv::Delegate<'tcx> for GlobalChecker {
fn consume(&mut self,
consume_id: ast::NodeId,
_consume_span: Span,
cmt: mc::cmt,
_mode: euv::ConsumeMode) {
let mut cur = &cmt;
loop {
match cur.cat {
mc::cat_static_item => {
self.static_consumptions.insert(consume_id);
break
}
mc::cat_deref(ref cmt, _, _) |
mc::cat_downcast(ref cmt, _) |
mc::cat_interior(ref cmt, _) => cur = cmt,
mc::cat_rvalue(..) |
mc::cat_upvar(..) |
mc::cat_local(..) => break,
}
}
}
fn borrow(&mut self,
borrow_id: ast::NodeId,
_borrow_span: Span,
cmt: mc::cmt,
_loan_region: ty::Region,
_bk: ty::BorrowKind,
_loan_cause: euv::LoanCause) {
let mut cur = &cmt;
let mut is_interior = false;
loop {
match cur.cat {
mc::cat_rvalue(..) => {
self.const_borrows.insert(borrow_id);
break
}
mc::cat_static_item => {
if is_interior {
self.static_interior_borrows.insert(borrow_id);
}
break
}
mc::cat_deref(ref cmt, _, _) |
mc::cat_interior(ref cmt, _) => {
is_interior = true;
cur = cmt;
}
mc::cat_downcast(..) |
mc::cat_upvar(..) => unreachable!(),
mc::cat_local(..) => {
self.static_local_borrows.insert(borrow_id);
break
}
}
}
}
fn decl_without_init(&mut self,
_id: ast::NodeId,
_span: Span) {}
fn mutate(&mut self,
_assignment_id: ast::NodeId,
_assignment_span: Span,
_assignee_cmt: mc::cmt,
_mode: euv::MutateMode) {}
fn matched_pat(&mut self,
_: &ast::Pat,
_: mc::cmt,
_: euv::MatchMode) {}
fn consume_pat(&mut self,
_consume_pat: &ast::Pat,
_cmt: mc::cmt,
_mode: euv::ConsumeMode) {}
}