From 0411f254b187cce1e263573b1da980110ed38729 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 30 Aug 2019 17:22:31 +0900 Subject: [PATCH 1/5] Move error::Error trait from libstd to liballoc --- src/{libstd => liballoc}/error.rs | 27 ++++++++++++++------------- src/liballoc/lib.rs | 6 ++++++ src/libstd/lib.rs | 4 +++- 3 files changed, 23 insertions(+), 14 deletions(-) rename src/{libstd => liballoc}/error.rs (98%) diff --git a/src/libstd/error.rs b/src/liballoc/error.rs similarity index 98% rename from src/libstd/error.rs rename to src/liballoc/error.rs index 117a430eec6b9..1c2532e3302f5 100644 --- a/src/libstd/error.rs +++ b/src/liballoc/error.rs @@ -13,18 +13,19 @@ // coherence challenge (e.g., specialization, neg impls, etc) we can // reconsider what crate these items belong in. +use core::any::TypeId; use core::array; +use core::cell; +use core::char; +use core::fmt::{self, Debug, Display}; +use core::mem::transmute; +use core::num; +use core::str; use crate::alloc::{AllocErr, LayoutErr, CannotReallocInPlace}; -use crate::any::TypeId; use crate::borrow::Cow; -use crate::cell; -use crate::char; -use crate::fmt::{self, Debug, Display}; -use crate::mem::transmute; -use crate::num; -use crate::str; -use crate::string; +use crate::boxed::Box; +use crate::string::{self, String}; /// `Error` is a trait representing the basic expectations for error values, /// i.e., values of type `E` in [`Result`]. Errors must describe @@ -38,9 +39,9 @@ use crate::string; /// provide its own errors while also revealing some of the implementation for /// debugging via [`source`] chains. /// -/// [`Result`]: ../result/enum.Result.html -/// [`Display`]: ../fmt/trait.Display.html -/// [`Debug`]: ../fmt/trait.Debug.html +/// [`Result`]: ../../std/result/enum.Result.html +/// [`Display`]: ../../std/fmt/trait.Display.html +/// [`Debug`]: ../../std/fmt/trait.Debug.html /// [`source`]: trait.Error.html#method.source #[stable(feature = "rust1", since = "1.0.0")] pub trait Error: Debug + Display { @@ -52,7 +53,7 @@ pub trait Error: Debug + Display { /// /// To obtain error description as a string, use `to_string()`. /// - /// [`Display`]: ../fmt/trait.Display.html + /// [`Display`]: ../../std/fmt/trait.Display.html /// /// # Examples /// @@ -891,7 +892,7 @@ impl dyn Error + Send + Sync { #[cfg(test)] mod tests { use super::Error; - use crate::fmt; + use core::fmt; #[derive(Debug, PartialEq)] struct A; diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 4a48945adc37a..0060f5b380442 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -77,10 +77,12 @@ #![feature(allocator_api)] #![feature(allow_internal_unstable)] #![feature(arbitrary_self_types)] +#![feature(array_error_internals)] #![feature(box_into_raw_non_null)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(cfg_target_has_atomic)] +#![feature(char_error_internals)] #![feature(coerce_unsized)] #![feature(const_generic_impls_guard)] #![feature(const_generics)] @@ -93,9 +95,11 @@ #![feature(fmt_internals)] #![feature(fn_traits)] #![feature(fundamental)] +#![feature(int_error_internals)] #![feature(internal_uninit_const)] #![feature(lang_items)] #![feature(libc)] +#![feature(never_type)] #![feature(nll)] #![feature(optin_builtin_traits)] #![feature(pattern)] @@ -167,6 +171,8 @@ pub mod str; pub mod string; pub mod vec; +pub mod error; + #[cfg(not(test))] mod std { pub use core::ops; // RangeFull diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index c3882bacf87eb..cbe8bdd94c0de 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -447,6 +447,9 @@ pub use core::hint; #[stable(feature = "core_array", since = "1.36.0")] pub use core::array; +#[stable(feature = "rust1", since = "1.0.0")] +pub use alloc_crate::error; + pub mod f32; pub mod f64; @@ -455,7 +458,6 @@ pub mod thread; pub mod ascii; pub mod collections; pub mod env; -pub mod error; pub mod ffi; pub mod fs; pub mod io; From 47c3ad10a449b5cae0c36385e54da224752093e9 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 30 Aug 2019 17:23:08 +0900 Subject: [PATCH 2/5] Error trait in liballoc is unstable --- src/liballoc/error.rs | 2 +- src/libstd/lib.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/liballoc/error.rs b/src/liballoc/error.rs index 1c2532e3302f5..d78290ce976d5 100644 --- a/src/liballoc/error.rs +++ b/src/liballoc/error.rs @@ -1,6 +1,6 @@ //! Traits for working with Errors. -#![stable(feature = "rust1", since = "1.0.0")] +#![unstable(feature = "alloc_error", reason = "Error trait in liballoc is unstable", issue="0")] // A note about crates and the facade: // diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index cbe8bdd94c0de..6ee55c1f2cc2e 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -229,6 +229,7 @@ // compiler details that will never be stable // NB: the following list is sorted to minimize merge conflicts. #![feature(alloc_error_handler)] +#![feature(alloc_error)] #![feature(alloc_layout_extra)] #![feature(allocator_api)] #![feature(allocator_internals)] @@ -447,9 +448,6 @@ pub use core::hint; #[stable(feature = "core_array", since = "1.36.0")] pub use core::array; -#[stable(feature = "rust1", since = "1.0.0")] -pub use alloc_crate::error; - pub mod f32; pub mod f64; @@ -481,6 +479,14 @@ pub mod task { #[stable(feature = "futures_api", since = "1.36.0")] pub mod future; +#[stable(feature = "rust1", since = "1.0.0")] +pub mod error { + //! Traits for working with Errors. + + #[stable(feature = "rust1", since = "1.0.0")] + pub use alloc_crate::error::*; +} + // Platform-abstraction modules #[macro_use] mod sys_common; From 0e6ea5debc7aee5d1b43865482a76c2ee0ad3513 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 30 Aug 2019 18:23:01 +0900 Subject: [PATCH 3/5] Separate tests into tests/error.rs --- src/liballoc/error.rs | 45 ------------------------------------- src/liballoc/tests/error.rs | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 45 deletions(-) create mode 100644 src/liballoc/tests/error.rs diff --git a/src/liballoc/error.rs b/src/liballoc/error.rs index d78290ce976d5..0a436d5e42cc3 100644 --- a/src/liballoc/error.rs +++ b/src/liballoc/error.rs @@ -888,48 +888,3 @@ impl dyn Error + Send + Sync { }) } } - -#[cfg(test)] -mod tests { - use super::Error; - use core::fmt; - - #[derive(Debug, PartialEq)] - struct A; - #[derive(Debug, PartialEq)] - struct B; - - impl fmt::Display for A { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "A") - } - } - impl fmt::Display for B { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "B") - } - } - - impl Error for A { - fn description(&self) -> &str { "A-desc" } - } - impl Error for B { - fn description(&self) -> &str { "A-desc" } - } - - #[test] - fn downcasting() { - let mut a = A; - let a = &mut a as &mut (dyn Error + 'static); - assert_eq!(a.downcast_ref::(), Some(&A)); - assert_eq!(a.downcast_ref::(), None); - assert_eq!(a.downcast_mut::(), Some(&mut A)); - assert_eq!(a.downcast_mut::(), None); - - let a: Box = Box::new(A); - match a.downcast::() { - Ok(..) => panic!("expected error"), - Err(e) => assert_eq!(*e.downcast::().unwrap(), A), - } - } -} diff --git a/src/liballoc/tests/error.rs b/src/liballoc/tests/error.rs new file mode 100644 index 0000000000000..0cf73f2cf0075 --- /dev/null +++ b/src/liballoc/tests/error.rs @@ -0,0 +1,41 @@ +use std::error::Error; +use std::fmt; + +#[derive(Debug, PartialEq)] +struct A; +#[derive(Debug, PartialEq)] +struct B; + +impl fmt::Display for A { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "A") + } +} +impl fmt::Display for B { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "B") + } +} + +impl Error for A { + fn description(&self) -> &str { "A-desc" } +} +impl Error for B { + fn description(&self) -> &str { "A-desc" } +} + +#[test] +fn downcasting() { + let mut a = A; + let a = &mut a as &mut (dyn Error + 'static); + assert_eq!(a.downcast_ref::(), Some(&A)); + assert_eq!(a.downcast_ref::(), None); + assert_eq!(a.downcast_mut::(), Some(&mut A)); + assert_eq!(a.downcast_mut::(), None); + + let a: Box = Box::new(A); + match a.downcast::() { + Ok(..) => panic!("expected error"), + Err(e) => assert_eq!(*e.downcast::().unwrap(), A), + } +} From 239a7ada49d4b6af81265bc7cdc454c610410d30 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 30 Aug 2019 20:11:08 +0900 Subject: [PATCH 4/5] Add allow(deprecated) to example of Error::cause --- src/liballoc/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/error.rs b/src/liballoc/error.rs index 0a436d5e42cc3..6a3bdd1ab8fe8 100644 --- a/src/liballoc/error.rs +++ b/src/liballoc/error.rs @@ -76,6 +76,7 @@ pub trait Error: Debug + Display { /// # Examples /// /// ``` + /// # #![allow(deprecated)] /// use std::error::Error; /// use std::fmt; /// From 2bdc6d7706d749536d69c6653551937f9d759355 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 30 Aug 2019 21:54:38 +0900 Subject: [PATCH 5/5] Revert "Error trait in liballoc is unstable" This reverts commit 47c3ad10a449b5cae0c36385e54da224752093e9. --- src/liballoc/error.rs | 2 +- src/libstd/lib.rs | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/liballoc/error.rs b/src/liballoc/error.rs index 6a3bdd1ab8fe8..3f479bc2f4ca4 100644 --- a/src/liballoc/error.rs +++ b/src/liballoc/error.rs @@ -1,6 +1,6 @@ //! Traits for working with Errors. -#![unstable(feature = "alloc_error", reason = "Error trait in liballoc is unstable", issue="0")] +#![stable(feature = "rust1", since = "1.0.0")] // A note about crates and the facade: // diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 6ee55c1f2cc2e..cbe8bdd94c0de 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -229,7 +229,6 @@ // compiler details that will never be stable // NB: the following list is sorted to minimize merge conflicts. #![feature(alloc_error_handler)] -#![feature(alloc_error)] #![feature(alloc_layout_extra)] #![feature(allocator_api)] #![feature(allocator_internals)] @@ -448,6 +447,9 @@ pub use core::hint; #[stable(feature = "core_array", since = "1.36.0")] pub use core::array; +#[stable(feature = "rust1", since = "1.0.0")] +pub use alloc_crate::error; + pub mod f32; pub mod f64; @@ -479,14 +481,6 @@ pub mod task { #[stable(feature = "futures_api", since = "1.36.0")] pub mod future; -#[stable(feature = "rust1", since = "1.0.0")] -pub mod error { - //! Traits for working with Errors. - - #[stable(feature = "rust1", since = "1.0.0")] - pub use alloc_crate::error::*; -} - // Platform-abstraction modules #[macro_use] mod sys_common;