Skip to content

Commit

Permalink
librustc: Allow mutable box substructure to be borrowed without requi…
Browse files Browse the repository at this point in the history
…ring purity. Closes rust-lang#4825.
  • Loading branch information
pcwalton committed Feb 7, 2013
1 parent f13ea41 commit a9f5ab1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl gather_loan_ctxt {
if req_mutbl == m_imm {
// if this is an @mut box, then it's generally OK to borrow as
// &imm; this will result in a write guard
if cmt.cat.is_mutable_box() {
if cmt.cat.derefs_through_mutable_box() {
Ok(PcOk)
} else {
// you can treat mutable things as imm if you are pure
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/borrowck/preserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ impl PreserveCtxt {
// live, so we must root the pointer (i.e., inc the ref
// count) for the duration of the loan.
debug!("base.mutbl = %?", self.bccx.mut_to_str(base.mutbl));
debug!("derefs through mutable box? %?",
cmt.cat.derefs_through_mutable_box());
if cmt.cat.derefs_through_mutable_box() {
self.attempt_root(cmt, base, derefs)
} else if base.mutbl == m_imm {
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-pass/borrowck-wg-mut-box-substructure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct Foo {
x: ~[int]
}

fn main() {
let x = @mut Foo { x: ~[ 1, 2, 3, 4, 5 ] };
for x.x.each |x| {
io::println(x.to_str());
}
}

2 comments on commit a9f5ab1

@catamorphism
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

@catamorphism
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Niko r+'d the pull request rather than the commit)

Please sign in to comment.