1
1
#![ doc = include_str ! ( "../../core/src/error.md" ) ]
2
2
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
3
3
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
-
15
4
#[ cfg( test) ]
16
5
mod tests;
17
6
@@ -69,12 +58,12 @@ pub use core::error::Error;
69
58
/// assert_eq!(err.to_string(), "invalid digit found in string");
70
59
/// ```
71
60
///
72
- /// Errors may provide cause chain information. [`Error::source()`] is generally
61
+ /// Errors may provide cause information. [`Error::source()`] is generally
73
62
/// used when errors cross "abstraction boundaries". If one module must report
74
63
/// an error that is caused by an error from a lower-level module, it can allow
75
64
/// accessing that error via [`Error::source()`]. This makes it possible for the
76
65
/// 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.
78
67
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
79
68
#[ cfg_attr( not( test) , rustc_diagnostic_item = "Error" ) ]
80
69
#[ cfg( bootstrap) ]
@@ -221,8 +210,8 @@ pub trait Error: Debug + Display {
221
210
/// }
222
211
///
223
212
/// 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
226
215
/// .provide_ref::<MyBacktrace>(&self.backtrace)
227
216
/// .provide_ref::<dyn std::error::Error + 'static>(&self.source);
228
217
/// }
@@ -240,14 +229,14 @@ pub trait Error: Debug + Display {
240
229
/// ```
241
230
#[ unstable( feature = "error_generic_member_access" , issue = "99301" ) ]
242
231
#[ allow( unused_variables) ]
243
- fn provide < ' a > ( & ' a self , req : & mut Demand < ' a > ) { }
232
+ fn provide < ' a > ( & ' a self , demand : & mut Demand < ' a > ) { }
244
233
}
245
234
246
235
#[ cfg( bootstrap) ]
247
236
#[ unstable( feature = "error_generic_member_access" , issue = "99301" ) ]
248
237
impl < ' 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 )
251
240
}
252
241
}
253
242
@@ -659,8 +648,8 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
659
648
Error :: source ( & * * self )
660
649
}
661
650
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 ) ;
664
653
}
665
654
}
666
655
@@ -681,8 +670,8 @@ impl<T: Error + ?Sized> Error for Arc<T> {
681
670
Error :: source ( & * * self )
682
671
}
683
672
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 ) ;
686
675
}
687
676
}
688
677
@@ -976,7 +965,7 @@ impl dyn Error {
976
965
/// // let err : Box<Error> = b.into(); // or
977
966
/// let err = &b as &(dyn Error);
978
967
///
979
- /// let mut iter = err.chain ();
968
+ /// let mut iter = err.sources ();
980
969
///
981
970
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
982
971
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -985,8 +974,19 @@ impl dyn Error {
985
974
/// ```
986
975
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
987
976
#[ 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 ) }
990
990
}
991
991
}
992
992
@@ -997,13 +997,13 @@ impl dyn Error {
997
997
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
998
998
#[ derive( Clone , Debug ) ]
999
999
#[ cfg( bootstrap) ]
1000
- pub struct Chain < ' a > {
1000
+ pub struct Sources < ' a > {
1001
1001
current : Option < & ' a ( dyn Error + ' static ) > ,
1002
1002
}
1003
1003
1004
1004
#[ cfg( bootstrap) ]
1005
1005
#[ unstable( feature = "error_iter" , issue = "58520" ) ]
1006
- impl < ' a > Iterator for Chain < ' a > {
1006
+ impl < ' a > Iterator for Sources < ' a > {
1007
1007
type Item = & ' a ( dyn Error + ' static ) ;
1008
1008
1009
1009
fn next ( & mut self ) -> Option < Self :: Item > {
@@ -1043,8 +1043,8 @@ impl dyn Error + Send + Sync {
1043
1043
1044
1044
/// An error reporter that prints an error and its sources.
1045
1045
///
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.
1048
1048
///
1049
1049
/// `Report` only requires that the wrapped error implement `Error`. It doesn't require that the
1050
1050
/// wrapped error be `Send`, `Sync`, or `'static`.
@@ -1389,7 +1389,7 @@ impl<E> Report<E> {
1389
1389
///
1390
1390
/// **Note**: Report will search for the first `Backtrace` it can find starting from the
1391
1391
/// outermost error. In this example it will display the backtrace from the second error in the
1392
- /// chain , `SuperErrorSideKick`.
1392
+ /// sources , `SuperErrorSideKick`.
1393
1393
///
1394
1394
/// ```rust
1395
1395
/// #![feature(error_reporter)]
@@ -1427,9 +1427,8 @@ impl<E> Report<E> {
1427
1427
/// }
1428
1428
///
1429
1429
/// 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);
1433
1432
/// }
1434
1433
/// }
1435
1434
///
@@ -1486,7 +1485,7 @@ where
1486
1485
let backtrace = backtrace. or_else ( || {
1487
1486
self . error
1488
1487
. source ( )
1489
- . map ( |source| source. chain ( ) . find_map ( |source| source. request_ref ( ) ) )
1488
+ . map ( |source| source. sources ( ) . find_map ( |source| source. request_ref ( ) ) )
1490
1489
. flatten ( )
1491
1490
} ) ;
1492
1491
backtrace
@@ -1497,7 +1496,7 @@ where
1497
1496
fn fmt_singleline ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1498
1497
write ! ( f, "{}" , self . error) ?;
1499
1498
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 ) ;
1501
1500
1502
1501
for cause in sources {
1503
1502
write ! ( f, ": {cause}" ) ?;
@@ -1518,7 +1517,7 @@ where
1518
1517
1519
1518
let multiple = cause. source ( ) . is_some ( ) ;
1520
1519
1521
- for ( ind, error) in cause. chain ( ) . enumerate ( ) {
1520
+ for ( ind, error) in cause. sources ( ) . enumerate ( ) {
1522
1521
writeln ! ( f) ?;
1523
1522
let mut indented = Indented { inner : f } ;
1524
1523
if multiple {
0 commit comments