@@ -496,6 +496,12 @@ impl<'self> CheckLoanCtxt<'self> {
496
496
// path, and check that the super path was not lent out as
497
497
// mutable or immutable (a const loan is ok).
498
498
//
499
+ // Mutability of a path can be dependent on the super path
500
+ // in two ways. First, it might be inherited mutability.
501
+ // Second, the pointee of an `&mut` pointer can only be
502
+ // mutated if it is found in an unaliased location, so we
503
+ // have to check that the owner location is not borrowed.
504
+ //
499
505
// Note that we are *not* checking for any and all
500
506
// restrictions. We are only interested in the pointers
501
507
// that the user created, whereas we add restrictions for
@@ -513,9 +519,12 @@ impl<'self> CheckLoanCtxt<'self> {
513
519
let mut loan_path = loan_path;
514
520
loop {
515
521
match * loan_path {
516
- // Peel back one layer if `loan_path` has
517
- // inherited mutability
518
- LpExtend ( lp_base, mc:: McInherited , _) => {
522
+ // Peel back one layer if, for `loan_path` to be
523
+ // mutable, `lp_base` must be mutable. This occurs
524
+ // with inherited mutability and with `&mut`
525
+ // pointers.
526
+ LpExtend ( lp_base, mc:: McInherited , _) |
527
+ LpExtend ( lp_base, _, LpDeref ( mc:: region_ptr( ast:: m_mutbl, _) ) ) => {
519
528
loan_path = lp_base;
520
529
}
521
530
0 commit comments