-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
rvalue_promotion.rs
756 lines (676 loc) · 26.5 KB
/
rvalue_promotion.rs
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Verifies that the types and values of const and static items
// are safe. The rules enforced by this module are:
//
// - For each *mutable* static item, it checks that its **type**:
// - doesn't have a destructor
// - doesn't own a box
//
// - For each *immutable* static item, it checks that its **value**:
// - doesn't own a box
// - doesn't contain a struct literal or a call to an enum variant / struct constructor where
// - the type of the struct/enum has a dtor
//
// Rules Enforced Elsewhere:
// - It's not possible to take the address of a static item with unsafe interior. This is enforced
// by borrowck::gather_loans
use rustc::ty::cast::CastKind;
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::DefId;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::middle::expr_use_visitor as euv;
use rustc::middle::mem_categorization as mc;
use rustc::middle::mem_categorization::Categorization;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::query::Providers;
use rustc::ty::subst::Substs;
use rustc::util::nodemap::{ItemLocalSet, NodeSet};
use rustc::hir;
use rustc_data_structures::sync::Lrc;
use syntax::ast;
use syntax::attr;
use syntax_pos::{Span, DUMMY_SP};
use self::Promotability::*;
use std::ops::{BitAnd, BitOr};
pub fn provide(providers: &mut Providers) {
*providers = Providers {
rvalue_promotable_map,
const_is_rvalue_promotable_to_static,
..*providers
};
}
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
for &body_id in &tcx.hir.krate().body_ids {
let def_id = tcx.hir.body_owner_def_id(body_id);
tcx.const_is_rvalue_promotable_to_static(def_id);
}
tcx.sess.abort_if_errors();
}
fn const_is_rvalue_promotable_to_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> bool
{
assert!(def_id.is_local());
let node_id = tcx.hir.as_local_node_id(def_id)
.expect("rvalue_promotable_map invoked with non-local def-id");
let body_id = tcx.hir.body_owned_by(node_id);
let body_hir_id = tcx.hir.node_to_hir_id(body_id.node_id);
tcx.rvalue_promotable_map(def_id).contains(&body_hir_id.local_id)
}
fn rvalue_promotable_map<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> Lrc<ItemLocalSet>
{
let outer_def_id = tcx.closure_base_def_id(def_id);
if outer_def_id != def_id {
return tcx.rvalue_promotable_map(outer_def_id);
}
let mut visitor = CheckCrateVisitor {
tcx,
tables: &ty::TypeckTables::empty(None),
in_fn: false,
in_static: false,
mut_rvalue_borrows: NodeSet(),
param_env: ty::ParamEnv::empty(),
identity_substs: Substs::empty(),
result: ItemLocalSet(),
};
// `def_id` should be a `Body` owner
let node_id = tcx.hir.as_local_node_id(def_id)
.expect("rvalue_promotable_map invoked with non-local def-id");
let body_id = tcx.hir.body_owned_by(node_id);
let _ = visitor.check_nested_body(body_id);
Lrc::new(visitor.result)
}
struct CheckCrateVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
in_fn: bool,
in_static: bool,
mut_rvalue_borrows: NodeSet,
param_env: ty::ParamEnv<'tcx>,
identity_substs: &'tcx Substs<'tcx>,
tables: &'a ty::TypeckTables<'tcx>,
result: ItemLocalSet,
}
#[must_use]
#[derive(Debug, PartialEq)]
enum Promotability {
Promotable,
NotPromotable
}
impl BitAnd for Promotability {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
match (self, rhs) {
(Promotable, NotPromotable) => NotPromotable,
(NotPromotable, Promotable) => NotPromotable,
(NotPromotable, NotPromotable) => NotPromotable,
(Promotable, Promotable) => Promotable,
}
}
}
impl BitOr for Promotability {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
match (self, rhs) {
(Promotable, NotPromotable) => Promotable,
(NotPromotable, Promotable) => Promotable,
(NotPromotable, NotPromotable) => NotPromotable,
(Promotable, Promotable) => Promotable,
}
}
}
impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
// Returns true iff all the values of the type are promotable.
fn type_promotability(&mut self, ty: Ty<'gcx>) -> Promotability {
debug!("type_promotability({})", ty);
if ty.is_freeze(self.tcx, self.param_env, DUMMY_SP) &&
!ty.needs_drop(self.tcx, self.param_env) {
Promotable
} else {
NotPromotable
}
}
fn handle_const_fn_call(&mut self, def_id: DefId,
ret_ty: Ty<'gcx>, span: Span) -> Promotability {
if let NotPromotable = self.type_promotability(ret_ty) {
return NotPromotable;
}
let node_check = if let Some(fn_id) = self.tcx.hir.as_local_node_id(def_id) {
FnLikeNode::from_node(self.tcx.hir.get(fn_id)).map_or(false, |fn_like| {
fn_like.constness() == hir::Constness::Const
})
} else {
self.tcx.is_const_fn(def_id)
};
if !node_check {
return NotPromotable
}
if let Some(&attr::Stability {
rustc_const_unstable: Some(attr::RustcConstUnstable {
feature: ref feature_name
}),
.. }) = self.tcx.lookup_stability(def_id) {
let stable_check =
// feature-gate is enabled,
self.tcx.features()
.declared_lib_features
.iter()
.any(|&(ref sym, _)| sym == feature_name) ||
// this comes from a crate with the feature-gate enabled,
!def_id.is_local() ||
// this comes from a macro that has #[allow_internal_unstable]
span.allows_unstable();
if !stable_check {
return NotPromotable
}
};
Promotable
}
/// While the `ExprUseVisitor` walks, we will identify which
/// expressions are borrowed, and insert their ids into this
/// table. Actually, we insert the "borrow-id", which is normally
/// the id of the expession being borrowed: but in the case of
/// `ref mut` borrows, the `id` of the pattern is
/// inserted. Therefore later we remove that entry from the table
/// and transfer it over to the value being matched. This will
/// then prevent said value from being promoted.
fn remove_mut_rvalue_borrow(&mut self, pat: &hir::Pat) -> bool {
let mut any_removed = false;
pat.walk(|p| {
any_removed |= self.mut_rvalue_borrows.remove(&p.id);
true
});
any_removed
}
}
impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
fn check_nested_body(&mut self, body_id: hir::BodyId) -> Promotability {
let item_id = self.tcx.hir.body_owner(body_id);
let item_def_id = self.tcx.hir.local_def_id(item_id);
let outer_in_fn = self.in_fn;
let outer_tables = self.tables;
let outer_param_env = self.param_env;
let outer_identity_substs = self.identity_substs;
self.in_fn = false;
self.in_static = false;
match self.tcx.hir.body_owner_kind(item_id) {
hir::BodyOwnerKind::Fn => self.in_fn = true,
hir::BodyOwnerKind::Static(_) => self.in_static = true,
_ => {}
};
self.tables = self.tcx.typeck_tables_of(item_def_id);
self.param_env = self.tcx.param_env(item_def_id);
self.identity_substs = Substs::identity_for_item(self.tcx, item_def_id);
let body = self.tcx.hir.body(body_id);
let tcx = self.tcx;
let param_env = self.param_env;
let region_scope_tree = self.tcx.region_scope_tree(item_def_id);
euv::ExprUseVisitor::new(self, tcx, param_env, ®ion_scope_tree, self.tables, None)
.consume_body(body);
let body_promotable = self.check_expr(&body.value);
self.in_fn = outer_in_fn;
self.tables = outer_tables;
self.param_env = outer_param_env;
self.identity_substs = outer_identity_substs;
body_promotable
}
fn check_stmt(&mut self, stmt: &'tcx hir::Stmt) -> Promotability {
match stmt.node {
hir::StmtKind::Decl(ref decl, _node_id) => {
match &decl.node {
hir::DeclKind::Local(local) => {
if self.remove_mut_rvalue_borrow(&local.pat) {
if let Some(init) = &local.init {
self.mut_rvalue_borrows.insert(init.id);
}
}
match local.init {
Some(ref expr) => { let _ = self.check_expr(&expr); },
None => {},
}
NotPromotable
}
// Item statements are allowed
hir::DeclKind::Item(_) => Promotable
}
}
hir::StmtKind::Expr(ref box_expr, _node_id) |
hir::StmtKind::Semi(ref box_expr, _node_id) => {
let _ = self.check_expr(box_expr);
NotPromotable
}
}
}
fn check_expr(&mut self, ex: &'tcx hir::Expr) -> Promotability {
let node_ty = self.tables.node_id_to_type(ex.hir_id);
let mut outer = check_expr_kind(self, ex, node_ty);
outer = outer & check_adjustments(self, ex);
// Handle borrows on (or inside the autorefs of) this expression.
if self.mut_rvalue_borrows.remove(&ex.id) {
outer = NotPromotable
}
if outer == Promotable {
self.result.insert(ex.hir_id.local_id);
}
outer
}
fn check_block(&mut self, block: &'tcx hir::Block) -> Promotability {
let mut iter_result = Promotable;
for index in block.stmts.iter() {
iter_result = iter_result & self.check_stmt(index);
}
match block.expr {
Some(ref box_expr) => iter_result & self.check_expr(&*box_expr),
None => iter_result,
}
}
}
/// This function is used to enforce the constraints on
/// const/static items. It walks through the *value*
/// of the item walking down the expression and evaluating
/// every nested expression. If the expression is not part
/// of a const/static item, it is qualified for promotion
/// instead of producing errors.
fn check_expr_kind<'a, 'tcx>(
v: &mut CheckCrateVisitor<'a, 'tcx>,
e: &'tcx hir::Expr, node_ty: Ty<'tcx>) -> Promotability {
let ty_result = match node_ty.sty {
ty::TyAdt(def, _) if def.has_dtor(v.tcx) => {
NotPromotable
}
_ => Promotable
};
let node_result = match e.node {
hir::ExprKind::Box(ref expr) => {
let _ = v.check_expr(&expr);
NotPromotable
}
hir::ExprKind::Unary(op, ref expr) => {
let expr_promotability = v.check_expr(expr);
if v.tables.is_method_call(e) {
return NotPromotable;
}
if op == hir::UnDeref {
return NotPromotable;
}
expr_promotability
}
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
let lefty = v.check_expr(lhs);
let righty = v.check_expr(rhs);
if v.tables.is_method_call(e) {
return NotPromotable;
}
match v.tables.node_id_to_type(lhs.hir_id).sty {
ty::TyRawPtr(_) => {
assert!(op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne ||
op.node == hir::BinOpKind::Le || op.node == hir::BinOpKind::Lt ||
op.node == hir::BinOpKind::Ge || op.node == hir::BinOpKind::Gt);
NotPromotable
}
_ => lefty & righty
}
}
hir::ExprKind::Cast(ref from, _) => {
let expr_promotability = v.check_expr(from);
debug!("Checking const cast(id={})", from.id);
match v.tables.cast_kinds().get(from.hir_id) {
None => {
v.tcx.sess.delay_span_bug(e.span, "no kind for cast");
NotPromotable
},
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
NotPromotable
}
_ => expr_promotability
}
}
hir::ExprKind::Path(ref qpath) => {
let def = v.tables.qpath_def(qpath, e.hir_id);
match def {
Def::VariantCtor(..) | Def::StructCtor(..) |
Def::Fn(..) | Def::Method(..) => Promotable,
// References to a static that are themselves within a static
// are inherently promotable with the exception
// of "#[thread_local]" statics, which may not
// outlive the current function
Def::Static(did, _) => {
if v.in_static {
for attr in &v.tcx.get_attrs(did)[..] {
if attr.check_name("thread_local") {
debug!("Reference to Static(id={:?}) is unpromotable \
due to a #[thread_local] attribute", did);
return NotPromotable;
}
}
Promotable
} else {
debug!("Reference to Static(id={:?}) is unpromotable as it is not \
referenced from a static", did);
NotPromotable
}
}
Def::Const(did) |
Def::AssociatedConst(did) => {
let promotable = if v.tcx.trait_of_item(did).is_some() {
// Don't peek inside trait associated constants.
NotPromotable
} else if v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did) {
Promotable
} else {
NotPromotable
};
// Just in case the type is more specific than the definition,
// e.g. impl associated const with type parameters, check it.
// Also, trait associated consts are relaxed by this.
promotable | v.type_promotability(node_ty)
}
_ => NotPromotable
}
}
hir::ExprKind::Call(ref callee, ref hirvec) => {
let mut call_result = v.check_expr(callee);
for index in hirvec.iter() {
call_result = call_result & v.check_expr(index);
}
let mut callee = &**callee;
loop {
callee = match callee.node {
hir::ExprKind::Block(ref block, _) => match block.expr {
Some(ref tail) => &tail,
None => break
},
_ => break
};
}
// The callee is an arbitrary expression, it doesn't necessarily have a definition.
let def = if let hir::ExprKind::Path(ref qpath) = callee.node {
v.tables.qpath_def(qpath, callee.hir_id)
} else {
Def::Err
};
let def_result = match def {
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => Promotable,
Def::Fn(did) => {
v.handle_const_fn_call(did, node_ty, e.span)
}
Def::Method(did) => {
match v.tcx.associated_item(did).container {
ty::ImplContainer(_) => {
v.handle_const_fn_call(did, node_ty, e.span)
}
ty::TraitContainer(_) => NotPromotable,
}
}
_ => NotPromotable,
};
def_result & call_result
}
hir::ExprKind::MethodCall(ref _pathsegment, ref _span, ref hirvec) => {
let mut method_call_result = Promotable;
for index in hirvec.iter() {
method_call_result = method_call_result & v.check_expr(index);
}
if let Some(def) = v.tables.type_dependent_defs().get(e.hir_id) {
let def_id = def.def_id();
match v.tcx.associated_item(def_id).container {
ty::ImplContainer(_) => {
method_call_result = method_call_result
& v.handle_const_fn_call(def_id, node_ty, e.span);
}
ty::TraitContainer(_) => return NotPromotable,
};
} else {
v.tcx.sess.delay_span_bug(e.span, "no type-dependent def for method call");
}
method_call_result
}
hir::ExprKind::Struct(ref _qpath, ref hirvec, ref option_expr) => {
let mut struct_result = Promotable;
for index in hirvec.iter() {
struct_result = struct_result & v.check_expr(&index.expr);
}
match *option_expr {
Some(ref expr) => { struct_result = struct_result & v.check_expr(&expr); },
None => {},
}
if let ty::TyAdt(adt, ..) = v.tables.expr_ty(e).sty {
// unsafe_cell_type doesn't necessarily exist with no_core
if Some(adt.did) == v.tcx.lang_items().unsafe_cell_type() {
return NotPromotable;
}
}
struct_result
}
hir::ExprKind::Lit(_) => Promotable,
hir::ExprKind::AddrOf(_, ref expr) |
hir::ExprKind::Repeat(ref expr, _) => {
v.check_expr(&expr)
}
hir::ExprKind::Closure(_capture_clause, ref _box_fn_decl,
body_id, _span, _option_generator_movability) => {
let nested_body_promotable = v.check_nested_body(body_id);
// Paths in constant contexts cannot refer to local variables,
// as there are none, and thus closures can't have upvars there.
if v.tcx.with_freevars(e.id, |fv| !fv.is_empty()) {
NotPromotable
} else {
nested_body_promotable
}
}
hir::ExprKind::Field(ref expr, _ident) => {
let expr_promotability = v.check_expr(&expr);
if let Some(def) = v.tables.expr_ty(expr).ty_adt_def() {
if def.is_union() {
return NotPromotable;
}
}
expr_promotability
}
hir::ExprKind::Block(ref box_block, ref _option_label) => {
v.check_block(box_block)
}
hir::ExprKind::Index(ref lhs, ref rhs) => {
let lefty = v.check_expr(lhs);
let righty = v.check_expr(rhs);
if v.tables.is_method_call(e) {
return NotPromotable;
}
lefty & righty
}
hir::ExprKind::Array(ref hirvec) => {
let mut array_result = Promotable;
for index in hirvec.iter() {
array_result = array_result & v.check_expr(index);
}
array_result
}
hir::ExprKind::Type(ref expr, ref _ty) => {
v.check_expr(&expr)
}
hir::ExprKind::Tup(ref hirvec) => {
let mut tup_result = Promotable;
for index in hirvec.iter() {
tup_result = tup_result & v.check_expr(index);
}
tup_result
}
// Conditional control flow (possible to implement).
hir::ExprKind::Match(ref expr, ref hirvec_arm, ref _match_source) => {
// Compute the most demanding borrow from all the arms'
// patterns and set that on the discriminator.
let mut mut_borrow = false;
for pat in hirvec_arm.iter().flat_map(|arm| &arm.pats) {
mut_borrow = v.remove_mut_rvalue_borrow(pat);
}
if mut_borrow {
v.mut_rvalue_borrows.insert(expr.id);
}
let _ = v.check_expr(expr);
for index in hirvec_arm.iter() {
let _ = v.check_expr(&*index.body);
match index.guard {
Some(ref expr) => {
let _ = v.check_expr(&expr);
},
None => {},
};
}
NotPromotable
}
hir::ExprKind::If(ref lhs, ref rhs, ref option_expr) => {
let _ = v.check_expr(lhs);
let _ = v.check_expr(rhs);
match option_expr {
Some(ref expr) => { let _ = v.check_expr(&expr); },
None => {},
};
NotPromotable
}
// Loops (not very meaningful in constants).
hir::ExprKind::While(ref expr, ref box_block, ref _option_label) => {
let _ = v.check_expr(expr);
let _ = v.check_block(box_block);
NotPromotable
}
hir::ExprKind::Loop(ref box_block, ref _option_label, ref _loop_source) => {
let _ = v.check_block(box_block);
NotPromotable
}
// More control flow (also not very meaningful).
hir::ExprKind::Break(_, ref option_expr) | hir::ExprKind::Ret(ref option_expr) => {
match *option_expr {
Some(ref expr) => { let _ = v.check_expr(&expr); },
None => {},
}
NotPromotable
}
hir::ExprKind::Continue(_) => {
NotPromotable
}
// Generator expressions
hir::ExprKind::Yield(ref expr) => {
let _ = v.check_expr(&expr);
NotPromotable
}
// Expressions with side-effects.
hir::ExprKind::AssignOp(_, ref lhs, ref rhs) | hir::ExprKind::Assign(ref lhs, ref rhs) => {
let _ = v.check_expr(lhs);
let _ = v.check_expr(rhs);
NotPromotable
}
hir::ExprKind::InlineAsm(ref _inline_asm, ref hirvec_lhs, ref hirvec_rhs) => {
for index in hirvec_lhs.iter() {
let _ = v.check_expr(index);
}
for index in hirvec_rhs.iter() {
let _ = v.check_expr(index);
}
NotPromotable
}
};
ty_result & node_result
}
/// Check the adjustments of an expression
fn check_adjustments<'a, 'tcx>(
v: &mut CheckCrateVisitor<'a, 'tcx>,
e: &hir::Expr) -> Promotability {
use rustc::ty::adjustment::*;
let mut adjustments = v.tables.expr_adjustments(e).iter().peekable();
while let Some(adjustment) = adjustments.next() {
match adjustment.kind {
Adjust::NeverToAny |
Adjust::ReifyFnPointer |
Adjust::UnsafeFnPointer |
Adjust::ClosureFnPointer |
Adjust::MutToConstPointer |
Adjust::Borrow(_) |
Adjust::Unsize => {}
Adjust::Deref(_) => {
if let Some(next_adjustment) = adjustments.peek() {
if let Adjust::Borrow(_) = next_adjustment.kind {
continue;
}
}
return NotPromotable;
}
}
}
Promotable
}
impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
fn consume(&mut self,
_consume_id: ast::NodeId,
_consume_span: Span,
_cmt: &mc::cmt_,
_mode: euv::ConsumeMode) {}
fn borrow(&mut self,
borrow_id: ast::NodeId,
_borrow_span: Span,
cmt: &mc::cmt_<'tcx>,
_loan_region: ty::Region<'tcx>,
bk: ty::BorrowKind,
loan_cause: euv::LoanCause) {
debug!(
"borrow(borrow_id={:?}, cmt={:?}, bk={:?}, loan_cause={:?})",
borrow_id,
cmt,
bk,
loan_cause,
);
// Kind of hacky, but we allow Unsafe coercions in constants.
// These occur when we convert a &T or *T to a *U, as well as
// when making a thin pointer (e.g., `*T`) into a fat pointer
// (e.g., `*Trait`).
match loan_cause {
euv::LoanCause::AutoUnsafe => {
return;
}
_ => {}
}
let mut cur = cmt;
loop {
match cur.cat {
Categorization::Rvalue(..) => {
if loan_cause == euv::MatchDiscriminant {
// Ignore the dummy immutable borrow created by EUV.
break;
}
if bk.to_mutbl_lossy() == hir::MutMutable {
self.mut_rvalue_borrows.insert(borrow_id);
}
break;
}
Categorization::StaticItem => {
break;
}
Categorization::Deref(ref cmt, _) |
Categorization::Downcast(ref cmt, _) |
Categorization::Interior(ref cmt, _) => {
cur = cmt;
}
Categorization::Upvar(..) |
Categorization::Local(..) => 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, _: &hir::Pat, _: &mc::cmt_, _: euv::MatchMode) {}
fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: &mc::cmt_, _mode: euv::ConsumeMode) {}
}