From bbbb80559c8e321dc023c48579367e2ef1349b4b Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 17 Jan 2013 16:15:08 -0800 Subject: [PATCH] librustc: Disallow `&mut` loans from overlapping with any other loans --- src/librustc/middle/borrowck/check_loans.rs | 9 ++++----- src/librustc/middle/borrowck/loan.rs | 2 +- src/libstd/sort.rs | 12 +++++++++--- src/test/run-pass/alt-implicit-copy-unique.rs | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/librustc/middle/borrowck/check_loans.rs b/src/librustc/middle/borrowck/check_loans.rs index 5635d5d18e59..c7ccf28755c0 100644 --- a/src/librustc/middle/borrowck/check_loans.rs +++ b/src/librustc/middle/borrowck/check_loans.rs @@ -296,12 +296,11 @@ impl check_loan_ctxt { } match (old_loan.mutbl, new_loan.mutbl) { - (m_const, _) | (_, m_const) | - (m_mutbl, m_mutbl) | (m_imm, m_imm) => { + (m_const, _) | (_, m_const) | (m_imm, m_imm) => { /*ok*/ } - (m_mutbl, m_imm) | (m_imm, m_mutbl) => { + (m_mutbl, m_mutbl) | (m_mutbl, m_imm) | (m_imm, m_mutbl) => { self.bccx.span_err( new_loan.cmt.span, fmt!("loan of %s as %s \ @@ -418,8 +417,8 @@ impl check_loan_ctxt { for self.walk_loans_of(ex.id, lp) |loan| { match loan.mutbl { - m_mutbl | m_const => { /*ok*/ } - m_imm => { + m_const => { /*ok*/ } + m_mutbl | m_imm => { self.bccx.span_err( ex.span, fmt!("%s prohibited due to outstanding loan", diff --git a/src/librustc/middle/borrowck/loan.rs b/src/librustc/middle/borrowck/loan.rs index 80e5dacae41e..a9bbcbb9cc2d 100644 --- a/src/librustc/middle/borrowck/loan.rs +++ b/src/librustc/middle/borrowck/loan.rs @@ -251,7 +251,7 @@ impl LoanContext { // Variant components: the base must be immutable, because // if it is overwritten, the types of the embedded data // could change. - do self.loan(cmt_base, m_imm).chain |_ok| { + do self.loan(cmt_base, m_imm).chain |_| { // can use static, as in loan_stable_comp() self.issue_loan(cmt, ty::re_static, req_mutbl) } diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index cce3dc725da7..3017d95f2a7e 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -630,9 +630,15 @@ impl MergeState { dest -= 1; c2 -= 1; len2 -= 1; if len2 == 1 { break_outer = true; break; } - let tmp_view = vec::mut_view(tmp, 0, len2); - let count2 = len2 - gallop_left(&const array[c1], - tmp_view, len2-1); + let count2; + { + let tmp_view = vec::mut_view(tmp, 0, len2); + count2 = len2 - gallop_left(&const array[c1], + tmp_view, + len2-1); + // Make tmp_view go out of scope to appease borrowck. + } + if count2 != 0 { dest -= count2; c2 -= count2; len2 -= count2; copy_vec(array, dest+1, tmp, c2+1, count2); diff --git a/src/test/run-pass/alt-implicit-copy-unique.rs b/src/test/run-pass/alt-implicit-copy-unique.rs index df9be797ed3f..f703eed6a21c 100644 --- a/src/test/run-pass/alt-implicit-copy-unique.rs +++ b/src/test/run-pass/alt-implicit-copy-unique.rs @@ -12,7 +12,7 @@ fn main() { let x = ~{mut a: ~10, b: ~20}; match x { ~{a: ref mut a, b: ref b} => { - assert **a == 10; (*x).a = ~30; assert **a == 30; + assert **a == 10; *a = ~30; assert **a == 30; } } }