From f7e1421debb5bc32f60f6d7d50fc356db16800e7 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 28 Apr 2016 15:01:47 +0200 Subject: [PATCH 01/18] Add `TAGS.rustc.emacs`/`TAGS.rustc.vi` make targets, (re-)including rustc source. --- mk/ctags.mk | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mk/ctags.mk b/mk/ctags.mk index a116f2aba6437..1fcb0bb4debbc 100644 --- a/mk/ctags.mk +++ b/mk/ctags.mk @@ -15,14 +15,21 @@ .PHONY: TAGS.emacs TAGS.vi -CTAGS_LOCATIONS=$(wildcard ${CFG_SRC_DIR}src/lib*) +CTAGS_RUSTC_LOCATIONS=$(patsubst ${CFG_SRC_DIR}src/lib%test,, \ + $(wildcard ${CFG_SRC_DIR}src/lib*)) ${CFG_SRC_DIR}src/libtest CTAGS_LOCATIONS=$(patsubst ${CFG_SRC_DIR}src/librust%,, \ $(patsubst ${CFG_SRC_DIR}src/lib%test,, \ $(wildcard ${CFG_SRC_DIR}src/lib*))) ${CFG_SRC_DIR}src/libtest -CTAGS_OPTS=--options="${CFG_SRC_DIR}src/etc/ctags.rust" --languages=Rust --recurse ${CTAGS_LOCATIONS} +CTAGS_OPTS=--options="${CFG_SRC_DIR}src/etc/ctags.rust" --languages=Rust --recurse + +TAGS.rustc.emacs: + ctags -e -f $@ ${CTAGS_OPTS} ${CTAGS_RUSTC_LOCATIONS} TAGS.emacs: - ctags -e -f $@ ${CTAGS_OPTS} + ctags -e -f $@ ${CTAGS_OPTS} ${CTAGS_LOCATIONS} + +TAGS.rustc.vi: + ctags -f $@ ${CTAGS_OPTS} ${CTAGS_RUSTC_LOCATIONS} TAGS.vi: - ctags -f $@ ${CTAGS_OPTS} + ctags -f $@ ${CTAGS_OPTS} ${CTAGS_LOCATIONS} From 27c01cb4973a1728813f9668d34522c0c83076d5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 28 Apr 2016 12:42:42 +0200 Subject: [PATCH 02/18] Add process types documentation --- src/libstd/process.rs | 200 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 4 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 1d2516a4c4099..7a86e726b3a6a 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -215,12 +215,38 @@ impl Command { /// /// Builder methods are provided to change these defaults and /// otherwise configure the process. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("sh") + /// .spawn() + /// .expect("sh command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn new>(program: S) -> Command { Command { inner: imp::Command::new(program.as_ref()) } } /// Add an argument to pass to the program. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .arg("-l") + /// .arg("-a") + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn arg>(&mut self, arg: S) -> &mut Command { self.inner.arg(arg.as_ref()); @@ -228,6 +254,19 @@ impl Command { } /// Add multiple arguments to pass to the program. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .args(&["-l", "-a"]) + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn args>(&mut self, args: &[S]) -> &mut Command { for arg in args { @@ -240,6 +279,19 @@ impl Command { /// /// Note that environment variable names are case-insensitive (but case-preserving) on Windows, /// and case-sensitive on all other platforms. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .env("PATH", "/bin") + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env(&mut self, key: K, val: V) -> &mut Command where K: AsRef, V: AsRef @@ -249,6 +301,19 @@ impl Command { } /// Removes an environment variable mapping. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .env_remove("PATH") + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env_remove>(&mut self, key: K) -> &mut Command { self.inner.env_remove(key.as_ref()); @@ -256,6 +321,19 @@ impl Command { } /// Clears the entire environment map for the child process. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .env_clear() + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env_clear(&mut self) -> &mut Command { self.inner.env_clear(); @@ -263,6 +341,19 @@ impl Command { } /// Sets the working directory for the child process. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .current_dir("/bin") + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn current_dir>(&mut self, dir: P) -> &mut Command { self.inner.cwd(dir.as_ref().as_ref()); @@ -270,6 +361,19 @@ impl Command { } /// Configuration for the child process's stdin handle (file descriptor 0). + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::{Command, Stdio}; + /// + /// Command::new("ls") + /// .stdin(Stdio::null()) + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn stdin(&mut self, cfg: Stdio) -> &mut Command { self.inner.stdin(cfg.0); @@ -277,6 +381,19 @@ impl Command { } /// Configuration for the child process's stdout handle (file descriptor 1). + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::{Command, Stdio}; + /// + /// Command::new("ls") + /// .stdout(Stdio::null()) + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn stdout(&mut self, cfg: Stdio) -> &mut Command { self.inner.stdout(cfg.0); @@ -284,6 +401,19 @@ impl Command { } /// Configuration for the child process's stderr handle (file descriptor 2). + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::{Command, Stdio}; + /// + /// Command::new("ls") + /// .stderr(Stdio::null()) + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn stderr(&mut self, cfg: Stdio) -> &mut Command { self.inner.stderr(cfg.0); @@ -293,6 +423,18 @@ impl Command { /// Executes the command as a child process, returning a handle to it. /// /// By default, stdin, stdout and stderr are inherited from the parent. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// Command::new("ls") + /// .spawn() + /// .expect("ls command failed to start"); + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn spawn(&mut self) -> io::Result { self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner) @@ -308,8 +450,10 @@ impl Command { /// /// ```should_panic /// use std::process::Command; - /// let output = Command::new("/bin/cat").arg("file.txt").output() - /// .expect("failed to execute process"); + /// let output = Command::new("/bin/cat") + /// .arg("file.txt") + /// .output() + /// .expect("failed to execute process"); /// /// println!("status: {}", output.status); /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); @@ -333,8 +477,10 @@ impl Command { /// ```should_panic /// use std::process::Command; /// - /// let status = Command::new("/bin/cat").arg("file.txt").status() - /// .expect("failed to execute process"); + /// let status = Command::new("/bin/cat") + /// .arg("file.txt") + /// .status() + /// .expect("failed to execute process"); /// /// println!("process exited with: {}", status); /// @@ -469,12 +615,42 @@ impl fmt::Display for ExitStatus { impl Child { /// Forces the child to exit. This is equivalent to sending a /// SIGKILL on unix platforms. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// let mut command = Command::new("yes"); + /// if let Ok(mut child) = command.spawn() { + /// child.kill().expect("command wasn't running"); + /// } else { + /// println!("yes command didn't start"); + /// } + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn kill(&mut self) -> io::Result<()> { self.handle.kill() } /// Returns the OS-assigned process identifier associated with this child. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// let mut command = Command::new("ls"); + /// if let Ok(child) = command.spawn() { + /// println!("Child's id is {}", child.id()); + /// } else { + /// println!("ls command didn't start"); + /// } + /// ``` #[stable(feature = "process_id", since = "1.3.0")] pub fn id(&self) -> u32 { self.handle.id() @@ -488,6 +664,22 @@ impl Child { /// before waiting. This helps avoid deadlock: it ensures that the /// child does not block waiting for input from the parent, while /// the parent waits for the child to exit. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```no_run + /// use std::process::Command; + /// + /// let mut command = Command::new("ls"); + /// if let Ok(mut child) = command.spawn() { + /// child.wait().expect("command wasn't running"); + /// println!("Child has finished its execution!"); + /// } else { + /// println!("ls command didn't start"); + /// } + /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn wait(&mut self) -> io::Result { drop(self.stdin.take()); From 2d156908569728151acdc8c9683e915d1f56f1ea Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 1 May 2016 14:04:13 +0200 Subject: [PATCH 03/18] Doc improvement on std::fmt module --- src/libcollections/fmt.rs | 12 ++++++++++++ src/libcore/fmt/mod.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/libcollections/fmt.rs b/src/libcollections/fmt.rs index e30e0b213afa1..bf13ea067b4c8 100644 --- a/src/libcollections/fmt.rs +++ b/src/libcollections/fmt.rs @@ -505,12 +505,24 @@ use string; /// /// # Examples /// +/// Basic usage: +/// /// ``` /// use std::fmt; /// /// let s = fmt::format(format_args!("Hello, {}!", "world")); /// assert_eq!(s, "Hello, world!".to_string()); /// ``` +/// +/// Please note that using `[format!]` might be preferrable. +/// Example: +/// +/// ``` +/// let s = format!("Hello, {}!", "world"); +/// assert_eq!(s, "Hello, world!".to_string()); +/// ``` +/// +/// [format!]: ../std/macro.format!.html #[stable(feature = "rust1", since = "1.0.0")] pub fn format(args: Arguments) -> string::String { let mut output = string::String::new(); diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 0c824b5a8e69a..3819657ebbb0d 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -776,6 +776,32 @@ pub trait UpperExp { /// /// * output - the buffer to write output to /// * args - the precompiled arguments generated by `format_args!` +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// use std::fmt; +/// +/// let mut output = String::new(); +/// fmt::write(&mut output, format_args!("Hello {}!", "world")) +/// .expect("Error occurred while trying to write in String"); +/// assert_eq!(output, "Hello world!"); +/// ``` +/// +/// Please note that using [write!][write_macro] might be preferrable. Example: +/// +/// ``` +/// use std::fmt::Write; +/// +/// let mut output = String::new(); +/// write!(&mut output, "Hello {}!", "world") +/// .expect("Error occurred while trying to write in String"); +/// assert_eq!(output, "Hello world!"); +/// ``` +/// +/// [write_macro]: ../std/macro.write!.html #[stable(feature = "rust1", since = "1.0.0")] pub fn write(output: &mut Write, args: Arguments) -> Result { let mut formatter = Formatter { From 47d9f49ebf3b32ac79011e282f5bd8d2dce2df39 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 1 May 2016 18:13:36 +0200 Subject: [PATCH 04/18] Remove rust flags from doc block --- src/librustc_driver/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 4aaeeed343016..3b25104437aee 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -352,7 +352,13 @@ fn handle_explain(code: &str, match descriptions.find_description(&normalised) { Some(ref description) => { // Slice off the leading newline and print. - print!("{}", &description[1..]); + print!("{}", &(&description[1..]).split("\n").map(|x| { + format!("{}\n", if x.starts_with("```") { + "```" + } else { + x + }) + }).collect::()); } None => { early_error(output, &format!("no extended information for {}", code)); From e3f13129b42354ca9cc408df084796f0404c30a9 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 1 May 2016 19:23:29 +0200 Subject: [PATCH 05/18] dep_graph: avoid panicking in thread when channel closed On my system, when the processor is already loaded, and I try to run the test suite, e.g. compile-fail/dep-graph-assoc-type-trans.rs fails because of undecodable JSON. Running the compiler manually, I can see that the dep graph thread panics (and puts non-JSON on stderr) while `send`ing on `swap_out`, presumably because the other end has already quit. I think that in this case, we can just gracefully exit the thread. --- src/librustc/dep_graph/thread.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/librustc/dep_graph/thread.rs b/src/librustc/dep_graph/thread.rs index 5b0e4a909c8d3..b15e0e33b8402 100644 --- a/src/librustc/dep_graph/thread.rs +++ b/src/librustc/dep_graph/thread.rs @@ -176,6 +176,9 @@ pub fn main(swap_in: Receiver>, DepMessage::Query => query_out.send(edges.query()).unwrap(), } } - swap_out.send(messages).unwrap(); + if let Err(_) = swap_out.send(messages) { + // the receiver must have been dropped already + break; + } } } From eba43fb5c3baaeb543edb6c0abbcf778a176c4a9 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 1 May 2016 22:59:20 +0200 Subject: [PATCH 06/18] std::thread docs: spawn() returns not a Thread anymore Also move the "Thread type" section down a bit, since it is not so important anymore. Fixes: #33321 --- src/libstd/thread/mod.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index b3549dc12645a..2f0dec759b3d5 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -13,7 +13,8 @@ //! ## The threading model //! //! An executing Rust program consists of a collection of native OS threads, -//! each with their own stack and local state. +//! each with their own stack and local state. Threads can be named, and +//! provide some built-in support for low-level synchronization. //! //! Communication between threads can be done through //! [channels](../../std/sync/mpsc/index.html), Rust's message-passing @@ -37,20 +38,6 @@ //! convenient facilities for automatically waiting for the termination of a //! child thread (i.e., join). //! -//! ## The `Thread` type -//! -//! Threads are represented via the `Thread` type, which you can -//! get in one of two ways: -//! -//! * By spawning a new thread, e.g. using the `thread::spawn` function. -//! * By requesting the current thread, using the `thread::current` function. -//! -//! Threads can be named, and provide some built-in support for low-level -//! synchronization (described below). -//! -//! The `thread::current()` function is available even for threads not spawned -//! by the APIs of this module. -//! //! ## Spawning a thread //! //! A new thread can be spawned using the `thread::spawn` function: @@ -99,6 +86,18 @@ //! }); //! ``` //! +//! ## The `Thread` type +//! +//! Threads are represented via the `Thread` type, which you can get in one of +//! two ways: +//! +//! * By spawning a new thread, e.g. using the `thread::spawn` function, and +//! calling `thread()` on the `JoinHandle`. +//! * By requesting the current thread, using the `thread::current` function. +//! +//! The `thread::current()` function is available even for threads not spawned +//! by the APIs of this module. +//! //! ## Blocking support: park and unpark //! //! Every thread is equipped with some basic low-level blocking support, via the From b75f81c9b3f996100c72f9141dcf6161f8fc90f4 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Mon, 2 May 2016 08:45:38 +0200 Subject: [PATCH 07/18] parser: do not try to continue with `unsafe` on foreign fns The changed line makes it look like `unsafe` is allowed, but the first statement of `parse_item_foreign_fn` is: `self.expect_keyword(keywords::Fn)?;` So we get the strange "expected one of `fn`, `pub`, `static`, or `unsafe`, found `unsafe`". Fixes: #27361 --- src/libsyntax/parse/parser.rs | 2 +- src/test/parse-fail/extern-no-fn.rs | 2 +- src/test/parse-fail/removed-syntax-extern-const.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 671a11b57dec1..c22a36739d678 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6025,7 +6025,7 @@ impl<'a> Parser<'a> { // FOREIGN STATIC ITEM return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?)); } - if self.check_keyword(keywords::Fn) || self.check_keyword(keywords::Unsafe) { + if self.check_keyword(keywords::Fn) { // FOREIGN FUNCTION ITEM return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?)); } diff --git a/src/test/parse-fail/extern-no-fn.rs b/src/test/parse-fail/extern-no-fn.rs index bf5cbe0c4592a..acf7187cf436f 100644 --- a/src/test/parse-fail/extern-no-fn.rs +++ b/src/test/parse-fail/extern-no-fn.rs @@ -11,7 +11,7 @@ // compile-flags: -Z parse-only extern { - f(); //~ ERROR expected one of `fn`, `pub`, `static`, `unsafe`, or `}`, found `f` + f(); //~ ERROR expected one of `fn`, `pub`, `static`, or `}`, found `f` } fn main() { diff --git a/src/test/parse-fail/removed-syntax-extern-const.rs b/src/test/parse-fail/removed-syntax-extern-const.rs index c42fae71237da..e632af6c83b1d 100644 --- a/src/test/parse-fail/removed-syntax-extern-const.rs +++ b/src/test/parse-fail/removed-syntax-extern-const.rs @@ -12,5 +12,5 @@ extern { const i: isize; - //~^ ERROR expected one of `fn`, `pub`, `static`, `unsafe`, or `}`, found `const` + //~^ ERROR expected one of `fn`, `pub`, `static`, or `}`, found `const` } From a11ddb3727ffa9f232f6bfbe69f4369bf8eafb39 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Wed, 4 May 2016 13:05:21 -0400 Subject: [PATCH 08/18] Replace copy-pasted variable name with relevant one --- src/libcollections/slice.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 588ad7a319ac3..0f77ebb3c5745 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -419,8 +419,8 @@ impl [T] { /// /// ```rust /// let v = &[1, 2, 3, 4, 5]; - /// for win in v.chunks(2) { - /// println!("{:?}", win); + /// for chunk in v.chunks(2) { + /// println!("{:?}", chunk); /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] From 2ca31205f3688f452eafccf0337fe381d9db8457 Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Wed, 4 May 2016 21:50:51 +0200 Subject: [PATCH 09/18] errors in the doc --- src/libcore/iter/iterator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 2033ae58d3804..c13512a399ad8 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -214,7 +214,7 @@ pub trait Iterator { /// Like most indexing operations, the count starts from zero, so `nth(0)` /// returns the first value, `nth(1)` the second, and so on. /// - /// `nth()` will return `None` if `n` is larger than the length of the + /// `nth()` will return `None` if `n` >= the length of the /// iterator. /// /// # Examples @@ -237,7 +237,7 @@ pub trait Iterator { /// assert_eq!(iter.nth(1), None); /// ``` /// - /// Returning `None` if there are less than `n` elements: + /// Returning `None` if there are less than `n + 1` elements: /// /// ``` /// let a = [1, 2, 3]; From 16219deb5c87bcc85f4c28dff7d83705abbcfe19 Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Wed, 4 May 2016 23:29:28 +0200 Subject: [PATCH 10/18] Update iterator.rs --- src/libcore/iter/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index c13512a399ad8..b80f77c0d25a9 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -214,7 +214,7 @@ pub trait Iterator { /// Like most indexing operations, the count starts from zero, so `nth(0)` /// returns the first value, `nth(1)` the second, and so on. /// - /// `nth()` will return `None` if `n` >= the length of the + /// `nth()` will return `None` if `n` is greater than or equal to the length of the /// iterator. /// /// # Examples From 3371b8a0c7aee247140daa02fa22542639e8f51c Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 3 May 2016 11:56:25 -0700 Subject: [PATCH 11/18] Add detailed error explanation for E0504 Removed unnecessary use of threads from E0504 Cleaned up line ending on E0504 Added more examples for E0504 Changed to erroneous code wording Switched Rc example to thread/Arc example Added comments describing why errors no longer occur --- src/librustc_borrowck/diagnostics.rs | 104 ++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index e838921a83178..93bbdf49d34c5 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -386,6 +386,109 @@ fn foo(a: &mut i32) { let bar = || { inside_closure(a) }; +``` +"##, + +E0504: r##" +This error occurs when an attempt is made to move a borrowed variable into a +closure. + +Example of erroneous code: + +```compile_fail +struct FancyNum { + num: u8 +} + +fn main() { + let fancy_num = FancyNum { num: 5 }; + let fancy_ref = &fancy_num; + + let x = move || { + println!("child function: {}", fancy_num.num); + // error: cannot move `fancy_num` into closure because it is borrowed + }; + + x(); + println!("main function: {}", fancy_ref.num); +} +``` + +Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into +the closure `x`. There is no way to move a value into a closure while it is +borrowed, as that would invalidate the borrow. + +If the closure can't outlive the value being moved, try using a reference +rather than moving: + +``` +struct FancyNum { + num: u8 +} + +fn main() { + let fancy_num = FancyNum { num: 5 }; + let fancy_ref = &fancy_num; + + let x = move || { + // fancy_ref is usable here because it doesn't move `fancy_num` + println!("child function: {}", fancy_ref.num); + }; + + x(); + + println!("main function: {}", fancy_num.num); +} +``` + +If the value has to be borrowed and then moved, try limiting the lifetime of +the borrow using a scoped block: + +``` +struct FancyNum { + num: u8 +} + +fn main() { + let fancy_num = FancyNum { num: 5 }; + + { + let fancy_ref = &fancy_num; + println!("main function: {}", fancy_ref.num); + // `fancy_ref` goes out of scope here + } + + let x = move || { + // `fancy_num` can be moved now (no more references exist) + println!("child function: {}", fancy_num.num); + }; + + x(); +} +``` + +If the lifetime of a reference isn't enough, such as in the case of threading, +consider using an `Arc` to create a reference-counted value: + +``` +use std::sync::Arc; +use std::thread; + +struct FancyNum { + num: u8 +} + +fn main() { + let fancy_ref1 = Arc::new(FancyNum { num: 5 }); + let fancy_ref2 = fancy_ref1.clone(); + + let x = thread::spawn(move || { + // `fancy_ref1` can be moved and has a `'static` lifetime + println!("child thread: {}", fancy_ref1.num); + }); + + x.join().expect("child thread should finish"); + println!("main thread: {}", fancy_ref2.num); } ``` "##, @@ -514,7 +617,6 @@ register_diagnostics! { E0500, // closure requires unique access to `..` but .. is already borrowed E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ... E0503, // cannot use `..` because it was mutably borrowed - E0504, // cannot move `..` into closure because it is borrowed E0505, // cannot move out of `..` because it is borrowed E0506, // cannot assign to `..` because it is borrowed E0508, // cannot move out of type `..`, a non-copy fixed-size array From d1c487e6c738ebaee576c03f711ed26a1117d7f1 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 5 May 2016 08:23:24 +0200 Subject: [PATCH 12/18] Add an example to Wrapping's documentation. --- src/libcore/num/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 589ac90b308ad..af4ac482cf7d0 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -37,6 +37,17 @@ use slice::SliceExt; /// `wrapping_add`, or through the `Wrapping` type, which says that /// all standard arithmetic operations on the underlying value are /// intended to have wrapping semantics. +/// +/// # Examples +/// +/// ``` +/// use std::num::Wrapping; +/// +/// let zero = Wrapping(0u32); +/// let one = Wrapping(1u32); +/// +/// assert_eq!(std::u32::MAX, (zero - one).0); +/// ``` #[stable(feature = "rust1", since = "1.0.0")] #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)] pub struct Wrapping(#[stable(feature = "rust1", since = "1.0.0")] pub T); From a22ca2872ef6782306012e6817dc4b8b778c43e9 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 5 May 2016 14:23:43 +0200 Subject: [PATCH 13/18] [Doc] Default cpu is "generic" (and not "default") --- src/librustc_back/target/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index 3f75201aad2cc..0f40de56e0279 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -201,7 +201,7 @@ pub struct TargetOptions { pub post_link_args: Vec, /// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults - /// to "default". + /// to "generic". pub cpu: String, /// Default target features to pass to LLVM. These features will *always* be /// passed, and cannot be disabled even via `-C`. Corresponds to `llc From 39eec8071c5b2920203c1ba98943efe2b1007e62 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 1 May 2016 10:40:01 -0700 Subject: [PATCH 14/18] mk: Fix building with --enable-ccache We will no longer use `ccache` in the makefiles for our local dependencies like miniz, but they're so small anyway it doesn't really matter. Closes #33285 --- mk/platform.mk | 6 +++--- mk/tests.mk | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mk/platform.mk b/mk/platform.mk index 59c8f7726c92f..c2644621c571a 100644 --- a/mk/platform.mk +++ b/mk/platform.mk @@ -169,7 +169,7 @@ ifdef CFG_CCACHE_BASEDIR export CCACHE_BASEDIR endif -FIND_COMPILER = $(word 1,$(1:ccache=)) +FIND_COMPILER = $(strip $(1:ccache=)) define CFG_MAKE_TOOLCHAIN # Prepend the tools with their prefix if cross compiling @@ -187,7 +187,7 @@ define CFG_MAKE_TOOLCHAIN endif endif - CFG_COMPILE_C_$(1) = '$$(CC_$(1))' \ + CFG_COMPILE_C_$(1) = '$$(call FIND_COMPILER,$$(CC_$(1)))' \ $$(CFLAGS) \ $$(CFG_GCCISH_CFLAGS) \ $$(CFG_GCCISH_CFLAGS_$(1)) \ @@ -198,7 +198,7 @@ define CFG_MAKE_TOOLCHAIN $$(CFG_GCCISH_LINK_FLAGS_$(1)) \ $$(CFG_GCCISH_DEF_FLAG_$(1))$$(3) $$(2) \ $$(call CFG_INSTALL_NAME_$(1),$$(4)) - CFG_COMPILE_CXX_$(1) = '$$(CXX_$(1))' \ + CFG_COMPILE_CXX_$(1) = '$$(call FIND_COMPILER,$$(CXX_$(1)))' \ $$(CXXFLAGS) \ $$(CFG_GCCISH_CFLAGS) \ $$(CFG_GCCISH_CXXFLAGS) \ diff --git a/mk/tests.mk b/mk/tests.mk index 90a7888af09c7..f2f3290ba6825 100644 --- a/mk/tests.mk +++ b/mk/tests.mk @@ -636,8 +636,8 @@ CTEST_COMMON_ARGS$(1)-T-$(2)-H-$(3) := \ --host-rustcflags "$(RUSTC_FLAGS_$(3)) $$(CTEST_RUSTC_FLAGS) -L $$(RT_OUTPUT_DIR_$(3))" \ --lldb-python-dir=$(CFG_LLDB_PYTHON_DIR) \ --target-rustcflags "$(RUSTC_FLAGS_$(2)) $$(CTEST_RUSTC_FLAGS) -L $$(RT_OUTPUT_DIR_$(2))" \ - --cc '$$(CC_$(2))' \ - --cxx '$$(CXX_$(2))' \ + --cc '$$(call FIND_COMPILER,$$(CC_$(2)))' \ + --cxx '$$(call FIND_COMPILER,$$(CXX_$(2)))' \ --cflags "$$(CFG_GCCISH_CFLAGS_$(2))" \ --llvm-components "$$(LLVM_ALL_COMPONENTS_$(2))" \ --llvm-cxxflags "$$(LLVM_CXXFLAGS_$(2))" \ From 2912bfb2b94c302e7395e49d8683356be5b9103e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 5 May 2016 18:20:31 +0000 Subject: [PATCH 15/18] doc: Update reference with better description of target_env The definition of this value recently changed slightly. It no longer corresponds directly to the target triple. Also shuffled things around to make the order of cfg descriptions more logical and added text related them to the target triple. cc #33403 --- src/doc/reference.md | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/doc/reference.md b/src/doc/reference.md index fcf9aefaba847..397abfe563c1f 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -2063,33 +2063,41 @@ arbitrarily complex configurations through nesting. The following configurations must be defined by the implementation: -* `debug_assertions` - Enabled by default when compiling without optimizations. - This can be used to enable extra debugging code in development but not in - production. For example, it controls the behavior of the standard library's - `debug_assert!` macro. -* `target_arch = "..."` - Target CPU architecture, such as `"x86"`, `"x86_64"` - `"mips"`, `"powerpc"`, `"powerpc64"`, `"arm"`, or `"aarch64"`. -* `target_endian = "..."` - Endianness of the target CPU, either `"little"` or - `"big"`. -* `target_env = ".."` - An option provided by the compiler by default - describing the runtime environment of the target platform. Some examples of - this are `musl` for builds targeting the MUSL libc implementation, `msvc` for - Windows builds targeting MSVC, and `gnu` frequently the rest of the time. This - option may also be blank on some platforms. +* `target_arch = "..."` - Target CPU architecture, such as `"x86"`, + `"x86_64"` `"mips"`, `"powerpc"`, `"powerpc64"`, `"arm"`, or + `"aarch64"`. This value is closely related to the first element of + the platform target triple, though it is not identical. +* `target_os = "..."` - Operating system of the target, examples + include `"windows"`, `"macos"`, `"ios"`, `"linux"`, `"android"`, + `"freebsd"`, `"dragonfly"`, `"bitrig"` , `"openbsd"` or + `"netbsd"`. This value is closely related to the second and third + element of the platform target triple, though it is not identical. * `target_family = "..."` - Operating system family of the target, e. g. `"unix"` or `"windows"`. The value of this configuration option is defined as a configuration itself, like `unix` or `windows`. -* `target_os = "..."` - Operating system of the target, examples include - `"windows"`, `"macos"`, `"ios"`, `"linux"`, `"android"`, `"freebsd"`, `"dragonfly"`, - `"bitrig"` , `"openbsd"` or `"netbsd"`. +* `unix` - See `target_family`. +* `windows` - See `target_family`. +* `target_env = ".."` - Further disambiguates the target platform with + information about the ABI/libc. Presently this value is either + `"gnu"`, `"msvc"`, `"musl"`, or the empty string. For historical + reasons this value has only been defined as non-empty when needed + for disambiguation. Thus on many GNU platforms this value will be + empty. This value is closely related to the fourth element of the + platform target triple, though it is not identical. For example, + embedded ABIs such as `gnueabihf` will simply define `target_env` as + `"gnu"`. +* `target_endian = "..."` - Endianness of the target CPU, either `"little"` or + `"big"`. * `target_pointer_width = "..."` - Target pointer width in bits. This is set to `"32"` for targets with 32-bit pointers, and likewise set to `"64"` for 64-bit pointers. * `target_vendor = "..."` - Vendor of the target, for example `apple`, `pc`, or simply `"unknown"`. * `test` - Enabled when compiling the test harness (using the `--test` flag). -* `unix` - See `target_family`. -* `windows` - See `target_family`. +* `debug_assertions` - Enabled by default when compiling without optimizations. + This can be used to enable extra debugging code in development but not in + production. For example, it controls the behavior of the standard library's + `debug_assert!` macro. You can also set another attribute based on a `cfg` variable with `cfg_attr`: From 26eb2bef2591ddb4cdf45d256cdcfa7c7353b0fc Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 5 May 2016 21:11:41 +0200 Subject: [PATCH 16/18] Fix some some duplicate words. --- src/bootstrap/build/job.rs | 2 +- src/librustc/session/config.rs | 2 +- src/librustc/traits/project.rs | 2 +- src/librustc/ty/trait_def.rs | 2 +- src/librustc/ty/wf.rs | 2 +- src/librustc_trans/intrinsic.rs | 2 +- src/libstd/net/ip.rs | 2 +- src/libstd/sys/unix/process.rs | 2 +- src/test/codegen-units/item-collection/cross-crate-closures.rs | 2 +- src/test/run-pass/regions-lub-ref-ref-rc.rs | 2 +- src/tools/linkchecker/main.rs | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/bootstrap/build/job.rs b/src/bootstrap/build/job.rs index a4e53bc45fcfb..4558e6f049432 100644 --- a/src/bootstrap/build/job.rs +++ b/src/bootstrap/build/job.rs @@ -54,7 +54,7 @@ pub unsafe fn setup() { // Indicate that when all handles to the job object are gone that all // process in the object should be killed. Note that this includes our - // entire process tree by default because we've added ourselves and and our + // entire process tree by default because we've added ourselves and our // children will reside in the job by default. let mut info = mem::zeroed::(); info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index b8dd750d3f1c2..1a2c1b9a09528 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1355,7 +1355,7 @@ pub mod nightly_options { early_error(ErrorOutputType::default(), &msg); } OptionStability::UnstableButNotReally => { - let msg = format!("the option `{}` is is unstable and should \ + let msg = format!("the option `{}` is unstable and should \ only be used on the nightly compiler, but \ it is currently accepted for backwards \ compatibility; this will soon change, \ diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs index a0d6f5f912b2c..7fb13f49cb4a5 100644 --- a/src/librustc/traits/project.rs +++ b/src/librustc/traits/project.rs @@ -40,7 +40,7 @@ use std::rc::Rc; pub enum ProjectionMode { /// FIXME (#32205) /// At coherence-checking time, we're still constructing the - /// specialization graph, and thus we only project project + /// specialization graph, and thus we only project /// non-`default` associated types that are defined directly in /// the applicable impl. (This behavior should be improved over /// time, to allow for successful projections modulo cycles diff --git a/src/librustc/ty/trait_def.rs b/src/librustc/ty/trait_def.rs index faae95e711699..f194afaa81762 100644 --- a/src/librustc/ty/trait_def.rs +++ b/src/librustc/ty/trait_def.rs @@ -176,7 +176,7 @@ impl<'tcx> TraitDef<'tcx> { /// Records a trait-to-implementation mapping for a non-local impl. /// /// The `parent_impl` is the immediately-less-specialized impl, or the - /// trait's def ID if the impl is is not a specialization -- information that + /// trait's def ID if the impl is not a specialization -- information that /// should be pulled from the metadata. pub fn record_remote_impl(&self, tcx: &TyCtxt<'tcx>, diff --git a/src/librustc/ty/wf.rs b/src/librustc/ty/wf.rs index f93332e07737d..7de83ef5cc920 100644 --- a/src/librustc/ty/wf.rs +++ b/src/librustc/ty/wf.rs @@ -268,7 +268,7 @@ impl<'a,'tcx> WfPredicates<'a,'tcx> { /// into `self.out`. fn compute_projection(&mut self, data: ty::ProjectionTy<'tcx>) { // A projection is well-formed if (a) the trait ref itself is - // WF WF and (b) the trait-ref holds. (It may also be + // WF and (b) the trait-ref holds. (It may also be // normalizable and be WF that way.) self.compute_trait_ref(&data.trait_ref); diff --git a/src/librustc_trans/intrinsic.rs b/src/librustc_trans/intrinsic.rs index 1220fbafa29c9..7c5ce371ee919 100644 --- a/src/librustc_trans/intrinsic.rs +++ b/src/librustc_trans/intrinsic.rs @@ -1327,7 +1327,7 @@ fn generate_filter_fn<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>, // %ret = call i32 @the_real_filter_function(%ehptrs, %arg) // ret i32 %ret // - // The recoverfp intrinsic is used to recover the frame frame pointer of the + // The recoverfp intrinsic is used to recover the frame pointer of the // `rust_try_fn` function, which is then in turn passed to the // `localrecover` intrinsic (pairing with the `localescape` intrinsic // mentioned above). Putting all this together means that we now have a diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index adceee6d73ec5..019241982502e 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -371,7 +371,7 @@ impl Ipv6Addr { } /// Returns true if this is an address reserved for documentation - /// This is defined to be 2001:db8::/32 in RFC RFC 3849 + /// This is defined to be 2001:db8::/32 in RFC 3849. pub fn is_documentation(&self) -> bool { (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8) } diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs index 270c2096b2c3b..0500480add22f 100644 --- a/src/libstd/sys/unix/process.rs +++ b/src/libstd/sys/unix/process.rs @@ -147,7 +147,7 @@ impl Command { let new_key = pair_to_key(key, val, &mut self.saw_nul); let (map, envp) = self.init_env_map(); - // If `key` is already present then we we just update `envp` in place + // If `key` is already present then we just update `envp` in place // (and store the owned value), but if it's not there we override the // trailing NULL pointer, add a new NULL pointer, and store where we // were located. diff --git a/src/test/codegen-units/item-collection/cross-crate-closures.rs b/src/test/codegen-units/item-collection/cross-crate-closures.rs index 30f3ef12d0743..546bb235a5f50 100644 --- a/src/test/codegen-units/item-collection/cross-crate-closures.rs +++ b/src/test/codegen-units/item-collection/cross-crate-closures.rs @@ -27,7 +27,7 @@ fn main() { //~ TRANS_ITEM fn cgu_extern_closures::inlined_fn_generic[0]::{{closure}}[0] let _ = cgu_extern_closures::inlined_fn_generic(3, 4, 5i32); - // Nothing should be generated for this call, we just link to the instance instance + // Nothing should be generated for this call, we just link to the instance // in the extern crate. let _ = cgu_extern_closures::non_inlined_fn(6, 7); } diff --git a/src/test/run-pass/regions-lub-ref-ref-rc.rs b/src/test/run-pass/regions-lub-ref-ref-rc.rs index 41c64197acbe8..ade742863a9da 100644 --- a/src/test/run-pass/regions-lub-ref-ref-rc.rs +++ b/src/test/run-pass/regions-lub-ref-ref-rc.rs @@ -9,7 +9,7 @@ // except according to those terms. // Test a corner case of LUB coercion. In this case, one arm of the -// match requires a deref coercion and other other doesn't, and there +// match requires a deref coercion and the other doesn't, and there // is an extra `&` on the `rc`. We want to be sure that the lifetime // assigned to this `&rc` value is not `'a` but something smaller. In // other words, the type from `rc` is `&'a Rc` and the type diff --git a/src/tools/linkchecker/main.rs b/src/tools/linkchecker/main.rs index 12419d4f7e5f7..a7c8c01fab850 100644 --- a/src/tools/linkchecker/main.rs +++ b/src/tools/linkchecker/main.rs @@ -110,7 +110,7 @@ fn walk(cache: &mut Cache, if let Some(pretty_path) = pretty_path { let entry = cache.get_mut(&pretty_path).unwrap(); // we don't need the source anymore, - // so drop to to reduce memory-usage + // so drop to reduce memory-usage entry.source = String::new(); } } From 10599e464107ccd7628e7189c286073f88b5063b Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 5 May 2016 21:15:15 +0200 Subject: [PATCH 17/18] doc: make RFC references consistent --- src/libstd/net/ip.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index adceee6d73ec5..87389299d9b37 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -89,7 +89,7 @@ impl Ipv4Addr { /// Returns true if this is a loopback address (127.0.0.0/8). /// - /// This property is defined by RFC 6890 + /// This property is defined by RFC 6890. #[stable(since = "1.7.0", feature = "ip_17")] pub fn is_loopback(&self) -> bool { self.octets()[0] == 127 @@ -97,7 +97,7 @@ impl Ipv4Addr { /// Returns true if this is a private address. /// - /// The private address ranges are defined in RFC1918 and include: + /// The private address ranges are defined in RFC 1918 and include: /// /// - 10.0.0.0/8 /// - 172.16.0.0/12 @@ -114,7 +114,7 @@ impl Ipv4Addr { /// Returns true if the address is link-local (169.254.0.0/16). /// - /// This property is defined by RFC 6890 + /// This property is defined by RFC 6890. #[stable(since = "1.7.0", feature = "ip_17")] pub fn is_link_local(&self) -> bool { self.octets()[0] == 169 && self.octets()[1] == 254 @@ -140,7 +140,7 @@ impl Ipv4Addr { /// Returns true if this is a multicast address. /// /// Multicast addresses have a most significant octet between 224 and 239, - /// and is defined by RFC 5771 + /// and is defined by RFC 5771. #[stable(since = "1.7.0", feature = "ip_17")] pub fn is_multicast(&self) -> bool { self.octets()[0] >= 224 && self.octets()[0] <= 239 @@ -354,7 +354,7 @@ impl Ipv6Addr { /// Returns true if this is a unique local address (IPv6). /// - /// Unique local addresses are defined in RFC4193 and have the form fc00::/7. + /// Unique local addresses are defined in RFC 4193 and have the form fc00::/7. pub fn is_unique_local(&self) -> bool { (self.segments()[0] & 0xfe00) == 0xfc00 } From 32edf1d7a81a0db65282374daf846727c8e2a8fd Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Fri, 6 May 2016 11:18:05 +0200 Subject: [PATCH 18/18] Fix Typo in Barrier::wait documentation This should be `have` instead of `has`. --- src/libstd/sync/barrier.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/sync/barrier.rs b/src/libstd/sync/barrier.rs index b543240c15afb..b1267acdee61a 100644 --- a/src/libstd/sync/barrier.rs +++ b/src/libstd/sync/barrier.rs @@ -71,7 +71,7 @@ impl Barrier { } } - /// Blocks the current thread until all threads has rendezvoused here. + /// Blocks the current thread until all threads have rendezvoused here. /// /// Barriers are re-usable after all threads have rendezvoused once, and can /// be used continuously.