From a3f5d8aea1e43a1c5b0814dac407e6ddfab07417 Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Fri, 15 Apr 2016 18:12:52 +0200 Subject: [PATCH 01/11] make the borrowing example more concrete --- src/doc/book/references-and-borrowing.md | 32 +++++++++++++++--------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/doc/book/references-and-borrowing.md b/src/doc/book/references-and-borrowing.md index a08d53f958ba3..3a1f3f004be62 100644 --- a/src/doc/book/references-and-borrowing.md +++ b/src/doc/book/references-and-borrowing.md @@ -62,19 +62,27 @@ This is not idiomatic Rust, however, as it doesn’t take advantage of borrowing the first step: ```rust -fn foo(v1: &Vec, v2: &Vec) -> i32 { - // do stuff with v1 and v2 - - // return the answer - 42 +fn main() { + // Don't worry if you don't understand how `fold` works, the point here is that an immutable reference is borrowed. + fn sum_vec(v: &Vec) -> i32 { + return v.iter().fold(0, |a, &b| a + b); + } + // Borrow two vectors and and sum them. + // This kind of borrowing does not allow mutation to the borrowed. + fn foo(v1: &Vec, v2: &Vec) -> i32 { + // do stuff with v1 and v2 + let s1 = sum_vec(v1); + let s2 = sum_vec(v2); + // return the answer + s1 + s2 + } + + let v1 = vec![1, 2, 3]; + let v2 = vec![4, 5, 6]; + + let answer = foo(&v1, &v2); + println!("{}", answer); } - -let v1 = vec![1, 2, 3]; -let v2 = vec![1, 2, 3]; - -let answer = foo(&v1, &v2); - -// we can use v1 and v2 here! ``` Instead of taking `Vec`s as our arguments, we take a reference: From 4d0b0e975b30d067e2347b74ea19f49c7f04fbda Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 17 Apr 2016 18:50:49 +0200 Subject: [PATCH 02/11] Improve as_mut ptr method example --- src/libcore/ptr.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index a3c7ab481a765..8b3a14b24df29 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -459,6 +459,9 @@ impl *mut T { /// ``` /// let mut s = [1, 2, 3]; /// let ptr: *mut u32 = s.as_mut_ptr(); + /// let first_value = unsafe { ptr.as_mut().unwrap() }; + /// *first_value = 4; + /// println!("{:?}", s); // It'll print: "[4, 2, 3]". /// ``` #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] From f252cfa2d4709c44bf7e9dd02e522e8e372482bc Mon Sep 17 00:00:00 2001 From: Michael Tiller Date: Tue, 19 Apr 2016 12:34:33 -0400 Subject: [PATCH 03/11] Update ownership.md Opening sentence was confusing and something cannot be "one of the most unique" (it either is or is not unique). --- src/doc/book/ownership.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/book/ownership.md b/src/doc/book/ownership.md index 988103a118032..e2e0403b738fa 100644 --- a/src/doc/book/ownership.md +++ b/src/doc/book/ownership.md @@ -1,7 +1,7 @@ % Ownership -This guide is one of three presenting Rust’s ownership system. This is one of -Rust’s most unique and compelling features, with which Rust developers should +This is the first of three sections presenting Rust’s ownership system. This is one of +Rust’s most distinct and compelling features, with which Rust developers should become quite acquainted. Ownership is how Rust achieves its largest goal, memory safety. There are a few distinct concepts, each with its own chapter: From 864eba884d860d7d0bfceb89af1a4bc690f21ea0 Mon Sep 17 00:00:00 2001 From: Michael Tiller Date: Tue, 19 Apr 2016 12:38:39 -0400 Subject: [PATCH 04/11] Opening sentence was confusing and something cannot be "one of the most unique" (it either is or is not unique). --- src/doc/book/references-and-borrowing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/book/references-and-borrowing.md b/src/doc/book/references-and-borrowing.md index a08d53f958ba3..dc6f885487c1f 100644 --- a/src/doc/book/references-and-borrowing.md +++ b/src/doc/book/references-and-borrowing.md @@ -1,7 +1,7 @@ % References and Borrowing -This guide is two of three presenting Rust’s ownership system. This is one of -Rust’s most unique and compelling features, with which Rust developers should +This is the second of three sections presenting Rust’s ownership system. This is one of +Rust’s most distinct and compelling features, with which Rust developers should become quite acquainted. Ownership is how Rust achieves its largest goal, memory safety. There are a few distinct concepts, each with its own chapter: From f7ec6873ccfbf7dcdbd1908c0857c866b3e7087a Mon Sep 17 00:00:00 2001 From: Michael Tiller Date: Tue, 19 Apr 2016 12:39:31 -0400 Subject: [PATCH 05/11] Opening sentence was confusing and something cannot be "one of the most unique" (it either is or is not unique). --- src/doc/book/lifetimes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/book/lifetimes.md b/src/doc/book/lifetimes.md index 695b1614fb70c..cb0757318984d 100644 --- a/src/doc/book/lifetimes.md +++ b/src/doc/book/lifetimes.md @@ -1,7 +1,7 @@ % Lifetimes -This guide is three of three presenting Rust’s ownership system. This is one of -Rust’s most unique and compelling features, with which Rust developers should +This is the last of three sections presenting Rust’s ownership system. This is one of +Rust’s most distinct and compelling features, with which Rust developers should become quite acquainted. Ownership is how Rust achieves its largest goal, memory safety. There are a few distinct concepts, each with its own chapter: From 3e9ea3b6044f06f588e01436cb4344ae93964846 Mon Sep 17 00:00:00 2001 From: Brayden Winterton Date: Fri, 22 Apr 2016 10:26:55 -0600 Subject: [PATCH 06/11] Make HashSet::Insert documentation more consistent --- src/libstd/collections/hash/set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index b353a4c1ba120..e4ef3fca55dc7 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -535,9 +535,9 @@ impl HashSet /// Adds a value to the set. /// - /// If the set did not have a value present, `true` is returned. + /// If the set did not have this value present, `true` is returned. /// - /// If the set did have this key present, `false` is returned. + /// If the set did have this value present, `false` is returned. /// /// # Examples /// From 93486180d99e6216941f72a1a3deff25545a181b Mon Sep 17 00:00:00 2001 From: York Xiang Date: Tue, 26 Apr 2016 13:36:18 +0800 Subject: [PATCH 07/11] Improve error message about regions of function body --- src/librustc/infer/error_reporting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/infer/error_reporting.rs b/src/librustc/infer/error_reporting.rs index a7553f4eb1f1c..1969c614146fb 100644 --- a/src/librustc/infer/error_reporting.rs +++ b/src/librustc/infer/error_reporting.rs @@ -158,7 +158,7 @@ impl<'tcx> TyCtxt<'tcx> { "scope of call-site for function" } region::CodeExtentData::ParameterScope { .. } => { - "scope of parameters for function" + "scope of function body" } region::CodeExtentData::DestructionScope(_) => { new_string = format!("destruction scope surrounding {}", tag); From 6343f261f4aeacdd292bf07998ef5faf6e90d57b Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 26 Apr 2016 16:27:10 +0200 Subject: [PATCH 08/11] allow InternedString to be compared to &str directly --- src/libsyntax/attr.rs | 6 +++--- src/libsyntax/parse/token.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index dd414c463c7b1..8761ca3717895 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -333,11 +333,11 @@ pub enum InlineAttr { pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr { attrs.iter().fold(InlineAttr::None, |ia,attr| { match attr.node.value.node { - MetaItemKind::Word(ref n) if *n == "inline" => { + MetaItemKind::Word(ref n) if n == "inline" => { mark_used(attr); InlineAttr::Hint } - MetaItemKind::List(ref n, ref items) if *n == "inline" => { + MetaItemKind::List(ref n, ref items) if n == "inline" => { mark_used(attr); if items.len() != 1 { diagnostic.map(|d|{ d.span_err(attr.span, "expected one argument"); }); @@ -711,7 +711,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P]) { pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec { let mut acc = Vec::new(); match attr.node.value.node { - ast::MetaItemKind::List(ref s, ref items) if *s == "repr" => { + ast::MetaItemKind::List(ref s, ref items) if s == "repr" => { mark_used(attr); for item in items { match item.node { diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fcb6c3539db59..47de32ed7d00f 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -566,6 +566,28 @@ impl<'a> PartialEq for &'a str { } } +impl PartialEq for InternedString { + #[inline(always)] + fn eq(&self, other: &str) -> bool { + PartialEq::eq(&self.string[..], other) + } + #[inline(always)] + fn ne(&self, other: &str) -> bool { + PartialEq::ne(&self.string[..], other) + } +} + +impl PartialEq for str { + #[inline(always)] + fn eq(&self, other: &InternedString) -> bool { + PartialEq::eq(self, &other.string[..]) + } + #[inline(always)] + fn ne(&self, other: &InternedString) -> bool { + PartialEq::ne(self, &other.string[..]) + } +} + impl Decodable for InternedString { fn decode(d: &mut D) -> Result { Ok(intern(d.read_str()?.as_ref()).as_str()) From 10abb666e48abdb747946db6de21317708e18cf5 Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Tue, 26 Apr 2016 17:40:59 +0200 Subject: [PATCH 09/11] Update references-and-borrowing.md add as 2nd example. --- src/doc/book/references-and-borrowing.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/doc/book/references-and-borrowing.md b/src/doc/book/references-and-borrowing.md index 3a1f3f004be62..67a8a82f2a33b 100644 --- a/src/doc/book/references-and-borrowing.md +++ b/src/doc/book/references-and-borrowing.md @@ -61,6 +61,24 @@ let (v1, v2, answer) = foo(v1, v2); This is not idiomatic Rust, however, as it doesn’t take advantage of borrowing. Here’s the first step: +```rust +fn foo(v1: &Vec, v2: &Vec) -> i32 { + // do stuff with v1 and v2 + + // return the answer + 42 +} + +let v1 = vec![1, 2, 3]; +let v2 = vec![1, 2, 3]; + +let answer = foo(&v1, &v2); + +// we can use v1 and v2 here! +``` + +A more concrete example: + ```rust fn main() { // Don't worry if you don't understand how `fold` works, the point here is that an immutable reference is borrowed. From e6b9760df21f580e8210f97c01a957a91eeb91b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20H=C3=BCbelbauer?= Date: Wed, 27 Apr 2016 10:56:35 +0200 Subject: [PATCH 10/11] Fix use of the `move` command in the Windows shell `move` work both in `cmd` and in Powershell. `mv` works only in Powershell and the book says nothing about which shell is recommended so this could confuse beginners. Closes #33219. --- src/doc/book/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/book/getting-started.md b/src/doc/book/getting-started.md index 3a20663cda62a..fc2a762088331 100644 --- a/src/doc/book/getting-started.md +++ b/src/doc/book/getting-started.md @@ -412,7 +412,7 @@ enter the following commands: ```bash $ mkdir src -$ mv main.rs src/main.rs +$ mv main.rs src/main.rs # or 'move main.rs src/main.rs' on Windows $ rm main # or 'del main.exe' on Windows ``` From d569228825c5fe2840d2eec5dbbf97bca253c803 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Wed, 27 Apr 2016 17:30:04 +0300 Subject: [PATCH 11/11] mir: drop temps outside-in by scheduling the drops inside-out. --- src/librustc_mir/build/expr/as_temp.rs | 5 +++-- src/test/run-pass/issue-23338-ensure-param-drop-order.rs | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/build/expr/as_temp.rs b/src/librustc_mir/build/expr/as_temp.rs index a2f7d2c9d725f..f580e41ec6e76 100644 --- a/src/librustc_mir/build/expr/as_temp.rs +++ b/src/librustc_mir/build/expr/as_temp.rs @@ -41,7 +41,7 @@ impl<'a,'tcx> Builder<'a,'tcx> { span_bug!(expr.span, "no temp_lifetime for expr"); } }; - this.schedule_drop(expr.span, temp_lifetime, &temp, expr_ty); + let expr_span = expr.span; // Careful here not to cause an infinite cycle. If we always // called `into`, then for lvalues like `x.f`, it would @@ -52,7 +52,6 @@ impl<'a,'tcx> Builder<'a,'tcx> { // course) `as_temp`. match Category::of(&expr.kind).unwrap() { Category::Lvalue => { - let expr_span = expr.span; let lvalue = unpack!(block = this.as_lvalue(block, expr)); let rvalue = Rvalue::Use(Operand::Consume(lvalue)); let scope_id = this.innermost_scope_id(); @@ -63,6 +62,8 @@ impl<'a,'tcx> Builder<'a,'tcx> { } } + this.schedule_drop(expr_span, temp_lifetime, &temp, expr_ty); + block.and(temp) } } diff --git a/src/test/run-pass/issue-23338-ensure-param-drop-order.rs b/src/test/run-pass/issue-23338-ensure-param-drop-order.rs index 73c52a0843cfb..507d482febfd9 100644 --- a/src/test/run-pass/issue-23338-ensure-param-drop-order.rs +++ b/src/test/run-pass/issue-23338-ensure-param-drop-order.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(rustc_attrs)] - // ignore-pretty : (#23623) problems when ending with // comments // This test is ensuring that parameters are indeed dropped after @@ -66,7 +64,6 @@ fn test<'a>(log: d::Log<'a>) { d::println(&format!("result {}", result)); } -#[rustc_no_mir] // FIXME #29855 MIR doesn't handle all drops correctly. fn foo<'a>(da0: D<'a>, de1: D<'a>) -> D<'a> { d::println("entered foo"); let de2 = de1.incr(); // creates D(de_2, 2)