-
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
Disambiguate method call for impl on dyn Trait #69161
Comments
Introducing a new trait gets the job done, though and the compiler will pick the right thing. |
Because `chain()` was only implemented for `dyn Error`, users had to cast errors to a `&(dyn Error)` to use the `chain()` method. ```rust let mut iter = (&my_error as &(dyn Error)).chain(); // or let mut iter = <dyn Error>::chain(&my_error); // or let mut iter = Error::chain(&my_error); ``` What I would liked to have is ```rust pub trait Error: Debug + Display { fn source(&self) -> Option<&(dyn Error + 'static)> { None } fn chain(&self) -> Chain<'_> where Self: Sized + 'static { Chain { current: Some(self), } } } impl dyn Error + 'static { fn chain(&self) -> Chain<'_> { Chain { current: Some(self), } } } ``` This doesn't work, because of: rust-lang#69161 This patch introduces an ErrorChain trait, which accomplishes the job and is implemented for `Error` and `dyn Error + 'static`.
I've been doing a bit of digging in the type checking code and I've found the core problem of this bug: This piece of code that checks for I'm writing this down for two reasons:
|
Any chance anybody looking into this? |
To produce an error iterator `std::error::Chain` one had to call `<dyn Error>::chain()`, which was not very ergonomic, because you have to manually cast to a trait object, if you didn't already have one with the type erased. ``` let mut iter = (&my_error as &(dyn Error)).chain(); // or let mut iter = <dyn Error>::chain(&my_error); // or let mut iter = Error::chain(&my_error); ``` The `chain()` method can't be implemented on the Error trait, because of rust-lang#69161 `Chain::new()` replaces `<dyn Error>::chain()` as a good alternative without confusing users, why they can't use `my_error.chain()` directly. The `Error::sources()` method doesn't have this problem, so implement it more efficiently, than one could achieve this with `Chain::new().skip(1)`. Related: rust-lang#58520
Seems like the compiler tries the trait method for an
dyn Error + 'static
even though it does not work, because of !Sized and does not consider taking theimpl dyn Error + 'static
.Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a6845b426451f1d7a5105a9bd3a00499
error:
The text was updated successfully, but these errors were encountered: