rust-lang / rust-lang/rust

Redundant `where` Clauses in `impl` Cause Confusing Cyclic Trait Reference Errors

Open
#137,369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-trait-system T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
// Dummy trait to mimic the `Related` trait from SeaORM.
trait Related<T> {}

// Symbol definitions to represent SeaORM entities.
struct User;
struct Post;
struct UserPostFavorite;

// Dummy implementations of `Related` for our types.
impl Related<UserPostFavorite> for Post {}
impl Related<UserPostFavorite> for User {}

// Trait with constraints on associated types via a `where` clause.
// This simulates a SeaORM relation-based query logic trait.
trait FavoriteAggregationQueryLogic
where
    Self::Favoritable: Related<Self::Bridge>,
    User: Related<Self::Bridge>,
{
    type Favoritable;
    type Bridge;
}

// A concrete type for which we implement the trait.
struct PostFavoriteAggregationQueryLogic;

// Implementing FavoriteAggregationQueryLogic for PostFavoriteAggregationQueryLogic.
// Note: We redundantly specify the same `where` constraints as in the trait declaration,
// which triggers a cyclic trait reference error.
impl FavoriteAggregationQueryLogic for PostFavoriteAggregationQueryLogic
where
    Self::Favoritable: Related<Self::Bridge>,
    User: Related<Self::Bridge>,
{
    type Favoritable = Post;
    type Bridge = UserPostFavorite;
}

fn main() {
    println!("This code is intended to reproduce the cyclic trait reference error.");
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0275]: overflow evaluating the requirement `<PostFavoriteAggregationQueryLogic as FavoriteAggregationQueryLogic>::Favoritable == _`
  --> src/main.rs:30:1
   |
30 | / impl FavoriteAggregationQueryLogic for PostFavoriteAggregationQueryLogic
31 | | where
32 | |     Self::Favoritable: Related<Self::Bridge>,
33 | |     User: Related<Self::Bridge>,
   | |________________________________^
34 |   {
35 |       type Favoritable = Post;
   |       ---------------- associated type `<PostFavoriteAggregationQueryLogic as FavoriteAggregationQueryLogic>::Favoritable` is specified here
   |
note: required for `PostFavoriteAggregationQueryLogic` to implement `FavoriteAggregationQueryLogic`
  --> src/main.rs:30:6
   |
30 | impl FavoriteAggregationQueryLogic for PostFavoriteAggregationQueryLogic
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31 | where
32 |     Self::Favoritable: Related<Self::Bridge>,
   |                        --------------------- unsatisfied trait bound introduced here
   = note: 1 redundant requirement hidden
   = note: required for `PostFavoriteAggregationQueryLogic` to implement `FavoriteAggregationQueryLogic`
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
   |
32 -     Self::Favoritable: Related<Self::Bridge>,
   |
Desired output

The redundant constraints should either be ignored by the compiler or yield a more informative error message that clearly indicates the duplication issue. Ideally, the compiler would accept the redundant constraints without triggering a cyclic trait reference error.

Rationale and extra context

When writing highly reusable code in SeaORM that leverages shared relation definitions, I encountered an issue where redundant where clauses in an impl block (duplicating constraints already specified in the trait) result in a cyclic trait reference error. This error message is misleading, making it hard to diagnose the root cause—the unnecessary duplication of trait bounds already declared in the trait.

In our SeaORM code, we define relationships such as User, Self::Favoritable (e.g., Post), and Self::Bridge (e.g., UserPostFavorite). The code above is a minimal reproduction of the issue using a simplified example.

Rust Version
❯ rustc --version --verbose
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and compile it with rustc 1.85.0, then compare the failing and passing Playground links. Investigate how the duplicated where clauses are handled and whether the compiler should accept them or report the duplication clearly; done means the redundant constraints no longer produce a misleading cyclic-reference error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.