Skip to content

Commit e5e865b

Browse files
committed
auto merge of #14536 : zwarich/rust/issue-14498, r=luqmana
Make check_for_assignment_to_restricted_or_frozen_location treat mutation through an owning pointer the same way it treats mutation through an &mut pointer, where mutability must be inherited from the base path. I also included GC pointers in this check, as that is what the corresponding code in gather_loans/restrictions.rs does, but I don't think there is a way to test this with the current language. Fixes #14498.
2 parents 24e489f + 5aff0e7 commit e5e865b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/librustc/middle/borrowck/check_loans.rs

+2
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ impl<'a> CheckLoanCtxt<'a> {
641641
// with inherited mutability and with `&mut`
642642
// pointers.
643643
LpExtend(ref lp_base, mc::McInherited, _) |
644+
LpExtend(ref lp_base, _, LpDeref(mc::OwnedPtr)) |
645+
LpExtend(ref lp_base, _, LpDeref(mc::GcPtr)) |
644646
LpExtend(ref lp_base, _, LpDeref(mc::BorrowedPtr(ty::MutBorrow, _))) => {
645647
lp_base.clone()
646648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 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+
// This tests that we can't modify Box<&mut T> contents while they
12+
// are borrowed.
13+
14+
struct A { a: int }
15+
struct B<'a> { a: Box<&'a mut int> }
16+
17+
fn borrow_in_var_from_var() {
18+
let mut x: int = 1;
19+
let y = box &mut x;
20+
let p = &y;
21+
let q = &***p;
22+
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
23+
drop(p);
24+
drop(q);
25+
}
26+
27+
fn borrow_in_var_from_field() {
28+
let mut x = A { a: 1 };
29+
let y = box &mut x.a;
30+
let p = &y;
31+
let q = &***p;
32+
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
33+
drop(p);
34+
drop(q);
35+
}
36+
37+
fn borrow_in_field_from_var() {
38+
let mut x: int = 1;
39+
let y = B { a: box &mut x };
40+
let p = &y.a;
41+
let q = &***p;
42+
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
43+
drop(p);
44+
drop(q);
45+
}
46+
47+
fn borrow_in_field_from_field() {
48+
let mut x = A { a: 1 };
49+
let y = B { a: box &mut x.a };
50+
let p = &y.a;
51+
let q = &***p;
52+
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
53+
drop(p);
54+
drop(q);
55+
}
56+
57+
fn main() {
58+
borrow_in_var_from_var();
59+
borrow_in_var_from_field();
60+
borrow_in_field_from_var();
61+
borrow_in_field_from_field();
62+
}
63+

0 commit comments

Comments
 (0)