Skip to content

Commit 9538d2d

Browse files
committed
Auto merge of #99431 - yaahc:error-in-core, r=joshtriplett
Remove `fn backtrace` and replace with usages of provider API This PR is part of moving error into core and has been split to facilitate review.
2 parents 9d5cd21 + b2bbca3 commit 9538d2d

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

library/std/src/error.rs

+33-41
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,6 @@ pub trait Error: Debug + Display {
260260
TypeId::of::<Self>()
261261
}
262262

263-
/// Returns a stack backtrace, if available, of where this error occurred.
264-
///
265-
/// This function allows inspecting the location, in code, of where an error
266-
/// happened. The returned `Backtrace` contains information about the stack
267-
/// trace of the OS thread of execution of where the error originated from.
268-
///
269-
/// Note that not all errors contain a `Backtrace`. Also note that a
270-
/// `Backtrace` may actually be empty. For more information consult the
271-
/// `Backtrace` type itself.
272-
#[unstable(feature = "backtrace", issue = "53487")]
273-
fn backtrace(&self) -> Option<&Backtrace> {
274-
None
275-
}
276-
277263
/// ```
278264
/// if let Err(e) = "xc".parse::<u32>() {
279265
/// // Print `e` itself, no need for description().
@@ -370,7 +356,7 @@ pub trait Error: Debug + Display {
370356
}
371357

372358
#[unstable(feature = "error_generic_member_access", issue = "99301")]
373-
impl Provider for dyn Error + 'static {
359+
impl<'b> Provider for dyn Error + 'b {
374360
fn provide<'a>(&'a self, req: &mut Demand<'a>) {
375361
self.provide(req)
376362
}
@@ -757,8 +743,8 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
757743
Error::source(&**self)
758744
}
759745

760-
fn backtrace(&self) -> Option<&Backtrace> {
761-
Error::backtrace(&**self)
746+
fn provide<'b>(&'b self, req: &mut Demand<'b>) {
747+
Error::provide(&**self, req);
762748
}
763749
}
764750

@@ -778,8 +764,8 @@ impl<T: Error + ?Sized> Error for Arc<T> {
778764
Error::source(&**self)
779765
}
780766

781-
fn backtrace(&self) -> Option<&Backtrace> {
782-
Error::backtrace(&**self)
767+
fn provide<'a>(&'a self, req: &mut Demand<'a>) {
768+
Error::provide(&**self, req);
783769
}
784770
}
785771

@@ -871,6 +857,20 @@ impl Error for alloc::ffi::IntoStringError {
871857
}
872858
}
873859

860+
impl<'a> dyn Error + 'a {
861+
/// Request a reference of type `T` as context about this error.
862+
#[unstable(feature = "error_generic_member_access", issue = "99301")]
863+
pub fn request_ref<T: ?Sized + 'static>(&'a self) -> Option<&'a T> {
864+
core::any::request_ref(self)
865+
}
866+
867+
/// Request a value of type `T` as context about this error.
868+
#[unstable(feature = "error_generic_member_access", issue = "99301")]
869+
pub fn request_value<T: 'static>(&'a self) -> Option<T> {
870+
core::any::request_value(self)
871+
}
872+
}
873+
874874
// Copied from `any.rs`.
875875
impl dyn Error + 'static {
876876
/// Returns `true` if the inner type is the same as `T`.
@@ -910,18 +910,6 @@ impl dyn Error + 'static {
910910
None
911911
}
912912
}
913-
914-
/// Request a reference of type `T` as context about this error.
915-
#[unstable(feature = "error_generic_member_access", issue = "99301")]
916-
pub fn request_ref<T: ?Sized + 'static>(&self) -> Option<&T> {
917-
core::any::request_ref(self)
918-
}
919-
920-
/// Request a value of type `T` as context about this error.
921-
#[unstable(feature = "error_generic_member_access", issue = "99301")]
922-
pub fn request_value<T: 'static>(&self) -> Option<T> {
923-
core::any::request_value(self)
924-
}
925913
}
926914

927915
impl dyn Error + 'static + Send {
@@ -949,13 +937,13 @@ impl dyn Error + 'static + Send {
949937
/// Request a reference of type `T` as context about this error.
950938
#[unstable(feature = "error_generic_member_access", issue = "99301")]
951939
pub fn request_ref<T: ?Sized + 'static>(&self) -> Option<&T> {
952-
<dyn Error + 'static>::request_ref(self)
940+
<dyn Error>::request_ref(self)
953941
}
954942

