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.
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.
11 //! See The Book chapter on the borrow checker for more details.
13 #![allow(non_camel_case_types)]
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::*;
21 use self::InteriorKind::*;
23 use rustc::hir::HirId;
24 use rustc::hir::map as hir_map;
25 use rustc::hir::map::blocks::FnLikeNode;
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;
45 use std::cell::RefCell;
48 use rustc_data_structures::sync::Lrc;
49 use std::hash::{Hash, Hasher};
51 use syntax_pos::{MultiSpan, Span};
52 use errors::{DiagnosticBuilder, DiagnosticId};
55 use rustc::hir::intravisit::{self, Visitor};
65 #[derive(Clone, Copy)]
66 pub struct LoanDataFlowOperator;
68 pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator>;
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);
76 pub fn provide(providers: &mut Providers) {
77 *providers = Providers {
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>,
90 fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId)
91 -> Lrc<BorrowCheckResult>
93 assert!(tcx.use_ast_borrowck());
95 debug!("borrowck(body_owner_def_id={:?})", owner_def_id);
97 let owner_id = tcx.hir.as_local_node_id(owner_def_id).unwrap();
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(),
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 {
123 used_mut_nodes: RefCell::new(FxHashSet()),
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.
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);
136 // option dance because you can't capture an uninitialized variable
139 if let Some(AnalysisData { all_loans,
141 move_data: flowed_moves }) =
142 build_borrowck_dataflow_data(&mut bccx, false, body_id,
144 cfg = Some(cfg::CFG::new(bccx.tcx, &body));
145 cfg.as_mut().unwrap()
148 check_loans::check_loans(&mut bccx, &loan_dfcx, &flowed_moves, &all_loans, body);
151 if !tcx.use_mir_borrowck() {
152 unused::check(&mut bccx, body);
155 Lrc::new(BorrowCheckResult {
156 used_mut_nodes: bccx.used_mut_nodes.into_inner(),
160 fn build_borrowck_dataflow_data<'a, 'c, 'tcx, F>(this: &mut BorrowckCtxt<'a, 'tcx>,
161 force_analysis: bool,
162 body_id: hir::BodyId,
164 -> Option<AnalysisData<'a, 'tcx>>
165 where F: FnOnce(&mut BorrowckCtxt<'a, 'tcx>) -> &'c cfg::CFG
167 // Check the body of fn items.
170 let mut visitor = intravisit::IdRangeComputingVisitor::new(&tcx.hir);
171 visitor.visit_body(this.body);
174 let (all_loans, move_data) =
175 gather_loans::gather_loans_in_fn(this, body_id);
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);
185 debug!("propagating loans in {:?}", body_id);
188 let cfg = get_cfg(this);
190 DataFlowContext::new(this.tcx,
194 LoanDataFlowOperator,
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(),
203 loan_dfcx.add_kills_from_flow_exits(cfg);
204 loan_dfcx.propagate(cfg, this.body);
206 let flowed_moves = move_data::FlowedMoveData::new(move_data,
212 Some(AnalysisData { all_loans,
214 move_data:flowed_moves })
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,
223 -> (BorrowckCtxt<'a, 'tcx>, AnalysisData<'a, 'tcx>)
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 {
236 used_mut_nodes: RefCell::new(FxHashSet()),
239 let dataflow_data = build_borrowck_dataflow_data(&mut bccx, true, body_id, |_| cfg);
240 (bccx, dataflow_data.unwrap())
243 // ----------------------------------------------------------------------
246 pub struct BorrowckCtxt<'a, 'tcx: 'a> {
247 tcx: TyCtxt<'a, 'tcx, 'tcx>,
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>,
253 region_scope_tree: Lrc<region::ScopeTree>,
257 body: &'tcx hir::Body,
259 used_mut_nodes: RefCell<FxHashSet<HirId>>,
262 impl<'a, 'b, 'tcx: 'b> BorrowckErrors<'a> for &'a BorrowckCtxt<'b, 'tcx> {
263 fn struct_span_err_with_code<S: Into<MultiSpan>>(self,
267 -> DiagnosticBuilder<'a>
269 self.tcx.sess.struct_span_err_with_code(sp, msg, code)
272 fn struct_span_err<S: Into<MultiSpan>>(self,
275 -> DiagnosticBuilder<'a>
277 self.tcx.sess.struct_span_err(sp, msg)
280 fn cancel_if_wrong_origin(self,
281 mut diag: DiagnosticBuilder<'a>,
283 -> DiagnosticBuilder<'a>
285 if !o.should_emit_errors(self.tcx.borrowck_mode()) {
286 self.tcx.sess.diagnostic().cancel(&mut diag);
292 ///////////////////////////////////////////////////////////////////////////
293 // Loans and loan paths
295 /// Record of a loan that was issued.
296 pub struct Loan<'tcx> {
298 loan_path: Rc<LoanPath<'tcx>>,
299 kind: ty::BorrowKind,
300 restricted_paths: Vec<Rc<LoanPath<'tcx>>>,
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,
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,
315 cause: euv::LoanCause,
318 impl<'tcx> Loan<'tcx> {
319 pub fn loan_path(&self) -> Rc<LoanPath<'tcx>> {
320 self.loan_path.clone()
325 pub struct LoanPath<'tcx> {
326 kind: LoanPathKind<'tcx>,
330 impl<'tcx> PartialEq for LoanPath<'tcx> {
331 fn eq(&self, that: &LoanPath<'tcx>) -> bool {
332 self.kind == that.kind
336 impl<'tcx> Hash for LoanPath<'tcx> {
337 fn hash<H: Hasher>(&self, state: &mut H) {
338 self.kind.hash(state);
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>)
350 impl<'tcx> LoanPath<'tcx> {
351 fn new(kind: LoanPathKind<'tcx>, ty: Ty<'tcx>) -> LoanPath<'tcx> {
352 LoanPath { kind: kind, ty: ty }
355 fn to_type(&self) -> Ty<'tcx> { self.ty }
357 fn has_downcast(&self) -> bool {
359 LpDowncast(_, _) => true,
360 LpExtend(ref lp, _, LpInterior(_, _)) => {
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 ";
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),
383 trait ToInteriorKind { fn cleaned(self) -> InteriorKind; }
384 impl ToInteriorKind for mc::InteriorKind {
385 fn cleaned(self) -> InteriorKind {
387 mc::InteriorField(name) => InteriorField(name),
388 mc::InteriorElement(_) => InteriorElement,
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),
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, _, _) => {
416 bug!("encountered non-closure id: {}", closure_id)
419 _ => bug!("encountered non-expr id: {}", closure_id)
423 impl<'a, 'tcx> LoanPath<'tcx> {
424 pub fn kill_scope(&self, bccx: &BorrowckCtxt<'a, 'tcx>) -> region::Scope {
427 let hir_id = bccx.tcx.hir.node_to_hir_id(local_id);
428 bccx.region_scope_tree.var_scope(hir_id.local_id)
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)
435 LpDowncast(ref base, _) |
436 LpExtend(ref base, ..) => base.kill_scope(bccx),
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)
449 (&LpExtend(ref base, _, LpDeref(_)), _) => base.has_fork(other),
450 (_, &LpExtend(ref base, _, LpDeref(_))) => self.has_fork(&base),
455 fn depth(&self) -> usize {
457 LpExtend(ref base, _, LpDeref(_)) => base.depth(),
458 LpExtend(ref base, _, LpInterior(..)) => base.depth() + 1,
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| {
470 if base.depth() == xd && base2.depth() == xd {
472 kind: LpExtend(Rc::new(x), a, LpInterior(opt_variant_id, id)),
483 (&LpExtend(ref base, _, LpDeref(_)), _) => base.common(other),
484 (_, &LpExtend(ref other, _, LpDeref(_))) => self.common(&other),
485 (&LpVar(id), &LpVar(id2)) => {
487 Some(LoanPath { kind: LpVar(id), ty: self.ty })
492 (&LpUpvar(id), &LpUpvar(id2)) => {
494 Some(LoanPath { kind: LpUpvar(id), ty: self.ty })
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));
510 Categorization::Rvalue(..) |
511 Categorization::StaticItem => {
515 Categorization::Local(id) => {
516 (Some(new_lp(LpVar(id))), false)
519 Categorization::Upvar(mc::Upvar { id, .. }) => {
520 (Some(new_lp(LpUpvar(id))), false)
523 Categorization::Deref(ref cmt_base, pk) => {
524 let lp = opt_loan_path_is_field(cmt_base);
526 new_lp(LpExtend(lp, cmt.mutbl, LpDeref(pk)))
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),
536 new_lp(LpExtend(lp, cmt.mutbl, LpInterior(opt_variant_id, ik.cleaned())))
540 Categorization::Downcast(ref cmt_base, variant_def_id) => {
541 let lp = opt_loan_path_is_field(cmt_base);
543 new_lp(LpDowncast(lp, variant_def_id))
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
558 ///////////////////////////////////////////////////////////////////////////
561 // Errors that can occur
562 #[derive(Debug, PartialEq)]
563 pub enum bckerr_code<'tcx> {
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
570 // Combination of an error code and the categorization of the expression
572 #[derive(Debug, PartialEq)]
573 pub struct BckError<'c, 'tcx: 'c> {
575 cause: AliasableViolationKind,
576 cmt: &'c mc::cmt_<'tcx>,
577 code: bckerr_code<'tcx>
580 #[derive(Copy, Clone, Debug, PartialEq)]
581 pub enum AliasableViolationKind {
583 BorrowViolation(euv::LoanCause)
586 #[derive(Copy, Clone, Debug)]
587 pub enum MovedValueUseKind {
592 ///////////////////////////////////////////////////////////////////////////
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>)
601 let region_rels = RegionRelations::new(self.tcx,
603 &self.region_scope_tree,
604 &self.tables.free_region_map);
605 region_rels.is_subregion_of(r_sub, r_sup)
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);
622 self.report_bckerr(&err);
625 pub fn report_use_of_moved_value(&self,
627 use_kind: MovedValueUseKind,
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"),
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
641 self.cannot_act_on_uninitialized_variable(use_span,
643 &self.loan_path_to_string(lp),
645 .span_label(use_span, format!("use of possibly uninitialized `{}`",
646 self.loan_path_to_string(lp)))
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:
654 // error: use of moved value: `x.b`
655 // note: `x.a` moved here...
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:
660 // error: use of moved value: `x`
661 // note: `x` moved here (through moving `x.a`)...
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());
670 let moved_lp_msg = format!(" (through moving `{}`)",
671 self.loan_path_to_string(moved_lp));
672 (nl, ol, moved_lp_msg)
674 (self.loan_path_to_string(lp),
675 self.loan_path_to_string(moved_lp),
679 let partial = moved_lp.depth() > lp.depth();
680 let msg = if !has_fork && partial { "partially " }
681 else if has_fork && !has_common { "collaterally "}
683 let mut err = self.cannot_act_on_moved_value(use_span,
686 Some(format!("{}", nl)),
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",
705 (ol, moved_lp_msg, err, need_note)
709 // Get type of value and span where it was previously
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
715 let (move_span, move_note) = match the_move.kind {
716 move_data::Declared => {
720 move_data::MoveExpr |
721 move_data::MovePat => (self.tcx.hir.span(node_id), ""),
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: {:?}",
728 }, " (into closure)"),
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 {
737 format!("value moved{} here in previous iteration of loop",
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));
748 "move occurs because {} has type `{}`, which does not implement the `Copy` trait",
749 if moved_lp.has_downcast() {
750 "the value".to_string()
752 format!("`{}`", self.loan_path_to_string(moved_lp))
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.
765 pub fn report_partial_reinitialization_of_uninitialized_structure(
768 lp: &LoanPath<'tcx>) {
769 self.cannot_partially_reinit_an_uninit_struct(span,
770 &self.loan_path_to_string(lp),
775 pub fn report_reassigned_immutable_variable(&self,
779 &move_data::Assignment) {
780 let mut err = self.cannot_reassign_immutable(span,
781 &self.loan_path_to_string(lp),
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)));
792 pub fn struct_span_err_with_code<S: Into<MultiSpan>>(&self,
796 -> DiagnosticBuilder<'a> {
797 self.tcx.sess.struct_span_err_with_code(s, msg, code)
800 pub fn span_err_with_code<S: Into<MultiSpan>>(
806 self.tcx.sess.span_err_with_code(s, msg, code);
809 fn report_bckerr(&self, err: &BckError<'a, 'tcx>) {
810 let error_span = err.span.clone();
814 let descr = match err.cmt.note {
815 mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
816 self.cmt_to_string(&err.cmt)
818 _ => match opt_loan_path_is_field(&err.cmt) {
820 format!("{} of {} binding",
821 self.cmt_to_string(&err.cmt),
822 err.cmt.mutbl.to_user_str())
827 err.cmt.mutbl.to_user_str(),
828 self.cmt_to_string(&err.cmt))
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())
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))
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, ..
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)
863 (Ok(ref snippet), ref cat) => {
864 let msg = &format!("consider making `{}` mutable", snippet);
865 let suggestion = format!("mut {}", snippet);
867 if let &Categorization::Deref(ref cmt, _) = cat {
868 if let Categorization::Upvar(mc::Upvar {
869 kind: ty::ClosureKind::Fn, ..
871 db.note(fn_closure_msg);
873 db.span_suggestion(sp, msg, suggestion);
876 db.span_suggestion(sp, msg, suggestion);
880 db.span_help(sp, "consider making this binding mutable");
886 BorrowViolation(euv::ClosureCapture(_)) => {
887 self.closure_cannot_assign_to_borrowed(error_span, &descr, Origin::Ast)
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)
898 BorrowViolation(euv::ClosureInvocation) => {
900 "err_mutbl with a closure invocation");
904 self.note_and_explain_mutbl_error(&mut db, &err, &error_span);
905 self.note_immutability_blame(
907 err.cmt.immutability_blame(),
908 self.tcx.hir.hir_to_node_id(err.cmt.hir_id)
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(),
916 format!("`{}`", self.loan_path_to_string(&lp))
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",
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() {
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",
944 db.span_label(error_span, format!("{} does not live long enough",
950 let sub_span = self.region_end_span(sub_scope);
951 let super_span = self.region_end_span(super_scope);
953 match (sub_span, super_span) {
954 (Some(s1), Some(s2)) if s1 == s2 => {
956 let msg = match opt_loan_path(&err.cmt) {
957 None => value_kind.to_string(),
959 format!("`{}`", self.loan_path_to_string(&lp))
963 format!("{} dropped here while still borrowed", msg));
965 db.span_label(s1, format!("{} dropped before borrower", value_kind));
967 db.note("values in a scope are dropped in the opposite order \
970 (Some(s1), Some(s2)) if !is_closure => {
971 let msg = match opt_loan_path(&err.cmt) {
972 None => value_kind.to_string(),
974 format!("`{}`", self.loan_path_to_string(&lp))
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));
983 db.span_label(s, format!("{} needs to live until here",
987 self.tcx.note_and_explain_region(
988 &self.region_scope_tree,
990 "borrowed value must be valid for ",
997 db.span_label(s, format!("{} only lives until here", value_kind));
1000 self.tcx.note_and_explain_region(
1001 &self.region_scope_tree,
1003 "...but borrowed value is only valid for ",
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");
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) {
1031 format!("`{}`", self.loan_path_to_string(&lp))
1033 None => self.cmt_to_string(&err.cmt),
1035 self.tcx.note_and_explain_region(
1036 &self.region_scope_tree,
1038 &format!("{} would have to be valid for ",
1042 self.tcx.note_and_explain_region(
1043 &self.region_scope_tree,
1045 &format!("...but {} is only valid for ", descr),
1054 pub fn report_aliasability_violation(&self,
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"
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"
1074 BorrowViolation(euv::ClosureInvocation) => {
1076 "closure invocation"
1079 BorrowViolation(euv::ForLoop) => {
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)
1090 mc::AliasableStatic | mc::AliasableBorrowed => {}
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
1101 "consider changing this to accept closures that implement `FnMut`"
1104 "consider changing this closure to take self by mutable reference"
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,
1110 (help_span, help_msg),
1114 self.cannot_assign_into_immutable_reference(span, prefix,
1118 self.note_immutability_blame(
1121 self.tcx.hir.hir_to_node_id(cmt.hir_id)
1125 err.help("closures behind references must be called via `&mut`");
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,
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,
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,
1152 Some(format!("use `&{}mut {}` here to make mutable",
1154 if is_implicit_self { "self" } else { &*pointee_snippet }))
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)
1167 hir::PatKind::Binding(..) => {
1169 .pat_binding_modes()
1171 .expect("missing binding mode")
1173 _ => bug!("local is not a binding: {:?}", pat)
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);
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)
1199 fn note_immutability_blame(&self,
1200 db: &mut DiagnosticBuilder,
1201 blame: Option<ImmutabilityBlame>,
1202 error_node_id: ast::NodeId) {
1205 Some(ImmutabilityBlame::ClosureEnv(_)) => {}
1206 Some(ImmutabilityBlame::ImmLocal(node_id)) => {
1207 self.note_immutable_local(db, error_node_id, node_id)
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 {
1217 "use a mutable reference instead",
1222 ty::BindByValue(..) => {
1223 if let (Some(local_ty), is_implicit_self) = self.local_ty(node_id) {
1225 self.suggest_mut_for_immutable(local_ty, is_implicit_self) {
1226 db.span_label(local_ty.span, msg);
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,
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);
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`.
1262 if let Some(&hir::TyKind::Rptr(
1265 mutbl: hir::MutMutable,
1268 )) = ty.map(|t| &t.node)
1270 let borrow_expr_id = self.tcx.hir.get_parent_node(borrowed_node_id);
1272 self.tcx.hir.span(borrow_expr_id),
1273 "consider removing the `&mut`, as it is an \
1274 immutable binding to a mutable reference",
1280 format!("consider changing this to `mut {}`", snippet),
1287 fn report_out_of_scope_escaping_closure_capture(&self,
1288 err: &BckError<'a, 'tcx>,
1291 let cmt_path_or_string = self.cmt_to_path_or_string(&err.cmt);
1294 match self.tcx.sess.codemap().span_to_snippet(err.span) {
1295 Ok(string) => format!("move {}", string),
1296 Err(_) => format!("move |<args>| <body>")
1299 self.cannot_capture_in_long_lived_closure(err.span,
1300 &cmt_path_or_string,
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),
1312 fn region_end_span(&self, region: ty::Region<'tcx>) -> Option<Span> {
1314 ty::ReScope(scope) => {
1315 Some(self.tcx.sess.codemap().end_point(
1316 scope.span(self.tcx, &self.region_scope_tree)))
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,
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");
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");
1351 db.span_label(*error_span, "cannot borrow mutably");
1354 db.span_label(*error_span, "cannot borrow mutably");
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");
1365 pub fn append_loan_path_to_string(&self,
1366 loan_path: &LoanPath<'tcx>,
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());
1373 out.push_str(&self.tcx.hir.name(id).as_str());
1376 LpDowncast(ref lp_base, variant_def_id) => {
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));
1384 LpExtend(ref lp_base, _, LpInterior(_, InteriorField(mc::FieldIndex(_, info)))) => {
1385 self.append_autoderefd_loan_path_to_string(&lp_base, out);
1387 out.push_str(&info.as_str());
1390 LpExtend(ref lp_base, _, LpInterior(_, InteriorElement)) => {
1391 self.append_autoderefd_loan_path_to_string(&lp_base, out);
1392 out.push_str("[..]");
1395 LpExtend(ref lp_base, _, LpDeref(_)) => {
1397 self.append_loan_path_to_string(&lp_base, out);
1402 pub fn append_autoderefd_loan_path_to_string(&self,
1403 loan_path: &LoanPath<'tcx>,
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)
1413 LpDowncast(ref lp_base, variant_def_id) => {
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));
1421 LpVar(..) | LpUpvar(..) | LpExtend(.., LpInterior(..)) => {
1422 self.append_loan_path_to_string(loan_path, out)
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);
1433 pub fn cmt_to_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
1434 cmt.descriptive_string(self.tcx)
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),
1445 impl BitwiseOperator for LoanDataFlowOperator {
1447 fn join(&self, succ: usize, pred: usize) -> usize {
1448 succ | pred // loans from both preds are in scope
1452 impl DataFlowOperator for LoanDataFlowOperator {
1454 fn initial_value(&self) -> bool {
1455 false // no loans in scope by default
1459 impl<'tcx> fmt::Debug for InteriorKind {
1460 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1462 InteriorField(mc::FieldIndex(_, info)) => write!(f, "{}", info),
1463 InteriorElement => write!(f, "[]"),
1468 impl<'tcx> fmt::Debug for Loan<'tcx> {
1469 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1470 write!(f, "Loan_{}({:?}, {:?}, {:?}-{:?}, {:?})",
1476 self.restricted_paths)
1480 impl<'tcx> fmt::Debug for LoanPath<'tcx> {
1481 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1484 write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_string(id)))
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)
1492 write!(f, "$({} captured by id={:?})", s, closure_expr_id)
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))
1499 format!("{:?}", variant_def_id)
1501 write!(f, "({:?}{}{})", lp, DOWNCAST_PRINTED_OPERATOR, variant_str)
1504 LpExtend(ref lp, _, LpDeref(_)) => {
1505 write!(f, "{:?}.*", lp)
1508 LpExtend(ref lp, _, LpInterior(_, ref interior)) => {
1509 write!(f, "{:?}.{:?}", lp, interior)
1515 impl<'tcx> fmt::Display for LoanPath<'tcx> {
1516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1519 write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_user_string(id)))
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)
1527 write!(f, "$({} captured by closure)", s)
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))
1534 format!("{:?}", variant_def_id)
1536 write!(f, "({}{}{})", lp, DOWNCAST_PRINTED_OPERATOR, variant_str)
1539 LpExtend(ref lp, _, LpDeref(_)) => {
1540 write!(f, "{}.*", lp)
1543 LpExtend(ref lp, _, LpInterior(_, ref interior)) => {
1544 write!(f, "{}.{:?}", lp, interior)