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

Allow using smartpointers with source_diagnostic #169

Merged
merged 2 commits into from
May 13, 2022

Conversation

TheNeikos
Copy link
Contributor

@TheNeikos TheNeikos commented May 6, 2022

Currently only a simple borrow is performed in some cases, and as_ref in other. AsRef is however not always implemented and &Box<dyn Diagnostic> does not implement Diagnostic. This patch makes use of Borrow that correctly handles both cases.

Problems before:

error[E0599]: the method `as_ref` exists for reference `&AnErr`, but its trait bounds were not satisfied
   --> tests/test_diagnostic_source_macro.rs:14:17
    |
5   | struct AnErr;
    | ------------- doesn't satisfy `AnErr: AsRef<_>`
...
14  | #[derive(Debug, miette::Diagnostic, thiserror::Error)]
    |                 ^^^^^^^^^^^^^^^^^^ method cannot be called on `&AnErr` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `AnErr: AsRef<_>`
            which is required by `&AnErr: AsRef<_>`
note: the following trait must be implemented
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `as_ref`, perhaps you need to implement it:
            candidate #1: `AsRef`
    = note: this error originates in the derive macro `miette::Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Box<(dyn Diagnostic + 'static)>: Diagnostic` is not satisfied
  --> tests/test_diagnostic_source_macro.rs:29:17
   |
29 | #[derive(Debug, miette::Diagnostic, thiserror::Error)]
   |                 ^^^^^^^^^^^^^^^^^^ the trait `Diagnostic` is not implemented for `Box<(dyn Diagnostic + 'static)>`
   |
   = note: required for the cast to the object type `dyn Diagnostic`
   = note: this error originates in the derive macro `miette::Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Arc<(dyn Diagnostic + 'static)>: Diagnostic` is not satisfied
  --> tests/test_diagnostic_source_macro.rs:33:17
   |
33 | #[derive(Debug, miette::Diagnostic, thiserror::Error)]
   |                 ^^^^^^^^^^^^^^^^^^ the trait `Diagnostic` is not implemented for `Arc<(dyn Diagnostic + 'static)>`
   |
   = note: required for the cast to the object type `dyn Diagnostic`
   = note: this error originates in the derive macro `miette::Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)

AsRef is not reflexive, meaning that it is not implemented as &T for all
T. Borrow is though, so we use that to make sure that we always get a
reference that is correct, even in the presence of smart pointers.
Copy link
Owner

@zkat zkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I seem to have forgotten about this PR. Thanks again for the patch!

@zkat zkat merged commit f783a0e into zkat:main May 13, 2022
@TheNeikos TheNeikos deleted the feature/expand_tests branch May 13, 2022 15:44
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 this pull request may close these issues.

2 participants