diff --git a/src/libstd/error.rs b/src/liballoc/error.rs similarity index 94% rename from src/libstd/error.rs rename to src/liballoc/error.rs index 117a430eec6b9..3f479bc2f4ca4 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 /// @@ -75,6 +76,7 @@ pub trait Error: Debug + Display { /// # Examples /// /// ``` + /// # #![allow(deprecated)] /// use std::error::Error; /// use std::fmt; /// @@ -887,48 +889,3 @@ impl dyn Error + Send + Sync { }) } } - -#[cfg(test)] -mod tests { - use super::Error; - use crate::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/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/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), + } +} 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;