Skip to content

Commit 80442f3

Browse files
committed
error::Error: rename the chain method to sources
Signed-off-by: Nick Cameron <[email protected]>
1 parent 4a24f08 commit 80442f3

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

library/core/src/error.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ use crate::fmt::{Debug, Display};
3030
/// assert_eq!(err.to_string(), "invalid digit found in string");
3131
/// ```
3232
///
33-
/// Errors may provide cause chain information. [`Error::source()`] is generally
33+
/// Errors may provide cause information. [`Error::source()`] is generally
3434
/// used when errors cross "abstraction boundaries". If one module must report
3535
/// an error that is caused by an error from a lower-level module, it can allow
3636
/// accessing that error via [`Error::source()`]. This makes it possible for the
3737
/// high-level module to provide its own errors while also revealing some of the
38-
/// implementation for debugging via `source` chains.
38+
/// implementation for debugging.
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
#[cfg_attr(not(test), rustc_diagnostic_item = "Error")]
4141
#[rustc_has_incoherent_inherent_impls]
@@ -397,7 +397,7 @@ impl dyn Error {
397397
/// // let err : Box<Error> = b.into(); // or
398398
/// let err = &b as &(dyn Error);
399399
///
400-
/// let mut iter = err.chain();
400+
/// let mut iter = err.sources();
401401
///
402402
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
403403
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -406,8 +406,23 @@ impl dyn Error {
406406
/// ```
407407
#[unstable(feature = "error_iter", issue = "58520")]
408408
#[inline]
409-
pub fn chain(&self) -> Chain<'_> {
410-
Chain { current: Some(self) }
409+
pub fn sources(&self) -> Source<'_> {
410+
// You may think this method would be better in the Error trait, and you'd be right.
411+
// Unfortunately that doesn't work, not because of the object safety rules but because we
412+
// save a reference to self in Sources below as a trait object. If this method was
413+
// declared in Error, then self would have the type &T where T is some concrete type which
414+
// implements Error. We would need to coerce self to have type &dyn Error, but that requires
415+
// that Self has a known size (i.e., Self: Sized). We can't put that bound on Error
416+
// since that would forbid Error trait objects, and we can't put that bound on the method
417+
// because that means the method can't be called on trait objects (we'd also need the
418+
// 'static bound, but that isn't allowed because methods with bounds on Self other than
419+
// Sized are not object-safe). Requiring an Unsize bound is not backwards compatible.
420+
//
421+
// Two possible solutions are to start the iterator at self.source() instead of self (see
422+
// discussion on the tracking issue), or to wait for dyn* to exist (which would then permit
423+
// the coercion).
424+
425+
Source { current: Some(self) }
411426
}
412427
}
413428

@@ -417,12 +432,12 @@ impl dyn Error {
417432
/// its sources, use `skip(1)`.
418433
#[unstable(feature = "error_iter", issue = "58520")]
419434
#[derive(Clone, Debug)]
420-
pub struct Chain<'a> {
435+
pub struct Source<'a> {
421436
current: Option<&'a (dyn Error + 'static)>,
422437
}
423438

424439
#[unstable(feature = "error_iter", issue = "58520")]
425-
impl<'a> Iterator for Chain<'a> {
440+
impl<'a> Iterator for Source<'a> {
426441
type Item = &'a (dyn Error + 'static);
427442

428443
fn next(&mut self) -> Option<Self::Item> {

library/std/src/error.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ pub use core::error::Error;
6969
/// assert_eq!(err.to_string(), "invalid digit found in string");
7070
/// ```
7171
///
72-
/// Errors may provide cause chain information. [`Error::source()`] is generally
72+
/// Errors may provide cause information. [`Error::source()`] is generally
7373
/// used when errors cross "abstraction boundaries". If one module must report
7474
/// an error that is caused by an error from a lower-level module, it can allow
7575
/// accessing that error via [`Error::source()`]. This makes it possible for the
7676
/// 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.
7878
#[stable(feature = "rust1", since = "1.0.0")]
7979
#[cfg_attr(not(test), rustc_diagnostic_item = "Error")]
8080
#[cfg(bootstrap)]
@@ -976,7 +976,7 @@ impl dyn Error {
976976
/// // let err : Box<Error> = b.into(); // or
977977
/// let err = &b as &(dyn Error);
978978
///
979-
/// let mut iter = err.chain();
979+
/// let mut iter = err.sources();
980980
///
981981
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
982982
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -985,8 +985,23 @@ impl dyn Error {
985985
/// ```
986986
#[unstable(feature = "error_iter", issue = "58520")]
987987
#[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) }
9901005
}
9911006
}
9921007

@@ -997,13 +1012,13 @@ impl dyn Error {
9971012
#[unstable(feature = "error_iter", issue = "58520")]
9981013
#[derive(Clone, Debug)]
9991014
#[cfg(bootstrap)]
1000-
pub struct Chain<'a> {
1015+
pub struct Sources<'a> {
10011016
current: Option<&'a (dyn Error + 'static)>,
10021017
}
10031018

10041019
#[cfg(bootstrap)]
10051020
#[unstable(feature = "error_iter", issue = "58520")]
1006-
impl<'a> Iterator for Chain<'a> {
1021+
impl<'a> Iterator for Sources<'a> {
10071022
type Item = &'a (dyn Error + 'static);
10081023

10091024
fn next(&mut self) -> Option<Self::Item> {
@@ -1043,8 +1058,8 @@ impl dyn Error + Send + Sync {
10431058

10441059
/// An error reporter that prints an error and its sources.
10451060
///
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.
10481063
///
10491064
/// `Report` only requires that the wrapped error implement `Error`. It doesn't require that the
10501065
/// wrapped error be `Send`, `Sync`, or `'static`.
@@ -1389,7 +1404,7 @@ impl<E> Report<E> {
13891404
///
13901405
/// **Note**: Report will search for the first `Backtrace` it can find starting from the
13911406
/// outermost error. In this example it will display the backtrace from the second error in the
1392-
/// chain, `SuperErrorSideKick`.
1407+
/// sources, `SuperErrorSideKick`.
13931408
///
13941409
/// ```rust
13951410
/// #![feature(error_reporter)]
@@ -1486,7 +1501,7 @@ where
14861501
let backtrace = backtrace.or_else(|| {
14871502
self.error
14881503
.source()
1489-
.map(|source| source.chain().find_map(|source| source.request_ref()))
1504+
.map(|source| source.sources().find_map(|source| source.request_ref()))
14901505
.flatten()
14911506
});
14921507
backtrace
@@ -1497,7 +1512,7 @@ where
14971512
fn fmt_singleline(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14981513
write!(f, "{}", self.error)?;
14991514

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);
15011516

15021517
for cause in sources {
15031518
write!(f, ": {cause}")?;
@@ -1518,7 +1533,7 @@ where
15181533

15191534
let multiple = cause.source().is_some();
15201535

1521-
for (ind, error) in cause.chain().enumerate() {
1536+
for (ind, error) in cause.sources().enumerate() {
15221537
writeln!(f)?;
15231538
let mut indented = Indented { inner: f };
15241539
if multiple {

0 commit comments

Comments
 (0)