mirror of https://github.com/zkat/miette.git
Allow using smartpointers with source_diagnostic (#169)
* Add more tests for diagnostic_source * Use Borrow::borrow instead of AsRef 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.
This commit is contained in:
parent
0a4cf4ad24
commit
f783a0e2ae
|
|
@ -59,7 +59,7 @@ impl DiagnosticSource {
|
||||||
};
|
};
|
||||||
quote! {
|
quote! {
|
||||||
Self::#ident #display_pat => {
|
Self::#ident #display_pat => {
|
||||||
std::option::Option::Some(#rel.as_ref())
|
std::option::Option::Some(std::borrow::Borrow::borrow(#rel))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -71,7 +71,7 @@ impl DiagnosticSource {
|
||||||
let rel = &self.0;
|
let rel = &self.0;
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
fn diagnostic_source<'a>(&'a self) -> std::option::Option<&'a dyn miette::Diagnostic> {
|
fn diagnostic_source<'a>(&'a self) -> std::option::Option<&'a dyn miette::Diagnostic> {
|
||||||
std::option::Option::Some(&self.#rel)
|
std::option::Option::Some(std::borrow::Borrow::borrow(&self.#rel))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,56 @@ struct AnErr;
|
||||||
|
|
||||||
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
||||||
#[error("TestError")]
|
#[error("TestError")]
|
||||||
struct TestError {
|
struct TestStructError {
|
||||||
#[diagnostic_source]
|
#[diagnostic_source]
|
||||||
asdf_inner_foo: AnErr,
|
asdf_inner_foo: AnErr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
||||||
|
#[error("TestError")]
|
||||||
|
enum TestEnumError {
|
||||||
|
Without,
|
||||||
|
WithTuple(#[diagnostic_source] AnErr),
|
||||||
|
WithStruct {
|
||||||
|
#[diagnostic_source]
|
||||||
|
inner: AnErr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
||||||
|
#[error("TestError")]
|
||||||
|
struct TestTupleError(#[diagnostic_source] AnErr);
|
||||||
|
|
||||||
|
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
||||||
|
#[error("TestError")]
|
||||||
|
struct TestBoxedError(#[diagnostic_source] Box<dyn Diagnostic>);
|
||||||
|
|
||||||
|
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
|
||||||
|
#[error("TestError")]
|
||||||
|
struct TestArcedError(#[diagnostic_source] std::sync::Arc<dyn Diagnostic>);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_diagnostic_source() {
|
fn test_diagnostic_source() {
|
||||||
let error = TestError {
|
let error = TestStructError {
|
||||||
asdf_inner_foo: AnErr,
|
asdf_inner_foo: AnErr,
|
||||||
};
|
};
|
||||||
assert!(error.diagnostic_source().is_some());
|
assert!(error.diagnostic_source().is_some());
|
||||||
|
|
||||||
|
let error = TestEnumError::Without;
|
||||||
|
assert!(error.diagnostic_source().is_none());
|
||||||
|
|
||||||
|
let error = TestEnumError::WithTuple(AnErr);
|
||||||
|
assert!(error.diagnostic_source().is_some());
|
||||||
|
|
||||||
|
let error = TestEnumError::WithStruct { inner: AnErr };
|
||||||
|
assert!(error.diagnostic_source().is_some());
|
||||||
|
|
||||||
|
let error = TestTupleError(AnErr);
|
||||||
|
assert!(error.diagnostic_source().is_some());
|
||||||
|
|
||||||
|
let error = TestBoxedError(Box::new(AnErr));
|
||||||
|
assert!(error.diagnostic_source().is_some());
|
||||||
|
|
||||||
|
let error = TestArcedError(std::sync::Arc::new(AnErr));
|
||||||
|
assert!(error.diagnostic_source().is_some());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue