diff --git a/src/doc/index.md b/src/doc/index.md index 124a82aa7dc80..5a437e959b7fb 100644 --- a/src/doc/index.md +++ b/src/doc/index.md @@ -19,7 +19,7 @@ ideas behind Rust. donated to the Rust project. As the name implies, it teaches you Rust through a series of small examples. -[rbe]: rustbyexample.com +[rbe]: http://rustbyexample.com/ # Community & Getting Help diff --git a/src/doc/trpl/ffi.md b/src/doc/trpl/ffi.md index 23f6e17b860b2..2c5e6b2e5fc7c 100644 --- a/src/doc/trpl/ffi.md +++ b/src/doc/trpl/ffi.md @@ -166,7 +166,7 @@ pub fn uncompress(src: &[u8]) -> Option> { } ``` -For reference, the examples used here are also available as an [library on +For reference, the examples used here are also available as a [library on GitHub](https://github.com/thestinger/rust-snappy). # Destructors diff --git a/src/doc/trpl/hello-world.md b/src/doc/trpl/hello-world.md index 6c320d27ba654..e58bac656d0c5 100644 --- a/src/doc/trpl/hello-world.md +++ b/src/doc/trpl/hello-world.md @@ -104,10 +104,10 @@ We’ll get to the details eventually, you’ll just have to trust us for now. Next, `"Hello, world!"` is a ‘string’. Strings are a surprisingly complicated topic in a systems programming language, and this is a ‘statically allocated’ -string. If you want to read further about allocation, check out [the stack and -the heap], but you don’t need to right now if you don’t want to. We pass this -string as an argument to `println!`, which prints the string to the screen. -Easy enough! +string. If you want to read further about allocation, check out +[the stack and the heap][allocation], but you don’t need to right now if you +don’t want to. We pass this string as an argument to `println!`, which prints the +string to the screen. Easy enough! [allocation]: the-stack-and-the-heap.html diff --git a/src/doc/trpl/installing-rust.md b/src/doc/trpl/installing-rust.md index e4054a097989d..09b4495ffe990 100644 --- a/src/doc/trpl/installing-rust.md +++ b/src/doc/trpl/installing-rust.md @@ -90,8 +90,8 @@ If not, there are a number of places where you can get help. The easiest is [the #rust IRC channel on irc.mozilla.org][irc], which you can access through [Mibbit][mibbit]. Click that link, and you'll be chatting with other Rustaceans (a silly nickname we call ourselves), and we can help you out. Other great -resources include [the user’s forum][users], and [Stack Overflow][stack -overflow]. +resources include [the user’s forum][users], and +[Stack Overflow][stack overflow]. [irc]: irc://irc.mozilla.org/#rust [mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust diff --git a/src/doc/trpl/no-stdlib.md b/src/doc/trpl/no-stdlib.md index 094c82a08cc9d..67db919c59f45 100644 --- a/src/doc/trpl/no-stdlib.md +++ b/src/doc/trpl/no-stdlib.md @@ -103,7 +103,7 @@ necessary functionality for writing idiomatic and effective Rust code. As an example, here is a program that will calculate the dot product of two vectors provided from C, using idiomatic Rust practices. -``` +```ignore #![feature(lang_items, start, no_std, core, libc)] #![no_std] diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index ff176d815916b..0cfdcd2413fd9 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -274,7 +274,7 @@ pub fn get_mut(this: &mut Arc) -> Option<&mut T> { // reference to the inner data. let inner = unsafe { &mut **this._ptr }; Some(&mut inner.data) - }else { + } else { None } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 4a0706906ee9c..e44b0d1147cbc 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -273,8 +273,9 @@ pub trait Iterator { FilterMap { iter: self, f: f } } - /// Creates an iterator that yields a pair of the value returned by this - /// iterator plus the current index of iteration. + /// Creates an iterator that yields pairs `(i, val)` where `i` is the + /// current index of iteration and `val` is the value returned by the + /// iterator. /// /// `enumerate` keeps its count as a `usize`. If you want to count by a /// different sized integer, the `zip` function provides similar @@ -1129,7 +1130,7 @@ pub trait FromIterator { /// Conversion into an `Iterator` /// /// Implementing this trait allows you to use your type with Rust's `for` loop. See -/// the [module level documentation](../index.html) for more details. +/// the [module level documentation](index.html) for more details. #[stable(feature = "rust1", since = "1.0.0")] pub trait IntoIterator { /// The type of the elements being iterated diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 9a165a2e3173b..0e9570df09d55 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -15,9 +15,9 @@ //! Working with unsafe pointers in Rust is uncommon, //! typically limited to a few patterns. //! -//! Use the [`null` function](fn.null.html) to create null pointers, and -//! the `is_null` method of the `*const T` type to check for null. -//! The `*const T` type also defines the `offset` method, for pointer math. +//! Use the `null` function to create null pointers, and the `is_null` method +//! of the `*const T` type to check for null. The `*const T` type also defines +//! the `offset` method, for pointer math. //! //! # Common ways to create unsafe pointers //! diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index b15304d6dc50c..938a74382e20e 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -112,6 +112,100 @@ reference when using guards or refactor the entire expression, perhaps by putting the condition inside the body of the arm. "##, +E0162: r##" +An if-let pattern attempts to match the pattern, and enters the body if the +match was succesful. If the match is irrefutable (when it cannot fail to match), +use a regular `let`-binding instead. For instance: + +struct Irrefutable(i32); +let irr = Irrefutable(0); + +// This fails to compile because the match is irrefutable. +if let Irrefutable(x) = irr { + // This body will always be executed. + foo(x); +} + +// Try this instead: +let Irrefutable(x) = irr; +foo(x); +"##, + +E0165: r##" +A while-let pattern attempts to match the pattern, and enters the body if the +match was succesful. If the match is irrefutable (when it cannot fail to match), +use a regular `let`-binding inside a `loop` instead. For instance: + +struct Irrefutable(i32); +let irr = Irrefutable(0); + +// This fails to compile because the match is irrefutable. +while let Irrefutable(x) = irr { + ... +} + +// Try this instead: +loop { + let Irrefutable(x) = irr; + ... +} +"##, + +E0297: r##" +Patterns used to bind names must be irrefutable. That is, they must guarantee +that a name will be extracted in all cases. Instead of pattern matching the +loop variable, consider using a `match` or `if let` inside the loop body. For +instance: + +// This fails because `None` is not covered. +for Some(x) in xs { + ... +} + +// Match inside the loop instead: +for item in xs { + match item { + Some(x) => ... + None => ... + } +} + +// Or use `if let`: +for item in xs { + if let Some(x) = item { + ... + } +} +"##, + +E0301: r##" +Mutable borrows are not allowed in pattern guards, because matching cannot have +side effects. Side effects could alter the matched object or the environment +on which the match depends in such a way, that the match would not be +exhaustive. For instance, the following would not match any arm if mutable +borrows were allowed: + +match Some(()) { + None => { }, + option if option.take().is_none() => { /* impossible, option is `Some` */ }, + Some(_) => { } // When the previous match failed, the option became `None`. +} +"##, + +E0302: r##" +Assignments are not allowed in pattern guards, because matching cannot have +side effects. Side effects could alter the matched object or the environment +on which the match depends in such a way, that the match would not be +exhaustive. For instance, the following would not match any arm if assignments +were allowed: + +match Some(()) { + None => { }, + option if { option = None; false } { }, + Some(_) => { } // When the previous match failed, the option became `None`. +} +"##, + E0303: r##" In certain cases it is possible for sub-bindings to violate memory safety. Updates to the borrow checker in a future version of Rust may remove this @@ -165,8 +259,6 @@ register_diagnostics! { E0152, E0158, E0161, - E0162, - E0165, E0170, E0261, // use of undeclared lifetime name E0262, // illegal lifetime parameter name @@ -194,12 +286,9 @@ register_diagnostics! { E0284, // cannot resolve type E0285, // overflow evaluation builtin bounds E0296, // malformed recursion limit attribute - E0297, // refutable pattern in for loop binding E0298, // mismatched types between arms E0299, // mismatched types between arms E0300, // unexpanded macro - E0301, // cannot mutable borrow in a pattern guard - E0302, // cannot assign in a pattern guard E0304, // expected signed integer constant E0305, // expected constant E0306, // expected positive integer for repeat count diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs index 7b63e38b58598..4a77f80d3b846 100644 --- a/src/librustc/metadata/loader.rs +++ b/src/librustc/metadata/loader.rs @@ -129,7 +129,7 @@ //! > Dear rustc, //! > //! > When you are attempting to load the immediate dependency `crate-name`, I -//! > would like you too assume that the library is located at +//! > would like you to assume that the library is located at //! > `path/to/the/crate.rlib`, and look nowhere else. Also, please do not //! > assume that the path I specified has the name `crate-name`. //! diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index c1c05da4ee4b5..04513e9d048be 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -172,7 +172,7 @@ impl CString { /// /// This method is equivalent to `new` except that no runtime assertion /// is made that `v` contains no 0 bytes, and it requires an actual - /// byte vector, not anyhting that can be converted to one with Into. + /// byte vector, not anything that can be converted to one with Into. #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_vec_unchecked(mut v: Vec) -> CString { v.push(0); diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 6e902a4739623..d30d44a04d336 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -1099,8 +1099,8 @@ impl PathExt for Path { /// Changes the timestamps for a file's last modification and access time. /// /// The file at the path specified will have its last access time set to -/// `atime` and its modification time set to `mtime`. The times specified should -/// be in milliseconds. +/// `accessed` and its modification time set to `modified`. The times specified +/// should be in milliseconds. #[unstable(feature = "fs_time", reason = "the argument type of u64 is not quite appropriate for \ this function and may change if the standard library \ diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index f3ba5cd7854d9..93c6deac6e59a 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -148,7 +148,7 @@ pub trait Read { /// /// If the return value of this method is `Ok(n)`, then it must be /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates - /// that the buffer `buf` has ben filled in with `n` bytes of data from this + /// that the buffer `buf` has been filled in with `n` bytes of data from this /// source. If `n` is `0`, then it can indicate one of two scenarios: /// /// 1. This reader has reached its "end of file" and will likely no longer diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index 209a0032fb441..e48d0e6008b87 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -213,7 +213,7 @@ impl TcpListener { /// Returns an iterator over the connections being received on this /// listener. /// - /// The returned iterator will never returned `None` and will also not yield + /// The returned iterator will never return `None` and will also not yield /// the peer's `SocketAddr` structure. #[stable(feature = "rust1", since = "1.0.0")] pub fn incoming(&self) -> Incoming { diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index c8f6aca7bd32e..45f389f0aebf8 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -44,6 +44,8 @@ pub const WSA_WAIT_TIMEOUT: libc::DWORD = libc::consts::os::extra::WAIT_TIMEOUT; pub const WSA_WAIT_EVENT_0: libc::DWORD = libc::consts::os::extra::WAIT_OBJECT_0; pub const WSA_WAIT_FAILED: libc::DWORD = libc::consts::os::extra::WAIT_FAILED; pub const WSAESHUTDOWN: libc::c_int = 10058; +pub const WSA_FLAG_OVERLAPPED: libc::DWORD = 0x01; +pub const WSA_FLAG_NO_HANDLE_INHERIT: libc::DWORD = 0x80; pub const ERROR_NO_MORE_FILES: libc::DWORD = 18; pub const TOKEN_READ: libc::DWORD = 0x20008; @@ -463,6 +465,10 @@ extern "system" { nOutBufferSize: libc::DWORD, lpBytesReturned: libc::LPDWORD, lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL; + pub fn CreatePipe(hReadPipe: libc::LPHANDLE, + hWritePipe: libc::LPHANDLE, + lpPipeAttributes: libc::LPSECURITY_ATTRIBUTES, + nSize: libc::DWORD) -> libc::BOOL; } #[link(name = "userenv")] diff --git a/src/libstd/sys/windows/handle.rs b/src/libstd/sys/windows/handle.rs index 0089dcad455df..c3a30aae9e0e8 100644 --- a/src/libstd/sys/windows/handle.rs +++ b/src/libstd/sys/windows/handle.rs @@ -36,11 +36,34 @@ impl Handle { } pub fn read(&self, buf: &mut [u8]) -> io::Result { - read(self.0, buf) + let mut read = 0; + let res = cvt(unsafe { + libc::ReadFile(self.0, buf.as_ptr() as libc::LPVOID, + buf.len() as libc::DWORD, &mut read, + ptr::null_mut()) + }); + + match res { + Ok(_) => Ok(read as usize), + + // The special treatment of BrokenPipe is to deal with Windows + // pipe semantics, which yields this error when *reading* from + // a pipe after the other end has closed; we interpret that as + // EOF on the pipe. + Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(0), + + Err(e) => Err(e) + } } pub fn write(&self, buf: &[u8]) -> io::Result { - write(self.0, buf) + let mut amt = 0; + try!(cvt(unsafe { + libc::WriteFile(self.0, buf.as_ptr() as libc::LPVOID, + buf.len() as libc::DWORD, &mut amt, + ptr::null_mut()) + })); + Ok(amt as usize) } } @@ -49,35 +72,3 @@ impl Drop for Handle { unsafe { let _ = libc::CloseHandle(self.0); } } } - - -pub fn read(h: HANDLE, buf: &mut [u8]) -> io::Result { - let mut read = 0; - let res = cvt(unsafe { - libc::ReadFile(h, buf.as_ptr() as libc::LPVOID, - buf.len() as libc::DWORD, &mut read, - ptr::null_mut()) - }); - - match res { - Ok(_) => Ok(read as usize), - - // The special treatment of BrokenPipe is to deal with Windows - // pipe semantics, which yields this error when *reading* from - // a pipe after the other end has closed; we interpret that as - // EOF on the pipe. - Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(0), - - Err(e) => Err(e) - } -} - -pub fn write(h: HANDLE, buf: &[u8]) -> io::Result { - let mut amt = 0; - try!(cvt(unsafe { - libc::WriteFile(h, buf.as_ptr() as libc::LPVOID, - buf.len() as libc::DWORD, &mut amt, - ptr::null_mut()) - })); - Ok(amt as usize) -} diff --git a/src/libstd/sys/windows/net.rs b/src/libstd/sys/windows/net.rs index 5ced8863e62a9..cbc3876dbb116 100644 --- a/src/libstd/sys/windows/net.rs +++ b/src/libstd/sys/windows/net.rs @@ -80,7 +80,11 @@ impl Socket { SocketAddr::V4(..) => libc::AF_INET, SocketAddr::V6(..) => libc::AF_INET6, }; - match unsafe { libc::socket(fam, ty, 0) } { + let socket = unsafe { + c::WSASocketW(fam, ty, 0, 0 as *mut _, 0, + c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT) + }; + match socket { INVALID_SOCKET => Err(last_error()), n => Ok(Socket(n)), } @@ -103,7 +107,9 @@ impl Socket { match c::WSASocketW(info.iAddressFamily, info.iSocketType, info.iProtocol, - &mut info, 0, 0) { + &mut info, 0, + c::WSA_FLAG_OVERLAPPED | + c::WSA_FLAG_NO_HANDLE_INHERIT) { INVALID_SOCKET => Err(last_error()), n => Ok(Socket(n)), } diff --git a/src/libstd/sys/windows/pipe2.rs b/src/libstd/sys/windows/pipe2.rs index ed41c95978289..b441d8beedbc0 100644 --- a/src/libstd/sys/windows/pipe2.rs +++ b/src/libstd/sys/windows/pipe2.rs @@ -10,70 +10,39 @@ use prelude::v1::*; -use sys::handle; use io; -use libc::{self, c_int, HANDLE}; +use libc; +use sys::cvt; +use sys::c; +use sys::handle::Handle; //////////////////////////////////////////////////////////////////////////////// // Anonymous pipes //////////////////////////////////////////////////////////////////////////////// pub struct AnonPipe { - fd: c_int + inner: Handle, } pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> { - // Windows pipes work subtly differently than unix pipes, and their - // inheritance has to be handled in a different way that I do not - // fully understand. Here we explicitly make the pipe non-inheritable, - // which means to pass it to a subprocess they need to be duplicated - // first, as in std::run. - let mut fds = [0; 2]; - unsafe { - match libc::pipe(fds.as_mut_ptr(), 1024 as ::libc::c_uint, - (libc::O_BINARY | libc::O_NOINHERIT) as c_int) { - 0 => { - assert!(fds[0] != -1 && fds[0] != 0); - assert!(fds[1] != -1 && fds[1] != 0); - - Ok((AnonPipe::from_fd(fds[0]), AnonPipe::from_fd(fds[1]))) - } - _ => Err(io::Error::last_os_error()), - } - } + let mut reader = libc::INVALID_HANDLE_VALUE; + let mut writer = libc::INVALID_HANDLE_VALUE; + try!(cvt(unsafe { + c::CreatePipe(&mut reader, &mut writer, 0 as *mut _, 0) + })); + let reader = Handle::new(reader); + let writer = Handle::new(writer); + Ok((AnonPipe { inner: reader }, AnonPipe { inner: writer })) } impl AnonPipe { - pub fn from_fd(fd: libc::c_int) -> AnonPipe { - AnonPipe { fd: fd } - } - - pub fn raw(&self) -> HANDLE { - unsafe { libc::get_osfhandle(self.fd) as libc::HANDLE } - } + pub fn handle(&self) -> &Handle { &self.inner } pub fn read(&self, buf: &mut [u8]) -> io::Result { - handle::read(self.raw(), buf) + self.inner.read(buf) } pub fn write(&self, buf: &[u8]) -> io::Result { - handle::write(self.raw(), buf) - } -} - -impl Drop for AnonPipe { - fn drop(&mut self) { - // closing stdio file handles makes no sense, so never do it. Also, note - // that errors are ignored when closing a file descriptor. The reason - // for this is that if an error occurs we don't actually know if the - // file descriptor was closed or not, and if we retried (for something - // like EINTR), we might close another valid file descriptor (opened - // after we closed ours. - if self.fd > libc::STDERR_FILENO { - let n = unsafe { libc::close(self.fd) }; - if n != 0 { - println!("error {} when closing file descriptor {}", n, self.fd); - } - } + self.inner.write(buf) } } diff --git a/src/libstd/sys/windows/process2.rs b/src/libstd/sys/windows/process2.rs index 7495392192170..fdb47eb8c8084 100644 --- a/src/libstd/sys/windows/process2.rs +++ b/src/libstd/sys/windows/process2.rs @@ -199,7 +199,7 @@ impl Process { } } Stdio::Piped(ref pipe) => { - let orig = pipe.raw(); + let orig = pipe.handle().raw(); if DuplicateHandle(cur_proc, orig, cur_proc, slot, 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE { return Err(Error::last_os_error()) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index a6f8a718b3381..659eb34323259 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -399,7 +399,7 @@ impl<'a> Context<'a> { } else { self.gate_feature("custom_attribute", attr.span, &format!("The attribute `{}` is currently \ - unknown to the the compiler and \ + unknown to the compiler and \ may have meaning \ added to it in the future", name)); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e45b7c1df91ca..bef2068f0ddcb 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1972,7 +1972,7 @@ impl<'a> Parser<'a> { } try!(self.bump()); - hi = self.span.hi; + hi = self.last_span.hi; return if es.len() == 1 && !trailing_comma { Ok(self.mk_expr(lo, hi, ExprParen(es.into_iter().nth(0).unwrap()))) } else { diff --git a/src/test/parse-fail/issue-24197.rs b/src/test/parse-fail/issue-24197.rs new file mode 100644 index 0000000000000..37d6218261234 --- /dev/null +++ b/src/test/parse-fail/issue-24197.rs @@ -0,0 +1,13 @@ +// Copyright 2015 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. + +fn main() { + let buf[0] = 0; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[` +} diff --git a/src/test/parse-fail/issue-24375.rs b/src/test/parse-fail/issue-24375.rs new file mode 100644 index 0000000000000..8723423325c19 --- /dev/null +++ b/src/test/parse-fail/issue-24375.rs @@ -0,0 +1,19 @@ +// Copyright 2015 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. + +static tmp : [&'static str; 2] = ["hello", "he"]; + +fn main() { + let z = "hello"; + match z { + tmp[0] => {} //~ ERROR expected one of `=>`, `@`, `if`, or `|`, found `[` + _ => {} + } +} diff --git a/src/test/run-pass/method-two-trait-defer-resolution-2.rs b/src/test/run-pass/method-two-trait-defer-resolution-2.rs index e1b64f250d8e1..cf9bc9bb56a89 100644 --- a/src/test/run-pass/method-two-trait-defer-resolution-2.rs +++ b/src/test/run-pass/method-two-trait-defer-resolution-2.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Test that when we write `x.foo()`, we do nothave to know the +// Test that when we write `x.foo()`, we do not have to know the // complete type of `x` in order to type-check the method call. In // this case, we know that `x: Vec<_1>`, but we don't know what type // `_1` is (because the call to `push` comes later). To pick between