Skip to content

Commit

Permalink
Improve how the report macro handles return type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Mar 11, 2024
1 parent c74192b commit d9fd1bc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
25 changes: 25 additions & 0 deletions compatibility-tests/futures/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ fn async_std_main_attribute_last() {
async fn async_std_test_attribute_last() -> Result<(), Error> {
Ok(())
}

#[test]
fn works_with_tough_inference() {
#[derive(Debug, Snafu)]
struct InnerError;

#[derive(Debug, Snafu)]
struct OuterError {
source: InnerError,
}

fn inner() -> Result<(), InnerError> {
InnerSnafu.fail()
}

#[snafu::report]
#[tokio::main(flavor = "current_thread")]
async fn mainlike() -> Result<(), OuterError> {
loop {
inner().context(OuterSnafu)?;
}
}

let _: Report<_> = mainlike();
}
16 changes: 8 additions & 8 deletions snafu-derive/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,31 @@ pub fn body(
if cfg!(feature = "rust_1_61") {
quote! {
{
let __snafu_body = async #block;
<::snafu::Report<_> as ::core::convert::From<_>>::from(__snafu_body.await)
let __snafu_body: #output_ty = async #block.await;
<::snafu::Report<_> as ::core::convert::From<_>>::from(__snafu_body)
}
}
} else {
quote! {
{
let __snafu_body = async #block;
::core::result::Result::map_err(__snafu_body.await, ::snafu::Report::from_error)
let __snafu_body: #output_ty = async #block.await;
::core::result::Result::map_err(__snafu_body, ::snafu::Report::from_error)
}
}
}
} else {
if cfg!(feature = "rust_1_61") {
quote! {
{
let __snafu_body = || #block;
<::snafu::Report<_> as ::core::convert::From<_>>::from(__snafu_body())
let __snafu_body: #output_ty = (|| #block)();
<::snafu::Report<_> as ::core::convert::From<_>>::from(__snafu_body)
}
}
} else {
quote! {
{
let __snafu_body = || #block;
::core::result::Result::map_err(__snafu_body(), ::snafu::Report::from_error)
let __snafu_body: #output_ty = (|| #block)();
::core::result::Result::map_err(__snafu_body, ::snafu::Report::from_error)
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ fn procedural_macro_works_with_result_return_type() {
let _: Report<Error> = mainlike_result();
}

#[test]
fn procedural_macro_works_with_tough_inference() {
#[derive(Debug, Snafu)]
struct InnerError;

#[derive(Debug, Snafu)]
struct OuterError {
source: InnerError,
}

fn inner() -> Result<(), InnerError> {
InnerSnafu.fail()
}

#[snafu::report]
fn mainlike_result() -> Result<(), OuterError> {
loop {
inner().context(OuterSnafu)?;
}
}

let _: Report<_> = mainlike_result();
}

#[test]
fn termination_returns_failure_code() {
use std::process::Termination;
Expand Down

0 comments on commit d9fd1bc

Please sign in to comment.