From 3b92689f3d0c3b90fa01d9873cdf01543d51c000 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Thu, 26 Dec 2019 13:54:33 -0500 Subject: Relax bounds on HashMap to match hashbrown No functional changes are made, and all APIs are moved to strictly less restrictive bounds. These APIs changed from the old bound listed to no trait bounds: K: Hash + Eq * new * with_capacity K: Eq + Hash, S: BuildHasher * with_hasher * with_capacity_and_hasher * hasher K: Eq + Hash + Debug -> K: Debug S: BuildHasher -> S K: Eq + Hash -> K S: BuildHasher + Default -> S: Default --- src/libstd/collections/hash/map.rs | 126 ++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 64 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index a928867d9de..84e1cee5d66 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -203,7 +203,7 @@ pub struct HashMap { base: base::HashMap, } -impl HashMap { +impl HashMap { /// Creates an empty `HashMap`. /// /// The hash map is initially created with a capacity of 0, so it will not allocate until it @@ -240,6 +240,59 @@ impl HashMap { } impl HashMap { + /// Creates an empty `HashMap` which will use the given hash builder to hash + /// keys. + /// + /// The created map has the default initial capacity. + /// + /// Warning: `hash_builder` is normally randomly generated, and + /// is designed to allow HashMaps to be resistant to attacks that + /// cause many collisions and very poor performance. Setting it + /// manually using this function can expose a DoS attack vector. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashMap; + /// use std::collections::hash_map::RandomState; + /// + /// let s = RandomState::new(); + /// let mut map = HashMap::with_hasher(s); + /// map.insert(1, 2); + /// ``` + #[inline] + #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] + pub fn with_hasher(hash_builder: S) -> HashMap { + HashMap { base: base::HashMap::with_hasher(hash_builder) } + } + + /// Creates an empty `HashMap` with the specified capacity, using `hash_builder` + /// to hash the keys. + /// + /// The hash map will be able to hold at least `capacity` elements without + /// reallocating. If `capacity` is 0, the hash map will not allocate. + /// + /// Warning: `hash_builder` is normally randomly generated, and + /// is designed to allow HashMaps to be resistant to attacks that + /// cause many collisions and very poor performance. Setting it + /// manually using this function can expose a DoS attack vector. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashMap; + /// use std::collections::hash_map::RandomState; + /// + /// let s = RandomState::new(); + /// let mut map = HashMap::with_capacity_and_hasher(10, s); + /// map.insert(1, 2); + /// ``` + #[inline] + #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] + pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap { + HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hash_builder) } + } + /// Returns the number of elements the map can hold without reallocating. /// /// This number is a lower bound; the `HashMap` might be able to hold @@ -457,65 +510,6 @@ impl HashMap { pub fn clear(&mut self) { self.base.clear(); } -} - -impl HashMap -where - K: Eq + Hash, - S: BuildHasher, -{ - /// Creates an empty `HashMap` which will use the given hash builder to hash - /// keys. - /// - /// The created map has the default initial capacity. - /// - /// Warning: `hash_builder` is normally randomly generated, and - /// is designed to allow HashMaps to be resistant to attacks that - /// cause many collisions and very poor performance. Setting it - /// manually using this function can expose a DoS attack vector. - /// - /// # Examples - /// - /// ``` - /// use std::collections::HashMap; - /// use std::collections::hash_map::RandomState; - /// - /// let s = RandomState::new(); - /// let mut map = HashMap::with_hasher(s); - /// map.insert(1, 2); - /// ``` - #[inline] - #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] - pub fn with_hasher(hash_builder: S) -> HashMap { - HashMap { base: base::HashMap::with_hasher(hash_builder) } - } - - /// Creates an empty `HashMap` with the specified capacity, using `hash_builder` - /// to hash the keys. - /// - /// The hash map will be able to hold at least `capacity` elements without - /// reallocating. If `capacity` is 0, the hash map will not allocate. - /// - /// Warning: `hash_builder` is normally randomly generated, and - /// is designed to allow HashMaps to be resistant to attacks that - /// cause many collisions and very poor performance. Setting it - /// manually using this function can expose a DoS attack vector. - /// - /// # Examples - /// - /// ``` - /// use std::collections::HashMap; - /// use std::collections::hash_map::RandomState; - /// - /// let s = RandomState::new(); - /// let mut map = HashMap::with_capacity_and_hasher(10, s); - /// map.insert(1, 2); - /// ``` - #[inline] - #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] - pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap { - HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hash_builder) } - } /// Returns a reference to the map's [`BuildHasher`]. /// @@ -536,7 +530,13 @@ where pub fn hasher(&self) -> &S { self.base.hasher() } +} +impl HashMap +where + K: Eq + Hash, + S: BuildHasher, +{ /// Reserves capacity for at least `additional` more elements to be inserted /// in the `HashMap`. The collection may reserve more space to avoid /// frequent reallocations. @@ -984,9 +984,8 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl Debug for HashMap where - K: Eq + Hash + Debug, + K: Debug, V: Debug, - S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_map().entries(self.iter()).finish() @@ -996,8 +995,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl Default for HashMap where - K: Eq + Hash, - S: BuildHasher + Default, + S: Default, { /// Creates an empty `HashMap`, with the `Default` value for the hasher. #[inline] -- cgit 1.4.1-3-g733a5 From 48859db151b839518bdd9d44a2387c0f6b65d141 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Thu, 26 Dec 2019 14:12:56 -0500 Subject: Relax bounds on HashSet to match hashbrown No functional changes are made, and all APIs are moved to strictly less restrictive bounds. These APIs changed from the old bound listed to the new bound: T: Hash + Eq -> T * new * with_capacity T: Eq + Hash, S: BuildHasher -> T * with_hasher * with_capacity_and_hasher * hasher T: Eq + Hash + Debug -> T: Debug S: BuildHasher -> S T: Eq + Hash -> T S: BuildHasher + Default -> S: Default --- src/libstd/collections/hash/set.rs | 20 +++++++++----------- .../array-impls/core-traits-no-impls-length-33.rs | 1 - .../core-traits-no-impls-length-33.stderr | 19 +++++-------------- 3 files changed, 14 insertions(+), 26 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index fff64e9fc90..f461f26572f 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -110,7 +110,7 @@ pub struct HashSet { map: HashMap, } -impl HashSet { +impl HashSet { /// Creates an empty `HashSet`. /// /// The hash set is initially created with a capacity of 0, so it will not allocate until it @@ -261,13 +261,7 @@ impl HashSet { pub fn clear(&mut self) { self.map.clear() } -} -impl HashSet -where - T: Eq + Hash, - S: BuildHasher, -{ /// Creates a new empty hash set which will use the given hasher to hash /// keys. /// @@ -340,7 +334,13 @@ where pub fn hasher(&self) -> &S { self.map.hasher() } +} +impl HashSet +where + T: Eq + Hash, + S: BuildHasher, +{ /// Reserves capacity for at least `additional` more elements to be inserted /// in the `HashSet`. The collection may reserve more space to avoid /// frequent reallocations. @@ -896,8 +896,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for HashSet where - T: Eq + Hash + fmt::Debug, - S: BuildHasher, + T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_set().entries(self.iter()).finish() @@ -945,8 +944,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl Default for HashSet where - T: Eq + Hash, - S: BuildHasher + Default, + S: Default, { /// Creates an empty `HashSet` with the `Default` value for the hasher. #[inline] diff --git a/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.rs b/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.rs index 7fa059583f5..8397d204f35 100644 --- a/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.rs +++ b/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.rs @@ -6,7 +6,6 @@ pub fn no_debug() { pub fn no_hash() { use std::collections::HashSet; let mut set = HashSet::new(); - //~^ ERROR arrays only have std trait implementations for lengths 0..=32 set.insert([0_usize; 33]); //~^ ERROR arrays only have std trait implementations for lengths 0..=32 } diff --git a/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr b/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr index d885c98dcb2..594a0d4b5d8 100644 --- a/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr +++ b/src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr @@ -8,24 +8,15 @@ LL | println!("{:?}", [0_usize; 33]); = note: required by `std::fmt::Debug::fmt` error[E0277]: arrays only have std trait implementations for lengths 0..=32 - --> $DIR/core-traits-no-impls-length-33.rs:10:16 + --> $DIR/core-traits-no-impls-length-33.rs:9:16 | LL | set.insert([0_usize; 33]); | ^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[usize; 33]` | = note: required because of the requirements on the impl of `std::cmp::Eq` for `[usize; 33]` -error[E0277]: arrays only have std trait implementations for lengths 0..=32 - --> $DIR/core-traits-no-impls-length-33.rs:8:19 - | -LL | let mut set = HashSet::new(); - | ^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[usize; 33]` - | - = note: required because of the requirements on the impl of `std::cmp::Eq` for `[usize; 33]` - = note: required by `std::collections::HashSet::::new` - error[E0369]: binary operation `==` cannot be applied to type `[usize; 33]` - --> $DIR/core-traits-no-impls-length-33.rs:15:19 + --> $DIR/core-traits-no-impls-length-33.rs:14:19 | LL | [0_usize; 33] == [1_usize; 33] | ------------- ^^ ------------- [usize; 33] @@ -35,7 +26,7 @@ LL | [0_usize; 33] == [1_usize; 33] = note: an implementation of `std::cmp::PartialEq` might be missing for `[usize; 33]` error[E0369]: binary operation `<` cannot be applied to type `[usize; 33]` - --> $DIR/core-traits-no-impls-length-33.rs:20:19 + --> $DIR/core-traits-no-impls-length-33.rs:19:19 | LL | [0_usize; 33] < [1_usize; 33] | ------------- ^ ------------- [usize; 33] @@ -45,7 +36,7 @@ LL | [0_usize; 33] < [1_usize; 33] = note: an implementation of `std::cmp::PartialOrd` might be missing for `[usize; 33]` error[E0277]: the trait bound `&[usize; 33]: std::iter::IntoIterator` is not satisfied - --> $DIR/core-traits-no-impls-length-33.rs:25:14 + --> $DIR/core-traits-no-impls-length-33.rs:24:14 | LL | for _ in &[0_usize; 33] { | ^^^^^^^^^^^^^^ the trait `std::iter::IntoIterator` is not implemented for `&[usize; 33]` @@ -57,7 +48,7 @@ LL | for _ in &[0_usize; 33] { <&'a mut [T] as std::iter::IntoIterator> = note: required by `std::iter::IntoIterator::into_iter` -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors Some errors have detailed explanations: E0277, E0369. For more information about an error, try `rustc --explain E0277`. -- cgit 1.4.1-3-g733a5 From 60274a95fef57a18113f7c48be68be31ece860eb Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Sun, 1 Sep 2019 17:23:20 +0200 Subject: Added From> for CString Updated tracking issue number Added safeguards for transmute_vec potentially being factored out elsewhere Clarified comment about avoiding mem::forget Removed unneeded unstable guard Added back a stability annotation for CI Minor documentation improvements Thanks to @Centril's code review Co-Authored-By: Mazdak Farrokhzad Improved layout checks, type annotations and removed unaccurate comment Removed unnecessary check on array layout Adapt the stability annotation to the new 1.41 milestone Co-Authored-By: Mazdak Farrokhzad Simplify the implementation. Use `Vec::into_raw_parts` instead of a manual implementation of `Vec::transmute`. If `Vec::into_raw_parts` uses `NonNull` instead, then the code here will need to be adjusted to take it into account (issue #65816) Reduce the whitespace of safety comments --- src/libstd/ffi/c_str.rs | 27 +++++++++++++++++++++++++++ src/libstd/lib.rs | 1 + 2 files changed, 28 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index d2ee65f0a74..217672ea292 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -6,6 +6,7 @@ use crate::fmt::{self, Write}; use crate::io; use crate::mem; use crate::memchr; +use crate::num::NonZeroU8; use crate::ops; use crate::os::raw::c_char; use crate::ptr; @@ -741,6 +742,32 @@ impl From> for CString { } } +#[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")] +impl From> for CString { + /// Converts a [`Vec`]`<`[`NonZeroU8`]`>` into a [`CString`] without + /// copying nor checking for inner null bytes. + /// + /// [`CString`]: ../ffi/struct.CString.html + /// [`NonZeroU8`]: ../num/struct.NonZeroU8.html + /// [`Vec`]: ../vec/struct.Vec.html + #[inline] + fn from(v: Vec) -> CString { + unsafe { + // Transmute `Vec` to `Vec`. + let v: Vec = { + // Safety: + // - transmuting between `NonZeroU8` and `u8` is sound; + // - `alloc::Layout == alloc::Layout`. + let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v); + Vec::from_raw_parts(ptr.cast::(), len, cap) + }; + // Safety: `v` cannot contain null bytes, given the type-level + // invariant of `NonZeroU8`. + CString::from_vec_unchecked(v) + } + } +} + #[stable(feature = "more_box_slice_clone", since = "1.29.0")] impl Clone for Box { #[inline] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 9e9df5ab9b6..c05ebc7349d 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -310,6 +310,7 @@ #![feature(unboxed_closures)] #![feature(untagged_unions)] #![feature(unwind_attributes)] +#![feature(vec_into_raw_parts)] // NB: the above list is sorted to minimize merge conflicts. #![default_lib_allocator] -- cgit 1.4.1-3-g733a5 From 178de46116b4fc8c1d9b30007f5e5ed24c809881 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Thu, 26 Dec 2019 12:55:13 -0500 Subject: Add primitive module to libcore/std This re-exports the primitive types from libcore at `core::primitive` to allow macro authors to have a reliable location to use them from. --- src/libcore/lib.rs | 3 + src/libcore/primitive.rs | 67 ++++++++++++++++++++++ src/libstd/lib.rs | 5 +- .../ui/resolve/resolve-primitive-fallback.stderr | 5 ++ src/test/ui/shadow-bool.rs | 18 ++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/libcore/primitive.rs create mode 100644 src/test/ui/shadow-bool.rs (limited to 'src/libstd') diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 1fd70e1a1b0..257a6d371b7 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -262,6 +262,9 @@ mod bool; mod tuple; mod unit; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub mod primitive; + // Pull in the `core_arch` crate directly into libcore. The contents of // `core_arch` are in a different repository: rust-lang/stdarch. // diff --git a/src/libcore/primitive.rs b/src/libcore/primitive.rs new file mode 100644 index 00000000000..d37bbfaf5df --- /dev/null +++ b/src/libcore/primitive.rs @@ -0,0 +1,67 @@ +//! This module reexports the primitive types to allow usage that is not +//! possibly shadowed by other declared types. +//! +//! This is normally only useful in macro generated code. +//! +//! An example of this is when generating a new struct and an impl for it: +//! +//! ```rust,compile_fail +//! pub struct bool; +//! +//! impl QueryId for bool { +//! const SOME_PROPERTY: bool = true; +//! } +//! +//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; } +//! ``` +//! +//! Note that the `SOME_PROPERTY` associated constant would not compile, as its +//! type `bool` refers to the struct, rather than to the primitive bool type. +//! +//! A correct implementation could look like: +//! +//! ```rust +//! # #[allow(non_camel_case_types)] +//! pub struct bool; +//! +//! impl QueryId for bool { +//! const SOME_PROPERTY: core::primitive::bool = true; +//! } +//! +//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; } +//! ``` + +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use bool; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use char; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use f32; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use f64; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use i128; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use i16; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use i32; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use i64; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use i8; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use isize; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use str; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use u128; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use u16; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use u32; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use u64; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use u8; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use usize; diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index f9c9f224730..2b54481ab56 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -233,12 +233,12 @@ #![feature(allocator_internals)] #![feature(allow_internal_unsafe)] #![feature(allow_internal_unstable)] -#![feature(atomic_mut_ptr)] #![feature(arbitrary_self_types)] #![feature(array_error_internals)] #![feature(asm)] #![feature(assoc_int_consts)] #![feature(associated_type_bounds)] +#![feature(atomic_mut_ptr)] #![feature(box_syntax)] #![feature(c_variadic)] #![feature(cfg_target_has_atomic)] @@ -550,6 +550,9 @@ pub use core::{ trace_macros, }; +#[stable(feature = "core_primitive", since = "1.42.0")] +pub use core::primitive; + // Include a number of private modules that exist solely to provide // the rustdoc documentation for primitive types. Using `include!` // because rustdoc only looks for these modules at the crate level. diff --git a/src/test/ui/resolve/resolve-primitive-fallback.stderr b/src/test/ui/resolve/resolve-primitive-fallback.stderr index 92c2a032983..72a854346fa 100644 --- a/src/test/ui/resolve/resolve-primitive-fallback.stderr +++ b/src/test/ui/resolve/resolve-primitive-fallback.stderr @@ -9,6 +9,11 @@ error[E0412]: cannot find type `u8` in the crate root | LL | let _: ::u8; | ^^ not found in the crate root + | +help: possible candidate is found in another module, you can import it into scope + | +LL | use std::primitive::u8; + | error[E0061]: this function takes 0 parameters but 1 parameter was supplied --> $DIR/resolve-primitive-fallback.rs:3:5 diff --git a/src/test/ui/shadow-bool.rs b/src/test/ui/shadow-bool.rs new file mode 100644 index 00000000000..f290a329eaa --- /dev/null +++ b/src/test/ui/shadow-bool.rs @@ -0,0 +1,18 @@ +// check-pass + +mod bar { + pub trait QueryId { + const SOME_PROPERTY: bool; + } +} + +use bar::QueryId; + +#[allow(non_camel_case_types)] +pub struct bool; + +impl QueryId for bool { + const SOME_PROPERTY: core::primitive::bool = true; +} + +fn main() {} -- cgit 1.4.1-3-g733a5 From 348278a7fd5fd459f555dd763e71e12c23c1661a Mon Sep 17 00:00:00 2001 From: Michael Bradshaw Date: Fri, 7 Feb 2020 21:53:22 -0800 Subject: Stabilize Once::is_completed --- src/libstd/sync/once.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 61c4d0c2dbf..b99b4d8d9fd 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -331,14 +331,14 @@ impl Once { /// * `call_once` was called, but has not yet completed, /// * the `Once` instance is poisoned /// - /// It is also possible that immediately after `is_completed` - /// returns false, some other thread finishes executing - /// `call_once`. + /// This function returning `false` does not mean that `Once` has not been + /// executed. For example, it may have been executed in the time between + /// when `is_completed` starts executing and when it returns, in which case + /// the `false` return value would be stale (but still permissible). /// /// # Examples /// /// ``` - /// #![feature(once_is_completed)] /// use std::sync::Once; /// /// static INIT: Once = Once::new(); @@ -351,7 +351,6 @@ impl Once { /// ``` /// /// ``` - /// #![feature(once_is_completed)] /// use std::sync::Once; /// use std::thread; /// @@ -364,7 +363,7 @@ impl Once { /// assert!(handle.join().is_err()); /// assert_eq!(INIT.is_completed(), false); /// ``` - #[unstable(feature = "once_is_completed", issue = "54890")] + #[stable(feature = "once_is_completed", since = "1.44.0")] #[inline] pub fn is_completed(&self) -> bool { // An `Acquire` load is enough because that makes all the initialization -- cgit 1.4.1-3-g733a5 From 1b12232b8f6ede69485d4fd48f1ac8044d5d7c7c Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Fri, 31 Jan 2020 15:19:22 +0100 Subject: Fix SGX RWLock representation for UnsafeCell niche fix --- src/ci/docker/dist-various-2/Dockerfile | 2 +- src/libstd/sys/sgx/rwlock.rs | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src/libstd') diff --git a/src/ci/docker/dist-various-2/Dockerfile b/src/ci/docker/dist-various-2/Dockerfile index 2ae6c58941e..5bb5436bec5 100644 --- a/src/ci/docker/dist-various-2/Dockerfile +++ b/src/ci/docker/dist-various-2/Dockerfile @@ -48,7 +48,7 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/ # We pass the commit id of the port of LLVM's libunwind to the build script. # Any update to the commit id here, should cause the container image to be re-built from this point on. -RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "53b586346f2c7870e20b170decdc30729d97c42b" +RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "5125c169b30837208a842f85f7ae44a83533bd0e" COPY dist-various-2/build-wasi-toolchain.sh /tmp/ RUN /tmp/build-wasi-toolchain.sh diff --git a/src/libstd/sys/sgx/rwlock.rs b/src/libstd/sys/sgx/rwlock.rs index fda2bb504d4..722b4f5e0ba 100644 --- a/src/libstd/sys/sgx/rwlock.rs +++ b/src/libstd/sys/sgx/rwlock.rs @@ -10,10 +10,10 @@ pub struct RWLock { writer: SpinMutex>, } -// Below is to check at compile time, that RWLock has size of 128 bytes. +// Check at compile time that RWLock size matches C definition (see test_c_rwlock_initializer below) #[allow(dead_code)] unsafe fn rw_lock_size_assert(r: RWLock) { - mem::transmute::(r); + mem::transmute::(r); } impl RWLock { @@ -210,15 +210,17 @@ mod tests { // be changed too. #[test] fn test_c_rwlock_initializer() { + #[rustfmt::skip] const RWLOCK_INIT: &[u8] = &[ - 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x00 */ 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x10 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x20 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x30 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x40 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x50 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x60 */ 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x70 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* 0x80 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ]; #[inline(never)] @@ -239,7 +241,7 @@ mod tests { zero_stack(); let mut init = MaybeUninit::::zeroed(); rwlock_new(&mut init); - assert_eq!(mem::transmute::<_, [u8; 128]>(init.assume_init()).as_slice(), RWLOCK_INIT) + assert_eq!(mem::transmute::<_, [u8; 144]>(init.assume_init()).as_slice(), RWLOCK_INIT) }; } } -- cgit 1.4.1-3-g733a5 From aeedc9dea9e0460488e0b6ce7fe3aaf50395774c Mon Sep 17 00:00:00 2001 From: Raoul Strackx Date: Fri, 7 Feb 2020 10:37:53 +0100 Subject: Corrected ac_mitigation patch. That patch used the untrusted stack to clear rflags during enclave (re-)entry --- src/libstd/sys/sgx/abi/entry.S | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index a3e059e8131..7c273a8a83d 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -134,6 +134,17 @@ elf_entry: ud2 /* should not be reached */ /* end elf_entry */ +/* This code needs to be called *after* the enclave stack has been setup. */ +/* There are 3 places where this needs to happen, so this is put in a macro. */ +.macro sanitize_rflags +/* Sanitize rflags received from user */ +/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */ +/* - AC flag: AEX on misaligned memory accesses leaks side channel info */ + pushfq + andq $~0x40400, (%rsp) + popfq +.endm + .text .global sgx_entry .type sgx_entry,function @@ -150,13 +161,6 @@ sgx_entry: stmxcsr %gs:tcsls_user_mxcsr fnstcw %gs:tcsls_user_fcw -/* reset user state */ -/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */ -/* - AC flag: AEX on misaligned memory accesses leaks side channel info */ - pushfq - andq $~0x40400, (%rsp) - popfq - /* check for debug buffer pointer */ testb $0xff,DEBUG(%rip) jz .Lskip_debug_init @@ -179,6 +183,7 @@ sgx_entry: lea IMAGE_BASE(%rip),%rax add %rax,%rsp mov %rsp,%gs:tcsls_tos + sanitize_rflags /* call tcs_init */ /* store caller-saved registers in callee-saved registers */ mov %rdi,%rbx @@ -194,7 +199,10 @@ sgx_entry: mov %r13,%rdx mov %r14,%r8 mov %r15,%r9 + jmp .Lafter_init .Lskip_init: + sanitize_rflags +.Lafter_init: /* call into main entry point */ load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */ call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */ @@ -292,6 +300,7 @@ usercall: movq $0,%gs:tcsls_last_rsp /* restore callee-saved state, cf. "save" above */ mov %r11,%rsp + sanitize_rflags ldmxcsr (%rsp) fldcw 4(%rsp) add $8, %rsp -- cgit 1.4.1-3-g733a5 From 236ab6e6d631f073a8c3c7439af6b2ec58ce1f25 Mon Sep 17 00:00:00 2001 From: Raoul Strackx Date: Fri, 7 Feb 2020 10:49:47 +0100 Subject: sanitize MXCSR/FPU control registers --- src/libstd/sys/sgx/abi/entry.S | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index 7c273a8a83d..a08caec3c2b 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -30,6 +30,14 @@ IMAGE_BASE: /* We can store a bunch of data in the gap between MXCSR and the XSAVE header */ +/* MXCSR initialization value for ABI */ +.Lmxcsr_init: + .int 0x1f80 + +/* x87 FPU control word initialization value for ABI */ +.Lfpucw_init: + .int 0x037f + /* The following symbols point at read-only data that will be filled in by the */ /* post-linker. */ @@ -173,6 +181,9 @@ sgx_entry: mov %gs:tcsls_last_rsp,%r11 test %r11,%r11 jnz .Lusercall_ret +/* reset user state */ + ldmxcsr .Lmxcsr_init(%rip) + fldcw .Lfpucw_init(%rip) /* setup stack */ mov %gs:tcsls_tos,%rsp /* initially, RSP is not set to the correct value */ /* here. This is fixed below under "adjust stack". */ -- cgit 1.4.1-3-g733a5 From 71b9ed4a36748be01826063951310a2da2717a9b Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Mon, 10 Feb 2020 21:00:22 +0100 Subject: Avoid jumping to Rust code with user %rsp (reentry_panic) --- src/libstd/sys/sgx/abi/entry.S | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index a08caec3c2b..ed4db287229 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -144,13 +144,15 @@ elf_entry: /* This code needs to be called *after* the enclave stack has been setup. */ /* There are 3 places where this needs to happen, so this is put in a macro. */ -.macro sanitize_rflags +.macro entry_sanitize_final /* Sanitize rflags received from user */ /* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */ /* - AC flag: AEX on misaligned memory accesses leaks side channel info */ pushfq andq $~0x40400, (%rsp) popfq + bt $0,.Laborted(%rip) + jc .Lreentry_panic .endm .text @@ -174,9 +176,6 @@ sgx_entry: jz .Lskip_debug_init mov %r10,%gs:tcsls_debug_panic_buf_ptr .Lskip_debug_init: -/* check for abort */ - bt $0,.Laborted(%rip) - jc .Lreentry_panic /* check if returning from usercall */ mov %gs:tcsls_last_rsp,%r11 test %r11,%r11 @@ -194,7 +193,7 @@ sgx_entry: lea IMAGE_BASE(%rip),%rax add %rax,%rsp mov %rsp,%gs:tcsls_tos - sanitize_rflags + entry_sanitize_final /* call tcs_init */ /* store caller-saved registers in callee-saved registers */ mov %rdi,%rbx @@ -212,7 +211,7 @@ sgx_entry: mov %r15,%r9 jmp .Lafter_init .Lskip_init: - sanitize_rflags + entry_sanitize_final .Lafter_init: /* call into main entry point */ load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */ @@ -311,10 +310,10 @@ usercall: movq $0,%gs:tcsls_last_rsp /* restore callee-saved state, cf. "save" above */ mov %r11,%rsp - sanitize_rflags ldmxcsr (%rsp) fldcw 4(%rsp) add $8, %rsp + entry_sanitize_final pop %rbx pop %rbp pop %r12 -- cgit 1.4.1-3-g733a5 From 4cf0365b19ddf852375175a9679572f1e0de4eda Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 14:20:21 -0800 Subject: Bump version to backtrace without the header --- src/libstd/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/Cargo.toml b/src/libstd/Cargo.toml index c9ff93eac02..b147aa55b2a 100644 --- a/src/libstd/Cargo.toml +++ b/src/libstd/Cargo.toml @@ -27,7 +27,7 @@ hashbrown = { version = "0.6.2", default-features = false, features = ['rustc-de [dependencies.backtrace_rs] package = "backtrace" -version = "0.3.37" +version = "0.3.44" default-features = false # without the libstd `backtrace` feature, stub out everything features = [ "rustc-dep-of-std" ] # enable build support for integrating into libstd -- cgit 1.4.1-3-g733a5 From b637c0e84a9dbb5883130e0ea1e5ee9e8acf3bc1 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 11:47:26 -0800 Subject: Add initial debug fmt for Backtrace --- src/libstd/backtrace.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 5ba1c940251..6b4ae77cec7 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -106,6 +106,7 @@ use backtrace_rs as backtrace; /// previous point in time. In some instances the `Backtrace` type may /// internally be empty due to configuration. For more information see /// `Backtrace::capture`. +#[derive(Debug)] pub struct Backtrace { inner: Inner, } @@ -126,12 +127,14 @@ pub enum BacktraceStatus { Captured, } +#[derive(Debug)] enum Inner { Unsupported, Disabled, Captured(Mutex), } +#[derive(Debug)] struct Capture { actual_start: usize, resolved: bool, @@ -143,11 +146,13 @@ fn _assert_send_sync() { _assert::(); } +#[derive(Debug)] struct BacktraceFrame { frame: backtrace::Frame, symbols: Vec, } +#[derive(Debug)] struct BacktraceSymbol { name: Option>, filename: Option, @@ -159,6 +164,20 @@ enum BytesOrWide { Wide(Vec), } +impl fmt::Debug for BytesOrWide { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + output_filename( + fmt, + match self { + BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w), + BytesOrWide::Wide(w) => BytesOrWideString::Wide(w), + }, + backtrace::PrintFmt::Full, + crate::env::current_dir().as_ref().ok(), + ) + } +} + impl Backtrace { /// Returns whether backtrace captures are enabled through environment /// variables. @@ -267,12 +286,6 @@ impl Backtrace { } impl fmt::Display for Backtrace { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self, fmt) - } -} - -impl fmt::Debug for Backtrace { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { let mut capture = match &self.inner { Inner::Unsupported => return fmt.write_str("unsupported backtrace"), -- cgit 1.4.1-3-g733a5 From 49204563e13f57917cc22ac8f8b608927a432038 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 13:15:03 -0800 Subject: Get vaguely working with a test for checking output --- src/libstd/backtrace.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 6b4ae77cec7..f6c34486d70 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -152,7 +152,6 @@ struct BacktraceFrame { symbols: Vec, } -#[derive(Debug)] struct BacktraceSymbol { name: Option>, filename: Option, @@ -164,6 +163,16 @@ enum BytesOrWide { Wide(Vec), } +impl fmt::Debug for BacktraceSymbol { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("BacktraceSymbol") + .field("name", &self.name.as_ref().map(|b| backtrace::SymbolName::new(b))) + .field("filename", &self.filename) + .field("lineno", &self.lineno) + .finish() + } +} + impl fmt::Debug for BytesOrWide { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { output_filename( @@ -364,3 +373,19 @@ impl Capture { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn debug_backtrace_fmt() { + let bt = Backtrace::capture(); + eprintln!("uncaptured: {:?}", bt); + let bt = Backtrace::force_capture(); + eprintln!("captured: {:?}", bt); + eprintln!("display print: {}", bt); + eprintln!("resolved: {:?}", bt); + unimplemented!(); + } +} -- cgit 1.4.1-3-g733a5 From c0ba79eefd82d0a5614e295659b18f7b31e542a3 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:15:13 -0800 Subject: less noisy format --- src/libstd/backtrace.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index f6c34486d70..87e6eafe479 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -106,7 +106,6 @@ use backtrace_rs as backtrace; /// previous point in time. In some instances the `Backtrace` type may /// internally be empty due to configuration. For more information see /// `Backtrace::capture`. -#[derive(Debug)] pub struct Backtrace { inner: Inner, } @@ -146,7 +145,6 @@ fn _assert_send_sync() { _assert::(); } -#[derive(Debug)] struct BacktraceFrame { frame: backtrace::Frame, symbols: Vec, @@ -163,13 +161,45 @@ enum BytesOrWide { Wide(Vec), } +impl fmt::Debug for Backtrace { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut capture = match &self.inner { + Inner::Unsupported => return fmt.write_str("unsupported backtrace"), + Inner::Disabled => return fmt.write_str("disabled backtrace"), + Inner::Captured(c) => c.lock().unwrap(), + }; + capture.resolve(); + + let mut dbg = fmt.debug_list(); + + for frame in &capture.frames { + dbg.entries(&frame.symbols); + } + + dbg.finish() + } +} + +impl fmt::Debug for BacktraceFrame { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_list().entries(&self.symbols).finish() + } +} + impl fmt::Debug for BacktraceSymbol { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_struct("BacktraceSymbol") - .field("name", &self.name.as_ref().map(|b| backtrace::SymbolName::new(b))) - .field("filename", &self.filename) - .field("lineno", &self.lineno) - .finish() + let mut dbg = fmt.debug_struct(""); + dbg.field("fn", &self.name.as_ref().map(|b| backtrace::SymbolName::new(b))); + + if let Some(fname) = self.filename.as_ref() { + dbg.field("file", fname); + } + + if let Some(line) = self.lineno.as_ref() { + dbg.field("line", &self.lineno); + } + + dbg.finish() } } -- cgit 1.4.1-3-g733a5 From 0d5444ffa6ac0447849627406d15d16630a6364b Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:16:24 -0800 Subject: remove unnecessary derives --- src/libstd/backtrace.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 87e6eafe479..571c13241b5 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -126,14 +126,12 @@ pub enum BacktraceStatus { Captured, } -#[derive(Debug)] enum Inner { Unsupported, Disabled, Captured(Mutex), } -#[derive(Debug)] struct Capture { actual_start: usize, resolved: bool, -- cgit 1.4.1-3-g733a5 From 76e6d6fe114944c88bea77baf700aa5ead2aa9e3 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:17:40 -0800 Subject: remove unnecessary Debug impl for BacktraceFrame --- src/libstd/backtrace.rs | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 571c13241b5..b3d26890fdd 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -178,12 +178,6 @@ impl fmt::Debug for Backtrace { } } -impl fmt::Debug for BacktraceFrame { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_list().entries(&self.symbols).finish() - } -} - impl fmt::Debug for BacktraceSymbol { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { let mut dbg = fmt.debug_struct(""); -- cgit 1.4.1-3-g733a5 From 583dd2c3eebafb72ec89fd4497c3cb751e2343ba Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:18:29 -0800 Subject: make it compile --- src/libstd/backtrace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index b3d26890fdd..63669532ebc 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -188,7 +188,7 @@ impl fmt::Debug for BacktraceSymbol { } if let Some(line) = self.lineno.as_ref() { - dbg.field("line", &self.lineno); + dbg.field("line", line); } dbg.finish() -- cgit 1.4.1-3-g733a5 From 87117783fb59a580d0a90200ac62ecf219142e49 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:39:32 -0800 Subject: final format cleanups --- src/libstd/backtrace.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 63669532ebc..46ea719b2ad 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -168,6 +168,8 @@ impl fmt::Debug for Backtrace { }; capture.resolve(); + write!(fmt, "Backtrace ")?; + let mut dbg = fmt.debug_list(); for frame in &capture.frames { @@ -181,7 +183,12 @@ impl fmt::Debug for Backtrace { impl fmt::Debug for BacktraceSymbol { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { let mut dbg = fmt.debug_struct(""); - dbg.field("fn", &self.name.as_ref().map(|b| backtrace::SymbolName::new(b))); + + if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { + dbg.field("fn", &fn_name); + } else { + dbg.field("fn", &""); + } if let Some(fname) = self.filename.as_ref() { dbg.field("file", fname); -- cgit 1.4.1-3-g733a5 From 70c91330143b7979ec2847d0016ece75053e5d39 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 15:48:09 -0800 Subject: remove Some from fn name --- src/libstd/backtrace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 46ea719b2ad..d5a7d6b8a94 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -185,7 +185,7 @@ impl fmt::Debug for BacktraceSymbol { let mut dbg = fmt.debug_struct(""); if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - dbg.field("fn", &fn_name); + dbg.field("fn", &format_args!("\"{}\"", fn_name)); } else { dbg.field("fn", &""); } -- cgit 1.4.1-3-g733a5 From 230ed3ea75a18984a0e34fb99dae69e8aa779c64 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 17:28:04 -0800 Subject: use debug_map and skip empty frames --- src/libstd/backtrace.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index d5a7d6b8a94..d481f227db7 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -173,6 +173,10 @@ impl fmt::Debug for Backtrace { let mut dbg = fmt.debug_list(); for frame in &capture.frames { + if frame.frame.ip().is_null() { + continue; + } + dbg.entries(&frame.symbols); } @@ -182,20 +186,20 @@ impl fmt::Debug for Backtrace { impl fmt::Debug for BacktraceSymbol { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut dbg = fmt.debug_struct(""); + let mut dbg = fmt.debug_map(); if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - dbg.field("fn", &format_args!("\"{}\"", fn_name)); + dbg.entry(&"fn", &format_args!("\"{}\"", fn_name)); } else { - dbg.field("fn", &""); + dbg.entry(&"fn", &""); } if let Some(fname) = self.filename.as_ref() { - dbg.field("file", fname); + dbg.entry(&"file", fname); } if let Some(line) = self.lineno.as_ref() { - dbg.field("line", line); + dbg.entry(&"line", line); } dbg.finish() @@ -415,6 +419,7 @@ mod tests { eprintln!("captured: {:?}", bt); eprintln!("display print: {}", bt); eprintln!("resolved: {:?}", bt); + eprintln!("resolved alt: {:#?}", bt); unimplemented!(); } } -- cgit 1.4.1-3-g733a5 From de25048a23bbec9ee0dc2f9485e45514444a0d42 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 17:42:04 -0800 Subject: add nice alt fmt for debug --- src/libstd/backtrace.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index d481f227db7..7ca7ab674ba 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -186,23 +186,23 @@ impl fmt::Debug for Backtrace { impl fmt::Debug for BacktraceSymbol { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut dbg = fmt.debug_map(); + write!(fmt, "{{ ")?; if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - dbg.entry(&"fn", &format_args!("\"{}\"", fn_name)); + write!(fmt, "fn: \"{:?}\"", fn_name)?; } else { - dbg.entry(&"fn", &""); + write!(fmt, "fn: \"\"")?; } if let Some(fname) = self.filename.as_ref() { - dbg.entry(&"file", fname); + write!(fmt, ", file: {:?}", fname)?; } if let Some(line) = self.lineno.as_ref() { - dbg.entry(&"line", line); + write!(fmt, ", line: {:?}", line)?; } - dbg.finish() + write!(fmt, " }}") } } -- cgit 1.4.1-3-g733a5 From 192b10391784de14e8cc672314e3b68e9c450fce Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 17:45:58 -0800 Subject: make symbol printing consistent with backtrace_rs --- src/libstd/backtrace.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 7ca7ab674ba..b82a44350cc 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -189,9 +189,10 @@ impl fmt::Debug for BacktraceSymbol { write!(fmt, "{{ ")?; if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - write!(fmt, "fn: \"{:?}\"", fn_name)?; + write!(fmt, "fn: ")?; + fmt::Display::fmt(&fn_name, fmt)?; } else { - write!(fmt, "fn: \"\"")?; + write!(fmt, "fn: ")?; } if let Some(fname) = self.filename.as_ref() { -- cgit 1.4.1-3-g733a5 From 7064a0ec59005f1e67e89a794ff70d687d2d7041 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 21:07:36 -0800 Subject: maximum alternative consistency! --- src/libstd/backtrace.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index b82a44350cc..448b988d99c 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -168,11 +168,17 @@ impl fmt::Debug for Backtrace { }; capture.resolve(); + let frames = if fmt.alternate() { + &capture.frames[..] + } else { + &capture.frames[capture.actual_start..] + }; + write!(fmt, "Backtrace ")?; let mut dbg = fmt.debug_list(); - for frame in &capture.frames { + for frame in frames { if frame.frame.ip().is_null() { continue; } @@ -215,7 +221,7 @@ impl fmt::Debug for BytesOrWide { BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w), BytesOrWide::Wide(w) => BytesOrWideString::Wide(w), }, - backtrace::PrintFmt::Full, + if fmt.alternate() { backtrace::PrintFmt::Full } else { backtrace::PrintFmt::Short }, crate::env::current_dir().as_ref().ok(), ) } @@ -419,6 +425,7 @@ mod tests { let bt = Backtrace::force_capture(); eprintln!("captured: {:?}", bt); eprintln!("display print: {}", bt); + eprintln!("display print alt: {:#}", bt); eprintln!("resolved: {:?}", bt); eprintln!("resolved alt: {:#?}", bt); unimplemented!(); -- cgit 1.4.1-3-g733a5 From 6797bfd203dea6601548098214504375ad0e45ac Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 21:38:59 -0800 Subject: rule over the code in libstd with an iron fist --- src/libstd/backtrace.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 448b988d99c..391d22df5e5 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -168,11 +168,7 @@ impl fmt::Debug for Backtrace { }; capture.resolve(); - let frames = if fmt.alternate() { - &capture.frames[..] - } else { - &capture.frames[capture.actual_start..] - }; + let frames = &capture.frames[capture.actual_start..]; write!(fmt, "Backtrace ")?; @@ -195,10 +191,9 @@ impl fmt::Debug for BacktraceSymbol { write!(fmt, "{{ ")?; if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - write!(fmt, "fn: ")?; - fmt::Display::fmt(&fn_name, fmt)?; + write!(fmt, "fn: \"{}\"", fn_name)?; } else { - write!(fmt, "fn: ")?; + write!(fmt, "fn: \"\"")?; } if let Some(fname) = self.filename.as_ref() { @@ -221,7 +216,7 @@ impl fmt::Debug for BytesOrWide { BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w), BytesOrWide::Wide(w) => BytesOrWideString::Wide(w), }, - if fmt.alternate() { backtrace::PrintFmt::Full } else { backtrace::PrintFmt::Short }, + backtrace::PrintFmt::Short, crate::env::current_dir().as_ref().ok(), ) } -- cgit 1.4.1-3-g733a5 From c8817aa521092b02d4b9e8494429e178259e506a Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 10 Feb 2020 21:54:27 -0800 Subject: backwards again, god damnit --- src/libstd/backtrace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 391d22df5e5..4b47a8cf169 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -191,7 +191,7 @@ impl fmt::Debug for BacktraceSymbol { write!(fmt, "{{ ")?; if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) { - write!(fmt, "fn: \"{}\"", fn_name)?; + write!(fmt, "fn: \"{:#}\"", fn_name)?; } else { write!(fmt, "fn: \"\"")?; } -- cgit 1.4.1-3-g733a5 From ec8ee7fb81c208fbd0fe53f11b1c792f3e0d6c6f Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 11 Feb 2020 08:39:27 -0800 Subject: remove intentionally failing test --- src/libstd/backtrace.rs | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/backtrace.rs b/src/libstd/backtrace.rs index 4b47a8cf169..a1c9aa75d77 100644 --- a/src/libstd/backtrace.rs +++ b/src/libstd/backtrace.rs @@ -408,21 +408,3 @@ impl Capture { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn debug_backtrace_fmt() { - let bt = Backtrace::capture(); - eprintln!("uncaptured: {:?}", bt); - let bt = Backtrace::force_capture(); - eprintln!("captured: {:?}", bt); - eprintln!("display print: {}", bt); - eprintln!("display print alt: {:#}", bt); - eprintln!("resolved: {:?}", bt); - eprintln!("resolved alt: {:#?}", bt); - unimplemented!(); - } -} -- cgit 1.4.1-3-g733a5 From 1f6fb338a5f775745595d32b61c1862887c948f9 Mon Sep 17 00:00:00 2001 From: Dario Gonzalez Date: Tue, 11 Feb 2020 10:11:58 -0800 Subject: make the sgx arg cleanup implementation a no op --- src/libstd/sys/sgx/args.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/sgx/args.rs b/src/libstd/sys/sgx/args.rs index b47a48e752c..5a53695a846 100644 --- a/src/libstd/sys/sgx/args.rs +++ b/src/libstd/sys/sgx/args.rs @@ -22,12 +22,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) { } } -pub unsafe fn cleanup() { - let args = ARGS.swap(0, Ordering::Relaxed); - if args != 0 { - drop(Box::::from_raw(args as _)) - } -} +pub unsafe fn cleanup() {} pub fn args() -> Args { let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() }; -- cgit 1.4.1-3-g733a5 From 8e26ad0c2c5c755f927ac971a3c1ab51de45f64f Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 11 Feb 2020 20:36:36 +0000 Subject: Keyword docs Co-Authored-By: Mazdak Farrokhzad Co-Authored-By: Tim Robinson Co-Authored-By: Peter Todd Co-Authored-By: Dylan DPC --- src/libstd/keyword_docs.rs | 49 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index 7901c8197b5..1d192cfddeb 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -1100,10 +1100,28 @@ mod trait_keyword {} // /// A value of type [`bool`] representing logical **true**. /// -/// The documentation for this keyword is [not yet complete]. Pull requests welcome! +/// Logically `true` is not equal to [`false`]. +/// +/// ## Control structures that check for **true** +/// +/// Several of Rust's control structures will check for a `bool` condition evaluating to **true**. +/// +/// * The condition in an [`if`] expression must be of type `bool`. +/// Whenever that condition evaluates to **true**, the `if` expression takes +/// on the value of the first block. If however, the condition evaluates +/// to `false`, the expression takes on value of the `else` block if there is one. /// +/// * [`while`] is another control flow construct expecting a `bool`-typed condition. +/// As long as the condition evaluates to **true**, the `while` loop will continually +/// evaluate its associated block. +/// +/// * [`match`] arms can have guard clauses on them. +/// +/// [`if`]: keyword.if.html +/// [`while`]: keyword.while.html +/// [`match`]: ../reference/expressions/match-expr.html#match-guards +/// [`false`]: keyword.false.html /// [`bool`]: primitive.bool.html -/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601 mod true_keyword {} #[doc(keyword = "type")] @@ -1167,12 +1185,33 @@ mod await_keyword {} #[doc(keyword = "dyn")] // -/// Name the type of a [trait object]. +/// `dyn` is a prefix of a [trait object]'s type. /// -/// The documentation for this keyword is [not yet complete]. Pull requests welcome! +/// The `dyn` keyword is used to highlight that calls to methods on the associated `Trait` +/// are dynamically dispatched. To use the trait this way, it must be 'object safe'. +/// +/// Unlike generic parameters or `impl Trait`, the compiler does not know the concrete type that +/// is being passed. That is, the type has been [erased]. +/// As such, a `dyn Trait` reference contains _two_ pointers. +/// One pointer goes to the data (e.g., an instance of a struct). +/// Another pointer goes to a map of method call names to function pointers +/// (known as a virtual method table or vtable). +/// +/// At run-time, when a method needs to be called on the `dyn Trait`, the vtable is consulted to get +/// the function pointer and then that function pointer is called. +/// +/// ## Trade-offs +/// +/// The above indirection is the additional runtime cost of calling a function on a `dyn Trait`. +/// Methods called by dynamic dispatch generally cannot be inlined by the compiler. +/// +/// However, `dyn Trait` is likely to produce smaller code than `impl Trait` / generic parameters as +/// the method won't be duplicated for each concrete type. +/// +/// Read more about `object safety` and [trait object]s. /// /// [trait object]: ../book/ch17-02-trait-objects.html -/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601 +/// [erased]: https://en.wikipedia.org/wiki/Type_erasure mod dyn_keyword {} #[doc(keyword = "union")] -- cgit 1.4.1-3-g733a5 From 090a1571d8c489588e2947ddb61b79f78b5b11b3 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 11 Feb 2020 16:57:22 -0800 Subject: Fix failing backtrace ui tests --- src/libstd/sys_common/backtrace.rs | 1 + src/test/ui/std-backtrace.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys_common/backtrace.rs b/src/libstd/sys_common/backtrace.rs index 191add2c31b..289ee07babf 100644 --- a/src/libstd/sys_common/backtrace.rs +++ b/src/libstd/sys_common/backtrace.rs @@ -70,6 +70,7 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt:: let mut print_path = move |fmt: &mut fmt::Formatter<'_>, bows: BytesOrWideString<'_>| { output_filename(fmt, bows, print_fmt, cwd.as_ref()) }; + write!(fmt, "stack backtrace:\n")?; let mut bt_fmt = BacktraceFmt::new(fmt, print_fmt, &mut print_path); bt_fmt.add_context()?; let mut idx = 0; diff --git a/src/test/ui/std-backtrace.rs b/src/test/ui/std-backtrace.rs index d84c493d535..902a2b6f5a0 100644 --- a/src/test/ui/std-backtrace.rs +++ b/src/test/ui/std-backtrace.rs @@ -16,9 +16,9 @@ use std::str; fn main() { let args: Vec = env::args().collect(); if args.len() >= 2 && args[1] == "force" { - println!("{}", std::backtrace::Backtrace::force_capture()); + println!("stack backtrace:\n{}", std::backtrace::Backtrace::force_capture()); } else if args.len() >= 2 { - println!("{}", std::backtrace::Backtrace::capture()); + println!("stack backtrace:\n{}", std::backtrace::Backtrace::capture()); } else { runtest(&args[0]); println!("test ok"); -- cgit 1.4.1-3-g733a5 From 8fb8bb4b3ff25570a7a9b105c1a569bb2307f25f Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 12 Feb 2020 19:51:30 +0000 Subject: Fix std::fs::copy on WASI target Previously `std::fs::copy` on wasm32-wasi would reuse code from the `sys_common` module and would successfully copy contents of the file just to fail right before closing it. This was happening because `sys_common::copy` tries to copy permissions of the file, but permissions are not a thing in WASI (at least yet) and `set_permissions` is implemented as an unconditional runtime error. This change instead adds a custom working implementation of `std::fs::copy` (like Rust already has on some other targets) that doesn't try to call `set_permissions` and is essentially a thin wrapper around `std::io::copy`. Fixes #68560. --- src/libstd/sys/wasi/fs.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/wasi/fs.rs b/src/libstd/sys/wasi/fs.rs index 04bfdf67e12..a11f61fdd69 100644 --- a/src/libstd/sys/wasi/fs.rs +++ b/src/libstd/sys/wasi/fs.rs @@ -12,7 +12,6 @@ use crate::sys::time::SystemTime; use crate::sys::unsupported; use crate::sys_common::FromInner; -pub use crate::sys_common::fs::copy; pub use crate::sys_common::fs::remove_dir_all; pub struct File { @@ -647,3 +646,12 @@ fn open_parent(p: &Path) -> io::Result<(ManuallyDrop, PathBuf)> { pub fn osstr2str(f: &OsStr) -> io::Result<&str> { f.to_str().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "input must be utf-8")) } + +pub fn copy(from: &Path, to: &Path) -> io::Result { + use crate::fs::File; + + let mut reader = File::open(from)?; + let mut writer = File::create(to)?; + + io::copy(&mut reader, &mut writer) +} -- cgit 1.4.1-3-g733a5 From 57a62f5335c1e8178802d00dfac94212726ee240 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Thu, 13 Feb 2020 10:16:28 +0100 Subject: Add comment to SGX entry code --- src/libstd/sys/sgx/abi/entry.S | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libstd') diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index ed4db287229..1f06c9da3a9 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -151,6 +151,7 @@ elf_entry: pushfq andq $~0x40400, (%rsp) popfq +/* check for abort */ bt $0,.Laborted(%rip) jc .Lreentry_panic .endm -- cgit 1.4.1-3-g733a5 From d863978f89c74bd5cb90baa47331d421bbfd3936 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Fri, 14 Feb 2020 22:39:55 +0000 Subject: Fix tests after rebase --- src/libstd/future.rs | 3 +- src/test/ui/async-await/no-const-async.rs | 1 + src/test/ui/async-await/no-const-async.stderr | 12 ++++- .../borrow-immutable-upvar-mutation-impl-trait.rs | 14 +++++ ...rrow-immutable-upvar-mutation-impl-trait.stderr | 16 ++++++ .../ui/borrowck/borrow-immutable-upvar-mutation.rs | 26 ++++----- .../borrow-immutable-upvar-mutation.stderr | 61 +++++++++------------- 7 files changed, 82 insertions(+), 51 deletions(-) create mode 100644 src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.rs create mode 100644 src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.stderr (limited to 'src/libstd') diff --git a/src/libstd/future.rs b/src/libstd/future.rs index f74c84e6dfd..7b1beb1ecda 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -16,9 +16,10 @@ pub use core::future::*; /// /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`). +// This is `const` to avoid extra errors after we recover from `const async fn` #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] -pub fn from_generator>(x: T) -> impl Future { +pub const fn from_generator>(x: T) -> impl Future { GenFuture(x) } diff --git a/src/test/ui/async-await/no-const-async.rs b/src/test/ui/async-await/no-const-async.rs index b3c59734e03..57a9f175ca3 100644 --- a/src/test/ui/async-await/no-const-async.rs +++ b/src/test/ui/async-await/no-const-async.rs @@ -3,3 +3,4 @@ pub const async fn x() {} //~^ ERROR functions cannot be both `const` and `async` +//~| ERROR `impl Trait` in const fn is unstable diff --git a/src/test/ui/async-await/no-const-async.stderr b/src/test/ui/async-await/no-const-async.stderr index f6ae0f1447c..07559cd240b 100644 --- a/src/test/ui/async-await/no-const-async.stderr +++ b/src/test/ui/async-await/no-const-async.stderr @@ -7,5 +7,15 @@ LL | pub const async fn x() {} | | `async` because of this | `const` because of this -error: aborting due to previous error +error[E0723]: `impl Trait` in const fn is unstable + --> $DIR/no-const-async.rs:4:24 + | +LL | pub const async fn x() {} + | ^ + | + = note: see issue #57563 for more information + = help: add `#![feature(const_fn)]` to the crate attributes to enable + +error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0723`. diff --git a/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.rs b/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.rs new file mode 100644 index 00000000000..57198cb95e7 --- /dev/null +++ b/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.rs @@ -0,0 +1,14 @@ +#![feature(unboxed_closures)] + +// Tests that we can't assign to or mutably borrow upvars from `Fn` +// closures (issue #17780) + +fn main() {} + +fn bar() -> impl Fn() -> usize { + let mut x = 0; + move || { + x += 1; //~ ERROR cannot assign + x + } +} diff --git a/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.stderr b/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.stderr new file mode 100644 index 00000000000..003c40d2773 --- /dev/null +++ b/src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.stderr @@ -0,0 +1,16 @@ +error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure + --> $DIR/borrow-immutable-upvar-mutation-impl-trait.rs:11:9 + | +LL | fn bar() -> impl Fn() -> usize { + | --- ------------------ change this to return `FnMut` instead of `Fn` +LL | let mut x = 0; +LL | / move || { +LL | | x += 1; + | | ^^^^^^ cannot assign +LL | | x +LL | | } + | |_____- in this closure + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. diff --git a/src/test/ui/borrowck/borrow-immutable-upvar-mutation.rs b/src/test/ui/borrowck/borrow-immutable-upvar-mutation.rs index 62e27bcf164..e2f016614bf 100644 --- a/src/test/ui/borrowck/borrow-immutable-upvar-mutation.rs +++ b/src/test/ui/borrowck/borrow-immutable-upvar-mutation.rs @@ -3,10 +3,16 @@ // Tests that we can't assign to or mutably borrow upvars from `Fn` // closures (issue #17780) -fn set(x: &mut usize) { *x = 5; } +fn set(x: &mut usize) { + *x = 5; +} -fn to_fn>(f: F) -> F { f } -fn to_fn_mut>(f: F) -> F { f } +fn to_fn>(f: F) -> F { + f +} +fn to_fn_mut>(f: F) -> F { + f +} fn main() { // By-ref captures @@ -33,7 +39,11 @@ fn main() { let _g = to_fn(move || set(&mut y)); //~ ERROR cannot borrow let mut z = 0; - let _h = to_fn_mut(move || { set(&mut z); to_fn(move || z = 42); }); //~ ERROR cannot assign + let _h = to_fn_mut(move || { + set(&mut z); + to_fn(move || z = 42); + //~^ ERROR cannot assign + }); } } @@ -44,11 +54,3 @@ fn foo() -> Box usize> { x }) } - -fn bar() -> impl Fn() -> usize { - let mut x = 0; - move || { - x += 1; //~ ERROR cannot assign - x - } -} diff --git a/src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr b/src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr index 3046b047d00..a28cb7431e6 100644 --- a/src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr +++ b/src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr @@ -1,8 +1,8 @@ error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:15:27 + --> $DIR/borrow-immutable-upvar-mutation.rs:21:27 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... LL | let _f = to_fn(|| x = 42); | ----- ^^^^^^ cannot assign @@ -10,10 +10,10 @@ LL | let _f = to_fn(|| x = 42); | expects `Fn` instead of `FnMut` error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:18:31 + --> $DIR/borrow-immutable-upvar-mutation.rs:24:31 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... LL | let _g = to_fn(|| set(&mut y)); | ----- ^^^^^^ cannot borrow as mutable @@ -21,10 +21,10 @@ LL | let _g = to_fn(|| set(&mut y)); | expects `Fn` instead of `FnMut` error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:23:22 + --> $DIR/borrow-immutable-upvar-mutation.rs:29:22 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... LL | to_fn(|| z = 42); | ----- ^^^^^^ cannot assign @@ -32,10 +32,10 @@ LL | to_fn(|| z = 42); | expects `Fn` instead of `FnMut` error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:30:32 + --> $DIR/borrow-immutable-upvar-mutation.rs:36:32 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... LL | let _f = to_fn(move || x = 42); | ----- ^^^^^^ cannot assign @@ -43,10 +43,10 @@ LL | let _f = to_fn(move || x = 42); | expects `Fn` instead of `FnMut` error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:33:36 + --> $DIR/borrow-immutable-upvar-mutation.rs:39:36 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... LL | let _g = to_fn(move || set(&mut y)); | ----- ^^^^^^ cannot borrow as mutable @@ -54,18 +54,18 @@ LL | let _g = to_fn(move || set(&mut y)); | expects `Fn` instead of `FnMut` error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:36:65 + --> $DIR/borrow-immutable-upvar-mutation.rs:44:27 | -LL | fn to_fn>(f: F) -> F { f } - | - change this to accept `FnMut` instead of `Fn` +LL | fn to_fn>(f: F) -> F { + | - change this to accept `FnMut` instead of `Fn` ... -LL | let _h = to_fn_mut(move || { set(&mut z); to_fn(move || z = 42); }); - | ----- ^^^^^^ cannot assign - | | - | expects `Fn` instead of `FnMut` +LL | to_fn(move || z = 42); + | ----- ^^^^^^ cannot assign + | | + | expects `Fn` instead of `FnMut` error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:43:9 + --> $DIR/borrow-immutable-upvar-mutation.rs:53:9 | LL | fn foo() -> Box usize> { | --- ---------------------- change this to return `FnMut` instead of `Fn` @@ -78,20 +78,7 @@ LL | | x LL | | }) | |_____- in this closure -error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure - --> $DIR/borrow-immutable-upvar-mutation.rs:51:9 - | -LL | fn bar() -> impl Fn() -> usize { - | --- ------------------ change this to return `FnMut` instead of `Fn` -LL | let mut x = 0; -LL | / move || { -LL | | x += 1; - | | ^^^^^^ cannot assign -LL | | x -LL | | } - | |_____- in this closure - -error: aborting due to 8 previous errors +error: aborting due to 7 previous errors Some errors have detailed explanations: E0594, E0596. For more information about an error, try `rustc --explain E0594`. -- cgit 1.4.1-3-g733a5 From 67068f35dd41409c8e79710f1335cc9dc64f1860 Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sun, 2 Feb 2020 21:25:38 +0900 Subject: macOS: avoid calling pthread_self() twice --- src/libstd/sys/unix/thread.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 3ca778354e4..674d4c71138 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -255,8 +255,9 @@ pub mod guard { #[cfg(target_os = "macos")] unsafe fn get_stack_start() -> Option<*mut libc::c_void> { - let stackaddr = libc::pthread_get_stackaddr_np(libc::pthread_self()) as usize - - libc::pthread_get_stacksize_np(libc::pthread_self()); + let th = libc::pthread_self(); + let stackaddr = + libc::pthread_get_stackaddr_np(th) as usize - libc::pthread_get_stacksize_np(th); Some(stackaddr as *mut libc::c_void) } -- cgit 1.4.1-3-g733a5 From c899dc14011a67b127bf59622cb13b7ff4a11e9a Mon Sep 17 00:00:00 2001 From: jumbatm Date: Wed, 19 Feb 2020 19:57:32 +1000 Subject: Reword OpenOptions::{create, create_new} doc. --- src/libstd/fs.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index cff7bbe5ef1..09be3f13050 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -844,10 +844,7 @@ impl OpenOptions { self } - /// Sets the option for creating a new file. - /// - /// This option indicates whether a new file will be created if the file - /// does not yet already exist. + /// Sets the option to create a new file, or open it if it already exists. /// /// In order for the file to be created, [`write`] or [`append`] access must /// be used. @@ -868,11 +865,10 @@ impl OpenOptions { self } - /// Sets the option to always create a new file. + /// Sets the option to create a new file, failing if it already exists. /// - /// This option indicates whether a new file will be created. - /// No file is allowed to exist at the target location, also no (dangling) - /// symlink. + /// No file is allowed to exist at the target location, also no (dangling) symlink. In this + /// way, if the call succeeds, the file returned is guaranteed to be new. /// /// This option is useful because it is atomic. Otherwise between checking /// whether a file exists and creating a new one, the file may have been -- cgit 1.4.1-3-g733a5 From 40c67221e272c007cfae407d4398b166754491c9 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Sat, 22 Feb 2020 15:13:22 -0800 Subject: make doc comments regular comments --- src/libstd/sys/wasi/fd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/wasi/fd.rs b/src/libstd/sys/wasi/fd.rs index 00327c1743c..bf7db3a799e 100644 --- a/src/libstd/sys/wasi/fd.rs +++ b/src/libstd/sys/wasi/fd.rs @@ -13,7 +13,7 @@ pub struct WasiFd { fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::Iovec] { assert_eq!(mem::size_of::>(), mem::size_of::()); assert_eq!(mem::align_of::>(), mem::align_of::()); - /// SAFETY: `IoSliceMut` and `IoVec` have exactly the same memory layout + // SAFETY: `IoSliceMut` and `IoVec` have exactly the same memory layout unsafe { mem::transmute(a) } @@ -22,7 +22,7 @@ fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::Iovec] { fn ciovec<'a>(a: &'a [IoSlice<'_>]) -> &'a [wasi::Ciovec] { assert_eq!(mem::size_of::>(), mem::size_of::()); assert_eq!(mem::align_of::>(), mem::align_of::()); - /// SAFETY: `IoSlice` and `CIoVec` have exactly the same memory layout + // SAFETY: `IoSlice` and `CIoVec` have exactly the same memory layout unsafe { mem::transmute(a) } -- cgit 1.4.1-3-g733a5 From 09bc5e3d969a154ffcbeb6827a901d36a6a854eb Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Sat, 22 Feb 2020 15:29:03 -0800 Subject: rustfmt darnit --- src/libstd/sys/wasi/fd.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/sys/wasi/fd.rs b/src/libstd/sys/wasi/fd.rs index bf7db3a799e..8458ded5db0 100644 --- a/src/libstd/sys/wasi/fd.rs +++ b/src/libstd/sys/wasi/fd.rs @@ -14,18 +14,14 @@ fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::Iovec] { assert_eq!(mem::size_of::>(), mem::size_of::()); assert_eq!(mem::align_of::>(), mem::align_of::()); // SAFETY: `IoSliceMut` and `IoVec` have exactly the same memory layout - unsafe { - mem::transmute(a) - } + unsafe { mem::transmute(a) } } fn ciovec<'a>(a: &'a [IoSlice<'_>]) -> &'a [wasi::Ciovec] { assert_eq!(mem::size_of::>(), mem::size_of::()); assert_eq!(mem::align_of::>(), mem::align_of::()); // SAFETY: `IoSlice` and `CIoVec` have exactly the same memory layout - unsafe { - mem::transmute(a) - } + unsafe { mem::transmute(a) } } impl WasiFd { -- cgit 1.4.1-3-g733a5 From b3e0d272af149c4126f90a0262d796d7401ba7a1 Mon Sep 17 00:00:00 2001 From: Jakub Kądziołka Date: Sun, 23 Feb 2020 21:19:25 +0100 Subject: docs: Stdin::read_line: mention the appending --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 6add644dcc2..d410faca30d 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -302,7 +302,7 @@ impl Stdin { StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) } } - /// Locks this handle and reads a line of input into the specified buffer. + /// Locks this handle and reads a line of input, appending it to the specified buffer. /// /// For detailed semantics of this method, see the documentation on /// [`BufRead::read_line`]. -- cgit 1.4.1-3-g733a5 From 9c3ee1bc351fcfabcfd47947dc44b0c020427181 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 23 Feb 2020 23:59:39 -0800 Subject: Bump core::primitive to 1.43 --- src/libcore/lib.rs | 2 +- src/libcore/primitive.rs | 34 +++++++++++++++++----------------- src/libstd/lib.rs | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src/libstd') diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 257a6d371b7..b939462afc7 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -262,7 +262,7 @@ mod bool; mod tuple; mod unit; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub mod primitive; // Pull in the `core_arch` crate directly into libcore. The contents of diff --git a/src/libcore/primitive.rs b/src/libcore/primitive.rs index d37bbfaf5df..e20b2c5c938 100644 --- a/src/libcore/primitive.rs +++ b/src/libcore/primitive.rs @@ -31,37 +31,37 @@ //! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; } //! ``` -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use bool; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use char; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use f32; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use f64; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use i128; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use i16; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use i32; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use i64; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use i8; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use isize; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use str; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use u128; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use u16; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use u32; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use u64; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use u8; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use usize; diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 2b54481ab56..a495d29cc47 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -550,7 +550,7 @@ pub use core::{ trace_macros, }; -#[stable(feature = "core_primitive", since = "1.42.0")] +#[stable(feature = "core_primitive", since = "1.43.0")] pub use core::primitive; // Include a number of private modules that exist solely to provide -- cgit 1.4.1-3-g733a5