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