Skip to content

Commit f39f218

Browse files
committed
Auto merge of #53533 - withoutboats:error-source, r=withoutboats
Add Error::source method per RFC 2504. This implements part of RFC 2504. * Adds `Error::source`, a replacement for `Error::cause` with the "right" signature, which will be instantly stable. * Deprecates `Error::cause` in 1.33 (this choice was based on the precedent in #52994, which we haven't finalized). * Redefines `Error::cause` to delegate to `Error::source` (the delegation can only go in this direction, not the other). @rfcbot fcp merge
2 parents 839d99c + e2e4f57 commit f39f218

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

src/libstd/error.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,72 @@ pub trait Error: Debug + Display {
138138
/// }
139139
/// ```
140140
#[stable(feature = "rust1", since = "1.0.0")]
141-
fn cause(&self) -> Option<&dyn Error> { None }
141+
#[rustc_deprecated(since = "1.33.0", reason = "replaced by Error::source, which can support \
142+
downcasting")]
143+
fn cause(&self) -> Option<&dyn Error> {
144+
self.source()
145+
}
146+
147+
/// The lower-level source of this error, if any.
148+
///
149+
/// # Examples
150+
///
151+
/// ```
152+
/// use std::error::Error;
153+
/// use std::fmt;
154+
///
155+
/// #[derive(Debug)]
156+
/// struct SuperError {
157+
/// side: SuperErrorSideKick,
158+
/// }
159+
///
160+
/// impl fmt::Display for SuperError {
161+
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162+
/// write!(f, "SuperError is here!")
163+
/// }
164+
/// }
165+
///
166+
/// impl Error for SuperError {
167+
/// fn description(&self) -> &str {
168+
/// "I'm the superhero of errors"
169+
/// }
170+
///
171+
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
172+
/// Some(&self.side)
173+
/// }
174+
/// }
175+
///
176+
/// #[derive(Debug)]
177+
/// struct SuperErrorSideKick;
178+
///
179+
/// impl fmt::Display for SuperErrorSideKick {
180+
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181+
/// write!(f, "SuperErrorSideKick is here!")
182+
/// }
183+
/// }
184+
///
185+
/// impl Error for SuperErrorSideKick {
186+
/// fn description(&self) -> &str {
187+
/// "I'm SuperError side kick"
188+
/// }
189+
/// }
190+
///
191+
/// fn get_super_error() -> Result<(), SuperError> {
192+
/// Err(SuperError { side: SuperErrorSideKick })
193+
/// }
194+
///
195+
/// fn main() {
196+
/// match get_super_error() {
197+
/// Err(e) => {
198+
/// println!("Error: {}", e.description());
199+
/// println!("Caused by: {}", e.source().unwrap());
200+
/// }
201+
/// _ => println!("No error"),
202+
/// }
203+
/// }
204+
/// ```
205+
#[stable(feature = "error_source", since = "1.30.0")]
206+
fn source(&self) -> Option<&(dyn Error + 'static)> { None }
142207

143208
/// Get the `TypeId` of `self`
144209
#[doc(hidden)]

0 commit comments

Comments
 (0)