1 // Copyright 2012-2013 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 // ----------------------------------------------------------------------
14 // Phase 2 of check: we walk down the tree and check that:
15 // 1. assignments are always made to mutable locations;
16 // 2. loans made in overlapping scopes do not conflict
17 // 3. assignments do not affect things loaned out as immutable
18 // 4. moves do not affect things loaned out in any way
19 use self::UseError
::*;
22 use borrowck
::InteriorKind
::{InteriorElement, InteriorField}
;
23 use rustc
::middle
::expr_use_visitor
as euv
;
24 use rustc
::middle
::expr_use_visitor
::MutateMode
;
26 use rustc
::middle
::mem_categorization
as mc
;
27 use rustc
::middle
::mem_categorization
::Categorization
;
28 use rustc
::middle
::region
;
29 use rustc
::ty
::{self, TyCtxt}
;
30 use rustc
::traits
::ProjectionMode
;
32 use syntax
::codemap
::Span
;
37 // FIXME (#16118): These functions are intended to allow the borrow checker to
38 // be less precise in its handling of Box while still allowing moves out of a
39 // Box. They should be removed when Unique is removed from LoanPath.
41 fn owned_ptr_base_path
<'a
, 'tcx
>(loan_path
: &'a LoanPath
<'tcx
>) -> &'a LoanPath
<'tcx
> {
42 //! Returns the base of the leftmost dereference of an Unique in
43 //! `loan_path`. If there is no dereference of an Unique in `loan_path`,
44 //! then it just returns `loan_path` itself.
46 return match helper(loan_path
) {
47 Some(new_loan_path
) => new_loan_path
,
48 None
=> loan_path
.clone()
51 fn helper
<'a
, 'tcx
>(loan_path
: &'a LoanPath
<'tcx
>) -> Option
<&'a LoanPath
<'tcx
>> {
52 match loan_path
.kind
{
53 LpVar(_
) | LpUpvar(_
) => None
,
54 LpExtend(ref lp_base
, _
, LpDeref(mc
::Unique
)) => {
55 match helper(&lp_base
) {
57 None
=> Some(&lp_base
)
60 LpDowncast(ref lp_base
, _
) |
61 LpExtend(ref lp_base
, _
, _
) => helper(&lp_base
)
66 fn owned_ptr_base_path_rc
<'tcx
>(loan_path
: &Rc
<LoanPath
<'tcx
>>) -> Rc
<LoanPath
<'tcx
>> {
67 //! The equivalent of `owned_ptr_base_path` for an &Rc<LoanPath> rather than
70 return match helper(loan_path
) {
71 Some(new_loan_path
) => new_loan_path
,
72 None
=> loan_path
.clone()
75 fn helper
<'tcx
>(loan_path
: &Rc
<LoanPath
<'tcx
>>) -> Option
<Rc
<LoanPath
<'tcx
>>> {
76 match loan_path
.kind
{
77 LpVar(_
) | LpUpvar(_
) => None
,
78 LpExtend(ref lp_base
, _
, LpDeref(mc
::Unique
)) => {
79 match helper(lp_base
) {
81 None
=> Some(lp_base
.clone())
84 LpDowncast(ref lp_base
, _
) |
85 LpExtend(ref lp_base
, _
, _
) => helper(lp_base
)
90 struct CheckLoanCtxt
<'a
, 'tcx
: 'a
> {
91 bccx
: &'a BorrowckCtxt
<'a
, 'tcx
>,
92 dfcx_loans
: &'a LoanDataFlow
<'a
, 'tcx
>,
93 move_data
: &'a move_data
::FlowedMoveData
<'a
, 'tcx
>,
94 all_loans
: &'a
[Loan
<'tcx
>],
95 param_env
: &'a ty
::ParameterEnvironment
<'a
, 'tcx
>,
98 impl<'a
, 'tcx
> euv
::Delegate
<'tcx
> for CheckLoanCtxt
<'a
, 'tcx
> {
100 consume_id
: ast
::NodeId
,
103 mode
: euv
::ConsumeMode
) {
104 debug
!("consume(consume_id={}, cmt={:?}, mode={:?})",
105 consume_id
, cmt
, mode
);
107 self.consume_common(consume_id
, consume_span
, cmt
, mode
);
110 fn matched_pat(&mut self,
111 _matched_pat
: &hir
::Pat
,
113 _mode
: euv
::MatchMode
) { }
115 fn consume_pat(&mut self,
116 consume_pat
: &hir
::Pat
,
118 mode
: euv
::ConsumeMode
) {
119 debug
!("consume_pat(consume_pat={:?}, cmt={:?}, mode={:?})",
124 self.consume_common(consume_pat
.id
, consume_pat
.span
, cmt
, mode
);
128 borrow_id
: ast
::NodeId
,
131 loan_region
: ty
::Region
,
133 loan_cause
: euv
::LoanCause
)
135 debug
!("borrow(borrow_id={}, cmt={:?}, loan_region={:?}, \
136 bk={:?}, loan_cause={:?})",
137 borrow_id
, cmt
, loan_region
,
140 match opt_loan_path(&cmt
) {
142 let moved_value_use_kind
= match loan_cause
{
143 euv
::ClosureCapture(_
) => MovedInCapture
,
146 self.check_if_path_is_moved(borrow_id
, borrow_span
, moved_value_use_kind
, &lp
);
151 self.check_for_conflicting_loans(borrow_id
);
155 assignment_id
: ast
::NodeId
,
156 assignment_span
: Span
,
157 assignee_cmt
: mc
::cmt
<'tcx
>,
158 mode
: euv
::MutateMode
)
160 debug
!("mutate(assignment_id={}, assignee_cmt={:?})",
161 assignment_id
, assignee_cmt
);
163 match opt_loan_path(&assignee_cmt
) {
166 MutateMode
::Init
| MutateMode
::JustWrite
=> {
167 // In a case like `path = 1`, then path does not
168 // have to be *FULLY* initialized, but we still
169 // must be careful lest it contains derefs of
171 self.check_if_assigned_path_is_moved(assignee_cmt
.id
,
176 MutateMode
::WriteAndRead
=> {
177 // In a case like `path += 1`, then path must be
178 // fully initialized, since we will read it before
180 self.check_if_path_is_moved(assignee_cmt
.id
,
190 self.check_assignment(assignment_id
, assignment_span
, assignee_cmt
);
193 fn decl_without_init(&mut self, _id
: ast
::NodeId
, _span
: Span
) { }
196 pub fn check_loans
<'a
, 'b
, 'c
, 'tcx
>(bccx
: &BorrowckCtxt
<'a
, 'tcx
>,
197 dfcx_loans
: &LoanDataFlow
<'b
, 'tcx
>,
198 move_data
: &move_data
::FlowedMoveData
<'c
, 'tcx
>,
199 all_loans
: &[Loan
<'tcx
>],
203 debug
!("check_loans(body id={})", body
.id
);
205 let param_env
= ty
::ParameterEnvironment
::for_item(bccx
.tcx
, fn_id
);
206 let infcx
= infer
::new_infer_ctxt(bccx
.tcx
,
209 ProjectionMode
::AnyFinal
);
211 let mut clcx
= CheckLoanCtxt
{
213 dfcx_loans
: dfcx_loans
,
214 move_data
: move_data
,
215 all_loans
: all_loans
,
216 param_env
: &infcx
.parameter_environment
220 let mut euv
= euv
::ExprUseVisitor
::new(&mut clcx
, &infcx
);
221 euv
.walk_fn(decl
, body
);
226 enum UseError
<'tcx
> {
228 UseWhileBorrowed(/*loan*/Rc
<LoanPath
<'tcx
>>, /*loan*/Span
)
231 fn compatible_borrow_kinds(borrow_kind1
: ty
::BorrowKind
,
232 borrow_kind2
: ty
::BorrowKind
)
234 borrow_kind1
== ty
::ImmBorrow
&& borrow_kind2
== ty
::ImmBorrow
237 impl<'a
, 'tcx
> CheckLoanCtxt
<'a
, 'tcx
> {
238 pub fn tcx(&self) -> &'a TyCtxt
<'tcx
> { self.bccx.tcx }
240 pub fn each_issued_loan
<F
>(&self, node
: ast
::NodeId
, mut op
: F
) -> bool
where
241 F
: FnMut(&Loan
<'tcx
>) -> bool
,
243 //! Iterates over each loan that has been issued
244 //! on entrance to `node`, regardless of whether it is
245 //! actually *in scope* at that point. Sometimes loans
246 //! are issued for future scopes and thus they may have been
247 //! *issued* but not yet be in effect.
249 self.dfcx_loans
.each_bit_on_entry(node
, |loan_index
| {
250 let loan
= &self.all_loans
[loan_index
];
255 pub fn each_in_scope_loan
<F
>(&self, scope
: region
::CodeExtent
, mut op
: F
) -> bool
where
256 F
: FnMut(&Loan
<'tcx
>) -> bool
,
258 //! Like `each_issued_loan()`, but only considers loans that are
259 //! currently in scope.
261 let tcx
= self.tcx();
262 self.each_issued_loan(scope
.node_id(&tcx
.region_maps
), |loan
| {
263 if tcx
.region_maps
.is_subscope_of(scope
, loan
.kill_scope
) {
271 fn each_in_scope_loan_affecting_path
<F
>(&self,
272 scope
: region
::CodeExtent
,
273 loan_path
: &LoanPath
<'tcx
>,
276 F
: FnMut(&Loan
<'tcx
>) -> bool
,
278 //! Iterates through all of the in-scope loans affecting `loan_path`,
279 //! calling `op`, and ceasing iteration if `false` is returned.
281 // First, we check for a loan restricting the path P being used. This
282 // accounts for borrows of P but also borrows of subpaths, like P.a.b.
283 // Consider the following example:
285 // let x = &mut a.b.c; // Restricts a, a.b, and a.b.c
286 // let y = a; // Conflicts with restriction
288 let loan_path
= owned_ptr_base_path(loan_path
);
289 let cont
= self.each_in_scope_loan(scope
, |loan
| {
291 for restr_path
in &loan
.restricted_paths
{
292 if **restr_path
== *loan_path
{
306 // Next, we must check for *loans* (not restrictions) on the path P or
307 // any base path. This rejects examples like the following:
312 // Limiting this search to *loans* and not *restrictions* means that
313 // examples like the following continue to work:
318 let mut loan_path
= loan_path
;
320 match loan_path
.kind
{
321 LpVar(_
) | LpUpvar(_
) => {
324 LpDowncast(ref lp_base
, _
) |
325 LpExtend(ref lp_base
, _
, _
) => {
326 loan_path
= &lp_base
;
330 let cont
= self.each_in_scope_loan(scope
, |loan
| {
331 if *loan
.loan_path
== *loan_path
{
346 pub fn loans_generated_by(&self, node
: ast
::NodeId
) -> Vec
<usize> {
347 //! Returns a vector of the loans that are generated as
350 let mut result
= Vec
::new();
351 self.dfcx_loans
.each_gen_bit(node
, |loan_index
| {
352 result
.push(loan_index
);
358 pub fn check_for_conflicting_loans(&self, node
: ast
::NodeId
) {
359 //! Checks to see whether any of the loans that are issued
360 //! on entrance to `node` conflict with loans that have already been
361 //! issued when we enter `node` (for example, we do not
362 //! permit two `&mut` borrows of the same variable).
364 //! (Note that some loans can be *issued* without necessarily
365 //! taking effect yet.)
367 debug
!("check_for_conflicting_loans(node={:?})", node
);
369 let new_loan_indices
= self.loans_generated_by(node
);
370 debug
!("new_loan_indices = {:?}", new_loan_indices
);
372 for &new_loan_index
in &new_loan_indices
{
373 self.each_issued_loan(node
, |issued_loan
| {
374 let new_loan
= &self.all_loans
[new_loan_index
];
375 // Only report an error for the first issued loan that conflicts
376 // to avoid O(n^2) errors.
377 self.report_error_if_loans_conflict(issued_loan
, new_loan
)
381 for (i
, &x
) in new_loan_indices
.iter().enumerate() {
382 let old_loan
= &self.all_loans
[x
];
383 for &y
in &new_loan_indices
[(i
+1) ..] {
384 let new_loan
= &self.all_loans
[y
];
385 self.report_error_if_loans_conflict(old_loan
, new_loan
);
390 pub fn report_error_if_loans_conflict(&self,
391 old_loan
: &Loan
<'tcx
>,
392 new_loan
: &Loan
<'tcx
>)
394 //! Checks whether `old_loan` and `new_loan` can safely be issued
397 debug
!("report_error_if_loans_conflict(old_loan={:?}, new_loan={:?})",
401 // Should only be called for loans that are in scope at the same time.
402 assert
!(self.tcx().region_maps
.scopes_intersect(old_loan
.kill_scope
,
403 new_loan
.kill_scope
));
405 self.report_error_if_loan_conflicts_with_restriction(
406 old_loan
, new_loan
, old_loan
, new_loan
) &&
407 self.report_error_if_loan_conflicts_with_restriction(
408 new_loan
, old_loan
, old_loan
, new_loan
)
411 pub fn report_error_if_loan_conflicts_with_restriction(&self,
414 old_loan
: &Loan
<'tcx
>,
415 new_loan
: &Loan
<'tcx
>)
417 //! Checks whether the restrictions introduced by `loan1` would
418 //! prohibit `loan2`. Returns false if an error is reported.
420 debug
!("report_error_if_loan_conflicts_with_restriction(\
421 loan1={:?}, loan2={:?})",
425 if compatible_borrow_kinds(loan1
.kind
, loan2
.kind
) {
429 let loan2_base_path
= owned_ptr_base_path_rc(&loan2
.loan_path
);
430 for restr_path
in &loan1
.restricted_paths
{
431 if *restr_path
!= loan2_base_path { continue; }
433 // If new_loan is something like `x.a`, and old_loan is something like `x.b`, we would
434 // normally generate a rather confusing message (in this case, for multiple mutable
437 // error: cannot borrow `x.b` as mutable more than once at a time
438 // note: previous borrow of `x.a` occurs here; the mutable borrow prevents
439 // subsequent moves, borrows, or modification of `x.a` until the borrow ends
441 // What we want to do instead is get the 'common ancestor' of the two borrow paths and
442 // use that for most of the message instead, giving is something like this:
444 // error: cannot borrow `x` as mutable more than once at a time
445 // note: previous borrow of `x` occurs here (through borrowing `x.a`); the mutable
446 // borrow prevents subsequent moves, borrows, or modification of `x` until the
449 let common
= new_loan
.loan_path
.common(&old_loan
.loan_path
);
450 let (nl
, ol
, new_loan_msg
, old_loan_msg
) =
451 if new_loan
.loan_path
.has_fork(&old_loan
.loan_path
) && common
.is_some() {
452 let nl
= self.bccx
.loan_path_to_string(&common
.unwrap());
454 let new_loan_msg
= format
!(" (here through borrowing `{}`)",
455 self.bccx
.loan_path_to_string(
456 &new_loan
.loan_path
));
457 let old_loan_msg
= format
!(" (through borrowing `{}`)",
458 self.bccx
.loan_path_to_string(
459 &old_loan
.loan_path
));
460 (nl
, ol
, new_loan_msg
, old_loan_msg
)
462 (self.bccx
.loan_path_to_string(&new_loan
.loan_path
),
463 self.bccx
.loan_path_to_string(&old_loan
.loan_path
),
464 String
::new(), String
::new())
467 let ol_pronoun
= if new_loan
.loan_path
== old_loan
.loan_path
{
473 let mut err
= match (new_loan
.kind
, old_loan
.kind
) {
474 (ty
::MutBorrow
, ty
::MutBorrow
) => {
475 struct_span_err
!(self.bccx
, new_loan
.span
, E0499
,
476 "cannot borrow `{}`{} as mutable \
477 more than once at a time",
481 (ty
::UniqueImmBorrow
, _
) => {
482 struct_span_err
!(self.bccx
, new_loan
.span
, E0500
,
483 "closure requires unique access to `{}` \
484 but {} is already borrowed{}",
485 nl
, ol_pronoun
, old_loan_msg
)
488 (_
, ty
::UniqueImmBorrow
) => {
489 struct_span_err
!(self.bccx
, new_loan
.span
, E0501
,
490 "cannot borrow `{}`{} as {} because \
491 previous closure requires unique access",
492 nl
, new_loan_msg
, new_loan
.kind
.to_user_str())
496 struct_span_err
!(self.bccx
, new_loan
.span
, E0502
,
497 "cannot borrow `{}`{} as {} because \
498 {} is also borrowed as {}{}",
501 new_loan
.kind
.to_user_str(),
503 old_loan
.kind
.to_user_str(),
508 match new_loan
.cause
{
509 euv
::ClosureCapture(span
) => {
512 &format
!("borrow occurs due to use of `{}` in closure",
518 let rule_summary
= match old_loan
.kind
{
520 format
!("the mutable borrow prevents subsequent \
521 moves, borrows, or modification of `{0}` \
522 until the borrow ends",
527 format
!("the immutable borrow prevents subsequent \
528 moves or mutable borrows of `{0}` \
529 until the borrow ends",
533 ty
::UniqueImmBorrow
=> {
534 format
!("the unique capture prevents subsequent \
535 moves or borrows of `{0}` \
536 until the borrow ends",
541 let borrow_summary
= match old_loan
.cause
{
542 euv
::ClosureCapture(_
) => {
543 format
!("previous borrow of `{}` occurs here{} due to \
548 euv
::OverloadedOperator
|
552 euv
::ClosureInvocation
|
555 euv
::MatchDiscriminant
=> {
556 format
!("previous borrow of `{}` occurs here{}",
563 &format
!("{}; {}", borrow_summary
, rule_summary
));
565 let old_loan_span
= self.tcx().map
.span(
566 old_loan
.kill_scope
.node_id(&self.tcx().region_maps
));
567 err
.span_end_note(old_loan_span
,
568 "previous borrow ends here");
576 fn consume_common(&self,
580 mode
: euv
::ConsumeMode
) {
581 match opt_loan_path(&cmt
) {
583 let moved_value_use_kind
= match mode
{
585 self.check_for_copy_of_frozen_path(id
, span
, &lp
);
589 match self.move_data
.kind_of_move_of_path(id
, &lp
) {
591 // Sometimes moves don't have a move kind;
592 // this either means that the original move
593 // was from something illegal to move,
594 // or was moved from referent of an unsafe
595 // pointer or something like that.
599 self.check_for_move_of_borrowed_path(id
, span
,
601 if move_kind
== move_data
::Captured
{
611 self.check_if_path_is_moved(id
, span
, moved_value_use_kind
, &lp
);
617 fn check_for_copy_of_frozen_path(&self,
620 copy_path
: &LoanPath
<'tcx
>) {
621 match self.analyze_restrictions_on_use(id
, copy_path
, ty
::ImmBorrow
) {
623 UseWhileBorrowed(loan_path
, loan_span
) => {
624 struct_span_err
!(self.bccx
, span
, E0503
,
625 "cannot use `{}` because it was mutably borrowed",
626 &self.bccx
.loan_path_to_string(copy_path
))
627 .span_note(loan_span
,
628 &format
!("borrow of `{}` occurs here",
629 &self.bccx
.loan_path_to_string(&loan_path
))
636 fn check_for_move_of_borrowed_path(&self,
639 move_path
: &LoanPath
<'tcx
>,
640 move_kind
: move_data
::MoveKind
) {
641 // We want to detect if there are any loans at all, so we search for
642 // any loans incompatible with MutBorrrow, since all other kinds of
643 // loans are incompatible with that.
644 match self.analyze_restrictions_on_use(id
, move_path
, ty
::MutBorrow
) {
646 UseWhileBorrowed(loan_path
, loan_span
) => {
647 let mut err
= match move_kind
{
648 move_data
::Captured
=>
649 struct_span_err
!(self.bccx
, span
, E0504
,
650 "cannot move `{}` into closure because it is borrowed",
651 &self.bccx
.loan_path_to_string(move_path
)),
652 move_data
::Declared
|
653 move_data
::MoveExpr
|
654 move_data
::MovePat
=>
655 struct_span_err
!(self.bccx
, span
, E0505
,
656 "cannot move out of `{}` because it is borrowed",
657 &self.bccx
.loan_path_to_string(move_path
))
662 &format
!("borrow of `{}` occurs here",
663 &self.bccx
.loan_path_to_string(&loan_path
))
670 pub fn analyze_restrictions_on_use(&self,
671 expr_id
: ast
::NodeId
,
672 use_path
: &LoanPath
<'tcx
>,
673 borrow_kind
: ty
::BorrowKind
)
675 debug
!("analyze_restrictions_on_use(expr_id={}, use_path={:?})",
676 self.tcx().map
.node_to_string(expr_id
),
681 self.each_in_scope_loan_affecting_path(
682 self.tcx().region_maps
.node_extent(expr_id
), use_path
, |loan
| {
683 if !compatible_borrow_kinds(loan
.kind
, borrow_kind
) {
684 ret
= UseWhileBorrowed(loan
.loan_path
.clone(), loan
.span
);
694 /// Reports an error if `expr` (which should be a path)
695 /// is using a moved/uninitialized value
696 fn check_if_path_is_moved(&self,
699 use_kind
: MovedValueUseKind
,
700 lp
: &Rc
<LoanPath
<'tcx
>>) {
701 debug
!("check_if_path_is_moved(id={}, use_kind={:?}, lp={:?})",
704 // FIXME (22079): if you find yourself tempted to cut and paste
705 // the body below and then specializing the error reporting,
706 // consider refactoring this instead!
708 let base_lp
= owned_ptr_base_path_rc(lp
);
709 self.move_data
.each_move_of(id
, &base_lp
, |the_move
, moved_lp
| {
710 self.bccx
.report_use_of_moved_value(
721 /// Reports an error if assigning to `lp` will use a
722 /// moved/uninitialized value. Mainly this is concerned with
723 /// detecting derefs of uninitialized pointers.
729 /// a = 10; // ok, even though a is uninitialized
731 /// struct Point { x: u32, y: u32 }
733 /// p.x = 22; // ok, even though `p` is uninitialized
735 /// let p: Box<Point>;
736 /// (*p).x = 22; // not ok, p is uninitialized, can't deref
738 fn check_if_assigned_path_is_moved(&self,
741 use_kind
: MovedValueUseKind
,
742 lp
: &Rc
<LoanPath
<'tcx
>>)
745 LpVar(_
) | LpUpvar(_
) => {
746 // assigning to `x` does not require that `x` is initialized
748 LpDowncast(ref lp_base
, _
) => {
749 // assigning to `(P->Variant).f` is ok if assigning to `P` is ok
750 self.check_if_assigned_path_is_moved(id
, span
,
753 LpExtend(ref lp_base
, _
, LpInterior(_
, InteriorField(_
))) => {
754 match lp_base
.to_type().sty
{
755 ty
::TyStruct(def
, _
) | ty
::TyEnum(def
, _
) if def
.has_dtor() => {
756 // In the case where the owner implements drop, then
757 // the path must be initialized to prevent a case of
758 // partial reinitialization
760 // FIXME (22079): could refactor via hypothetical
761 // generalized check_if_path_is_moved
762 let loan_path
= owned_ptr_base_path_rc(lp_base
);
763 self.move_data
.each_move_of(id
, &loan_path
, |_
, _
| {
765 .report_partial_reinitialization_of_uninitialized_structure(
775 // assigning to `P.f` is ok if assigning to `P` is ok
776 self.check_if_assigned_path_is_moved(id
, span
,
779 LpExtend(ref lp_base
, _
, LpInterior(_
, InteriorElement(..))) |
780 LpExtend(ref lp_base
, _
, LpDeref(_
)) => {
781 // assigning to `P[i]` requires `P` is initialized
782 // assigning to `(*P)` requires `P` is initialized
783 self.check_if_path_is_moved(id
, span
, use_kind
, lp_base
);
788 fn check_assignment(&self,
789 assignment_id
: ast
::NodeId
,
790 assignment_span
: Span
,
791 assignee_cmt
: mc
::cmt
<'tcx
>) {
792 debug
!("check_assignment(assignee_cmt={:?})", assignee_cmt
);
794 // Check that we don't invalidate any outstanding loans
795 if let Some(loan_path
) = opt_loan_path(&assignee_cmt
) {
796 let scope
= self.tcx().region_maps
.node_extent(assignment_id
);
797 self.each_in_scope_loan_affecting_path(scope
, &loan_path
, |loan
| {
798 self.report_illegal_mutation(assignment_span
, &loan_path
, loan
);
803 // Check for reassignments to (immutable) local variables. This
804 // needs to be done here instead of in check_loans because we
805 // depend on move data.
806 if let Categorization
::Local(local_id
) = assignee_cmt
.cat
{
807 let lp
= opt_loan_path(&assignee_cmt
).unwrap();
808 self.move_data
.each_assignment_of(assignment_id
, &lp
, |assign
| {
809 if assignee_cmt
.mutbl
.is_mutable() {
810 self.tcx().used_mut_nodes
.borrow_mut().insert(local_id
);
812 self.bccx
.report_reassigned_immutable_variable(
823 pub fn report_illegal_mutation(&self,
825 loan_path
: &LoanPath
<'tcx
>,
827 struct_span_err
!(self.bccx
, span
, E0506
,
828 "cannot assign to `{}` because it is borrowed",
829 self.bccx
.loan_path_to_string(loan_path
))
830 .span_note(loan
.span
,
831 &format
!("borrow of `{}` occurs here",
832 self.bccx
.loan_path_to_string(loan_path
)))