]> git.lizzy.rs Git - rust.git/blob - src/librustc_borrowck/borrowck/mod.rs
Auto merge of #52735 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
[rust.git] / src / librustc_borrowck / borrowck / mod.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 //! See The Book chapter on the borrow checker for more details.
12
13 #![allow(non_camel_case_types)]
14
15 pub use self::LoanPathKind::*;
16 pub use self::LoanPathElem::*;
17 pub use self::bckerr_code::*;
18 pub use self::AliasableViolationKind::*;
19 pub use self::MovedValueUseKind::*;
20
21 use self::InteriorKind::*;
22
23 use rustc::hir::HirId;
24 use rustc::hir::map as hir_map;
25 use rustc::hir::map::blocks::FnLikeNode;
26 use rustc::cfg;
27 use rustc::middle::dataflow::DataFlowContext;
28 use rustc::middle::dataflow::BitwiseOperator;
29 use rustc::middle::dataflow::DataFlowOperator;
30 use rustc::middle::dataflow::KillFrom;
31 use rustc::middle::borrowck::BorrowCheckResult;
32 use rustc::hir::def_id::{DefId, LocalDefId};
33 use rustc::middle::expr_use_visitor as euv;
34 use rustc::middle::mem_categorization as mc;
35 use rustc::middle::mem_categorization::Categorization;
36 use rustc::middle::mem_categorization::ImmutabilityBlame;
37 use rustc::middle::region;
38 use rustc::middle::free_region::RegionRelations;
39 use rustc::ty::{self, Ty, TyCtxt};
40 use rustc::ty::query::Providers;
41 use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};
42 use rustc_mir::util::suggest_ref_mut;
43 use rustc::util::nodemap::FxHashSet;
44
45 use std::cell::RefCell;
46 use std::fmt;
47 use std::rc::Rc;
48 use rustc_data_structures::sync::Lrc;
49 use std::hash::{Hash, Hasher};
50 use syntax::ast;
51 use syntax_pos::{MultiSpan, Span};
52 use errors::{DiagnosticBuilder, DiagnosticId};
53
54 use rustc::hir;
55 use rustc::hir::intravisit::{self, Visitor};
56
57 pub mod check_loans;
58
59 pub mod gather_loans;
60
61 pub mod move_data;
62
63 mod unused;
64
65 #[derive(Clone, Copy)]
66 pub struct LoanDataFlowOperator;
67
68 pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator>;
69
70 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
71     tcx.par_body_owners(|body_owner_def_id| {
72         tcx.borrowck(body_owner_def_id);
73     });
74 }
75
76 pub fn provide(providers: &mut Providers) {
77     *providers = Providers {
78         borrowck,
79         ..*providers
80     };
81 }
82
83 /// Collection of conclusions determined via borrow checker analyses.
84 pub struct AnalysisData<'a, 'tcx: 'a> {
85     pub all_loans: Vec<Loan<'tcx>>,
86     pub loans: DataFlowContext<'a, 'tcx, LoanDataFlowOperator>,
87     pub move_data: move_data::FlowedMoveData<'a, 'tcx>,
88 }
89
90 fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId)
91     -> Lrc<BorrowCheckResult>
92 {
93     assert!(tcx.use_ast_borrowck());
94
95     debug!("borrowck(body_owner_def_id={:?})", owner_def_id);
96
97     let owner_id = tcx.hir.as_local_node_id(owner_def_id).unwrap();
98
99     match tcx.hir.get(owner_id) {
100         hir_map::NodeStructCtor(_) |
101         hir_map::NodeVariant(_) => {
102             // We get invoked with anything that has MIR, but some of
103             // those things (notably the synthesized constructors from
104             // tuple structs/variants) do not have an associated body
105             // and do not need borrowchecking.
106             return Lrc::new(BorrowCheckResult {
107                 used_mut_nodes: FxHashSet(),
108             })
109         }
110         _ => { }
111     }
112
113     let body_id = tcx.hir.body_owned_by(owner_id);
114     let tables = tcx.typeck_tables_of(owner_def_id);
115     let region_scope_tree = tcx.region_scope_tree(owner_def_id);
116     let body = tcx.hir.body(body_id);
117     let mut bccx = BorrowckCtxt {
118         tcx,
119         tables,
120         region_scope_tree,
121         owner_def_id,
122         body,
123         used_mut_nodes: RefCell::new(FxHashSet()),
124     };
125
126     // Eventually, borrowck will always read the MIR, but at the
127     // moment we do not. So, for now, we always force MIR to be
128     // constructed for a given fn, since this may result in errors
129     // being reported and we want that to happen.
130     //
131     // Note that `mir_validated` is a "stealable" result; the
132     // thief, `optimized_mir()`, forces borrowck, so we know that
133     // is not yet stolen.
134     ty::query::queries::mir_validated::ensure(tcx, owner_def_id);
135
136     // option dance because you can't capture an uninitialized variable
137     // by mut-ref.
138     let mut cfg = None;
139     if let Some(AnalysisData { all_loans,
140                                loans: loan_dfcx,
141                                move_data: flowed_moves }) =
142         build_borrowck_dataflow_data(&mut bccx, false, body_id,
143                                      |bccx| {
144                                          cfg = Some(cfg::CFG::new(bccx.tcx, &body));
145                                          cfg.as_mut().unwrap()
146                                      })
147     {
148         check_loans::check_loans(&mut bccx, &loan_dfcx, &flowed_moves, &all_loans, body);
149     }
150
151     if !tcx.use_mir_borrowck() {
152         unused::check(&mut bccx, body);
153     }
154
155     Lrc::new(BorrowCheckResult {
156         used_mut_nodes: bccx.used_mut_nodes.into_inner(),
157     })
158 }
159
160 fn build_borrowck_dataflow_data<'a, 'c, 'tcx, F>(this: &mut BorrowckCtxt<'a, 'tcx>,
161                                                  force_analysis: bool,
162                                                  body_id: hir::BodyId,
163                                                  get_cfg: F)
164                                                  -> Option<AnalysisData<'a, 'tcx>>
165     where F: FnOnce(&mut BorrowckCtxt<'a, 'tcx>) -> &'c cfg::CFG
166 {
167     // Check the body of fn items.
168     let tcx = this.tcx;
169     let id_range = {
170         let mut visitor = intravisit::IdRangeComputingVisitor::new(&tcx.hir);
171         visitor.visit_body(this.body);
172         visitor.result()
173     };
174     let (all_loans, move_data) =
175         gather_loans::gather_loans_in_fn(this, body_id);
176
177     if !force_analysis && move_data.is_empty() && all_loans.is_empty() {
178         // large arrays of data inserted as constants can take a lot of
179         // time and memory to borrow-check - see issue #36799. However,
180         // they don't have places, so no borrow-check is actually needed.
181         // Recognize that case and skip borrow-checking.
182         debug!("skipping loan propagation for {:?} because of no loans", body_id);
183         return None;
184     } else {
185         debug!("propagating loans in {:?}", body_id);
186     }
187
188     let cfg = get_cfg(this);
189     let mut loan_dfcx =
190         DataFlowContext::new(this.tcx,
191                              "borrowck",
192                              Some(this.body),
193                              cfg,
194                              LoanDataFlowOperator,
195                              id_range,
196                              all_loans.len());
197     for (loan_idx, loan) in all_loans.iter().enumerate() {
198         loan_dfcx.add_gen(loan.gen_scope.item_local_id(), loan_idx);
199         loan_dfcx.add_kill(KillFrom::ScopeEnd,
200                            loan.kill_scope.item_local_id(),
201                            loan_idx);
202     }
203     loan_dfcx.add_kills_from_flow_exits(cfg);
204     loan_dfcx.propagate(cfg, this.body);
205
206     let flowed_moves = move_data::FlowedMoveData::new(move_data,
207                                                       this,
208                                                       cfg,
209                                                       id_range,
210                                                       this.body);
211
212     Some(AnalysisData { all_loans,
213                         loans: loan_dfcx,
214                         move_data:flowed_moves })
215 }
216
217 /// Accessor for introspective clients inspecting `AnalysisData` and
218 /// the `BorrowckCtxt` itself , e.g. the flowgraph visualizer.
219 pub fn build_borrowck_dataflow_data_for_fn<'a, 'tcx>(
220     tcx: TyCtxt<'a, 'tcx, 'tcx>,
221     body_id: hir::BodyId,
222     cfg: &cfg::CFG)
223     -> (BorrowckCtxt<'a, 'tcx>, AnalysisData<'a, 'tcx>)
224 {
225     let owner_id = tcx.hir.body_owner(body_id);
226     let owner_def_id = tcx.hir.local_def_id(owner_id);
227     let tables = tcx.typeck_tables_of(owner_def_id);
228     let region_scope_tree = tcx.region_scope_tree(owner_def_id);
229     let body = tcx.hir.body(body_id);
230     let mut bccx = BorrowckCtxt {
231         tcx,
232         tables,
233         region_scope_tree,
234         owner_def_id,
235         body,
236         used_mut_nodes: RefCell::new(FxHashSet()),
237     };
238
239     let dataflow_data = build_borrowck_dataflow_data(&mut bccx, true, body_id, |_| cfg);
240     (bccx, dataflow_data.unwrap())
241 }
242
243 // ----------------------------------------------------------------------
244 // Type definitions
245
246 pub struct BorrowckCtxt<'a, 'tcx: 'a> {
247     tcx: TyCtxt<'a, 'tcx, 'tcx>,
248
249     // tables for the current thing we are checking; set to
250     // Some in `borrowck_fn` and cleared later
251     tables: &'a ty::TypeckTables<'tcx>,
252
253     region_scope_tree: Lrc<region::ScopeTree>,
254
255     owner_def_id: DefId,
256
257     body: &'tcx hir::Body,
258
259     used_mut_nodes: RefCell<FxHashSet<HirId>>,
260 }
261
262 impl<'a, 'b, 'tcx: 'b> BorrowckErrors<'a> for &'a BorrowckCtxt<'b, 'tcx> {
263     fn struct_span_err_with_code<S: Into<MultiSpan>>(self,
264                                                      sp: S,
265                                                      msg: &str,
266                                                      code: DiagnosticId)
267                                                      -> DiagnosticBuilder<'a>
268     {
269         self.tcx.sess.struct_span_err_with_code(sp, msg, code)
270     }
271
272     fn struct_span_err<S: Into<MultiSpan>>(self,
273                                            sp: S,
274                                            msg: &str)
275                                            -> DiagnosticBuilder<'a>
276     {
277         self.tcx.sess.struct_span_err(sp, msg)
278     }
279
280     fn cancel_if_wrong_origin(self,
281                               mut diag: DiagnosticBuilder<'a>,
282                               o: Origin)
283                               -> DiagnosticBuilder<'a>
284     {
285         if !o.should_emit_errors(self.tcx.borrowck_mode()) {
286             self.tcx.sess.diagnostic().cancel(&mut diag);
287         }
288         diag
289     }
290 }
291
292 ///////////////////////////////////////////////////////////////////////////
293 // Loans and loan paths
294
295 /// Record of a loan that was issued.
296 pub struct Loan<'tcx> {
297     index: usize,
298     loan_path: Rc<LoanPath<'tcx>>,
299     kind: ty::BorrowKind,
300     restricted_paths: Vec<Rc<LoanPath<'tcx>>>,
301
302     /// gen_scope indicates where loan is introduced. Typically the
303     /// loan is introduced at the point of the borrow, but in some
304     /// cases, notably method arguments, the loan may be introduced
305     /// only later, once it comes into scope.  See also
306     /// `GatherLoanCtxt::compute_gen_scope`.
307     gen_scope: region::Scope,
308
309     /// kill_scope indicates when the loan goes out of scope.  This is
310     /// either when the lifetime expires or when the local variable
311     /// which roots the loan-path goes out of scope, whichever happens
312     /// faster. See also `GatherLoanCtxt::compute_kill_scope`.
313     kill_scope: region::Scope,
314     span: Span,
315     cause: euv::LoanCause,
316 }
317
318 impl<'tcx> Loan<'tcx> {
319     pub fn loan_path(&self) -> Rc<LoanPath<'tcx>> {
320         self.loan_path.clone()
321     }
322 }
323
324 #[derive(Eq)]
325 pub struct LoanPath<'tcx> {
326     kind: LoanPathKind<'tcx>,
327     ty: Ty<'tcx>,
328 }
329
330 impl<'tcx> PartialEq for LoanPath<'tcx> {
331     fn eq(&self, that: &LoanPath<'tcx>) -> bool {
332         self.kind == that.kind
333     }
334 }
335
336 impl<'tcx> Hash for LoanPath<'tcx> {
337     fn hash<H: Hasher>(&self, state: &mut H) {
338         self.kind.hash(state);
339     }
340 }
341
342 #[derive(PartialEq, Eq, Hash, Debug)]
343 pub enum LoanPathKind<'tcx> {
344     LpVar(ast::NodeId),                         // `x` in README.md
345     LpUpvar(ty::UpvarId),                       // `x` captured by-value into closure
346     LpDowncast(Rc<LoanPath<'tcx>>, DefId), // `x` downcast to particular enum variant
347     LpExtend(Rc<LoanPath<'tcx>>, mc::MutabilityCategory, LoanPathElem<'tcx>)
348 }
349
350 impl<'tcx> LoanPath<'tcx> {
351     fn new(kind: LoanPathKind<'tcx>, ty: Ty<'tcx>) -> LoanPath<'tcx> {
352         LoanPath { kind: kind, ty: ty }
353     }
354
355     fn to_type(&self) -> Ty<'tcx> { self.ty }
356
357     fn has_downcast(&self) -> bool {
358         match self.kind {
359             LpDowncast(_, _) => true,
360             LpExtend(ref lp, _, LpInterior(_, _)) => {
361                 lp.has_downcast()
362             }
363             _ => false,
364         }
365     }
366 }
367
368 // FIXME (pnkfelix): See discussion here
369 // https://github.com/pnkfelix/rust/commit/
370 //     b2b39e8700e37ad32b486b9a8409b50a8a53aa51#commitcomment-7892003
371 const DOWNCAST_PRINTED_OPERATOR: &'static str = " as ";
372
373 // A local, "cleaned" version of `mc::InteriorKind` that drops
374 // information that is not relevant to loan-path analysis. (In
375 // particular, the distinction between how precisely an array-element
376 // is tracked is irrelevant here.)
377 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
378 pub enum InteriorKind {
379     InteriorField(mc::FieldIndex),
380     InteriorElement,
381 }
382
383 trait ToInteriorKind { fn cleaned(self) -> InteriorKind; }
384 impl ToInteriorKind for mc::InteriorKind {
385     fn cleaned(self) -> InteriorKind {
386         match self {
387             mc::InteriorField(name) => InteriorField(name),
388             mc::InteriorElement(_) => InteriorElement,
389         }
390     }
391 }
392
393 // This can be:
394 // - a pointer dereference (`*P` in README.md)
395 // - a field reference, with an optional definition of the containing
396 //   enum variant (`P.f` in README.md)
397 // `DefId` is present when the field is part of struct that is in
398 // a variant of an enum. For instance in:
399 // `enum E { X { foo: u32 }, Y { foo: u32 }}`
400 // each `foo` is qualified by the definitition id of the variant (`X` or `Y`).
401 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
402 pub enum LoanPathElem<'tcx> {
403     LpDeref(mc::PointerKind<'tcx>),
404     LpInterior(Option<DefId>, InteriorKind),
405 }
406
407 fn closure_to_block(closure_id: LocalDefId,
408                     tcx: TyCtxt) -> ast::NodeId {
409     let closure_id = tcx.hir.local_def_id_to_node_id(closure_id);
410     match tcx.hir.get(closure_id) {
411         hir_map::NodeExpr(expr) => match expr.node {
412             hir::ExprKind::Closure(.., body_id, _, _) => {
413                 body_id.node_id
414             }
415             _ => {
416                 bug!("encountered non-closure id: {}", closure_id)
417             }
418         },
419         _ => bug!("encountered non-expr id: {}", closure_id)
420     }
421 }
422
423 impl<'a, 'tcx> LoanPath<'tcx> {
424     pub fn kill_scope(&self, bccx: &BorrowckCtxt<'a, 'tcx>) -> region::Scope {
425         match self.kind {
426             LpVar(local_id) => {
427                 let hir_id = bccx.tcx.hir.node_to_hir_id(local_id);
428                 bccx.region_scope_tree.var_scope(hir_id.local_id)
429             }
430             LpUpvar(upvar_id) => {
431                 let block_id = closure_to_block(upvar_id.closure_expr_id, bccx.tcx);
432                 let hir_id = bccx.tcx.hir.node_to_hir_id(block_id);
433                 region::Scope::Node(hir_id.local_id)
434             }
435             LpDowncast(ref base, _) |
436             LpExtend(ref base, ..) => base.kill_scope(bccx),
437         }
438     }
439
440     fn has_fork(&self, other: &LoanPath<'tcx>) -> bool {
441         match (&self.kind, &other.kind) {
442             (&LpExtend(ref base, _, LpInterior(opt_variant_id, id)),
443              &LpExtend(ref base2, _, LpInterior(opt_variant_id2, id2))) =>
444                 if id == id2 && opt_variant_id == opt_variant_id2 {
445                     base.has_fork(&base2)
446                 } else {
447                     true
448                 },
449             (&LpExtend(ref base, _, LpDeref(_)), _) => base.has_fork(other),
450             (_, &LpExtend(ref base, _, LpDeref(_))) => self.has_fork(&base),
451             _ => false,
452         }
453     }
454
455     fn depth(&self) -> usize {
456         match self.kind {
457             LpExtend(ref base, _, LpDeref(_)) => base.depth(),
458             LpExtend(ref base, _, LpInterior(..)) => base.depth() + 1,
459             _ => 0,
460         }
461     }
462
463     fn common(&self, other: &LoanPath<'tcx>) -> Option<LoanPath<'tcx>> {
464         match (&self.kind, &other.kind) {
465             (&LpExtend(ref base, a, LpInterior(opt_variant_id, id)),
466              &LpExtend(ref base2, _, LpInterior(opt_variant_id2, id2))) => {
467                 if id == id2 && opt_variant_id == opt_variant_id2 {
468                     base.common(&base2).map(|x| {
469                         let xd = x.depth();
470                         if base.depth() == xd && base2.depth() == xd {
471                             LoanPath {
472                                 kind: LpExtend(Rc::new(x), a, LpInterior(opt_variant_id, id)),
473                                 ty: self.ty,
474                             }
475                         } else {
476                             x
477                         }
478                     })
479                 } else {
480                     base.common(&base2)
481                 }
482             }
483             (&LpExtend(ref base, _, LpDeref(_)), _) => base.common(other),
484             (_, &LpExtend(ref other, _, LpDeref(_))) => self.common(&other),
485             (&LpVar(id), &LpVar(id2)) => {
486                 if id == id2 {
487                     Some(LoanPath { kind: LpVar(id), ty: self.ty })
488                 } else {
489                     None
490                 }
491             }
492             (&LpUpvar(id), &LpUpvar(id2)) => {
493                 if id == id2 {
494                     Some(LoanPath { kind: LpUpvar(id), ty: self.ty })
495                 } else {
496                     None
497                 }
498             }
499             _ => None,
500         }
501     }
502 }
503
504 // Avoid "cannot borrow immutable field `self.x` as mutable" as that implies that a field *can* be
505 // mutable independently of the struct it belongs to. (#35937)
506 pub fn opt_loan_path_is_field<'tcx>(cmt: &mc::cmt_<'tcx>) -> (Option<Rc<LoanPath<'tcx>>>, bool) {
507     let new_lp = |v: LoanPathKind<'tcx>| Rc::new(LoanPath::new(v, cmt.ty));
508
509     match cmt.cat {
510         Categorization::Rvalue(..) |
511         Categorization::StaticItem => {
512             (None, false)
513         }
514
515         Categorization::Local(id) => {
516             (Some(new_lp(LpVar(id))), false)
517         }
518
519         Categorization::Upvar(mc::Upvar { id, .. }) => {
520             (Some(new_lp(LpUpvar(id))), false)
521         }
522
523         Categorization::Deref(ref cmt_base, pk) => {
524             let lp = opt_loan_path_is_field(cmt_base);
525             (lp.0.map(|lp| {
526                 new_lp(LpExtend(lp, cmt.mutbl, LpDeref(pk)))
527             }), lp.1)
528         }
529
530         Categorization::Interior(ref cmt_base, ik) => {
531             (opt_loan_path(cmt_base).map(|lp| {
532                 let opt_variant_id = match cmt_base.cat {
533                     Categorization::Downcast(_, did) =>  Some(did),
534                     _ => None
535                 };
536                 new_lp(LpExtend(lp, cmt.mutbl, LpInterior(opt_variant_id, ik.cleaned())))
537             }), true)
538         }
539
540         Categorization::Downcast(ref cmt_base, variant_def_id) => {
541             let lp = opt_loan_path_is_field(cmt_base);
542             (lp.0.map(|lp| {
543                 new_lp(LpDowncast(lp, variant_def_id))
544             }), lp.1)
545         }
546     }
547 }
548
549 /// Computes the `LoanPath` (if any) for a `cmt`.
550 /// Note that this logic is somewhat duplicated in
551 /// the method `compute()` found in `gather_loans::restrictions`,
552 /// which allows it to share common loan path pieces as it
553 /// traverses the CMT.
554 pub fn opt_loan_path<'tcx>(cmt: &mc::cmt_<'tcx>) -> Option<Rc<LoanPath<'tcx>>> {
555     opt_loan_path_is_field(cmt).0
556 }
557
558 ///////////////////////////////////////////////////////////////////////////
559 // Errors
560
561 // Errors that can occur
562 #[derive(Debug, PartialEq)]
563 pub enum bckerr_code<'tcx> {
564     err_mutbl,
565     /// superscope, subscope, loan cause
566     err_out_of_scope(ty::Region<'tcx>, ty::Region<'tcx>, euv::LoanCause),
567     err_borrowed_pointer_too_short(ty::Region<'tcx>, ty::Region<'tcx>), // loan, ptr
568 }
569
570 // Combination of an error code and the categorization of the expression
571 // that caused it
572 #[derive(Debug, PartialEq)]
573 pub struct BckError<'c, 'tcx: 'c> {
574     span: Span,
575     cause: AliasableViolationKind,
576     cmt: &'c mc::cmt_<'tcx>,
577     code: bckerr_code<'tcx>
578 }
579
580 #[derive(Copy, Clone, Debug, PartialEq)]
581 pub enum AliasableViolationKind {
582     MutabilityViolation,
583     BorrowViolation(euv::LoanCause)
584 }
585
586 #[derive(Copy, Clone, Debug)]
587 pub enum MovedValueUseKind {
588     MovedInUse,
589     MovedInCapture,
590 }
591
592 ///////////////////////////////////////////////////////////////////////////
593 // Misc
594
595 impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
596     pub fn is_subregion_of(&self,
597                            r_sub: ty::Region<'tcx>,
598                            r_sup: ty::Region<'tcx>)
599                            -> bool
600     {
601         let region_rels = RegionRelations::new(self.tcx,
602                                                self.owner_def_id,
603                                                &self.region_scope_tree,
604                                                &self.tables.free_region_map);
605         region_rels.is_subregion_of(r_sub, r_sup)
606     }
607
608     pub fn report(&self, err: BckError<'a, 'tcx>) {
609         // Catch and handle some particular cases.
610         match (&err.code, &err.cause) {
611             (&err_out_of_scope(&ty::ReScope(_), &ty::ReStatic, _),
612              &BorrowViolation(euv::ClosureCapture(span))) |
613             (&err_out_of_scope(&ty::ReScope(_), &ty::ReEarlyBound(..), _),
614              &BorrowViolation(euv::ClosureCapture(span))) |
615             (&err_out_of_scope(&ty::ReScope(_), &ty::ReFree(..), _),
616              &BorrowViolation(euv::ClosureCapture(span))) => {
617                 return self.report_out_of_scope_escaping_closure_capture(&err, span);
618             }
619             _ => { }
620         }
621
622         self.report_bckerr(&err);
623     }
624
625     pub fn report_use_of_moved_value(&self,
626                                      use_span: Span,
627                                      use_kind: MovedValueUseKind,
628                                      lp: &LoanPath<'tcx>,
629                                      the_move: &move_data::Move,
630                                      moved_lp: &LoanPath<'tcx>,
631                                      _param_env: ty::ParamEnv<'tcx>) {
632         let (verb, verb_participle) = match use_kind {
633             MovedInUse => ("use", "used"),
634             MovedInCapture => ("capture", "captured"),
635         };
636
637         let (_ol, _moved_lp_msg, mut err, need_note) = match the_move.kind {
638             move_data::Declared => {
639                 // If this is an uninitialized variable, just emit a simple warning
640                 // and return.
641                 self.cannot_act_on_uninitialized_variable(use_span,
642                                                           verb,
643                                                           &self.loan_path_to_string(lp),
644                                                           Origin::Ast)
645                     .span_label(use_span, format!("use of possibly uninitialized `{}`",
646                                                   self.loan_path_to_string(lp)))
647                     .emit();
648                 return;
649             }
650             _ => {
651                 // If moved_lp is something like `x.a`, and lp is something like `x.b`, we would
652                 // normally generate a rather confusing message:
653                 //
654                 //     error: use of moved value: `x.b`
655                 //     note: `x.a` moved here...
656                 //
657                 // What we want to do instead is get the 'common ancestor' of the two moves and
658                 // use that for most of the message instead, giving is something like this:
659                 //
660                 //     error: use of moved value: `x`
661                 //     note: `x` moved here (through moving `x.a`)...
662
663                 let common = moved_lp.common(lp);
664                 let has_common = common.is_some();
665                 let has_fork = moved_lp.has_fork(lp);
666                 let (nl, ol, moved_lp_msg) =
667                     if has_fork && has_common {
668                         let nl = self.loan_path_to_string(&common.unwrap());
669                         let ol = nl.clone();
670                         let moved_lp_msg = format!(" (through moving `{}`)",
671                                                    self.loan_path_to_string(moved_lp));
672                         (nl, ol, moved_lp_msg)
673                     } else {
674                         (self.loan_path_to_string(lp),
675                          self.loan_path_to_string(moved_lp),
676                          String::new())
677                     };
678
679                 let partial = moved_lp.depth() > lp.depth();
680                 let msg = if !has_fork && partial { "partially " }
681                           else if has_fork && !has_common { "collaterally "}
682                 else { "" };
683                 let mut err = self.cannot_act_on_moved_value(use_span,
684                                                              verb,
685                                                              msg,
686                                                              Some(format!("{}", nl)),
687                                                              Origin::Ast);
688                 let need_note = match lp.ty.sty {
689                     ty::TypeVariants::TyClosure(id, _) => {
690                         let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
691                         let hir_id = self.tcx.hir.node_to_hir_id(node_id);
692                         if let Some((span, name)) = self.tables.closure_kind_origins().get(hir_id) {
693                             err.span_note(*span, &format!(
694                                 "closure cannot be invoked more than once because \
695                                 it moves the variable `{}` out of its environment",
696                                 name
697                             ));
698                             false
699                         } else {
700                             true
701                         }
702                     }
703                     _ => true,
704                 };
705                 (ol, moved_lp_msg, err, need_note)
706             }
707         };
708
709         // Get type of value and span where it was previously
710         // moved.
711         let node_id = self.tcx.hir.hir_to_node_id(hir::HirId {
712             owner: self.body.value.hir_id.owner,
713             local_id: the_move.id
714         });
715         let (move_span, move_note) = match the_move.kind {
716             move_data::Declared => {
717                 unreachable!();
718             }
719
720             move_data::MoveExpr |
721             move_data::MovePat => (self.tcx.hir.span(node_id), ""),
722
723             move_data::Captured =>
724                 (match self.tcx.hir.expect_expr(node_id).node {
725                     hir::ExprKind::Closure(.., fn_decl_span, _) => fn_decl_span,
726                     ref r => bug!("Captured({:?}) maps to non-closure: {:?}",
727                                   the_move.id, r),
728                 }, " (into closure)"),
729         };
730
731         // Annotate the use and the move in the span. Watch out for
732         // the case where the use and the move are the same. This
733         // means the use is in a loop.
734         err = if use_span == move_span {
735             err.span_label(
736                 use_span,
737                 format!("value moved{} here in previous iteration of loop",
738                          move_note));
739             err
740         } else {
741             err.span_label(use_span, format!("value {} here after move", verb_participle));
742             err.span_label(move_span, format!("value moved{} here", move_note));
743             err
744         };
745
746         if need_note {
747             err.note(&format!(
748                 "move occurs because {} has type `{}`, which does not implement the `Copy` trait",
749                 if moved_lp.has_downcast() {
750                     "the value".to_string()
751                 } else {
752                     format!("`{}`", self.loan_path_to_string(moved_lp))
753                 },
754                 moved_lp.ty));
755         }
756
757         // Note: we used to suggest adding a `ref binding` or calling
758         // `clone` but those suggestions have been removed because
759         // they are often not what you actually want to do, and were
760         // not considered particularly helpful.
761
762         err.emit();
763     }
764
765     pub fn report_partial_reinitialization_of_uninitialized_structure(
766             &self,
767             span: Span,
768             lp: &LoanPath<'tcx>) {
769         self.cannot_partially_reinit_an_uninit_struct(span,
770                                                       &self.loan_path_to_string(lp),
771                                                       Origin::Ast)
772             .emit();
773     }
774
775     pub fn report_reassigned_immutable_variable(&self,
776                                                 span: Span,
777                                                 lp: &LoanPath<'tcx>,
778                                                 assign:
779                                                 &move_data::Assignment) {
780         let mut err = self.cannot_reassign_immutable(span,
781                                                      &self.loan_path_to_string(lp),
782                                                      false,
783                                                      Origin::Ast);
784         err.span_label(span, "cannot assign twice to immutable variable");
785         if span != assign.span {
786             err.span_label(assign.span, format!("first assignment to `{}`",
787                                                 self.loan_path_to_string(lp)));
788         }
789         err.emit();
790     }
791
792     pub fn struct_span_err_with_code<S: Into<MultiSpan>>(&self,
793                                                          s: S,
794                                                          msg: &str,
795                                                          code: DiagnosticId)
796                                                          -> DiagnosticBuilder<'a> {
797         self.tcx.sess.struct_span_err_with_code(s, msg, code)
798     }
799
800     pub fn span_err_with_code<S: Into<MultiSpan>>(
801         &self,
802         s: S,
803         msg: &str,
804         code: DiagnosticId,
805     ) {
806         self.tcx.sess.span_err_with_code(s, msg, code);
807     }
808
809     fn report_bckerr(&self, err: &BckError<'a, 'tcx>) {
810         let error_span = err.span.clone();
811
812         match err.code {
813             err_mutbl => {
814                 let descr = match err.cmt.note {
815                     mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
816                         self.cmt_to_string(&err.cmt)
817                     }
818                     _ => match opt_loan_path_is_field(&err.cmt) {
819                         (None, true) => {
820                             format!("{} of {} binding",
821                                     self.cmt_to_string(&err.cmt),
822                                     err.cmt.mutbl.to_user_str())
823
824                         }
825                         (None, false) => {
826                             format!("{} {}",
827                                     err.cmt.mutbl.to_user_str(),
828                                     self.cmt_to_string(&err.cmt))
829
830                         }
831                         (Some(lp), true) => {
832                             format!("{} `{}` of {} binding",
833                                     self.cmt_to_string(&err.cmt),
834                                     self.loan_path_to_string(&lp),
835                                     err.cmt.mutbl.to_user_str())
836                         }
837                         (Some(lp), false) => {
838                             format!("{} {} `{}`",
839                                     err.cmt.mutbl.to_user_str(),
840                                     self.cmt_to_string(&err.cmt),
841                                     self.loan_path_to_string(&lp))
842                         }
843                     }
844                 };
845
846                 let mut db = match err.cause {
847                     MutabilityViolation => {
848                         let mut db = self.cannot_assign(error_span, &descr, Origin::Ast);
849                         if let mc::NoteClosureEnv(upvar_id) = err.cmt.note {
850                             let node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
851                             let sp = self.tcx.hir.span(node_id);
852                             let fn_closure_msg = "`Fn` closures cannot capture their enclosing \
853                                                   environment for modifications";
854                             match (self.tcx.sess.codemap().span_to_snippet(sp), &err.cmt.cat) {
855                                 (_, &Categorization::Upvar(mc::Upvar {
856                                     kind: ty::ClosureKind::Fn, ..
857                                 })) => {
858                                     db.note(fn_closure_msg);
859                                     // we should point at the cause for this closure being
860                                     // identified as `Fn` (like in signature of method this
861                                     // closure was passed into)
862                                 }
863                                 (Ok(ref snippet), ref cat) => {
864                                     let msg = &format!("consider making `{}` mutable", snippet);
865                                     let suggestion = format!("mut {}", snippet);
866
867                                     if let &Categorization::Deref(ref cmt, _) = cat {
868                                         if let Categorization::Upvar(mc::Upvar {
869                                             kind: ty::ClosureKind::Fn, ..
870                                         }) = cmt.cat {
871                                             db.note(fn_closure_msg);
872                                         } else {
873                                             db.span_suggestion(sp, msg, suggestion);
874                                         }
875                                     } else {
876                                         db.span_suggestion(sp, msg, suggestion);
877                                     }
878                                 }
879                                 _ => {
880                                     db.span_help(sp, "consider making this binding mutable");
881                                 }
882                             }
883                         }
884                         db
885                     }
886                     BorrowViolation(euv::ClosureCapture(_)) => {
887                         self.closure_cannot_assign_to_borrowed(error_span, &descr, Origin::Ast)
888                     }
889                     BorrowViolation(euv::OverloadedOperator) |
890                     BorrowViolation(euv::AddrOf) |
891                     BorrowViolation(euv::RefBinding) |
892                     BorrowViolation(euv::AutoRef) |
893                     BorrowViolation(euv::AutoUnsafe) |
894                     BorrowViolation(euv::ForLoop) |
895                     BorrowViolation(euv::MatchDiscriminant) => {
896                         self.cannot_borrow_path_as_mutable(error_span, &descr, Origin::Ast)
897                     }
898                     BorrowViolation(euv::ClosureInvocation) => {
899                         span_bug!(err.span,
900                             "err_mutbl with a closure invocation");
901                     }
902                 };
903
904                 self.note_and_explain_mutbl_error(&mut db, &err, &error_span);
905                 self.note_immutability_blame(
906                     &mut db,
907                     err.cmt.immutability_blame(),
908                     self.tcx.hir.hir_to_node_id(err.cmt.hir_id)
909                 );
910                 db.emit();
911             }
912             err_out_of_scope(super_scope, sub_scope, cause) => {
913                 let msg = match opt_loan_path(&err.cmt) {
914                     None => "borrowed value".to_string(),
915                     Some(lp) => {
916                         format!("`{}`", self.loan_path_to_string(&lp))
917                     }
918                 };
919
920                 let mut db = self.path_does_not_live_long_enough(error_span, &msg, Origin::Ast);
921                 let value_kind = match err.cmt.cat {
922                     mc::Categorization::Rvalue(..) => "temporary value",
923                     _ => "borrowed value",
924                 };
925
926                 let is_closure = match cause {
927                     euv::ClosureCapture(s) => {
928                         // The primary span starts out as the closure creation point.
929                         // Change the primary span here to highlight the use of the variable
930                         // in the closure, because it seems more natural. Highlight
931                         // closure creation point as a secondary span.
932                         match db.span.primary_span() {
933                             Some(primary) => {
934                                 db.span = MultiSpan::from_span(s);
935                                 db.span_label(primary, "capture occurs here");
936                                 db.span_label(s, format!("{} does not live long enough",
937                                                          value_kind));
938                                 true
939                             }
940                             None => false
941                         }
942                     }
943                     _ => {
944                         db.span_label(error_span, format!("{} does not live long enough",
945                                                           value_kind));
946                         false
947                     }
948                 };
949
950                 let sub_span = self.region_end_span(sub_scope);
951                 let super_span = self.region_end_span(super_scope);
952
953                 match (sub_span, super_span) {
954                     (Some(s1), Some(s2)) if s1 == s2 => {
955                         if !is_closure {
956                             let msg = match opt_loan_path(&err.cmt) {
957                                 None => value_kind.to_string(),
958                                 Some(lp) => {
959                                     format!("`{}`", self.loan_path_to_string(&lp))
960                                 }
961                             };
962                             db.span_label(s1,
963                                           format!("{} dropped here while still borrowed", msg));
964                         } else {
965                             db.span_label(s1, format!("{} dropped before borrower", value_kind));
966                         }
967                         db.note("values in a scope are dropped in the opposite order \
968                                 they are created");
969                     }
970                     (Some(s1), Some(s2)) if !is_closure => {
971                         let msg = match opt_loan_path(&err.cmt) {
972                             None => value_kind.to_string(),
973                             Some(lp) => {
974                                 format!("`{}`", self.loan_path_to_string(&lp))
975                             }
976                         };
977                         db.span_label(s2, format!("{} dropped here while still borrowed", msg));
978                         db.span_label(s1, format!("{} needs to live until here", value_kind));
979                     }
980                     _ => {
981                         match sub_span {
982                             Some(s) => {
983                                 db.span_label(s, format!("{} needs to live until here",
984                                                           value_kind));
985                             }
986                             None => {
987                                 self.tcx.note_and_explain_region(
988                                     &self.region_scope_tree,
989                                     &mut db,
990                                     "borrowed value must be valid for ",
991                                     sub_scope,
992                                     "...");
993                             }
994                         }
995                         match super_span {
996                             Some(s) => {
997                                 db.span_label(s, format!("{} only lives until here", value_kind));
998                             }
999                             None => {
1000                                 self.tcx.note_and_explain_region(
1001                                     &self.region_scope_tree,
1002                                     &mut db,
1003                                     "...but borrowed value is only valid for ",
1004                                     super_scope,
1005                                     "");
1006                             }
1007                         }
1008                     }
1009                 }
1010
1011                 if let ty::ReScope(scope) = *super_scope {
1012                     let node_id = scope.node_id(self.tcx, &self.region_scope_tree);
1013                     match self.tcx.hir.find(node_id) {
1014                         Some(hir_map::NodeStmt(_)) => {
1015                             if *sub_scope != ty::ReStatic {
1016                                 db.note("consider using a `let` binding to increase its lifetime");
1017                             }
1018
1019                         }
1020                         _ => {}
1021                     }
1022                 }
1023
1024                 db.emit();
1025             }
1026             err_borrowed_pointer_too_short(loan_scope, ptr_scope) => {
1027                 let descr = self.cmt_to_path_or_string(err.cmt);
1028                 let mut db = self.lifetime_too_short_for_reborrow(error_span, &descr, Origin::Ast);
1029                 let descr = match opt_loan_path(&err.cmt) {
1030                     Some(lp) => {
1031                         format!("`{}`", self.loan_path_to_string(&lp))
1032                     }
1033                     None => self.cmt_to_string(&err.cmt),
1034                 };
1035                 self.tcx.note_and_explain_region(
1036                     &self.region_scope_tree,
1037                     &mut db,
1038                     &format!("{} would have to be valid for ",
1039                             descr),
1040                     loan_scope,
1041                     "...");
1042                 self.tcx.note_and_explain_region(
1043                     &self.region_scope_tree,
1044                     &mut db,
1045                     &format!("...but {} is only valid for ", descr),
1046                     ptr_scope,
1047                     "");
1048
1049                 db.emit();
1050             }
1051         }
1052     }
1053
1054     pub fn report_aliasability_violation(&self,
1055                                          span: Span,
1056                                          kind: AliasableViolationKind,
1057                                          cause: mc::AliasableReason,
1058                                          cmt: &mc::cmt_<'tcx>) {
1059         let mut is_closure = false;
1060         let prefix = match kind {
1061             MutabilityViolation => {
1062                 "cannot assign to data"
1063             }
1064             BorrowViolation(euv::ClosureCapture(_)) |
1065             BorrowViolation(euv::OverloadedOperator) |
1066             BorrowViolation(euv::AddrOf) |
1067             BorrowViolation(euv::AutoRef) |
1068             BorrowViolation(euv::AutoUnsafe) |
1069             BorrowViolation(euv::RefBinding) |
1070             BorrowViolation(euv::MatchDiscriminant) => {
1071                 "cannot borrow data mutably"
1072             }
1073
1074             BorrowViolation(euv::ClosureInvocation) => {
1075                 is_closure = true;
1076                 "closure invocation"
1077             }
1078
1079             BorrowViolation(euv::ForLoop) => {
1080                 "`for` loop"
1081             }
1082         };
1083
1084         match cause {
1085             mc::AliasableStaticMut => {
1086                 // This path cannot occur. `static mut X` is not checked
1087                 // for aliasability violations.
1088                 span_bug!(span, "aliasability violation for static mut `{}`", prefix)
1089             }
1090             mc::AliasableStatic | mc::AliasableBorrowed => {}
1091         };
1092         let blame = cmt.immutability_blame();
1093         let mut err = match blame {
1094             Some(ImmutabilityBlame::ClosureEnv(id)) => {
1095                 // FIXME: the distinction between these 2 messages looks wrong.
1096                 let help_msg = if let BorrowViolation(euv::ClosureCapture(_)) = kind {
1097                     // The aliasability violation with closure captures can
1098                     // happen for nested closures, so we know the enclosing
1099                     // closure incorrectly accepts an `Fn` while it needs to
1100                     // be `FnMut`.
1101                     "consider changing this to accept closures that implement `FnMut`"
1102
1103                 } else {
1104                     "consider changing this closure to take self by mutable reference"
1105                 };
1106                 let node_id = self.tcx.hir.local_def_id_to_node_id(id);
1107                 let help_span = self.tcx.hir.span(node_id);
1108                 self.cannot_act_on_capture_in_sharable_fn(span,
1109                                                           prefix,
1110                                                           (help_span, help_msg),
1111                                                           Origin::Ast)
1112             }
1113             _ =>  {
1114                 self.cannot_assign_into_immutable_reference(span, prefix,
1115                                                             Origin::Ast)
1116             }
1117         };
1118         self.note_immutability_blame(
1119             &mut err,
1120             blame,
1121             self.tcx.hir.hir_to_node_id(cmt.hir_id)
1122         );
1123
1124         if is_closure {
1125             err.help("closures behind references must be called via `&mut`");
1126         }
1127         err.emit();
1128     }
1129
1130     /// Given a type, if it is an immutable reference, return a suggestion to make it mutable
1131     fn suggest_mut_for_immutable(&self, pty: &hir::Ty, is_implicit_self: bool) -> Option<String> {
1132         // Check whether the argument is an immutable reference
1133         debug!("suggest_mut_for_immutable({:?}, {:?})", pty, is_implicit_self);
1134         if let hir::TyKind::Rptr(lifetime, hir::MutTy {
1135             mutbl: hir::Mutability::MutImmutable,
1136             ref ty
1137         }) = pty.node {
1138             // Account for existing lifetimes when generating the message
1139             let pointee_snippet = match self.tcx.sess.codemap().span_to_snippet(ty.span) {
1140                 Ok(snippet) => snippet,
1141                 _ => return None
1142             };
1143
1144             let lifetime_snippet = if !lifetime.is_elided() {
1145                 format!("{} ", match self.tcx.sess.codemap().span_to_snippet(lifetime.span) {
1146                     Ok(lifetime_snippet) => lifetime_snippet,
1147                     _ => return None
1148                 })
1149             } else {
1150                 String::new()
1151             };
1152             Some(format!("use `&{}mut {}` here to make mutable",
1153                          lifetime_snippet,
1154                          if is_implicit_self { "self" } else { &*pointee_snippet }))
1155         } else {
1156             None
1157         }
1158     }
1159
1160     fn local_binding_mode(&self, node_id: ast::NodeId) -> ty::BindingMode {
1161         let pat = match self.tcx.hir.get(node_id) {
1162             hir_map::Node::NodeBinding(pat) => pat,
1163             node => bug!("bad node for local: {:?}", node)
1164         };
1165
1166         match pat.node {
1167             hir::PatKind::Binding(..) => {
1168                 *self.tables
1169                      .pat_binding_modes()
1170                      .get(pat.hir_id)
1171                      .expect("missing binding mode")
1172             }
1173             _ => bug!("local is not a binding: {:?}", pat)
1174         }
1175     }
1176
1177     fn local_ty(&self, node_id: ast::NodeId) -> (Option<&hir::Ty>, bool) {
1178         let parent = self.tcx.hir.get_parent_node(node_id);
1179         let parent_node = self.tcx.hir.get(parent);
1180
1181         // The parent node is like a fn
1182         if let Some(fn_like) = FnLikeNode::from_node(parent_node) {
1183             // `nid`'s parent's `Body`
1184             let fn_body = self.tcx.hir.body(fn_like.body());
1185             // Get the position of `node_id` in the arguments list
1186             let arg_pos = fn_body.arguments.iter().position(|arg| arg.pat.id == node_id);
1187             if let Some(i) = arg_pos {
1188                 // The argument's `Ty`
1189                 (Some(&fn_like.decl().inputs[i]),
1190                  i == 0 && fn_like.decl().has_implicit_self)
1191             } else {
1192                 (None, false)
1193             }
1194         } else {
1195             (None, false)
1196         }
1197     }
1198
1199     fn note_immutability_blame(&self,
1200                                db: &mut DiagnosticBuilder,
1201                                blame: Option<ImmutabilityBlame>,
1202                                error_node_id: ast::NodeId) {
1203         match blame {
1204             None => {}
1205             Some(ImmutabilityBlame::ClosureEnv(_)) => {}
1206             Some(ImmutabilityBlame::ImmLocal(node_id)) => {
1207                 self.note_immutable_local(db, error_node_id, node_id)
1208             }
1209             Some(ImmutabilityBlame::LocalDeref(node_id)) => {
1210                 match self.local_binding_mode(node_id) {
1211                     ty::BindByReference(..) => {
1212                         let let_span = self.tcx.hir.span(node_id);
1213                         let suggestion = suggest_ref_mut(self.tcx, let_span);
1214                         if let Some((let_span, replace_str)) = suggestion {
1215                             db.span_suggestion(
1216                                 let_span,
1217                                 "use a mutable reference instead",
1218                                 replace_str,
1219                             );
1220                         }
1221                     }
1222                     ty::BindByValue(..) => {
1223                         if let (Some(local_ty), is_implicit_self) = self.local_ty(node_id) {
1224                             if let Some(msg) =
1225                                  self.suggest_mut_for_immutable(local_ty, is_implicit_self) {
1226                                 db.span_label(local_ty.span, msg);
1227                             }
1228                         }
1229                     }
1230                 }
1231             }
1232             Some(ImmutabilityBlame::AdtFieldDeref(_, field)) => {
1233                 let node_id = match self.tcx.hir.as_local_node_id(field.did) {
1234                     Some(node_id) => node_id,
1235                     None => return
1236                 };
1237
1238                 if let hir_map::Node::NodeField(ref field) = self.tcx.hir.get(node_id) {
1239                     if let Some(msg) = self.suggest_mut_for_immutable(&field.ty, false) {
1240                         db.span_label(field.ty.span, msg);
1241                     }
1242                 }
1243             }
1244         }
1245     }
1246
1247      // Suggest a fix when trying to mutably borrow an immutable local
1248      // binding: either to make the binding mutable (if its type is
1249      // not a mutable reference) or to avoid borrowing altogether
1250     fn note_immutable_local(&self,
1251                             db: &mut DiagnosticBuilder,
1252                             borrowed_node_id: ast::NodeId,
1253                             binding_node_id: ast::NodeId) {
1254         let let_span = self.tcx.hir.span(binding_node_id);
1255         if let ty::BindByValue(..) = self.local_binding_mode(binding_node_id) {
1256             if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
1257                 let (ty, is_implicit_self) = self.local_ty(binding_node_id);
1258                 if is_implicit_self && snippet != "self" {
1259                     // avoid suggesting `mut &self`.
1260                     return
1261                 }
1262                 if let Some(&hir::TyKind::Rptr(
1263                     _,
1264                     hir::MutTy {
1265                         mutbl: hir::MutMutable,
1266                         ..
1267                     },
1268                 )) = ty.map(|t| &t.node)
1269                 {
1270                     let borrow_expr_id = self.tcx.hir.get_parent_node(borrowed_node_id);
1271                     db.span_suggestion(
1272                         self.tcx.hir.span(borrow_expr_id),
1273                         "consider removing the `&mut`, as it is an \
1274                         immutable binding to a mutable reference",
1275                         snippet
1276                     );
1277                 } else {
1278                     db.span_label(
1279                         let_span,
1280                         format!("consider changing this to `mut {}`", snippet),
1281                     );
1282                 }
1283             }
1284         }
1285     }
1286
1287     fn report_out_of_scope_escaping_closure_capture(&self,
1288                                                     err: &BckError<'a, 'tcx>,
1289                                                     capture_span: Span)
1290     {
1291         let cmt_path_or_string = self.cmt_to_path_or_string(&err.cmt);
1292
1293         let suggestion =
1294             match self.tcx.sess.codemap().span_to_snippet(err.span) {
1295                 Ok(string) => format!("move {}", string),
1296                 Err(_) => format!("move |<args>| <body>")
1297             };
1298
1299         self.cannot_capture_in_long_lived_closure(err.span,
1300                                                   &cmt_path_or_string,
1301                                                   capture_span,
1302                                                   Origin::Ast)
1303             .span_suggestion(err.span,
1304                              &format!("to force the closure to take ownership of {} \
1305                                        (and any other referenced variables), \
1306                                        use the `move` keyword",
1307                                        cmt_path_or_string),
1308                              suggestion)
1309             .emit();
1310     }
1311
1312     fn region_end_span(&self, region: ty::Region<'tcx>) -> Option<Span> {
1313         match *region {
1314             ty::ReScope(scope) => {
1315                 Some(self.tcx.sess.codemap().end_point(
1316                         scope.span(self.tcx, &self.region_scope_tree)))
1317             }
1318             _ => None
1319         }
1320     }
1321
1322     fn note_and_explain_mutbl_error(&self, db: &mut DiagnosticBuilder, err: &BckError<'a, 'tcx>,
1323                                     error_span: &Span) {
1324         match err.cmt.note {
1325             mc::NoteClosureEnv(upvar_id) | mc::NoteUpvarRef(upvar_id) => {
1326                 // If this is an `Fn` closure, it simply can't mutate upvars.
1327                 // If it's an `FnMut` closure, the original variable was declared immutable.
1328                 // We need to determine which is the case here.
1329                 let kind = match err.cmt.upvar_cat().unwrap() {
1330                     Categorization::Upvar(mc::Upvar { kind, .. }) => kind,
1331                     _ => bug!()
1332                 };
1333                 if *kind == ty::ClosureKind::Fn {
1334                     let closure_node_id =
1335                         self.tcx.hir.local_def_id_to_node_id(upvar_id.closure_expr_id);
1336                     db.span_help(self.tcx.hir.span(closure_node_id),
1337                                  "consider changing this closure to take \
1338                                   self by mutable reference");
1339                 }
1340             }
1341             _ => {
1342                 if let Categorization::Deref(..) = err.cmt.cat {
1343                     db.span_label(*error_span, "cannot borrow as mutable");
1344                 } else if let Categorization::Local(local_id) = err.cmt.cat {
1345                     let span = self.tcx.hir.span(local_id);
1346                     if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
1347                         if snippet.starts_with("ref mut ") || snippet.starts_with("&mut ") {
1348                             db.span_label(*error_span, "cannot reborrow mutably");
1349                             db.span_label(*error_span, "try removing `&mut` here");
1350                         } else {
1351                             db.span_label(*error_span, "cannot borrow mutably");
1352                         }
1353                     } else {
1354                         db.span_label(*error_span, "cannot borrow mutably");
1355                     }
1356                 } else if let Categorization::Interior(ref cmt, _) = err.cmt.cat {
1357                     if let mc::MutabilityCategory::McImmutable = cmt.mutbl {
1358                         db.span_label(*error_span,
1359                                       "cannot mutably borrow field of immutable binding");
1360                     }
1361                 }
1362             }
1363         }
1364     }
1365     pub fn append_loan_path_to_string(&self,
1366                                       loan_path: &LoanPath<'tcx>,
1367                                       out: &mut String) {
1368         match loan_path.kind {
1369             LpUpvar(ty::UpvarId { var_id: id, closure_expr_id: _ }) => {
1370                 out.push_str(&self.tcx.hir.name(self.tcx.hir.hir_to_node_id(id)).as_str());
1371             }
1372             LpVar(id) => {
1373                 out.push_str(&self.tcx.hir.name(id).as_str());
1374             }
1375
1376             LpDowncast(ref lp_base, variant_def_id) => {
1377                 out.push('(');
1378                 self.append_loan_path_to_string(&lp_base, out);
1379                 out.push_str(DOWNCAST_PRINTED_OPERATOR);
1380                 out.push_str(&self.tcx.item_path_str(variant_def_id));
1381                 out.push(')');
1382             }
1383
1384             LpExtend(ref lp_base, _, LpInterior(_, InteriorField(mc::FieldIndex(_, info)))) => {
1385                 self.append_autoderefd_loan_path_to_string(&lp_base, out);
1386                 out.push('.');
1387                 out.push_str(&info.as_str());
1388             }
1389
1390             LpExtend(ref lp_base, _, LpInterior(_, InteriorElement)) => {
1391                 self.append_autoderefd_loan_path_to_string(&lp_base, out);
1392                 out.push_str("[..]");
1393             }
1394
1395             LpExtend(ref lp_base, _, LpDeref(_)) => {
1396                 out.push('*');
1397                 self.append_loan_path_to_string(&lp_base, out);
1398             }
1399         }
1400     }
1401
1402     pub fn append_autoderefd_loan_path_to_string(&self,
1403                                                  loan_path: &LoanPath<'tcx>,
1404                                                  out: &mut String) {
1405         match loan_path.kind {
1406             LpExtend(ref lp_base, _, LpDeref(_)) => {
1407                 // For a path like `(*x).f` or `(*x)[3]`, autoderef
1408                 // rules would normally allow users to omit the `*x`.
1409                 // So just serialize such paths to `x.f` or x[3]` respectively.
1410                 self.append_autoderefd_loan_path_to_string(&lp_base, out)
1411             }
1412
1413             LpDowncast(ref lp_base, variant_def_id) => {
1414                 out.push('(');
1415                 self.append_autoderefd_loan_path_to_string(&lp_base, out);
1416                 out.push_str(DOWNCAST_PRINTED_OPERATOR);
1417                 out.push_str(&self.tcx.item_path_str(variant_def_id));
1418                 out.push(')');
1419             }
1420
1421             LpVar(..) | LpUpvar(..) | LpExtend(.., LpInterior(..)) => {
1422                 self.append_loan_path_to_string(loan_path, out)
1423             }
1424         }
1425     }
1426
1427     pub fn loan_path_to_string(&self, loan_path: &LoanPath<'tcx>) -> String {
1428         let mut result = String::new();
1429         self.append_loan_path_to_string(loan_path, &mut result);
1430         result
1431     }
1432
1433     pub fn cmt_to_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
1434         cmt.descriptive_string(self.tcx)
1435     }
1436
1437     pub fn cmt_to_path_or_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
1438         match opt_loan_path(cmt) {
1439             Some(lp) => format!("`{}`", self.loan_path_to_string(&lp)),
1440             None => self.cmt_to_string(cmt),
1441         }
1442     }
1443 }
1444
1445 impl BitwiseOperator for LoanDataFlowOperator {
1446     #[inline]
1447     fn join(&self, succ: usize, pred: usize) -> usize {
1448         succ | pred // loans from both preds are in scope
1449     }
1450 }
1451
1452 impl DataFlowOperator for LoanDataFlowOperator {
1453     #[inline]
1454     fn initial_value(&self) -> bool {
1455         false // no loans in scope by default
1456     }
1457 }
1458
1459 impl<'tcx> fmt::Debug for InteriorKind {
1460     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1461         match *self {
1462             InteriorField(mc::FieldIndex(_, info)) => write!(f, "{}", info),
1463             InteriorElement => write!(f, "[]"),
1464         }
1465     }
1466 }
1467
1468 impl<'tcx> fmt::Debug for Loan<'tcx> {
1469     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1470         write!(f, "Loan_{}({:?}, {:?}, {:?}-{:?}, {:?})",
1471                self.index,
1472                self.loan_path,
1473                self.kind,
1474                self.gen_scope,
1475                self.kill_scope,
1476                self.restricted_paths)
1477     }
1478 }
1479
1480 impl<'tcx> fmt::Debug for LoanPath<'tcx> {
1481     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1482         match self.kind {
1483             LpVar(id) => {
1484                 write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_string(id)))
1485             }
1486
1487             LpUpvar(ty::UpvarId{ var_id, closure_expr_id }) => {
1488                 let s = ty::tls::with(|tcx| {
1489                     let var_node_id = tcx.hir.hir_to_node_id(var_id);
1490                     tcx.hir.node_to_string(var_node_id)
1491                 });
1492                 write!(f, "$({} captured by id={:?})", s, closure_expr_id)
1493             }
1494
1495             LpDowncast(ref lp, variant_def_id) => {
1496                 let variant_str = if variant_def_id.is_local() {
1497                     ty::tls::with(|tcx| tcx.item_path_str(variant_def_id))
1498                 } else {
1499                     format!("{:?}", variant_def_id)
1500                 };
1501                 write!(f, "({:?}{}{})", lp, DOWNCAST_PRINTED_OPERATOR, variant_str)
1502             }
1503
1504             LpExtend(ref lp, _, LpDeref(_)) => {
1505                 write!(f, "{:?}.*", lp)
1506             }
1507
1508             LpExtend(ref lp, _, LpInterior(_, ref interior)) => {
1509                 write!(f, "{:?}.{:?}", lp, interior)
1510             }
1511         }
1512     }
1513 }
1514
1515 impl<'tcx> fmt::Display for LoanPath<'tcx> {
1516     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1517         match self.kind {
1518             LpVar(id) => {
1519                 write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_user_string(id)))
1520             }
1521
1522             LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
1523                 let s = ty::tls::with(|tcx| {
1524                     let var_node_id = tcx.hir.hir_to_node_id(var_id);
1525                     tcx.hir.node_to_string(var_node_id)
1526                 });
1527                 write!(f, "$({} captured by closure)", s)
1528             }
1529
1530             LpDowncast(ref lp, variant_def_id) => {
1531                 let variant_str = if variant_def_id.is_local() {
1532                     ty::tls::with(|tcx| tcx.item_path_str(variant_def_id))
1533                 } else {
1534                     format!("{:?}", variant_def_id)
1535                 };
1536                 write!(f, "({}{}{})", lp, DOWNCAST_PRINTED_OPERATOR, variant_str)
1537             }
1538
1539             LpExtend(ref lp, _, LpDeref(_)) => {
1540                 write!(f, "{}.*", lp)
1541             }
1542
1543             LpExtend(ref lp, _, LpInterior(_, ref interior)) => {
1544                 write!(f, "{}.{:?}", lp, interior)
1545             }
1546         }
1547     }
1548 }