955943
/// Request a value of type `T` as context about this error.
956944
#[unstable(feature = "error_generic_member_access", issue = "99301")]
957945
pub fn request_value<T: 'static>(&self) -> Option<T> {
958-
<dyn Error + 'static>::request_value(self)
946+
<dyn Error>::request_value(self)
959947
}
960948
}
961949

@@ -984,13 +972,13 @@ impl dyn Error + 'static + Send + Sync {
984972
/// Request a reference of type `T` as context about this error.
985973
#[unstable(feature = "error_generic_member_access", issue = "99301")]
986974
pub fn request_ref<T: ?Sized + 'static>(&self) -> Option<&T> {
987-
<dyn Error + 'static>::request_ref(self)
975+
<dyn Error>::request_ref(self)
988976
}
989977

990978
/// Request a value of type `T` as context about this error.
991979
#[unstable(feature = "error_generic_member_access", issue = "99301")]
992980
pub fn request_value<T: 'static>(&self) -> Option<T> {
993-
<dyn Error + 'static>::request_value(self)
981+
<dyn Error>::request_value(self)
994982
}
995983
}
996984

@@ -1467,8 +1455,11 @@ impl<E> Report<E> {
14671455
/// ```rust
14681456
/// #![feature(error_reporter)]
14691457
/// #![feature(backtrace)]
1458+
/// #![feature(provide_any)]
1459+
/// #![feature(error_generic_member_access)]
14701460
/// # use std::error::Error;
14711461
/// # use std::fmt;
1462+
/// use std::any::Demand;
14721463
/// use std::error::Report;
14731464
/// use std::backtrace::Backtrace;
14741465
///
@@ -1498,8 +1489,9 @@ impl<E> Report<E> {
14981489
/// }
14991490
///
15001491
/// impl Error for SuperErrorSideKick {
1501-
/// fn backtrace(&self) -> Option<&Backtrace> {
1502-
/// Some(&self.backtrace)
1492+
/// fn provide<'a>(&'a self, req: &mut Demand<'a>) {
1493+
/// req
1494+
/// .provide_ref::<Backtrace>(&self.backtrace);
15031495
/// }
15041496
/// }
15051497
///
@@ -1552,11 +1544,11 @@ where
15521544
fn backtrace(&self) -> Option<&Backtrace> {
15531545
// have to grab the backtrace on the first error directly since that error may not be
15541546
// 'static
1555-
let backtrace = self.error.backtrace();
1547+
let backtrace = (&self.error as &dyn Error).request_ref();
15561548
let backtrace = backtrace.or_else(|| {
15571549
self.error
15581550
.source()
1559-
.map(|source| source.chain().find_map(|source| source.backtrace()))
1551+
.map(|source| source.chain().find_map(|source| source.request_ref()))
15601552
.flatten()
15611553
});
15621554
backtrace
@@ -1618,11 +1610,11 @@ impl Report<Box<dyn Error>> {
16181610
fn backtrace(&self) -> Option<&Backtrace> {
16191611
// have to grab the backtrace on the first error directly since that error may not be
16201612
// 'static
1621-
let backtrace = self.error.backtrace();
1613+
let backtrace = self.error.request_ref();
16221614
let backtrace = backtrace.or_else(|| {
16231615
self.error
16241616
.source()
1625-
.map(|source| source.chain().find_map(|source| source.backtrace()))
1617+
.map(|source| source.chain().find_map(|source| source.request_ref()))
16261618
.flatten()
16271619
});
16281620
backtrace

library/std/src/error/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::Error;
22
use crate::fmt;
3+
use core::any::Demand;
34

45
#[derive(Debug, PartialEq)]
56
struct A;
@@ -198,8 +199,8 @@ where
198199
self.source.as_deref()
199200
}
200201

201-
fn backtrace(&self) -> Option<&Backtrace> {
202-
self.backtrace.as_ref()
202+
fn provide<'a>(&'a self, req: &mut Demand<'a>) {
203+
self.backtrace.as_ref().map(|bt| req.provide_ref::<Backtrace>(bt));
203204
}
204205
}
205206

0 commit comments

Comments
 (0)