Skip to content

Compiler hangs and consumes unbounded memory with complex trait bounds #126952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
i1i1 opened this issue Jun 25, 2024 · 8 comments
Open

Compiler hangs and consumes unbounded memory with complex trait bounds #126952

i1i1 opened this issue Jun 25, 2024 · 8 comments
Labels
C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@i1i1
Copy link

i1i1 commented Jun 25, 2024

I tried this code:

trait TraitWithOneGeneric<P> {
    fn method(&self);
}

impl<C, P1, P2> TraitWithOneGeneric<(P1, P2)> for C
where
    C: TraitWithOneGeneric<P1> + TraitWithOneGeneric<P2>,
{
    fn method(&self) {}
}

// Generic instead of impl trait also doesn't work
fn do_smth<P>(a: &&impl TraitWithOneGeneric<P>) {
    a.method(1, 2, 3, 4)
}

I expected to see this happen: Code should throw an error and rustc should exit (or compile)

Instead, this happened: rustc doesn't exit at all and leaks memory infinitely.

Meta

I ran this code either on rust playground or via rustc --crate-type rlib -v lib.rs

rustc --version --verbose:

rustc 1.81.0-nightly (6b0f4b5ec 2024-06-24)
binary: rustc
commit-hash: 6b0f4b5ec3aa707ecaa78230722117324a4ce23c
commit-date: 2024-06-24
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Same behavior on stable:
rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
@i1i1 i1i1 added the C-bug Category: This is a bug. label Jun 25, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 25, 2024
@matthiaskrgr
Copy link
Member

Might be a duplicate of #80066

@i1i1
Copy link
Author

i1i1 commented Jun 25, 2024

Maybe you're right, but perf top tells that it spends 55-70% of the time in this function:

@lqd
Copy link
Member

lqd commented Jun 25, 2024

as expected in a lot of these cases of hangs: @rustbot label +fixed-by-next-solver

@rustbot

This comment was marked as outdated.

@rustbot

This comment was marked as outdated.

@rustbot rustbot added the fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. label Jun 25, 2024
@saethlin saethlin changed the title Compiler hangs and leaks memory with complex trait bounds Compiler hangs and consumes unbounded memory with complex trait bounds Jun 25, 2024
@saethlin saethlin added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 25, 2024
@saethlin
Copy link
Member

This is not a leak. It is very different to leak memory and have unbounded data structure growth or unbounded recursion, which is what's happening here.

@workingjubilee
Copy link
Member

the eventual stabilization PR for next-solver is gonna fix like half of the open issues for rust-lang/rust at this rate.

@i1i1
Copy link
Author

i1i1 commented Jun 26, 2024

Just in case, I've tried rustc with -Znext-solver and it shows the following error:

error[E0275]: overflow evaluating the requirement `&impl TraitWithOneGeneric<P>: TraitWithOneGeneric<_>`
  --> lib.rs:16:7
   |
16 |     a.method()
   |       ^^^^^^

While if I do this: (*a).method() everything works perfectly (even without next-solver)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants