Repository navigation
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(error_generic_member_access)]
This is a tracking issue for the generic member access API on the error trait, which generalizes the pattern of fn source(&self) -> Option<&dyn Error> and fn backtrace(&self) -> Option<&Backtrace> into fn request_ref::<T>(&self) -> Option<&T>
Public API
// std::error
pub trait Error: Debug + Display {
// existing API unchanged
fn provide<'a>(&'a self, req: &mut Request<'a>) {}
}
pub struct Request<'a> { .. }
impl<'a> Request<'a> {
pub fn provide_value<T>(&mut self, value: T) -> &mut Request<'a>
where
T: 'static;
pub fn provide_value_with<T>(
&mut self,
fulfil: impl FnOnce() -> T,
) -> &mut Request<'a>
where
T: 'static;
pub fn provide_ref<T>(&mut self, value: &'a T) -> &mut Request<'a>
where
T: 'static + ?Sized;
pub fn provide_ref_with<T>(
&mut self,
fulfil: impl FnOnce() -> &'a T,
) -> &mut Request<'a>
where
T: 'static + ?Sized;
pub fn would_be_satisfied_by_value_of<T>(&self) -> bool
where
T: 'static;
pub fn would_be_satisfied_by_ref_of<T>(&self) -> bool
where
T: 'static + ?Sized;
}
pub fn request_ref<'a, T: ?Sized + 'static>(err: &'a (impl Error + ?Sized)) -> Option<&'a T>;
pub fn request_value<T: 'static>(err: &(impl Error + ?Sized)) -> Option<T>;Steps / History
Now tracked by @waynr in #99301 (comment)
- recent zulip discussion: #t-libs > Pushing error_generic_member_access forward
Unresolved Questions
- None yet.
Reacted by Stepan Koltsov, Trevor Gross, thebluetropics, Jess, Felipe S. S. Schneider, Mykhailo Hnap, jpramosi, Dillon, Tv, Friedel Ziegelmayer and 13 more
Reacted by Robsdedude, AdriΓ‘n Delgado, Arto Bendiken, Chris Hamons, JosΓ© Luis Cruz and Victor Schappert
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
chore(deps): update rust crate thiserror to 1.0.33 (#259)
dtolnay commentedon Sep 5, 2022
Hello from dtolnay/thiserror#185. I noticed it can be problematic for macros that
ProviderandErrorboth have a method namedprovide. I know this has been the case for other traits (DisplayandDebugboth have afmtmethod, for example) but one thing that makes the situation worse forprovideis that we're commonly forced to rely on deref in order to call it, so fully-qualified method call syntax is not suitable to resolve the ambiguity:In the case of
Display, macros can writecore::fmt::Display::fmt(&$thing, formatter)and that will always work, because even if$thingis something likeBox<dyn Display>, there is animpl<T> Display for Box<T> where T: Display + ?Sizedwhich makes it work.However in the case of
Error, writingcore::error::Error::provide(&$thing, demand)is less good than$thing.provide(demand)because there is noimpl<E> Error for Box<E> where E: Error + ?Sized. If$thingisBox<dyn Error + Send + Sync + 'static>, oranyhow::Error, these things rely on the deref done implicitly by.provide(demand)in order to deref todyn Error + Send + Sync + 'static, which calling fully qualifiedcore::error::Error::providewill not do.The situation gets worse as other traits in the ecosystem follow the lead of
Errorand possibly add their ownprovidemethod. Maybe it's not so common thatProviderandErrorwould be both in scope at the same time, but are we confident thatErrorand arbitrary other ecosystem trait won't be?Two proposals:
There should be
impl Provider for Box<dyn Error + β¦>in the standard library. This makescore::any::Provider::provide(&$thing, demand)accommodate more cases, but still not as good as having a deref.Is there someone who might be interested in pursuing a "fully qualified method call but with deref/autoref" RFC? The syntax
thing.core::any::Provider::provide(demand)has been suggested for this in the past, and would eliminate the difficulty for macros that want a deref or autoref but also need to be robust to multiple traits having the same method name.JonasJebing commentedon Oct 23, 2022
I agree, that
would be great.
An immediate remedy would also be to rename
Error::provide.The
error_generic_member_accessRFC and thedyno/provide_anyRFC were already usingError::provide_contextinstead ofError::providein their examples.I'd gladly create a PR to change the name if there are no objections.
Update All dependencies (patch only) (#483)
robamler commentedon Jan 21, 2023
Not sure if this has been proposed before, but maybe an alternative to "fully qualified method call but with deref/autoref" could be that, in case of an ambiguity, trait method resolution favors traits that were introduced in inner scopes.
Currently, the following code doesn't compile:
The proposal would be to allow the above code, and to resolve
x.fmt(formatter)tocore::fmt::Display::fmt(&x, formatter)sinceDisplaywasused in a strictly narrower scope thanDebug.I don't think this would change any behavior in code that currently compiles since it would only disambiguate cases that are currently ambiguous. And it would allow macro authors to simply introduce a new scope and
useany traits that their macros will need (this wouldn't leak any identifies to the outer scope since, already today, thoseuses are scoped to the inner block).fix(deps): update all non-major dependencies (#1479)
nrc commentedon Feb 22, 2023
An easier solution would be to rename
Error::provide. At one point it wasprovide_contextand we should be able to find some other name for it.drmason13 commentedon Apr 27, 2023
For what it's worth,
provide_contextfeels like a good name for this method to me.Sure it's longer but it's more clear that it's about the error providing some sort of context to a user that comes from the concrete error type.
Whereas a plain
providehad me thinking "provide what?".jyn514 commentedon May 16, 2023
@JonasJebing that sounds great! do you have time to follow up on that? :)
waynr commentedon Jul 15, 2023
FYI for anyone subscribed to this issue hasn't been following #96024 or #113464 - I have volunteered to take on this issue to see what i can do to help push the generic member access work forward.
Next steps for this issue include:
provide_anyfeature aka https://github.com/rust-lang/rfcs/blob/master/text/3192-dyno.mdprovide_anyfeature namethiserrorto incorporate removal of theProvidertrait, use that PR branch in core/any: remove Provider trait, rename Demand to RequestΒ #113464 to update the dependencies for nightly rustfmt and rust-analyzer. I left some thoughts on a merge strategy between rust and thiserror in my PRwaynr commentedon Jul 15, 2023
@rustbot claim
yaahc commentedon Jul 18, 2023
Not that I know of. The only way I can think of is via team membership since I think permissions are set up so that team members can usually edit any comments in the repo, for instance, I can edit your reply in this issue for whatever reason...
Feel free to just ping me (zulip works best), I'll start by grabbing the next steps you added and vendoring them into the top level comment.
Edit: alright, I've gone ahead and edited the todolist on the issue. Not sure if the provide_context rename is still relevant, lmk either way.
197 remaining items
lygstate commentedon Feb 24, 2026
If it's frustrated, better not have it, we already deal with all kinds of fault of C for so many years π
Amanieu commentedon Feb 26, 2026
RFC 3885 was discussed in the lang meeting and there was a lot of enthusiasm for it. As I've said before, the libs-api team strongly prefers language-level trait object casting to the provider API. However this fundamentally requires that someone put the work in to actually implement this feature. If anyone is interested then they should say so on the RFC thread.
In the meantime I will cancel the FCP since we have a better alternative solution in sight.
@rfcbot cancel
rust-rfcbot commentedon Feb 26, 2026
@Amanieu proposal cancelled.
yaahc commentedon Mar 3, 2026
For what it's worth, @arielb1 brought up a concern in zulip the other day is how a language level solution would interact generics+
&dyn Error(and I think generally any error wrapping types that masquerade as their wrapped error).#t-libs > Pushing error_generic_member_access forward @ π¬
To give a gist, if you have a function that takes an
E: Errorgeneric type and you pass in a&dyn Errorthat has already been erased then attempt to extract some context such as a Backtrace by casting theE: Errortype to some accessor trait you'd likely get None since&dyn Erroror any equivalent wrapper wouldn't necessarily or may not be able to implement the accessor traits implemented on the underlying erased error.This isn't necessarily a hard blocker IMO. I like the argument that this may be a positive pressure in that it discourages proxying error types, and it shouldn't break intertrait casts on subsequent source errors in the chain of errors. Also, notably it shouldn't affect the likes of
Box<dyn Error>andanyhow/eyregiven that they do not themselves implement theErrortrait.Provider avoids this by allowing proxying types to directly invoke the provide impl of their wrapped errors.
Either way though I think this is an important factor to consider when deciding which approach to prefer.