shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.4k stars 60 forks source link

Stop automatically treating location as implicit #396

Closed shepmaster closed 1 year ago

shepmaster commented 1 year ago

Closes #339

netlify[bot] commented 1 year ago

Deploy Preview for shepmaster-snafu ready!

Name Link
Latest commit b1e3e0972bf62d463369694c76e8f8689a00e8c9
Latest deploy log https://app.netlify.com/sites/shepmaster-snafu/deploys/64f28db65e351a00080eff4b
Deploy Preview https://deploy-preview-396--shepmaster-snafu.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

shepmaster commented 1 year ago

cannot reliably identify whether the field location is indeed of type

That's correct. Proc macros only have knowledge of the literal source code, not any types or anything fancy.

mhnap commented 3 weeks ago

Proc macros only have knowledge of the literal source code, not any types or anything fancy.

Seems thiserror somehow handles this correctly, as the backtrace field can be named differently than backtrace but if it has the std::backtrace::Backtrace type it still will be recognized as backtrace (provide method will be generated).

Firstly, I wanted to propose making Location implicit by default, but after I found this issue :)

shepmaster commented 3 weeks ago

Proc macros only have knowledge of the literal source code, not any types or anything fancy.

Seems thiserror somehow handles this correctly

Can you please provide a code example demonstrating what you mean? I stated that the literal source code is all that the macro gets, but it seems like you are saying that is not true. If so, you should be able to write code where the error type has no literal words like "backtrace" and the automatic backtrace behavior still works:

use std::backtrace::Backtrace as Wubble;

#[derive(Error, Debug)]
pub struct MyError {
    msg: String,
    wubble: Wubble,
}

If you provide that, then I can go digging into thiserror to see how it works.

mhnap commented 3 weeks ago

Yes, you're right. It will work if the field name differs from backtrace but the field type still needs to contain Backtrace. So, it indeed will not work for your example above. It works for:

    #[derive(Error, Debug)]
    pub struct MyError {
        msg: String,
        wubble: std::backtrace::Backtrace,
    }

or:

    use std::backtrace::Backtrace as Backtrace;

    #[derive(Error, Debug)]
    pub struct MyError {
        msg: String,
        wubble: Backtrace,
    }

Sorry for misleading.