From 1f80880467c6bab0903b7790c9b40a3d4c92a47c Mon Sep 17 00:00:00 2001 From: Jonathan Hansford Date: Thu, 23 Jul 2015 12:24:55 +0100 Subject: [PATCH 1/8] Added entries to explain expression-oriented languages Added definitions for 'Expression', 'Expression-Oriented Language' and 'Statement'. Sorted the definitions alphabetically. --- src/doc/trpl/glossary.md | 56 ++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/doc/trpl/glossary.md b/src/doc/trpl/glossary.md index c97da0e95b8..bd425145b96 100644 --- a/src/doc/trpl/glossary.md +++ b/src/doc/trpl/glossary.md @@ -3,24 +3,12 @@ Not every Rustacean has a background in systems programming, nor in computer science, so we've added explanations of terms that might be unfamiliar. -### Arity - -Arity refers to the number of arguments a function or operation takes. - -```rust -let x = (2, 3); -let y = (4, 6); -let z = (8, 2, 6); -``` - -In the example above `x` and `y` have arity 2. `z` has arity 3. - ### Abstract Syntax Tree -When a compiler is compiling your program, it does a number of different -things. One of the things that it does is turn the text of your program into an -‘abstract syntax tree’, or ‘AST’. This tree is a representation of the -structure of your program. For example, `2 + 3` can be turned into a tree: +When a compiler is compiling your program, it does a number of different things. +One of the things that it does is turn the text of your program into an +‘abstract syntax tree’, or ‘AST’. This tree is a representation of the structure +of your program. For example, `2 + 3` can be turned into a tree: ```text + @@ -37,3 +25,39 @@ And `2 + (3 * 4)` would look like this: / \ 3 4 ``` + +### Arity + +Arity refers to the number of arguments a function or operation takes. + +```rust +let x = (2, 3); +let y = (4, 6); +let z = (8, 2, 6); +``` + +In the example above `x` and `y` have arity 2. `z` has arity 3. + +### Expression + +In computer programming, an expression is a combination of values, constants, +variables, operators and functions that evaluate to a single value. For example, +`2 + (3 * 4)` is an expression that returns the value 14. It is worth noting +that expressions can have side-effects. For example, a function included in an +expression might perform actions other than simply returning a value. + +### Expression-Oriented Language + +In early programming languages [expressions] and [statements] were two separate +syntactic categories: expressions had a value and statements did things. +However, later languages blurred this distinction, allowing expressions to do +things and statements to have a value. In an expression-oriented language, +(nearly) every statement is an expression and therefore returns a value. + +[expressions]: glossary.html#expression +[statements]: glossary.html#statement + +### Statement + +In computer programming, a statement is the smallest standalone element of a +programming language that commands a computer to perform an action. From 30584b06efd06899f3cb99cffeef5ac61913bf22 Mon Sep 17 00:00:00 2001 From: Jonathan Hansford Date: Fri, 24 Jul 2015 09:01:34 +0100 Subject: [PATCH 2/8] Additional information on Expression-Oriented Languages Added the fact that expression statements can form part of larger expressions. --- src/doc/trpl/glossary.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/doc/trpl/glossary.md b/src/doc/trpl/glossary.md index bd425145b96..900211200af 100644 --- a/src/doc/trpl/glossary.md +++ b/src/doc/trpl/glossary.md @@ -53,6 +53,8 @@ syntactic categories: expressions had a value and statements did things. However, later languages blurred this distinction, allowing expressions to do things and statements to have a value. In an expression-oriented language, (nearly) every statement is an expression and therefore returns a value. +Consequently these expression statements can themselves form part of larger +expressions. [expressions]: glossary.html#expression [statements]: glossary.html#statement From bcee0a83b8ffd02269bd7cfa23bc1fc760719393 Mon Sep 17 00:00:00 2001 From: Jonathan Hansford Date: Fri, 24 Jul 2015 12:12:11 +0100 Subject: [PATCH 3/8] Added link in glossary to expression-oriented language Tidied up glossary.md and added link from hello-world.md to 'expression-oriented language' in glossary.md --- src/doc/trpl/glossary.md | 18 +++++++++--------- src/doc/trpl/hello-world.md | 11 +++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/doc/trpl/glossary.md b/src/doc/trpl/glossary.md index 900211200af..46d87e51f0d 100644 --- a/src/doc/trpl/glossary.md +++ b/src/doc/trpl/glossary.md @@ -48,16 +48,16 @@ expression might perform actions other than simply returning a value. ### Expression-Oriented Language -In early programming languages [expressions] and [statements] were two separate -syntactic categories: expressions had a value and statements did things. -However, later languages blurred this distinction, allowing expressions to do -things and statements to have a value. In an expression-oriented language, -(nearly) every statement is an expression and therefore returns a value. -Consequently these expression statements can themselves form part of larger -expressions. +In early programming languages [expressions][expression] and +[statements][statement] were two separate syntactic categories: expressions had +a value and statements did things. However, later languages blurred this +distinction, allowing expressions to do things and statements to have a value. +In an expression-oriented language, (nearly) every statement is an expression +and therefore returns a value. Consequently these expression statements can +themselves form part of larger expressions. -[expressions]: glossary.html#expression -[statements]: glossary.html#statement +[expression]: glossary.html#expression +[statement]: glossary.html#statement ### Statement diff --git a/src/doc/trpl/hello-world.md b/src/doc/trpl/hello-world.md index eec6fe62f22..cd4326a28d8 100644 --- a/src/doc/trpl/hello-world.md +++ b/src/doc/trpl/hello-world.md @@ -111,10 +111,13 @@ string to the screen. Easy enough! [allocation]: the-stack-and-the-heap.html -Finally, the line ends with a semicolon (`;`). Rust is an ‘expression oriented’ -language, which means that most things are expressions, rather than statements. -The `;` is used to indicate that this expression is over, and the next one is -ready to begin. Most lines of Rust code end with a `;`. +Finally, the line ends with a semicolon (`;`). Rust is an [‘expression oriented’ +language][expression-oriented language], which means that most things are +expressions, rather than statements. The `;` is used to indicate that this +expression is over, and the next one is ready to begin. Most lines of Rust code +end with a `;`. + +[expression-oriented language]: glossary.html#expression-oriented-language Finally, actually compiling and running our program. We can compile with our compiler, `rustc`, by passing it the name of our source file: From 8fee56777c2b88509e81e5e71773e1b588fee614 Mon Sep 17 00:00:00 2001 From: Jonathan Hansford Date: Sat, 25 Jul 2015 10:42:46 +0100 Subject: [PATCH 4/8] Commas added, as requested. --- src/doc/trpl/glossary.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/trpl/glossary.md b/src/doc/trpl/glossary.md index 46d87e51f0d..307aef80180 100644 --- a/src/doc/trpl/glossary.md +++ b/src/doc/trpl/glossary.md @@ -48,12 +48,12 @@ expression might perform actions other than simply returning a value. ### Expression-Oriented Language -In early programming languages [expressions][expression] and +In early programming languages, [expressions][expression] and [statements][statement] were two separate syntactic categories: expressions had a value and statements did things. However, later languages blurred this distinction, allowing expressions to do things and statements to have a value. In an expression-oriented language, (nearly) every statement is an expression -and therefore returns a value. Consequently these expression statements can +and therefore returns a value. Consequently, these expression statements can themselves form part of larger expressions. [expression]: glossary.html#expression From 91397a6aa33d16cb367f181a1174e80895e46780 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Tue, 28 Jul 2015 17:53:50 +0300 Subject: [PATCH 5/8] Replace occurences of illegal in user facing docs --- src/libcollections/fmt.rs | 20 +++++++++++--------- src/libcore/marker.rs | 2 +- src/libcore/num/mod.rs | 8 ++++---- src/librustc/diagnostics.rs | 6 +++--- src/librustc_resolve/diagnostics.rs | 2 +- src/librustc_typeck/diagnostics.rs | 6 +++--- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/libcollections/fmt.rs b/src/libcollections/fmt.rs index 7df259e9b36..3f284235431 100644 --- a/src/libcollections/fmt.rs +++ b/src/libcollections/fmt.rs @@ -85,9 +85,9 @@ //! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" //! ``` //! -//! It is illegal to put positional parameters (those without names) after -//! arguments which have names. Like with positional parameters, it is illegal -//! to provide named parameters that are unused by the format string. +//! It is not valid to put positional parameters (those without names) after +//! arguments which have names. Like with positional parameters, it is not +//! valid to provide named parameters that are unused by the format string. //! //! ## Argument types //! @@ -103,8 +103,9 @@ //! hexadecimal as well as an //! octal. //! -//! There are various parameters which do require a particular type, however. Namely, the `{:.*}` -//! syntax, which sets the number of numbers after the decimal in floating-point types: +//! There are various parameters which do require a particular type, however. +//! Namely, the `{:.*}` syntax, which sets the number of numbers after the +//! decimal in floating-point types: //! //! ``` //! let formatted_number = format!("{:.*}", 2, 1.234567); @@ -112,10 +113,11 @@ //! assert_eq!("1.23", formatted_number) //! ``` //! -//! If this syntax is used, then the number of characters to print precedes the actual object being -//! formatted, and the number of characters must have the type `usize`. Although a `usize` can be -//! printed with `{}`, it is illegal to reference an argument as such. For example this is another -//! invalid format string: +//! If this syntax is used, then the number of characters to print precedes the +//! actual object being formatted, and the number of characters must have the +//! type `usize`. Although a `usize` can be printed with `{}`, it is invalid to +//! reference an argument as such. For example this is another invalid format +//! string: //! //! ```text //! {:.*} {0} diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index dd60164a114..ebd6ba544e4 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -205,7 +205,7 @@ pub trait Copy : Clone { /// Any types with interior mutability must also use the `std::cell::UnsafeCell` /// wrapper around the value(s) which can be mutated when behind a `&` /// reference; not doing this is undefined behaviour (for example, -/// `transmute`-ing from `&T` to `&mut T` is illegal). +/// `transmute`-ing from `&T` to `&mut T` is invalid). #[stable(feature = "rust1", since = "1.0.0")] #[lang = "sync"] #[rustc_on_unimplemented = "`{Self}` cannot be shared between threads safely"] diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index bfbb2ded078..ad891bf8fa6 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -479,8 +479,8 @@ macro_rules! int_impl { /// wrapping around at the boundary of the type. /// /// Such wrap-around never actually occurs mathematically; - /// implementation artifacts make `x % y` illegal for `MIN / - /// -1` on a signed type illegal (where `MIN` is the negative + /// implementation artifacts make `x % y` invalid for `MIN / + /// -1` on a signed type (where `MIN` is the negative /// minimal value). In such a case, this function returns `0`. #[stable(feature = "num_wrapping", since = "1.2.0")] #[inline(always)] @@ -1051,8 +1051,8 @@ macro_rules! uint_impl { /// wrapping around at the boundary of the type. /// /// Such wrap-around never actually occurs mathematically; - /// implementation artifacts make `x % y` illegal for `MIN / - /// -1` on a signed type illegal (where `MIN` is the negative + /// implementation artifacts make `x % y` invalid for `MIN / + /// -1` on a signed type (where `MIN` is the negative /// minimal value). In such a case, this function returns `0`. #[stable(feature = "num_wrapping", since = "1.2.0")] #[inline(always)] diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 879a2d740cb..ed447d06a50 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -41,7 +41,7 @@ arms. "##, E0002: r##" -This error indicates that an empty match expression is illegal because the type +This error indicates that an empty match expression is invalid because the type it is matching on is non-empty (there exist values of this type). In safe code it is impossible to create an instance of an empty type, so empty match expressions are almost never desired. This error is typically fixed by adding @@ -1052,7 +1052,7 @@ because the `'static` lifetime is a special built-in lifetime name denoting the lifetime of the entire program, this is an error: ``` -// error, illegal lifetime parameter name `'static` +// error, invalid lifetime parameter name `'static` fn foo<'static>(x: &'static str) { } ``` "##, @@ -1802,7 +1802,7 @@ For more information about `const fn`'s, see [RFC 911]. E0394: r##" From [RFC 246]: - > It is illegal for a static to reference another static by value. It is + > It is invalid for a static to reference another static by value. It is > required that all references be borrowed. [RFC 246]: https://github.com/rust-lang/rfcs/pull/246 diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 9f8a5c90d4e..4c9a460d65a 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -106,7 +106,7 @@ mod foo { use foo::MyTrait::do_something; ``` -It's illegal to directly import methods belonging to a trait or concrete type. +It's invalid to directly import methods belonging to a trait or concrete type. "##, E0255: r##" diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 73ee3bbbe5b..eb3822db167 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -584,7 +584,7 @@ extern "C" { ``` Using this declaration, it must be called with at least one argument, so -simply calling `printf()` is illegal. But the following uses are allowed: +simply calling `printf()` is invalid. But the following uses are allowed: ``` unsafe { @@ -778,7 +778,7 @@ the pointer the size of the type would need to be unbounded. Consider the following erroneous definition of a type for a list of bytes: ``` -// error, illegal recursive struct type +// error, invalid recursive struct type struct ListNode { head: u8, tail: Option, @@ -2345,7 +2345,7 @@ register_diagnostics! { E0241, E0242, // internal error looking up a definition E0245, // not a trait - E0246, // illegal recursive type + E0246, // invalid recursive type E0247, // found module name used as a type E0248, // found value name used as a type E0319, // trait impls for defaulted traits allowed just for structs/enums From 64641985087e1aadfa2fcc1c849953b827a9fcd1 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 28 Jul 2015 16:22:05 -0700 Subject: [PATCH 6/8] std: Remove some old #[cfg(test) hacks Since most lang items are actually defined in core, these hack reexports don't actually do anything useful. --- src/libstd/lib.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 1e82a03f286..710c3b8075c 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -179,28 +179,23 @@ extern crate libc; #[macro_use] #[no_link] extern crate rustc_bitflags; -// Make std testable by not duplicating lang items. See #2912 +// Make std testable by not duplicating lang items and other globals. See #2912 #[cfg(test)] extern crate std as realstd; -#[cfg(test)] pub use realstd::marker; -#[cfg(test)] pub use realstd::ops; -#[cfg(test)] pub use realstd::cmp; -#[cfg(test)] pub use realstd::boxed; - // NB: These reexports are in the order they should be listed in rustdoc pub use core::any; pub use core::cell; pub use core::clone; -#[cfg(not(test))] pub use core::cmp; +pub use core::cmp; pub use core::convert; pub use core::default; pub use core::hash; pub use core::intrinsics; pub use core::iter; -#[cfg(not(test))] pub use core::marker; +pub use core::marker; pub use core::mem; -#[cfg(not(test))] pub use core::ops; +pub use core::ops; pub use core::ptr; pub use core::raw; pub use core::simd; @@ -208,7 +203,7 @@ pub use core::result; pub use core::option; pub mod error; -#[cfg(not(test))] pub use alloc::boxed; +pub use alloc::boxed; pub use alloc::rc; pub use core_collections::borrow; From a067b4588aabfab1de57829cdfe5d660733a158d Mon Sep 17 00:00:00 2001 From: Blake Loring Date: Wed, 29 Jul 2015 10:06:50 +0100 Subject: [PATCH 7/8] Modified to add 'found' to error message closes #26485 --- src/librustc/middle/ty.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index a3b67941741..88fc6181f92 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -44,7 +44,7 @@ use metadata::csearch; use middle; use middle::cast; use middle::check_const; -use middle::const_eval::{self, ConstVal}; +use middle::const_eval::{self, ConstVal, ErrKind}; use middle::const_eval::EvalHint::UncheckedExprHint; use middle::def::{self, DefMap, ExportMap}; use middle::dependency_format; @@ -6107,20 +6107,20 @@ impl<'tcx> ctxt<'tcx> { found); } Err(err) => { - let err_description = err.description(); - let found = match count_expr.node { + let err_msg = match count_expr.node { ast::ExprPath(None, ast::Path { global: false, ref segments, .. }) if segments.len() == 1 => - format!("{}", "found variable"), - _ => - format!("but {}", err_description), + format!("found variable"), + _ => match err.kind { + ErrKind::MiscCatchAll => format!("but found {}", err.description()), + _ => format!("but {}", err.description()) + } }; span_err!(self.sess, count_expr.span, E0307, - "expected constant integer for repeat count, {}", - found); + "expected constant integer for repeat count, {}", err_msg); } } 0 From 43b2c4781e5d6e25dedfc480218ceda92d9dffad Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 28 Jul 2015 21:13:02 -0700 Subject: [PATCH 8/8] std: Fix sub-second Condvar::wait_timeout_ms The API we're calling requires us to pass an absolute point in time as an argument (`pthread_cond_timedwait`) so we call `gettimeofday` ahead of time to then add the specified duration to. Unfortuantely the current "add the duration" logic forgot to take into account the current time's sub-second precision (e.g. the `tv_usec` field was ignored), causing sub-second duration waits to return spuriously. --- src/libstd/sys/unix/condvar.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/libstd/sys/unix/condvar.rs b/src/libstd/sys/unix/condvar.rs index c8708190a2e..beecb445e8d 100644 --- a/src/libstd/sys/unix/condvar.rs +++ b/src/libstd/sys/unix/condvar.rs @@ -60,21 +60,22 @@ impl Condvar { let r = ffi::gettimeofday(&mut sys_now, ptr::null_mut()); debug_assert_eq!(r, 0); + let nsec = dur.extra_nanos() as libc::c_long + + (sys_now.tv_usec * 1000) as libc::c_long; + let extra = (nsec / 1_000_000_000) as libc::time_t; + let nsec = nsec % 1_000_000_000; let seconds = dur.secs() as libc::time_t; - let timeout = match sys_now.tv_sec.checked_add(seconds) { - Some(sec) => { - libc::timespec { - tv_sec: sec, - tv_nsec: dur.extra_nanos() as libc::c_long, - } + + let timeout = sys_now.tv_sec.checked_add(extra).and_then(|s| { + s.checked_add(seconds) + }).map(|s| { + libc::timespec { tv_sec: s, tv_nsec: nsec } + }).unwrap_or_else(|| { + libc::timespec { + tv_sec: ::max_value(), + tv_nsec: 1_000_000_000 - 1, } - None => { - libc::timespec { - tv_sec: ::max_value(), - tv_nsec: 1_000_000_000 - 1, - } - } - }; + }); // And wait! let r = ffi::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex),