diesel-rs/diesel · error · syn::Error

Function return type should be explicitly specified as `->…

Error message

Function return type should be explicitly specified as `-> _` for auto_type

What it means

The function's return type must be written as `-> _` (or be a partially specified type containing `_`) so auto_type can substitute the inferred type. Any other explicit return type (e.g. `-> i32`, `-> Vec<T>`) cannot be handled and expansion fails; the message names the common case `-> _`, though `_`-containing types are also accepted.

Solutions

  1. Change the return type to `-> _` and let the macro infer it.
  2. If you need a partially specified type, use `_` inside it where the macro should infer (e.g. `-> Vec<_>`), which is also accepted.
  3. Remove #[auto_type] if you want to keep the explicit return type.

Example fix

// before
#[auto_type]
fn f() -> i32 { 1 }
// after
#[auto_type]
fn f() -> _ { 1 }
Defensive patterns

Strategy: validation

Validate before calling

fn ret_is_infer(sig: &syn::Signature) -> bool {
    matches!(&sig.output, syn::ReturnType::Default) || ty_contains_infer(&sig.output)
} // require `-> _` (or `-> ...<_>`) before applying #[auto_type]

Prevention

When it happens

Trigger: `#[auto_type] fn f() -> i32 { ... }` or any concrete/complex return type not containing `_` on an auto_type function.

Common situations: Adding #[auto_type] to an existing fully-typed function to obtain the type alias; leftover explicit return types after introducing the macro; confusion about whether `_` is legal in return position (it is, and required here).

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07). Data as JSON: /api/errors/766e9f03d1c5b5e0. Report an issue: GitHub.

Appendix: source

Thrown at dsl_auto_type/src/auto_type/mod.rs:168

            // Add local variables from the function body, and finally infer the type
            local_variables_map.infer_block_expression_type(
                return_expression,
                Some(&return_type),
                &input_function.block,
                &mut errors,
            )
        }
        _ => {
            // This error message is not strictly correct: we also support
            // partially-specified return types that involve `_`, but for simplicity we just
            // put the overwhelmingly most common case in this error message
            return Err(syn::Error::new(
                input_function.sig.output.span(),
                "Function return type should be explicitly specified as `-> _` for auto_type",
            )
            .into());
        }
    };

    let type_alias = match type_alias {
        Some(type_alias) => {
            // We're generating a type alias so we need to extract the necessary lifetimes and
            // generic type parameters for that type alias
            let type_alias_generics = referenced_generics::extract_referenced_generics(
                &return_type,
                &input_function.sig.generics,
                &mut errors,
            );

            let vis = &input_function.vis;
            input_function.sig.output = parse_quote!(-> #type_alias #type_alias_generics);
            quote! {
                #[allow(non_camel_case_types)]
                #vis type #type_alias #type_alias_generics = #return_type;
            }
        }

View on GitHub (pinned to 6fa6ed01b2)