Recursion Error E0275 in Generic Repository Implementation
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
When implementing a generic repository for insertion operations using async_trait and diesel_async, a recursion error (E0275) is triggered during compilation. This error is related to the Insertable trait implementation and occurs due to overloaded trait requirements in the insertion expressions. The insertion query (constructed with diesel::insert_into(...).values(&item).returning(...)) leads to excessively deep trait evaluation. The compiler suggests increasing the recursion limit with #![recursion_limit = "256"], but this workaround only postpones the problem without addressing the underlying cause. A refactoring to simplify or isolate the insertion logic might resolve the issue.
Code
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 / Salida del Error:
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 https://doc.rust-lang.org/cargo/reference/resolver.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 / Ejemplo Adicional de Implementación:
#[async_trait]
impl BaseRepository for UserRepository {
type Entity = NewUser;
type Table = users::table;
type Conn = diesel_async::pooled_connection::AsyncDieselConnection;
type Schema = (diesel::sql_types::Integer, diesel::sql_types::Text);
fn table() -> Self::Table {
users::table
}
}
Additional ICE Output when using #![recursion_limit = "2048"] / Salida Adicional de ICE al usar #![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"], indicating that the issue goes beyond just needing a higher 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 trait evaluation.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run cargo run in the vornix-core-rs workspace using the shown BaseRepository and UserRepository examples, then inspect the generated long-type file named in the E0275 output. Compare the normal recursion-limit failure with the reported ICE at 2048; done means the behavior is reduced to a reproducible compiler case or otherwise clearly characterized without relying on an unbounded recursion limit.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100