Skip to content

whatever_context with ?-based return requires type annotations when a transparent member exists in the error enum #521

@TapGhoul

Description

@TapGhoul

This is on Snafu 0.8.9, default features, on rust 1.90.0.

When creating a snafu error enum that uses a whatever-based return, the code fails to compile. Below is an example where this fails:

use snafu::{whatever, ResultExt, Snafu};

#[derive(Debug, Snafu)]
enum MyError {
    #[snafu(transparent)]
    SomeTransparentError { source: std::io::Error },

    #[snafu(whatever, display("{message}"))]
    SomeWhateverError {
        message: String,
        #[snafu(source(from(Box<dyn std::error::Error>, Some)))]
        source: Option<Box<dyn std::error::Error>>,
    },
}

fn broken_fn() -> Result<(), MyError> {
    inner_fn().whatever_context("some context")?;
    Ok(())
}

fn working_fn() -> Result<(), MyError> {
    inner_fn().whatever_context("some context")
}

fn inner_fn() -> Result<(), MyError> {
    whatever!("die");
}

The broken_fn() fails to compile with the following error:

error[E0284]: type annotations needed
  --> scheduler/src/buggy.rs:17:16
   |
17 |     inner_fn().whatever_context("some context")?;
   |                ^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `E2` declared on the method `whatever_context`
   |
   = note: cannot satisfy `<_ as FromString>::Source == _`
help: consider specifying the generic arguments
   |
17 |     inner_fn().whatever_context::<&str, E2>("some context")?;
   |                                ++++++++++++

If you remove the #[snafu(transparent)] annotation, this compiles perfectly. You can have as many variants as you want, and it doesn't seem to matter what the transparent error actually is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    found in the fieldA user of SNAFU found this when trying to use it

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions