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

#[label] derive doesn't seem to work with Option<T> where T implements Into<SourceSpan> but not Copy #377

Open
cdisselkoen opened this issue May 10, 2024 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cdisselkoen
Copy link

The Diagnostic derive fails in the following:

use miette::{Diagnostic, SourceCode, SourceSpan};
use thiserror::Error;

#[derive(Debug, Clone)]
pub struct MySpan {
    span: SourceSpan,
    src: String,
}

impl From<MySpan> for SourceSpan {
    fn from(value: MySpan) -> Self {
        value.span
    }
}

impl From<&MySpan> for SourceSpan {
    fn from(value: &MySpan) -> Self {
        value.span
    }
}

impl SourceCode for MySpan {
    fn read_span<'a>(
            &'a self,
            span: &SourceSpan,
            context_lines_before: usize,
            context_lines_after: usize,
        ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
        self.src.read_span(span, context_lines_before, context_lines_after)
    }
}

#[derive(Debug, Clone, Error, Diagnostic)]
#[error("it didn't work: {details}")]
pub struct MyError {
    details: String,
    #[help]
    help: Option<String>,
    #[label("here's the problem")]
    span: Option<MySpan>,
}

The error message is "cannot move out of a shared reference" and says the problem is that Option<MySpan> does not implement Copy.

The derive works fine for span: MySpan, just not for span: Option<MySpan>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants