@@ -69,12 +69,12 @@ pub use core::error::Error;
69
69
/// assert_eq!(err.to_string(), "invalid digit found in string");
70
70
/// ```
71
71
///
72
- /// Errors may provide cause chain information. [`Error::source()`] is generally
72
+ /// Errors may provide cause information. [`Error::source()`] is generally
73
73
/// used when errors cross "abstraction boundaries". If one module must report
74
74
/// an error that is caused by an error from a lower-level module, it can allow
75
75
/// accessing that error via [`Error::source()`]. This makes it possible for the
76
76
/// high-level module to provide its own errors while also revealing some of the
77
- /// implementation for debugging via `source` chains .
77
+ /// implementation for debugging.
78
78
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
79
79
#[ cfg_attr( not( test) , rustc_diagnostic_item = "Error" ) ]
80
80
#[ cfg( bootstrap) ]
@@ -976,7 +976,7 @@ impl dyn Error {
976
976
/// // let err : Box<Error> = b.into(); // or
977
977
/// let err = &b as &(dyn Error);
978
978
///
979
- /// let mut iter = err.chain ();
979
+ /// let mut iter = err.sources ();
980
980
///
981
981
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
982
982
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -985,8 +985,23 @@ impl dyn Error {
985
985
/// ```
986
986
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
987
987
#[ inline]
988
- pub fn chain ( & self ) -> Chain < ' _ > {
989
- Chain { current : Some ( self ) }
988
+ pub fn sources ( & self ) -> Sources < ' _ > {
989
+ // You may think this method would be better in the Error trait, and you'd be right.
990
+ // Unfortunately that doesn't work, not because of the object safety rules but because we
991
+ // save a reference to self in Sources below as a trait object. If this method was
992
+ // declared in Error, then self would have the type &T where T is some concrete type which
993
+ // implements Error. We would need to coerce self to have type &dyn Error, but that requires
994
+ // that Self has a known size (i.e., Self: Sized). We can't put that bound on Error
995
+ // since that would forbid Error trait objects, and we can't put that bound on the method
996
+ // because that means the method can't be called on trait objects (we'd also need the
997
+ // 'static bound, but that isn't allowed because methods with bounds on Self other than
998
+ // Sized are not object-safe). Requiring an Unsize bound is not backwards compatible.
999
+ //
1000
+ // Two possible solutions are to start the iterator at self.source() instead of self (see
1001
+ // discussion on the tracking issue), or to wait for dyn* to exist (which would then permit
1002
+ // the coercion).
1003
+
1004
+ Sources { current : Some ( self ) }
990
1005
}
991
1006
}
992
1007
@@ -997,13 +1012,13 @@ impl dyn Error {
997
1012
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
998
1013
#[ derive( Clone , Debug ) ]
999
1014
#[ cfg( bootstrap) ]
1000
- pub struct Chain < ' a > {
1015
+ pub struct Sources < ' a > {
1001
1016
current : Option < & ' a ( dyn Error + ' static ) > ,
1002
1017
}
1003
1018
1004
1019
#[ cfg( bootstrap) ]
1005
1020
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
1006
- impl < ' a > Iterator for Chain < ' a > {
1021
+ impl < ' a > Iterator for Sources < ' a > {
1007
1022
type Item = & ' a ( dyn Error + ' static ) ;
1008
1023
1009
1024
fn next ( & mut self ) -> Option < Self :: Item > {
@@ -1043,8 +1058,8 @@ impl dyn Error + Send + Sync {
1043
1058
1044
1059
/// An error reporter that prints an error and its sources.
1045
1060
///
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.
1061
+ /// Report also exposes configuration options for formatting the error sources , either entirely on a
1062
+ /// single line, or in multi-line format with each source on a new line.
1048
1063
///
1049
1064
/// `Report` only requires that the wrapped error implement `Error`. It doesn't require that the
1050
1065
/// wrapped error be `Send`, `Sync`, or `'static`.
@@ -1389,7 +1404,7 @@ impl<E> Report<E> {
1389
1404
///
1390
1405
/// **Note**: Report will search for the first `Backtrace` it can find starting from the
1391
1406
/// outermost error. In this example it will display the backtrace from the second error in the
1392
- /// chain , `SuperErrorSideKick`.
1407
+ /// sources , `SuperErrorSideKick`.
1393
1408
///
1394
1409
/// ```rust
1395
1410
/// #![feature(error_reporter)]
@@ -1486,7 +1501,7 @@ where
1486
1501
let backtrace = backtrace. or_else ( || {
1487
1502
self . error
1488
1503
. source ( )
1489
- . map ( |source| source. chain ( ) . find_map ( |source| source. request_ref ( ) ) )
1504
+ . map ( |source| source. sources ( ) . find_map ( |source| source. request_ref ( ) ) )
1490
1505
. flatten ( )
1491
1506
} ) ;
1492
1507
backtrace
@@ -1497,7 +1512,7 @@ where
1497
1512
fn fmt_singleline ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1498
1513
write ! ( f, "{}" , self . error) ?;
1499
1514
1500
- let sources = self . error . source ( ) . into_iter ( ) . flat_map ( <dyn Error >:: chain ) ;
1515
+ let sources = self . error . source ( ) . into_iter ( ) . flat_map ( <dyn Error >:: sources ) ;
1501
1516
1502
1517
for cause in sources {
1503
1518
write ! ( f, ": {cause}" ) ?;
@@ -1518,7 +1533,7 @@ where
1518
1533
1519
1534
let multiple = cause. source ( ) . is_some ( ) ;
1520
1535
1521
- for ( ind, error) in cause. chain ( ) . enumerate ( ) {
1536
+ for ( ind, error) in cause. sources ( ) . enumerate ( ) {
1522
1537
writeln ! ( f) ?;
1523
1538
let mut indented = Indented { inner : f } ;
1524
1539
if multiple {
0 commit comments