Skip to content

Commit 4255be8

Browse files
authored
Rollup merge of #100955 - nrc:chain, r=joshtriplett
Some papercuts on error::Error Renames the chain method, since I chain could mean anything and doesn't refer to a chain of sources (cc #58520) (and adds a comment explaining why sources is not a provided method on Error). Renames arguments to the request method from `req` to `demand` since the type is `Demand` rather than Request or Requisition. r? ``@yaahc``
2 parents e5c904a + aa619a7 commit 4255be8

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

core/src/error.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
#![doc = include_str!("error.md")]
22
#![unstable(feature = "error_in_core", issue = "none")]
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)]
165
mod tests;
176

@@ -30,12 +19,12 @@ use crate::fmt::{Debug, Display};
3019
/// assert_eq!(err.to_string(), "invalid digit found in string");
3120
/// ```
3221
///
33-
/// Errors may provide cause chain information. [`Error::source()`] is generally
22+
/// Errors may provide cause information. [`Error::source()`] is generally
3423
/// used when errors cross "abstraction boundaries". If one module must report
3524
/// an error that is caused by an error from a lower-level module, it can allow
3625
/// accessing that error via [`Error::source()`]. This makes it possible for the
3726
/// high-level module to provide its own errors while also revealing some of the
38-
/// implementation for debugging via `source` chains.
27+
/// implementation for debugging.
3928
#[stable(feature = "rust1", since = "1.0.0")]
4029
#[cfg_attr(not(test), rustc_diagnostic_item = "Error")]
4130
#[rustc_has_incoherent_inherent_impls]
@@ -182,8 +171,8 @@ pub trait Error: Debug + Display {
182171
/// }
183172
///
184173
/// impl std::error::Error for Error {
185-
/// fn provide<'a>(&'a self, req: &mut Demand<'a>) {
186-
/// req
174+
/// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
175+
/// demand
187176
/// .provide_ref::<MyBacktrace>(&self.backtrace)
188177
/// .provide_ref::<dyn std::error::Error + 'static>(&self.source);
189178
/// }
@@ -201,16 +190,16 @@ pub trait Error: Debug + Display {
201190
/// ```
202191
#[unstable(feature = "error_generic_member_access", issue = "99301")]
203192
#[allow(unused_variables)]
204-
fn provide<'a>(&'a self, req: &mut Demand<'a>) {}
193+
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {}
205194
}
206195

207196
#[unstable(feature = "error_generic_member_access", issue = "99301")]
208197
impl<E> Provider for E
209198
where
210199
E: Error + ?Sized,
211200
{
212-
fn provide<'a>(&'a self, req: &mut Demand<'a>) {
213-
self.provide(req)
201+
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
202+
self.provide(demand)
214203
}
215204
}
216205

@@ -397,7 +386,7 @@ impl dyn Error {
397386
/// // let err : Box<Error> = b.into(); // or
398387
/// let err = &b as &(dyn Error);
399388
///
400-
/// let mut iter = err.chain();
389+
/// let mut iter = err.sources();
401390
///
402391
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
403392
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -406,8 +395,19 @@ impl dyn Error {
406395
/// ```
407396
#[unstable(feature = "error_iter", issue = "58520")]
408397
#[inline]
409-
pub fn chain(&self) -> Chain<'_> {
410-
Chain { current: Some(self) }
398+
pub fn sources(&self) -> Source<'_> {
399+
// You may think this method would be better in the Error trait, and you'd be right.
400+
// Unfortunately that doesn't work, not because of the object safety rules but because we
401+
// save a reference to self in Sources below as a trait object. If this method was
402+
// declared in Error, then self would have the type &T where T is some concrete type which
403+
// implements Error. We would need to coerce self to have type &dyn Error, but that requires
404+
// that Self has a known size (i.e., Self: Sized). We can't put that bound on Error
405+
// since that would forbid Error trait objects, and we can't put that bound on the method
406+
// because that means the method can't be called on trait objects (we'd also need the
407+
// 'static bound, but that isn't allowed because methods with bounds on Self other than
408+
// Sized are not object-safe). Requiring an Unsize bound is not backwards compatible.
409+
410+
Source { current: Some(self) }
411411
}
412412
}
413413

@@ -417,12 +417,12 @@ impl dyn Error {
417417
/// its sources, use `skip(1)`.
418418
#[unstable(feature = "error_iter", issue = "58520")]
419419
#[derive(Clone, Debug)]
420-
pub struct Chain<'a> {
420+
pub struct Source<'a> {
421421
current: Option<&'a (dyn Error + 'static)>,
422422
}
423423

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

428428
fn next(&mut self) -> Option<Self::Item> {
@@ -448,8 +448,8 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
448448
Error::source(&**self)
449449
}
450450

451-
fn provide<'b>(&'b self, req: &mut Demand<'b>) {
452-
Error::provide(&**self, req);
451+
fn provide<'b>(&'b self, demand: &mut Demand<'b>) {
452+
Error::provide(&**self, demand);
453453
}
454454
}
455455

std/src/error.rs

+35-36
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
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)]
165
mod 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")]
248237
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)
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

Comments
 (0)