最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asynchronous - Recursion Error E0275 in Generic Repository Implementation - Stack Overflow

programmeradmin3浏览0评论

I'm implementing a generic repository for insertion operations using async_trait and diesel_async in Rust, and I'm encountering a recursion error during compilation. The error (E0275) appears to be related to the Insertable trait implementation due to overloaded trait requirements in the insertion expressions. In my code, the insertion query is constructed using diesel::insert_into(...).values(&item).returning(...), which seems to trigger an excessively deep trait evaluation.

The compiler suggests increasing the recursion limit with #![recursion_limit = "256"], but this workaround only postpones the error without addressing the root cause. Additionally, when I set #![recursion_limit = "2048"], I receive an internal compiler error (ICE) indicating that the trait requirements and associated types in the insertion query are too complex to resolve.

I'm looking for guidance on how to refactor or simplify the insertion logic to avoid this deep recursive trait evaluation.

Code:

use async_trait::async_trait;
use diesel::{
    pg::Pg,
    query_builder::{AsQuery, InsertStatement, QueryFragment}, 
    Identifiable, Insertable, Queryable, Selectable,
    Table,
    sql_types::SingleValue, QuerySource
};
use diesel_async::{AsyncConnection, RunQueryDsl, methods::LoadQuery};

// Trait principal reestructurado para evitar recursión
#[async_trait]
pub trait BaseRepository: Send + 'static {
    // Tipos asociados claramente definidos
    type Entity: Identifiable + Send + 'static;
    type Table: Table + QuerySource + Send + 'static;
    type Conn: AsyncConnection<Backend = Pg> + Send + 'static;
    type Schema;

    // Método para obtener la tabla / Method to get the table
    fn table() -> Self::Table;

    // Método de inserción reestructurado / Restructured insertion method
    async fn insert<I>(
        &self,
        conn: &mut Self::Conn,
        item: I,
    ) -> Result<Self::Entity, diesel::result::Error>
    where
        Self::Entity: Queryable<Self::Schema, Pg> + Selectable<Pg> + 'static,
        Self::Schema: SingleValue + Send + 'static,
        I: Insertable<Self::Table> + Send + 'static,
        I::Values: QueryFragment<Pg> + Send + 'static,
        InsertStatement<Self::Table, I::Values>: AsQuery + Send + 'static,
        <InsertStatement<Self::Table, I::Values> as AsQuery>::Query: 
            LoadQuery<'static, Self::Conn, Self::Entity> + Send,
        <Self::Table as QuerySource>::FromClause: Send,
    {
        let query = diesel::insert_into(Self::table())
            .values(&item)
            .returning(Self::table().all_columns());
        
        query.get_result(conn).await
    }
}

Error Output:

PS D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\vornix-ecomerce-erp-service> cargo run
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\vornix-billing-service\Cargo.toml
workspace: D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\Cargo.toml
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\vornix-ecomerce-erp-service\Cargo.toml
workspace: D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\Cargo.toml
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see .html#resolver-versions
   Compiling vornix-core-rs v0.1.0 (D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\vornix-core-rs)
error[E0275]: overflow evaluating the requirement `diesel::expression::operators::Eq<_, &_>: Insertable<_>`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`vornix_core_rs`)
  = note: required for `&diesel::expression::operators::Eq<_, _>` to implement `Insertable<_>`
  = note: 126 redundant requirements hidden
  = note: required for `&Option<Option<Option<Option<Option<Option<...>>>>>>` to implement `Insertable<_>`
  = note: the full name for the type has been written to 'D:\my-proyects\Vornix-azure\Ecomerce-Optica\backend\target\debug\deps\vornix_core_rs-da120415f0e5dafb.long-type-12242326637788567344.txt'
  = note: consider using `--verbose` to print the full type name to the console

For more information about this error, try `rustc --explain E0275`.
error: could not compile `vornix-core-rs` (lib) due to 1 previous error

Additional Implementation Example:

#[async_trait]
impl BaseRepository for UserRepository {
    type Entity = NewUser;
    type Table = users::table;
    type Conn = diesel_async::pooled_connection::AsyncDieselConnection<Pg>;
    type Schema = (diesel::sql_types::Integer, diesel::sql_types::Text);

    fn table() -> Self::Table {
        users::table
    }
}

Additional ICE Output when using #![recursion_limit = "2048"]:

741:     0x7ff82e25b71f - <rustc_trait_selection[696cc11a6b5294cd]::traits::select::SelectionContext>::poly_select
742:     0x7ff82e1084ef - core[b826f7616f2dc1fa]::slice::sort::unstable::heapsort::heapsort::<rustc_span[3904f55a5cfd74f4]::def_id::DefId, <[rustc_span[3904f55a5cfd74f4]::def_id::DefId]>::sort_unstable_by_key<(i64, usize), rustc_trait_selection[696cc11a6b5294cd]::traits::specialize::specialization_graph_provider::{closure#0}>::{closure#0}>
743:     0x7ff82e868d21 - <rustc_data_structures[5dc475d9950331dc]::profiling::_::InternalBitFlags as core[b826f7616f2dc1fa]::str::traits::FromStr>::from_str
744:     0x7ff8b137fef0 - SetSecurityDescriptorControl
745:     0x7ff8b409bf29 - RtlUserFiberStart

error: the compiler unexpectedly panicked. this is a bug.

Notes:

The ICE (Internal Compiler Error) occurs when using #![recursion_limit = "2048"], which indicates that the issue goes beyond simply increasing the recursion limit.

This is not a simple misconfiguration. It suggests that the trait requirements and associated types in the insertion query are too complex for the compiler to resolve—possibly due to deeply nested types generated by Diesel's query builder.

A refactoring to simplify or isolate parts of the insertion logic is recommended to avoid such deep recursive trait evaluation.

Any help or suggestions to resolve this would be greatly appreciated. Thank you!

发布评论

评论列表(0)

  1. 暂无评论