-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement error interoperation (RFC 70) #17753
Conversation
9b13971
to
4f22561
Compare
//! pub trait Error: Send + Any { | ||
//! fn description(&self) -> &str; | ||
//! | ||
//! fn detail(&self) -> Option<&str> { None } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, the return type is Option<&str>
, but in the actual definition of the trait, the return type is Option<String>
@japaric Thanks, fixed. |
// } | ||
|
||
// Note: the definitions below are copied from core::any, and should be unified | ||
// as soon as possible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a path forward for unifying these other than allowing &Error
to be casted to &Any
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the ideal path; I'm not sure how soon that's likely to happen.
Alternatively, we could probably define macros for this, but given visibility rules that seemed more pain than gain right now.
@alexcrichton fixed, thanks. |
@@ -66,6 +64,7 @@ | |||
#[doc(no_inline)] pub use cmp::{Ordering, Less, Equal, Greater, Equiv}; | |||
#[doc(no_inline)] pub use collections::{Collection, Mutable, Map, MutableMap, MutableSeq}; | |||
#[doc(no_inline)] pub use collections::{Set, MutableSet}; | |||
#[doc(no_inline)] pub use error::Error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would actually expect inspection of errors to not fall under "an overwhelming majority of rust code does this", but rather just the tip of the stack is the one to inspect the error. Along those lines, I'm not sure I'd put this in the prelude to get access to the methods.
On the other hand, implementing the trait is always convenient if you don't have to import it (and it avoids the std::error::Error
stutter).
The RFC doesn't look like it mentions any modifications to the prelude, so I'm curious, what was your rationale for placing this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just writing a comment to get your opinion about this. You're right, it wasn't mentioned in the RFC and for that reason alone probably shouldn't happen here.
I also worry about having the name Error
show up in the prelude. So maybe an explicit import makes more sense.
(My rationale was just that you'd generally expect methods like description
to be available on error types without needing to import a trait, but that's pretty weak sauce.)
Do you think it may be best to wait for multidispatch to land before landing this? This is a pretty minimal change, so I don't think it's too susceptible to bitrot (ok on that front). I'd be more curious in going ahead and leveraging this trait throughout some examples in the stdlib/compiler/etc to give it a bit of usage and make sure the |
@alexcrichton Addressed comments, removed from prelude. |
@alexcrichton Sure, we can wait to land this until multidispatch, and I can add tests/examples to docs. |
4110531
to
5a7810e
Compare
Just an update: I've tried rolling this out with multidispatch now being snapshotted. However, there are some problems with type inference. @nikomatsakis is looking into it. We can't land this change to In general, also, the two blanket |
cc #18019 |
@aturon Is this good to go after the next snapshot? |
cc #18259 for new snapshots |
@alexcrichton I've updated with documentation and use of multidispatch. Building fine here. re-r? |
This PR: * Adds the error interoperation traits (`Error` and `FromError`) to a new module, `std::error`, as per [RFC 70](https://github.com/rust-lang/rfcs/blob/master/active/0070-error-chaining.md). Note that this module must live in `std` in order to refer to `String`. Note that, until multidispatch lands, the `FromError` trait cannot be usefully implemented outside of the blanket impl given here. * Incorporates `std::error::FromError` into the `try!` macro. * Implements `Error` for most existing error enumerations. Closes #17747
This PR: * Adds the error interoperation traits (`Error` and `FromError`) to a new module, `std::error`, as per [RFC 70](https://github.com/rust-lang/rfcs/blob/master/active/0070-error-chaining.md). Note that this module must live in `std` in order to refer to `String`. Note that, until multidispatch lands, the `FromError` trait cannot be usefully implemented outside of the blanket impl given here. * Incorporates `std::error::FromError` into the `try!` macro. * Implements `Error` for most existing error enumerations. Closes #17747
As per [RFC 70](https://github.com/rust-lang/rfcs/blob/master/active/0070-error-chaining.md) Closes rust-lang#17747 Note that the `error` module must live in `std` in order to refer to `String`. Note that, until multidispatch lands, the `FromError` trait cannot be usefully implemented outside of the blanket impl given here.
This PR: * Adds the error interoperation traits (`Error` and `FromError`) to a new module, `std::error`, as per [RFC 70](https://github.com/rust-lang/rfcs/blob/master/active/0070-error-chaining.md). Note that this module must live in `std` in order to refer to `String`. Note that, until multidispatch lands, the `FromError` trait cannot be usefully implemented outside of the blanket impl given here. * Incorporates `std::error::FromError` into the `try!` macro. * Implements `Error` for most existing error enumerations. Closes #17747
This PR:
Adds the error interoperation traits (
Error
andFromError
) to a new module,std::error
, as per RFC 70. Note that this module must live instd
in order to refer toString
.Note that, until multidispatch lands, the
FromError
trait cannot beusefully implemented outside of the blanket impl given here.
Incorporates
std::error::FromError
into thetry!
macro.Implements
Error
for most existing error enumerations.Closes #17747