rust-lang / rust-lang/rust

Polynomial slowdown in `impl Trait` composition

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

Nobody has claimed this yet.

A-impl-trait C-bug I-compiletime T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code:

pub trait A<'a> {
    fn or(&self, other: impl A<'a>) -> impl A<'a> {
        ()
    }
}

impl<'a> A<'a> for () {
}

pub fn main() {
    let x = ().or(()).or(());
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
    let x = x.or(x).or(x).or(x);
}

Playground link

I expected to see this happen: Give me lots of errors

Instead, this happened: Hang for a lot of time, then give me lots of errors

Meta

The issue persists both in stable and nightly.

I distilled this code from a real slowdown I encountered in a large code-base.

rustc --version --verbose:

rustc 1.87.0-nightly (617aad8c2 2025-02-24)
binary: rustc
commit-hash: 617aad8c2e8783f6df8e5d1f8bb1e4bcdc70aa7b
commit-date: 2025-02-24
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

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

Use the supplied Rust reproducer and Playground link as the entry point; first reproduce the hang with the reported stable and nightly rustc versions and measure how compilation time grows. Done means a confirmed compiler diagnosis and an actionable fix or report; the issue names no source file or regression test.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.