11#![ doc = include_str ! ( "../../core/src/error.md" ) ]
22#![ stable( feature = "rust1" , since = "1.0.0" ) ]
33
4- // A note about crates and the facade:
5- //
6- // Originally, the `Error` trait was defined in libcore, and the impls
7- // were scattered about. However, coherence objected to this
8- // arrangement, because to create the blanket impls for `Box` required
9- // knowing that `&str: !Error`, and we have no means to deal with that
10- // sort of conflict just now. Therefore, for the time being, we have
11- // moved the `Error` trait into libstd. As we evolve a sol'n to the
12- // coherence challenge (e.g., specialization, neg impls, etc) we can
13- // reconsider what crate these items belong in.
14-
154#[ cfg( test) ]
165mod tests;
176
@@ -69,12 +58,12 @@ pub use core::error::Error;
6958/// assert_eq!(err.to_string(), "invalid digit found in string");
7059/// ```
7160///
72- /// Errors may provide cause chain information. [`Error::source()`] is generally
61+ /// Errors may provide cause information. [`Error::source()`] is generally
7362/// used when errors cross "abstraction boundaries". If one module must report
7463/// an error that is caused by an error from a lower-level module, it can allow
7564/// accessing that error via [`Error::source()`]. This makes it possible for the
7665/// high-level module to provide its own errors while also revealing some of the
77- /// implementation for debugging via `source` chains .
66+ /// implementation for debugging.
7867#[ stable( feature = "rust1" , since = "1.0.0" ) ]
7968#[ cfg_attr( not( test) , rustc_diagnostic_item = "Error" ) ]
8069#[ cfg( bootstrap) ]
@@ -221,8 +210,8 @@ pub trait Error: Debug + Display {
221210 /// }
222211 ///
223212 /// impl std::error::Error for Error {
224- /// fn provide<'a>(&'a self, req : &mut Demand<'a>) {
225- /// req
213+ /// fn provide<'a>(&'a self, demand : &mut Demand<'a>) {
214+ /// demand
226215 /// .provide_ref::<MyBacktrace>(&self.backtrace)
227216 /// .provide_ref::<dyn std::error::Error + 'static>(&self.source);
228217 /// }
@@ -240,14 +229,14 @@ pub trait Error: Debug + Display {
240229 /// ```
241230 #[ unstable( feature = "error_generic_member_access" , issue = "99301" ) ]
242231 #[ allow( unused_variables) ]
243- fn provide < ' a > ( & ' a self , req : & mut Demand < ' a > ) { }
232+ fn provide < ' a > ( & ' a self , demand : & mut Demand < ' a > ) { }
244233}
245234
246235#[ cfg( bootstrap) ]
247236#[ unstable( feature = "error_generic_member_access" , issue = "99301" ) ]
248237impl < ' b > Provider for dyn Error + ' b {
249- fn provide < ' a > ( & ' a self , req : & mut Demand < ' a > ) {
250- self . provide ( req )
238+ fn provide < ' a > ( & ' a self , demand : & mut Demand < ' a > ) {
239+ self . provide ( demand )
251240 }
252241}
253242
@@ -659,8 +648,8 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
659648 Error :: source ( & * * self )
660649 }
661650
662- fn provide < ' b > ( & ' b self , req : & mut Demand < ' b > ) {
663- Error :: provide ( & * * self , req ) ;
651+ fn provide < ' b > ( & ' b self , demand : & mut Demand < ' b > ) {
652+ Error :: provide ( & * * self , demand ) ;
664653 }
665654}
666655
@@ -681,8 +670,8 @@ impl<T: Error + ?Sized> Error for Arc<T> {
681670 Error :: source ( & * * self )
682671 }
683672
684- fn provide < ' a > ( & ' a self , req : & mut Demand < ' a > ) {
685- Error :: provide ( & * * self , req ) ;
673+ fn provide < ' a > ( & ' a self , demand : & mut Demand < ' a > ) {
674+ Error :: provide ( & * * self , demand ) ;
686675 }
687676}
688677
@@ -976,7 +965,7 @@ impl dyn Error {
976965 /// // let err : Box<Error> = b.into(); // or
977966 /// let err = &b as &(dyn Error);
978967 ///
979- /// let mut iter = err.chain ();
968+ /// let mut iter = err.sources ();
980969 ///
981970 /// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
982971 /// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -985,8 +974,19 @@ impl dyn Error {
985974 /// ```
986975 #[ unstable( feature = "error_iter" , issue = "58520" ) ]
987976 #[ inline]
988- pub fn chain ( & self ) -> Chain < ' _ > {
989- Chain { current : Some ( self ) }
977+ pub fn sources ( & self ) -> Sources < ' _ > {
978+ // You may think this method would be better in the Error trait, and you'd be right.
979+ // Unfortunately that doesn't work, not because of the object safety rules but because we
980+ // save a reference to self in Sources below as a trait object. If this method was
981+ // declared in Error, then self would have the type &T where T is some concrete type which
982+ // implements Error. We would need to coerce self to have type &dyn Error, but that requires
983+ // that Self has a known size (i.e., Self: Sized). We can't put that bound on Error
984+ // since that would forbid Error trait objects, and we can't put that bound on the method
985+ // because that means the method can't be called on trait objects (we'd also need the
986+ // 'static bound, but that isn't allowed because methods with bounds on Self other than
987+ // Sized are not object-safe). Requiring an Unsize bound is not backwards compatible.
988+
989+ Sources { current : Some ( self ) }
990990 }
991991}
992992
@@ -997,13 +997,13 @@ impl dyn Error {
997997#[ unstable( feature = "error_iter" , issue = "58520" ) ]
998998#[ derive( Clone , Debug ) ]
999999#[ cfg( bootstrap) ]
1000- pub struct Chain < ' a > {
1000+ pub struct Sources < ' a > {
10011001 current : Option < & ' a ( dyn Error + ' static ) > ,
10021002}
10031003
10041004#[ cfg( bootstrap) ]
10051005#[ unstable( feature = "error_iter" , issue = "58520" ) ]
1006- impl < ' a > Iterator for Chain < ' a > {
1006+ impl < ' a > Iterator for Sources < ' a > {
10071007 type Item = & ' a ( dyn Error + ' static ) ;
10081008
10091009 fn next ( & mut self ) -> Option < Self :: Item > {
@@ -1043,8 +1043,8 @@ impl dyn Error + Send + Sync {
10431043
10441044/// An error reporter that prints an error and its sources.
10451045///
1046- /// Report also exposes configuration options for formatting the error chain , either entirely on a
1047- /// single line, or in multi-line format with each cause in the error chain on a new line.
1046+ /// Report also exposes configuration options for formatting the error sources , either entirely on a
1047+ /// single line, or in multi-line format with each source on a new line.
10481048///
10491049/// `Report` only requires that the wrapped error implement `Error`. It doesn't require that the
10501050/// wrapped error be `Send`, `Sync`, or `'static`.
@@ -1389,7 +1389,7 @@ impl<E> Report<E> {
13891389 ///
13901390 /// **Note**: Report will search for the first `Backtrace` it can find starting from the
13911391 /// outermost error. In this example it will display the backtrace from the second error in the
1392- /// chain , `SuperErrorSideKick`.
1392+ /// sources , `SuperErrorSideKick`.
13931393 ///
13941394 /// ```rust
13951395 /// #![feature(error_reporter)]
@@ -1427,9 +1427,8 @@ impl<E> Report<E> {
14271427 /// }
14281428 ///
14291429 /// impl Error for SuperErrorSideKick {
1430- /// fn provide<'a>(&'a self, req: &mut Demand<'a>) {
1431- /// req
1432- /// .provide_ref::<Backtrace>(&self.backtrace);
1430+ /// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
1431+ /// demand.provide_ref::<Backtrace>(&self.backtrace);
14331432 /// }
14341433 /// }
14351434 ///
@@ -1486,7 +1485,7 @@ where
14861485 let backtrace = backtrace. or_else ( || {
14871486 self . error
14881487 . source ( )
1489- . map ( |source| source. chain ( ) . find_map ( |source| source. request_ref ( ) ) )
1488+ . map ( |source| source. sources ( ) . find_map ( |source| source. request_ref ( ) ) )
14901489 . flatten ( )
14911490 } ) ;
14921491 backtrace
@@ -1497,7 +1496,7 @@ where
14971496 fn fmt_singleline ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
14981497 write ! ( f, "{}" , self . error) ?;
14991498
1500- let sources = self . error . source ( ) . into_iter ( ) . flat_map ( <dyn Error >:: chain ) ;
1499+ let sources = self . error . source ( ) . into_iter ( ) . flat_map ( <dyn Error >:: sources ) ;
15011500
15021501 for cause in sources {
15031502 write ! ( f, ": {cause}" ) ?;
@@ -1518,7 +1517,7 @@ where
15181517
15191518 let multiple = cause. source ( ) . is_some ( ) ;
15201519
1521- for ( ind, error) in cause. chain ( ) . enumerate ( ) {
1520+ for ( ind, error) in cause. sources ( ) . enumerate ( ) {
15221521 writeln ! ( f) ?;
15231522 let mut indented = Indented { inner : f } ;
15241523 if multiple {
0 commit comments