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

cannot find argument corresponding to the generic

Error message

cannot find argument corresponding to the generic

What it means

When expanding a variadic SQL function, diesel maps each generic type parameter to the function argument that uses it. In `expand_nonvariadic`, if a generic parameter appears in the signature but no argument with that exact type is found, the macro cannot wire the generic into the generated code and errors at the type argument's span.

Solutions

  1. Make every generic type parameter correspond to exactly one function argument written as the bare generic name.
  2. Remove generics that have no corresponding argument.
  3. For wrapped types (e.g. `Nullable<T>`), check how diesel's existing sql_function definitions model them and follow that pattern.

Example fix

// before
#[sql_function]
fn coalesce<T: SqlType>(x: Nullable<T>); // T has no bare argument

// after
#[sql_function]
fn coalesce<T: SqlType + SingleValue>(x: Nullable<T>, y: T); // T maps to argument y
Defensive patterns

Strategy: validation

Validate before calling

// Every generic <T: SqlType> must appear as a bare argument type
// fn f<T: SqlType>(x: T)  -- OK
// fn f<T: SqlType>(x: Nullable<T>) -- generic T has no matching bare argument

Prevention

When it happens

Trigger: Declaring a `#[sql_function]` whose generic parameter is not used directly as a plain argument type (e.g. wrapped in `Option<T>`, renamed, or the generic has no corresponding argument at all) while using `#[variadic]` expansion.

Common situations: Writing generic SQL function definitions where a type parameter appears only inside another type constructor, or removing/renaming an argument without removing its generic.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at diesel_derives/src/sql_function.rs:632

        } else {
            let auto_derived_types = type_args
                .iter()
                .map(|type_arg| {
                    for arg in &args {
                        let Type::Path(path) = &arg.ty else {
                            continue;
                        };

                        let Some(path_ident) = path.path.get_ident() else {
                            continue;
                        };

                        if path_ident == type_arg {
                            return Ok(arg.name.clone());
                        }
                    }

                    Err(syn::Error::new(
                        type_arg.span(),
                        "cannot find argument corresponding to the generic",
                    ))
                })
                .collect::<Result<Vec<_>>>()?;

            let arg_names_iter: Vec<_> = args.iter().map(|arg| arg.name.clone()).collect();

            let return_type_module_name =
                Ident::new(&format!("__{fn_name}_return_type"), fn_name.span());

            let doc =
                format!("Return type of the [`{fn_name}()`](fn@super::{fn_name}) SQL function.");
            let return_type_helper_module = quote! {
                #[allow(non_camel_case_types, non_snake_case, unused_imports)]
                #[doc(inline)]
                mod #return_type_module_name {
                    #[doc = #doc]

View on GitHub (pinned to 6fa6ed01b2)