Skip to content

Generic types are not handled properly in the Type proc-macro #3882

Open
@ivan770

Description

@ivan770

I have found these related issues/pull requests

#3195 seems to mention this issue, but in a different context.

Description

This codegen snippet omits generic types when generating the sqlx::Type impl:

tts.extend(quote!(
#[automatically_derived]
impl ::sqlx::Type<::sqlx::Postgres> for #ident {
fn type_info() -> ::sqlx::postgres::PgTypeInfo {
::sqlx::postgres::PgTypeInfo::with_name(#ty_name)
}
}
));
if !attributes.no_pg_array {
tts.extend(quote!(
#[automatically_derived]
impl ::sqlx::postgres::PgHasArrayType for #ident {
fn array_type_info() -> ::sqlx::postgres::PgTypeInfo {
::sqlx::postgres::PgTypeInfo::array_of(#ty_name)
}
}
));
}
}

The same file contains a valid codegen logic just above the previously mentioned code fragment:

let mut tokens = quote!(
#[automatically_derived]
impl #impl_generics ::sqlx::Type< DB > for #ident #ty_generics #where_clause {
fn type_info() -> DB::TypeInfo {
<#ty as ::sqlx::Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> ::std::primitive::bool {
<#ty as ::sqlx::Type<DB>>::compatible(ty)
}
}
);
if cfg!(feature = "postgres") && !attr.no_pg_array {
tokens.extend(quote!(
#[automatically_derived]
impl #array_impl_generics ::sqlx::postgres::PgHasArrayType for #ident #ty_generics
#array_where_clause {
fn array_type_info() -> ::sqlx::postgres::PgTypeInfo {
<#ty as ::sqlx::postgres::PgHasArrayType>::array_type_info()
}
}
));
}

Reproduction steps

  1. Create a struct with a generic type
  2. Derive sqlx::Type for it
  3. Enable postgres-related features in sqlx

This should result in an error similar to this one:

error[E0107]: missing generics for struct `Test`
   --> [snip]
    |
118 | pub struct Test<OC: Serialize + DeserializeOwned + 'static> {
    |            ^^^^ expected 1 generic argument
    |
note: struct defined here, with 1 generic parameter: `OC`
   --> [snip]
    |
118 | pub struct Test<OC: Serialize + DeserializeOwned + 'static> {
    |            ^^^^ --
help: add missing generic argument
    |
118 | pub struct Test<OC><OC: Serialize + DeserializeOwned + 'static> {
    |                ++++

SQLx version

0.8.6

Enabled SQLx features

runtime-tokio-native-tls,postgres,chrono,json,macros,migrate,uuid

Database server and version

Postgres 16.9

Operating system

NixOS 25.05

Rust version

rustc 1.87.0 (17067e9ac 2025-05-09)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions