rust-lang / rust-lang/rust

Infinite call recursion in rustc when resolving associated types

Open
#145,998 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug fixed-by-next-solver I-hang T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When compiling the code below, rustc hangs indefinitely by calling a chain of functions recursively and continuously allocates memory, eventually causing the computer to run out of memory.

Note keeping FooTypeA and removing FooTypeB seems to produce the intended behaviour.

Code
pub trait Foo {
    type FooTypeA<'a>;
    type FooTypeB<'a>;
}

pub struct Implementor<C> {
    member: C
}

impl<C> Foo for Implementor<C>
where
        for<'a> C: Foo<FooTypeA<'a>=Self::FooTypeA<'a>, FooTypeB<'a>=Self::FooTypeB<'a>>,
{
    type FooTypeA<'a> = ();
    type FooTypeB<'a> = ();
}
Meta

The bug can be reproduced with both the latest stable and nightly compiler.

Stable
rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Nightly
rustc --version --verbose:

rustc 1.91.0-nightly (f2824da98 2025-08-28)
binary: rustc
commit-hash: f2824da98d44c4a4e17bf39eb45103c8fc249117
commit-date: 2025-08-28
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0
Error output

No error output, the compiler hangs until the machine gets OOM.

Stacktrace

To produce a stacktrace, I used heaptrack and attached it to the running compiler.
I obviously only kept a short part of the stacktrace, since it's calling functions recursively.

Backtrace

rustc_trait_selection::traits::project::opt_normalize_projection_term in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::select::SelectionContext>::poly_select::{closure#0} in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_binder::<rustc_type_ir::predicate_kind::PredicateKind<rustc_middle::ty::context::TyCtxt>> in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty in librustc_driver-10840eac1eb39611.so
rustc_trait_selection::traits::project::opt_normalize_projection_term in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::select::SelectionContext>::poly_select::{closure#0} in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_binder::<rustc_type_ir::predicate_kind::PredicateKind<rustc_middle::ty::context::TyCtxt>> in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty in librustc_driver-10840eac1eb39611.so
rustc_trait_selection::traits::project::opt_normalize_projection_term in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::select::SelectionContext>::poly_select::{closure#0} in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_binder::<rustc_type_ir::predicate_kind::PredicateKind<rustc_middle::ty::context::TyCtxt>> in librustc_driver-10840eac1eb39611.so
<rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty in librustc_driver-10840eac1eb39611.so
rustc_trait_selection::traits::project::opt_normalize_projection_term in librustc_driver-10840eac1eb39611.so
[snip]

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

Compile the provided minimal example with the reported stable and nightly rustc versions, then inspect the recursion through opt_normalize_projection_term, poly_select, AssocTypeNormalizer::fold_ty, and fold_binder. Done means the compiler terminates with a diagnostic instead of exhausting memory, while retaining the behavior observed when FooTypeB is removed.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.