From d204cfd739bfecb6252b54c5007a28559d0f9a73 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 14 May 2013 09:26:21 -0400 Subject: [PATCH 1/4] Issue #5967: Make rvalues mutable --- src/librustc/middle/borrowck/gather_loans/lifetime.rs | 10 +++++++++- src/librustc/middle/mem_categorization.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/borrowck/gather_loans/lifetime.rs b/src/librustc/middle/borrowck/gather_loans/lifetime.rs index 330d60a59d3..b0ddf2057d9 100644 --- a/src/librustc/middle/borrowck/gather_loans/lifetime.rs +++ b/src/librustc/middle/borrowck/gather_loans/lifetime.rs @@ -93,7 +93,7 @@ impl GuaranteeLifetimeContext { let omit_root = ( ptr_mutbl == m_imm && self.bccx.is_subregion_of(self.loan_region, base_scope) && - base.mutbl.is_immutable() && + self.is_rvalue_or_immutable(base) && !self.is_moved(base) ); @@ -168,6 +168,14 @@ impl GuaranteeLifetimeContext { } } + fn is_rvalue_or_immutable(&self, + cmt: mc::cmt) -> bool { + cmt.mutbl.is_immutable() || match cmt.guarantor().cat { + mc::cat_rvalue => true, + _ => false + } + } + fn check_root(&self, cmt_deref: mc::cmt, cmt_base: mc::cmt, diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 95ed7fe8efc..91c0b8e61cc 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -551,7 +551,7 @@ pub impl mem_categorization_ctxt { id:elt.id(), span:elt.span(), cat:cat_rvalue, - mutbl:McImmutable, + mutbl:McDeclared, ty:expr_ty } } From 87398f31e5615bef744f2ac3f66c00b1a7d4959e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 14 May 2013 09:28:58 -0400 Subject: [PATCH 2/4] Add explanatory comment to is_rvalue_or_immutable() --- src/librustc/middle/borrowck/gather_loans/lifetime.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/librustc/middle/borrowck/gather_loans/lifetime.rs b/src/librustc/middle/borrowck/gather_loans/lifetime.rs index b0ddf2057d9..e01a26a1281 100644 --- a/src/librustc/middle/borrowck/gather_loans/lifetime.rs +++ b/src/librustc/middle/borrowck/gather_loans/lifetime.rs @@ -170,6 +170,11 @@ impl GuaranteeLifetimeContext { fn is_rvalue_or_immutable(&self, cmt: mc::cmt) -> bool { + //! We can omit the root on an `@T` value if the location + //! that holds the box is either (1) an rvalue, in which case + //! is is in a non-user-accessible temporary, or (2) an immutable + //! lvalue. + cmt.mutbl.is_immutable() || match cmt.guarantor().cat { mc::cat_rvalue => true, _ => false From e665fdfc8e40dcecd2a9268ecabc4b172c709ed1 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 16 May 2013 15:10:50 -0400 Subject: [PATCH 3/4] Add two tests for rvalue mutability --- .../borrowck-rvalues-mutable-bad.rs | 38 +++++++++++++++++++ src/test/run-pass/borrowck-rvalues-mutable.rs | 30 +++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/test/compile-fail/borrowck-rvalues-mutable-bad.rs create mode 100644 src/test/run-pass/borrowck-rvalues-mutable.rs diff --git a/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs b/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs new file mode 100644 index 00000000000..10bef907a28 --- /dev/null +++ b/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs @@ -0,0 +1,38 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Tests that rvalue lifetimes is limited to the enclosing trans +// cleanup scope. It is unclear that this is the correct lifetime for +// rvalues, but that's what it is right now. + +struct Counter { + value: uint +} + +impl Counter { + fn new(v: uint) -> Counter { + Counter {value: v} + } + + fn inc<'a>(&'a mut self) -> &'a mut Counter { + self.value += 1; + self + } + + fn get(&self) -> uint { + self.value + } +} + +pub fn main() { + let v = Counter::new(22).inc().inc().get(); + //~^ ERROR borrowed value does not live long enough + assert_eq!(v, 24);; +} diff --git a/src/test/run-pass/borrowck-rvalues-mutable.rs b/src/test/run-pass/borrowck-rvalues-mutable.rs new file mode 100644 index 00000000000..cf5a9341c9d --- /dev/null +++ b/src/test/run-pass/borrowck-rvalues-mutable.rs @@ -0,0 +1,30 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Counter { + value: uint +} + +impl Counter { + fn new(v: uint) -> Counter { + Counter {value: v} + } + + fn get_and_inc(&mut self) -> uint { + let v = self.value; + self.value += 1; + v + } +} + +pub fn main() { + let v = Counter::new(22).get_and_inc(); + assert_eq!(v, 22); +} From 63d878f36b2630ac0fcb4bcaa123b135126b091a Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 16 May 2013 15:12:39 -0400 Subject: [PATCH 4/4] correct typo --- src/librustc/middle/borrowck/gather_loans/lifetime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/borrowck/gather_loans/lifetime.rs b/src/librustc/middle/borrowck/gather_loans/lifetime.rs index e01a26a1281..e377bebcc26 100644 --- a/src/librustc/middle/borrowck/gather_loans/lifetime.rs +++ b/src/librustc/middle/borrowck/gather_loans/lifetime.rs @@ -172,7 +172,7 @@ impl GuaranteeLifetimeContext { cmt: mc::cmt) -> bool { //! We can omit the root on an `@T` value if the location //! that holds the box is either (1) an rvalue, in which case - //! is is in a non-user-accessible temporary, or (2) an immutable + //! it is in a non-user-accessible temporary, or (2) an immutable //! lvalue. cmt.mutbl.is_immutable() || match cmt.guarantor().cat {