Skip to content
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

#[diagnostic(transparent)] for passthrough diagnostics #16

Closed
zkat opened this issue Aug 16, 2021 · 4 comments · Fixed by #36
Closed

#[diagnostic(transparent)] for passthrough diagnostics #16

zkat opened this issue Aug 16, 2021 · 4 comments · Fixed by #36

Comments

@zkat
Copy link
Owner

zkat commented Aug 16, 2021

No description provided.

@zkat zkat mentioned this issue Aug 16, 2021
12 tasks
@zkat zkat closed this as completed Aug 16, 2021
@zkat
Copy link
Owner Author

zkat commented Aug 16, 2021

actuallly I don't think we need this, if we have #[error(transparent)] already...

@cormacrelf
Copy link
Contributor

@zkat This is still necessary. Just placing #[error(transparent)] on a single item enum variant doesn't do anything WRT the Diagnostic implementation. It needs to generate code that implements Diagnostic through the inner type, the way thiserror's does for the Display and Error::backtrace etc implementations.

Thiserror does give you the From implementation via Variant(#[from] Inner) for free.


The use case for miette is pretty obvious, same as thiserror's, but for some context, I'm doing this:

#[derive(Debug, Error, Diagnostic)]
enum Diag {
    #[error(transparent)] #[diagnostic(transparent)] Error(#[from] Error),
    // The rest of the variants go here, all warnings/advice, no errors
}

// this would be weird -- you could fail your whole thing because of a warning
fn bad_function(...) -> Result<i32, Diag> {}
// this way your Err case is never a warning, so you are never in the situation where
// there is only a warning but you don't have an Ok value to continue with
fn better_function(...) -> Result<i32, Error> {}

@cormacrelf
Copy link
Contributor

There is another way, which is to box all of your diagnostics and let the dynamism do the work. But sometimes you don't want to erase all the types, if you're like me (the guy from twitter who wanted to collect all the diagnostics into a Vec and expose them to library users) then maybe you just want the one type. The code goes a long way but I'm not sure.

You might also want to delegate only some of the Diagnostic implementation. I'm thinking about having a lot of different variants where practically every single one has a source, a snippet and an optional hint. Being able to forward the snippet implementation to a single struct Snip but provide a new code etc would be very useful.

@cormacrelf
Copy link
Contributor

I'm having a crack at a PR for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants