]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/rvalue_promotion.rs
Auto merge of #52847 - upsuper:thread-stack-reserve, r=alexcrichton
[rust.git] / src / librustc_passes / rvalue_promotion.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Verifies that the types and values of const and static items
12 // are safe. The rules enforced by this module are:
13 //
14 // - For each *mutable* static item, it checks that its **type**:
15 //     - doesn't have a destructor
16 //     - doesn't own a box
17 //
18 // - For each *immutable* static item, it checks that its **value**:
19 //       - doesn't own a box
20 //       - doesn't contain a struct literal or a call to an enum variant / struct constructor where
21 //           - the type of the struct/enum has a dtor
22 //
23 // Rules Enforced Elsewhere:
24 // - It's not possible to take the address of a static item with unsafe interior. This is enforced
25 // by borrowck::gather_loans
26
27 use rustc::ty::cast::CastKind;
28 use rustc::hir::def::{Def, CtorKind};
29 use rustc::hir::def_id::DefId;
30 use rustc::hir::map::blocks::FnLikeNode;
31 use rustc::middle::expr_use_visitor as euv;
32 use rustc::middle::mem_categorization as mc;
33 use rustc::middle::mem_categorization::Categorization;
34 use rustc::ty::{self, Ty, TyCtxt};
35 use rustc::ty::query::Providers;
36 use rustc::ty::subst::Substs;
37 use rustc::util::nodemap::{ItemLocalSet, NodeSet};
38 use rustc::hir;
39 use rustc_data_structures::sync::Lrc;
40 use syntax::ast;
41 use syntax::attr;
42 use syntax_pos::{Span, DUMMY_SP};
43 use self::Promotability::*;
44 use std::ops::{BitAnd, BitOr};
45
46
47 pub fn provide(providers: &mut Providers) {
48     *providers = Providers {
49         rvalue_promotable_map,
50         const_is_rvalue_promotable_to_static,
51         ..*providers
52     };
53 }
54
55 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
56     for &body_id in &tcx.hir.krate().body_ids {
57         let def_id = tcx.hir.body_owner_def_id(body_id);
58         tcx.const_is_rvalue_promotable_to_static(def_id);
59     }
60     tcx.sess.abort_if_errors();
61 }
62
63 fn const_is_rvalue_promotable_to_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
64                                                   def_id: DefId)
65                                                   -> bool
66 {
67     assert!(def_id.is_local());
68
69     let node_id = tcx.hir.as_local_node_id(def_id)
70         .expect("rvalue_promotable_map invoked with non-local def-id");
71     let body_id = tcx.hir.body_owned_by(node_id);
72     let body_hir_id = tcx.hir.node_to_hir_id(body_id.node_id);
73     tcx.rvalue_promotable_map(def_id).contains(&body_hir_id.local_id)
74 }
75
76 fn rvalue_promotable_map<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
77                                    def_id: DefId)
78                                    -> Lrc<ItemLocalSet>
79 {
80     let outer_def_id = tcx.closure_base_def_id(def_id);
81     if outer_def_id != def_id {
82         return tcx.rvalue_promotable_map(outer_def_id);
83     }
84
85     let mut visitor = CheckCrateVisitor {
86         tcx,
87         tables: &ty::TypeckTables::empty(None),
88         in_fn: false,
89         in_static: false,
90         mut_rvalue_borrows: NodeSet(),
91         param_env: ty::ParamEnv::empty(),
92         identity_substs: Substs::empty(),
93         result: ItemLocalSet(),
94     };
95
96     // `def_id` should be a `Body` owner
97     let node_id = tcx.hir.as_local_node_id(def_id)
98         .expect("rvalue_promotable_map invoked with non-local def-id");
99     let body_id = tcx.hir.body_owned_by(node_id);
100     let _ = visitor.check_nested_body(body_id);
101
102     Lrc::new(visitor.result)
103 }
104
105 struct CheckCrateVisitor<'a, 'tcx: 'a> {
106     tcx: TyCtxt<'a, 'tcx, 'tcx>,
107     in_fn: bool,
108     in_static: bool,
109     mut_rvalue_borrows: NodeSet,
110     param_env: ty::ParamEnv<'tcx>,
111     identity_substs: &'tcx Substs<'tcx>,
112     tables: &'a ty::TypeckTables<'tcx>,
113     result: ItemLocalSet,
114 }
115
116 #[must_use]
117 #[derive(Debug, PartialEq)]
118 enum Promotability {
119     Promotable,
120     NotPromotable
121 }
122
123 impl BitAnd for Promotability {
124     type Output = Self;
125
126     fn bitand(self, rhs: Self) -> Self {
127         match (self, rhs) {
128             (Promotable, NotPromotable) => NotPromotable,
129             (NotPromotable, Promotable) => NotPromotable,
130             (NotPromotable, NotPromotable) => NotPromotable,
131             (Promotable, Promotable) => Promotable,
132         }
133     }
134 }
135
136 impl BitOr for Promotability {
137     type Output = Self;
138
139     fn bitor(self, rhs: Self) -> Self {
140         match (self, rhs) {
141             (Promotable, NotPromotable) => Promotable,
142             (NotPromotable, Promotable) => Promotable,
143             (NotPromotable, NotPromotable) => NotPromotable,
144             (Promotable, Promotable) => Promotable,
145         }
146     }
147 }
148
149 impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
150     // Returns true iff all the values of the type are promotable.
151     fn type_promotability(&mut self, ty: Ty<'gcx>) -> Promotability {
152         debug!("type_promotability({})", ty);
153
154         if ty.is_freeze(self.tcx, self.param_env, DUMMY_SP) &&
155             !ty.needs_drop(self.tcx, self.param_env) {
156             Promotable
157         } else {
158             NotPromotable
159         }
160     }
161
162     fn handle_const_fn_call(&mut self, def_id: DefId,
163                             ret_ty: Ty<'gcx>, span: Span) -> Promotability {
164         if let NotPromotable = self.type_promotability(ret_ty) {
165             return NotPromotable;
166         }
167
168         let node_check = if let Some(fn_id) = self.tcx.hir.as_local_node_id(def_id) {
169             FnLikeNode::from_node(self.tcx.hir.get(fn_id)).map_or(false, |fn_like| {
170                 fn_like.constness() == hir::Constness::Const
171             })
172         } else {
173             self.tcx.is_const_fn(def_id)
174         };
175
176         if !node_check {
177             return NotPromotable
178         }
179
180         if let Some(&attr::Stability {
181             rustc_const_unstable: Some(attr::RustcConstUnstable {
182                                            feature: ref feature_name
183                                        }),
184             .. }) = self.tcx.lookup_stability(def_id) {
185             let stable_check =
186                 // feature-gate is enabled,
187                 self.tcx.features()
188                     .declared_lib_features
189                     .iter()
190                     .any(|&(ref sym, _)| sym == feature_name) ||
191
192                     // this comes from a crate with the feature-gate enabled,
193                     !def_id.is_local() ||
194
195                     // this comes from a macro that has #[allow_internal_unstable]
196                     span.allows_unstable();
197             if !stable_check {
198                 return NotPromotable
199             }
200         };
201         Promotable
202     }
203
204     /// While the `ExprUseVisitor` walks, we will identify which
205     /// expressions are borrowed, and insert their ids into this
206     /// table. Actually, we insert the "borrow-id", which is normally
207     /// the id of the expession being borrowed: but in the case of
208     /// `ref mut` borrows, the `id` of the pattern is
209     /// inserted. Therefore later we remove that entry from the table
210     /// and transfer it over to the value being matched. This will
211     /// then prevent said value from being promoted.
212     fn remove_mut_rvalue_borrow(&mut self, pat: &hir::Pat) -> bool {
213         let mut any_removed = false;
214         pat.walk(|p| {
215             any_removed |= self.mut_rvalue_borrows.remove(&p.id);
216             true
217         });
218         any_removed
219     }
220 }
221
222 impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
223     fn check_nested_body(&mut self, body_id: hir::BodyId) -> Promotability {
224         let item_id = self.tcx.hir.body_owner(body_id);
225         let item_def_id = self.tcx.hir.local_def_id(item_id);
226
227         let outer_in_fn = self.in_fn;
228         let outer_tables = self.tables;
229         let outer_param_env = self.param_env;
230         let outer_identity_substs = self.identity_substs;
231
232         self.in_fn = false;
233         self.in_static = false;
234
235         match self.tcx.hir.body_owner_kind(item_id) {
236             hir::BodyOwnerKind::Fn => self.in_fn = true,
237             hir::BodyOwnerKind::Static(_) => self.in_static = true,
238             _ => {}
239         };
240
241
242         self.tables = self.tcx.typeck_tables_of(item_def_id);
243         self.param_env = self.tcx.param_env(item_def_id);
244         self.identity_substs = Substs::identity_for_item(self.tcx, item_def_id);
245
246         let body = self.tcx.hir.body(body_id);
247
248         let tcx = self.tcx;
249         let param_env = self.param_env;
250         let region_scope_tree = self.tcx.region_scope_tree(item_def_id);
251         let tables = self.tables;
252         euv::ExprUseVisitor::new(self, tcx, param_env, &region_scope_tree, tables, None)
253             .consume_body(body);
254
255         let body_promotable = self.check_expr(&body.value);
256         self.in_fn = outer_in_fn;
257         self.tables = outer_tables;
258         self.param_env = outer_param_env;
259         self.identity_substs = outer_identity_substs;
260         body_promotable
261     }
262
263     fn check_stmt(&mut self, stmt: &'tcx hir::Stmt) -> Promotability {
264         match stmt.node {
265             hir::StmtKind::Decl(ref decl, _node_id) => {
266                 match &decl.node {
267                     hir::DeclKind::Local(local) => {
268                         if self.remove_mut_rvalue_borrow(&local.pat) {
269                             if let Some(init) = &local.init {
270                                 self.mut_rvalue_borrows.insert(init.id);
271                             }
272                         }
273
274                         match local.init {
275                             Some(ref expr) => { let _ = self.check_expr(&expr); },
276                             None => {},
277                         }
278                         NotPromotable
279                     }
280                     // Item statements are allowed
281                     hir::DeclKind::Item(_) => Promotable
282                 }
283             }
284             hir::StmtKind::Expr(ref box_expr, _node_id) |
285             hir::StmtKind::Semi(ref box_expr, _node_id) => {
286                 let _ = self.check_expr(box_expr);
287                 NotPromotable
288             }
289         }
290     }
291
292     fn check_expr(&mut self, ex: &'tcx hir::Expr) -> Promotability {
293         let node_ty = self.tables.node_id_to_type(ex.hir_id);
294         let mut outer = check_expr_kind(self, ex, node_ty);
295         outer = outer & check_adjustments(self, ex);
296
297         // Handle borrows on (or inside the autorefs of) this expression.
298         if self.mut_rvalue_borrows.remove(&ex.id) {
299             outer = NotPromotable
300         }
301
302         if outer == Promotable {
303             self.result.insert(ex.hir_id.local_id);
304         }
305         outer
306     }
307
308     fn check_block(&mut self, block: &'tcx hir::Block) -> Promotability {
309         let mut iter_result = Promotable;
310         for index in block.stmts.iter() {
311             iter_result = iter_result & self.check_stmt(index);
312         }
313         match block.expr {
314             Some(ref box_expr) => iter_result & self.check_expr(&*box_expr),
315             None => iter_result,
316         }
317     }
318 }
319
320 /// This function is used to enforce the constraints on
321 /// const/static items. It walks through the *value*
322 /// of the item walking down the expression and evaluating
323 /// every nested expression. If the expression is not part
324 /// of a const/static item, it is qualified for promotion
325 /// instead of producing errors.
326 fn check_expr_kind<'a, 'tcx>(
327     v: &mut CheckCrateVisitor<'a, 'tcx>,
328     e: &'tcx hir::Expr, node_ty: Ty<'tcx>) -> Promotability {
329
330     let ty_result = match node_ty.sty {
331         ty::TyAdt(def, _) if def.has_dtor(v.tcx) => {
332             NotPromotable
333         }
334         _ => Promotable
335     };
336
337     let node_result = match e.node {
338         hir::ExprKind::Box(ref expr) => {
339             let _ = v.check_expr(&expr);
340             NotPromotable
341         }
342         hir::ExprKind::Unary(op, ref expr) => {
343             let expr_promotability = v.check_expr(expr);
344             if v.tables.is_method_call(e) {
345                 return NotPromotable;
346             }
347             if op == hir::UnDeref {
348                 return NotPromotable;
349             }
350             expr_promotability
351         }
352         hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
353             let lefty = v.check_expr(lhs);
354             let righty = v.check_expr(rhs);
355             if v.tables.is_method_call(e) {
356                 return NotPromotable;
357             }
358             match v.tables.node_id_to_type(lhs.hir_id).sty {
359                 ty::TyRawPtr(_) => {
360                     assert!(op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne ||
361                         op.node == hir::BinOpKind::Le || op.node == hir::BinOpKind::Lt ||
362                         op.node == hir::BinOpKind::Ge || op.node == hir::BinOpKind::Gt);
363
364                     NotPromotable
365                 }
366                 _ => lefty & righty
367             }
368         }
369         hir::ExprKind::Cast(ref from, _) => {
370             let expr_promotability = v.check_expr(from);
371             debug!("Checking const cast(id={})", from.id);
372             match v.tables.cast_kinds().get(from.hir_id) {
373                 None => {
374                     v.tcx.sess.delay_span_bug(e.span, "no kind for cast");
375                     NotPromotable
376                 },
377                 Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
378                     NotPromotable
379                 }
380                 _ => expr_promotability
381             }
382         }
383         hir::ExprKind::Path(ref qpath) => {
384             let def = v.tables.qpath_def(qpath, e.hir_id);
385             match def {
386                 Def::VariantCtor(..) | Def::StructCtor(..) |
387                 Def::Fn(..) | Def::Method(..) => Promotable,
388
389                 // References to a static that are themselves within a static
390                 // are inherently promotable with the exception
391                 //  of "#[thread_local]" statics, which may not
392                 // outlive the current function
393                 Def::Static(did, _) => {
394
395                     if v.in_static {
396                         for attr in &v.tcx.get_attrs(did)[..] {
397                             if attr.check_name("thread_local") {
398                                 debug!("Reference to Static(id={:?}) is unpromotable \
399                                        due to a #[thread_local] attribute", did);
400                                 return NotPromotable;
401                             }
402                         }
403                         Promotable
404                     } else {
405                         debug!("Reference to Static(id={:?}) is unpromotable as it is not \
406                                referenced from a static", did);
407                         NotPromotable
408
409                     }
410                 }
411
412                 Def::Const(did) |
413                 Def::AssociatedConst(did) => {
414                     let promotable = if v.tcx.trait_of_item(did).is_some() {
415                         // Don't peek inside trait associated constants.
416                         NotPromotable
417                     } else if v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did) {
418                         Promotable
419                     } else {
420                         NotPromotable
421                     };
422                     // Just in case the type is more specific than the definition,
423                     // e.g. impl associated const with type parameters, check it.
424                     // Also, trait associated consts are relaxed by this.
425                     promotable | v.type_promotability(node_ty)
426                 }
427                 _ => NotPromotable
428             }
429         }
430         hir::ExprKind::Call(ref callee, ref hirvec) => {
431             let mut call_result = v.check_expr(callee);
432             for index in hirvec.iter() {
433                 call_result = call_result & v.check_expr(index);
434             }
435             let mut callee = &**callee;
436             loop {
437                 callee = match callee.node {
438                     hir::ExprKind::Block(ref block, _) => match block.expr {
439                         Some(ref tail) => &tail,
440                         None => break
441                     },
442                     _ => break
443                 };
444             }
445             // The callee is an arbitrary expression, it doesn't necessarily have a definition.
446             let def = if let hir::ExprKind::Path(ref qpath) = callee.node {
447                 v.tables.qpath_def(qpath, callee.hir_id)
448             } else {
449                 Def::Err
450             };
451             let def_result = match def {
452                 Def::StructCtor(_, CtorKind::Fn) |
453                 Def::VariantCtor(_, CtorKind::Fn) => Promotable,
454                 Def::Fn(did) => {
455                     v.handle_const_fn_call(did, node_ty, e.span)
456                 }
457                 Def::Method(did) => {
458                     match v.tcx.associated_item(did).container {
459                         ty::ImplContainer(_) => {
460                             v.handle_const_fn_call(did, node_ty, e.span)
461                         }
462                         ty::TraitContainer(_) => NotPromotable,
463                     }
464                 }
465                 _ => NotPromotable,
466             };
467             def_result & call_result
468         }
469         hir::ExprKind::MethodCall(ref _pathsegment, ref _span, ref hirvec) => {
470             let mut method_call_result = Promotable;
471             for index in hirvec.iter() {
472                 method_call_result = method_call_result & v.check_expr(index);
473             }
474             if let Some(def) = v.tables.type_dependent_defs().get(e.hir_id) {
475                 let def_id = def.def_id();
476                 match v.tcx.associated_item(def_id).container {
477                     ty::ImplContainer(_) => {
478                         method_call_result = method_call_result
479                             & v.handle_const_fn_call(def_id, node_ty, e.span);
480                     }
481                     ty::TraitContainer(_) => return NotPromotable,
482                 };
483             } else {
484                 v.tcx.sess.delay_span_bug(e.span, "no type-dependent def for method call");
485             }
486             method_call_result
487         }
488         hir::ExprKind::Struct(ref _qpath, ref hirvec, ref option_expr) => {
489             let mut struct_result = Promotable;
490             for index in hirvec.iter() {
491                 struct_result = struct_result & v.check_expr(&index.expr);
492             }
493             match *option_expr {
494                 Some(ref expr) => { struct_result = struct_result & v.check_expr(&expr); },
495                 None => {},
496             }
497             if let ty::TyAdt(adt, ..) = v.tables.expr_ty(e).sty {
498                 // unsafe_cell_type doesn't necessarily exist with no_core
499                 if Some(adt.did) == v.tcx.lang_items().unsafe_cell_type() {
500                     return NotPromotable;
501                 }
502             }
503             struct_result
504         }
505
506         hir::ExprKind::Lit(_) => Promotable,
507
508         hir::ExprKind::AddrOf(_, ref expr) |
509         hir::ExprKind::Repeat(ref expr, _) => {
510             v.check_expr(&expr)
511         }
512
513         hir::ExprKind::Closure(_capture_clause, ref _box_fn_decl,
514                          body_id, _span, _option_generator_movability) => {
515             let nested_body_promotable = v.check_nested_body(body_id);
516             // Paths in constant contexts cannot refer to local variables,
517             // as there are none, and thus closures can't have upvars there.
518             if v.tcx.with_freevars(e.id, |fv| !fv.is_empty()) {
519                 NotPromotable
520             } else {
521                 nested_body_promotable
522             }
523         }
524
525         hir::ExprKind::Field(ref expr, _ident) => {
526             let expr_promotability = v.check_expr(&expr);
527             if let Some(def) = v.tables.expr_ty(expr).ty_adt_def() {
528                 if def.is_union() {
529                     return NotPromotable;
530                 }
531             }
532             expr_promotability
533         }
534
535         hir::ExprKind::Block(ref box_block, ref _option_label) => {
536             v.check_block(box_block)
537         }
538
539         hir::ExprKind::Index(ref lhs, ref rhs) => {
540             let lefty = v.check_expr(lhs);
541             let righty = v.check_expr(rhs);
542             if v.tables.is_method_call(e) {
543                 return NotPromotable;
544             }
545             lefty & righty
546         }
547
548         hir::ExprKind::Array(ref hirvec) => {
549             let mut array_result = Promotable;
550             for index in hirvec.iter() {
551                 array_result = array_result & v.check_expr(index);
552             }
553             array_result
554         }
555
556         hir::ExprKind::Type(ref expr, ref _ty) => {
557             v.check_expr(&expr)
558         }
559
560         hir::ExprKind::Tup(ref hirvec) => {
561             let mut tup_result = Promotable;
562             for index in hirvec.iter() {
563                 tup_result = tup_result & v.check_expr(index);
564             }
565             tup_result
566         }
567
568
569         // Conditional control flow (possible to implement).
570         hir::ExprKind::Match(ref expr, ref hirvec_arm, ref _match_source) => {
571             // Compute the most demanding borrow from all the arms'
572             // patterns and set that on the discriminator.
573             let mut mut_borrow = false;
574             for pat in hirvec_arm.iter().flat_map(|arm| &arm.pats) {
575                 mut_borrow = v.remove_mut_rvalue_borrow(pat);
576             }
577             if mut_borrow {
578                 v.mut_rvalue_borrows.insert(expr.id);
579             }
580
581             let _ = v.check_expr(expr);
582             for index in hirvec_arm.iter() {
583                 let _ = v.check_expr(&*index.body);
584                 match index.guard {
585                     Some(ref expr) => {
586                         let _ = v.check_expr(&expr);
587                     },
588                     None => {},
589                 };
590             }
591             NotPromotable
592         }
593
594         hir::ExprKind::If(ref lhs, ref rhs, ref option_expr) => {
595             let _ = v.check_expr(lhs);
596             let _ = v.check_expr(rhs);
597             match option_expr {
598                 Some(ref expr) => { let _ = v.check_expr(&expr); },
599                 None => {},
600             };
601             NotPromotable
602         }
603
604         // Loops (not very meaningful in constants).
605         hir::ExprKind::While(ref expr, ref box_block, ref _option_label) => {
606             let _ = v.check_expr(expr);
607             let _ = v.check_block(box_block);
608             NotPromotable
609         }
610
611         hir::ExprKind::Loop(ref box_block, ref _option_label, ref _loop_source) => {
612             let _ = v.check_block(box_block);
613             NotPromotable
614         }
615
616         // More control flow (also not very meaningful).
617         hir::ExprKind::Break(_, ref option_expr) | hir::ExprKind::Ret(ref option_expr) => {
618             match *option_expr {
619                 Some(ref expr) => { let _ = v.check_expr(&expr); },
620                 None => {},
621             }
622             NotPromotable
623         }
624
625         hir::ExprKind::Continue(_) => {
626             NotPromotable
627         }
628
629         // Generator expressions
630         hir::ExprKind::Yield(ref expr) => {
631             let _ = v.check_expr(&expr);
632             NotPromotable
633         }
634
635         // Expressions with side-effects.
636         hir::ExprKind::AssignOp(_, ref lhs, ref rhs) | hir::ExprKind::Assign(ref lhs, ref rhs) => {
637             let _ = v.check_expr(lhs);
638             let _ = v.check_expr(rhs);
639             NotPromotable
640         }
641
642         hir::ExprKind::InlineAsm(ref _inline_asm, ref hirvec_lhs, ref hirvec_rhs) => {
643             for index in hirvec_lhs.iter() {
644                 let _ = v.check_expr(index);
645             }
646             for index in hirvec_rhs.iter() {
647                 let _ = v.check_expr(index);
648             }
649             NotPromotable
650         }
651     };
652     ty_result & node_result
653 }
654
655 /// Check the adjustments of an expression
656 fn check_adjustments<'a, 'tcx>(
657     v: &mut CheckCrateVisitor<'a, 'tcx>,
658     e: &hir::Expr) -> Promotability {
659     use rustc::ty::adjustment::*;
660
661     let mut adjustments = v.tables.expr_adjustments(e).iter().peekable();
662     while let Some(adjustment) = adjustments.next() {
663         match adjustment.kind {
664             Adjust::NeverToAny |
665             Adjust::ReifyFnPointer |
666             Adjust::UnsafeFnPointer |
667             Adjust::ClosureFnPointer |
668             Adjust::MutToConstPointer |
669             Adjust::Borrow(_) |
670             Adjust::Unsize => {}
671
672             Adjust::Deref(_) => {
673                 if let Some(next_adjustment) = adjustments.peek() {
674                     if let Adjust::Borrow(_) = next_adjustment.kind {
675                         continue;
676                     }
677                 }
678                 return NotPromotable;
679             }
680         }
681     }
682     Promotable
683 }
684
685 impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
686     fn consume(&mut self,
687                _consume_id: ast::NodeId,
688                _consume_span: Span,
689                _cmt: &mc::cmt_,
690                _mode: euv::ConsumeMode) {}
691
692     fn borrow(&mut self,
693               borrow_id: ast::NodeId,
694               _borrow_span: Span,
695               cmt: &mc::cmt_<'tcx>,
696               _loan_region: ty::Region<'tcx>,
697               bk: ty::BorrowKind,
698               loan_cause: euv::LoanCause) {
699         debug!(
700             "borrow(borrow_id={:?}, cmt={:?}, bk={:?}, loan_cause={:?})",
701             borrow_id,
702             cmt,
703             bk,
704             loan_cause,
705         );
706
707         // Kind of hacky, but we allow Unsafe coercions in constants.
708         // These occur when we convert a &T or *T to a *U, as well as
709         // when making a thin pointer (e.g., `*T`) into a fat pointer
710         // (e.g., `*Trait`).
711         match loan_cause {
712             euv::LoanCause::AutoUnsafe => {
713                 return;
714             }
715             _ => {}
716         }
717
718         let mut cur = cmt;
719         loop {
720             match cur.cat {
721                 Categorization::Rvalue(..) => {
722                     if loan_cause == euv::MatchDiscriminant {
723                         // Ignore the dummy immutable borrow created by EUV.
724                         break;
725                     }
726                     if bk.to_mutbl_lossy() == hir::MutMutable {
727                         self.mut_rvalue_borrows.insert(borrow_id);
728                     }
729                     break;
730                 }
731                 Categorization::StaticItem => {
732                     break;
733                 }
734                 Categorization::Deref(ref cmt, _) |
735                 Categorization::Downcast(ref cmt, _) |
736                 Categorization::Interior(ref cmt, _) => {
737                     cur = cmt;
738                 }
739
740                 Categorization::Upvar(..) |
741                 Categorization::Local(..) => break,
742             }
743         }
744     }
745
746     fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {}
747     fn mutate(&mut self,
748               _assignment_id: ast::NodeId,
749               _assignment_span: Span,
750               _assignee_cmt: &mc::cmt_,
751               _mode: euv::MutateMode) {
752     }
753
754     fn matched_pat(&mut self, _: &hir::Pat, _: &mc::cmt_, _: euv::MatchMode) {}
755
756     fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: &mc::cmt_, _mode: euv::ConsumeMode) {}
757 }