rust-lang / rust-lang/rust

Trait unexpectedly not implemented with recursive where clause, with poor error message

Open
#141,111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-system C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This is an issue we came across in Firefox. Note that the Animate impls are derived (including the where clause), which made it a bit more subtle. This is a reduced example of what's going on:

trait Animate : Sized {
    fn animate(&self) {}
}

struct GenericFoo<T> {
    something: i32,
    fallback: T,
}
enum GenericBar<T> {
    None,
    TheThing(T),
    Foo(Box<GenericFoo<Self>>),
}

impl Animate for i32 {}
impl Animate for f32 {}
impl<T: Animate> Animate for GenericFoo<T> {}

impl<T: Animate> Animate for GenericBar<T>
where
    GenericFoo<Self>: Animate,
{}

fn main() {
    let bar = GenericBar::<f32>::None;
    bar.animate();
}

I expect this code to work (like it does if you remove the where GenericFoo<Self>: Animate), or at least provide a more descriptive error message (something about the where clause being cyclic or something, which is what I assume is going on, as GenericFoo<Self>: Animate requires Self: Animate?)

Instead, the compiler silently doesn't implement the trait for GenericBar<f32>, and there's nothing in the error output which would point you to the culprit (the where clause).

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (16d2276fa 2025-05-16)

But happens also in stable.

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 reduced Rust example in the issue and run it with rustc on stable and nightly, recording the trait error output. Compare the behavior with and without the recursive where clause. Done means either the valid implementation is accepted or the diagnostic identifies the cyclic where-clause requirement clearly.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.