Skip to content

Cargo check runs forever and memory usage grows unboundedly #116914

Closed

Description

@oliverb123

Apologies, I really couldn't find a way to minimise this example, but this commit causes cargo check to run forever and consume an ever-growing amount of memory. I tried to narrow down what was causing the problem, but just pulling the various trait definitions out into another crate didn't reproduce it. I think the issue is related to (almost certainly) poorly-defined associated type bounds in the various traits under src/proxy/routes, as it seemed to start happening after I started adding those, but I'm really not very confident in that assessment.

I expected to see this happen: cargo check would run in a bounded amount of space and time

Instead, this happened: cargo check seems to run forever and consume all of my memory

I can confirm the issue occurs on both nightly and stable, with versions:

rustc --version --verbose:

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

rustc --version --verbose:

rustc 1.75.0-nightly (09df6108c 2023-10-17)
binary: rustc
commit-hash: 09df6108c84fdec400043d99d9ee232336fd5a9f
commit-date: 2023-10-17
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

I can't include a backtrace because, naturally, the compiler doesn't exit, so setting RUST_BACKTRACE=1 has no effect

This is a screenshot of rustc consuming 23GB of ram while compiling the linked commit. I don't know why I thought this would be useful, but I guess I did, so here it is.
Screenshot from 2023-10-19 01-49-09

Activity

  1. added
    C-bug
    on Oct 19, 2023
  2. added
    needs-triage
    on Oct 19, 2023
  3. oliverb123 commented on Oct 19, 2023

    @oliverb123
    Author

    Good news! this commit has no dependencies and still reproduces the issue. cloc reports this as ~360 lines of code now, which is kind of small (not counting the example files, which I could have deleted, they're not relevant to the failure afaict), although I understand it's still not ideal, apologies again.

  4. added
    T-compiler
    I-hang
    E-needs-mcve
    and removed
    needs-triage
    on Oct 19, 2023
  5. horse-princess commented on Oct 19, 2023

    @horse-princess
    Contributor
    trait Filter {
        type ToMatch;
    }
    
    impl<T> Filter for T
    where
        T: Fn(Self::ToMatch),
    {
    }
    
    trait Rule {}
    
    impl<T> Rule for T
    where
        T: Filter,
    {
    }
    
    struct JustFilter<F: Filter> {
        filter: F,
    }
    
    impl<T: Filter> Rule for JustFilter<T> {}
    

    minimal repro, if you delete everything with Rule it correctly determines that the Filter impl overflows trying to check if T already implements Filter. interestingly enough, you seemingly cant change Fn into another trait that depends on Self::ToMatch, if i try to change the constraint into T: Trait<Self::ToMatch> rustc finds the overflow

  6. saethlin commented on Oct 19, 2023

    @saethlin
    Member

    Some quick poking with a debugger suggests that the compiler is endlessly emitting diagnostics.

    If this is a regression it isn't recent. This hangs on builds back to 1.60.

  7. estebank commented on Oct 19, 2023

    @estebank
    Contributor

    Running the repro with RUSTC_LOG=info I can see that calls to rustc_trait_selection::traits::project::project and rustc_trait_selection::traits::project::normalize_with_depth_to never go beyond depth=129.

    I did a quick and dirty test to check for normalize_with_depth.*\( and recursion_depth and add + 1 to every call that didn't have it. Doing so did cause the compilation to stop, but it was too indiscriminate so it would also break existing tests.

  8. erickt commented on Oct 19, 2023

    @erickt
    Contributor

    I think we're running into this on Fuchsia, which may take a long time for us to make a reproduction, but I can try to explain what we're seeing. We're on rustc 1.75.0-nightly (9d1e4b787 2023-10-11). Normally cargo build --release can complete in a few seconds, but if we insert a simple bug, such as adding a HashMap to a struct but forgetting to add use std::collections::HashMap), the compiler will emit the "did you mean to import ...?", but then it'll fall into an apparent infinite loop around rustc_trait_selection::traits::project::normalize_with_depth_to. I've seen a build go past 30 minutes before I've killed it.

    This sounds pretty analogous to a exponential expansion bug that was fixed in #114948. Perhaps diagnostics also needs to add something similar do avoid the same thing?

  9. lqd commented on Oct 21, 2023

    @lqd
    Member

    Not that it matters much but this looks quite old: it used to trigger a solving overflow but seems to have started infinite looping in nightly-2021-09-21. I seem to recall #89125 from this nightly being involved in some other compilation time issues.

  10. Zannick commented on Oct 22, 2023

    @Zannick

    Does turning overflow-checks off solve this issue?

    [profile.dev]
    overflow-checks: false
    

    I ask because I want to see if it's the same as my issue: I've been having a similar problem with cargo build only in dev profile spiking very sharply to 24 GiB during the build, and after trying some of the differences in profiles, I can now safely build and check in dev without a memory explosion, just by turning off overflow-checks. (More details)

  11. lqd commented on Oct 22, 2023

    @lqd
    Member

    @Zannick You're probably seeing a different issue. Here, the infinite loop is related to error diagnostics and trait solving.

  12. kpreid commented on Dec 25, 2023

    @kpreid
    Contributor

    @rustbot label -E-needs-mcve

    Triage: #116914 (comment) is a MCVE for this

  13. removed
    E-needs-mcve
    on Dec 25, 2023
  14. estebank commented on Dec 29, 2023

    @estebank
    Contributor

    I wonder if an early error checking for these unconditionally recursive definitions might be worth it while we track down the right place to fix this correctly.

  15. jakubadamw commented on Apr 1, 2026

    @jakubadamw
    Contributor

    The type checking now completes with:

    error[E0391]: cycle detected when computing whether `<impl at main.rs:5:1: 7:26>` has a guaranteed unsized self type
     --> main.rs:5:1
      |
    5 | / impl<T> Filter for T
    6 | | where
    7 | |     T: Fn(Self::ToMatch),
      | |_________________________^
      |
    note: ...which requires computing normalized predicates of `<impl at main.rs:5:1: 7:26>`...
     --> main.rs:5:1
      |
    5 | / impl<T> Filter for T
    6 | | where
    7 | |     T: Fn(Self::ToMatch),
      | |_________________________^
      = note: ...which again requires computing whether `<impl at main.rs:5:1: 7:26>` has a guaranteed unsized self type, completing the cycle
    note: cycle used when checking that `<impl at main.rs:5:1: 7:26>` is well-formed
     --> main.rs:5:1
      |
    5 | / impl<T> Filter for T
    6 | | where
    7 | |     T: Fn(Self::ToMatch),
      | |_________________________^
      = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
    
    error: aborting due to 1 previous error
    
    For more information about this error, try `rustc --explain E0391`.
    

    Can be either closed or
    @rustbot label: +E-needs-test.

  16. added
    E-needs-test
    on Apr 1, 2026
  17. zakrad commented on Aug 8, 2026

    @zakrad
    Contributor

    @rustbot claim

  18. added a commit that references this issue on Aug 9, 2026
    ca191f8
  19. added 2 commits that reference this issue on Aug 9, 2026
    bcba91d
    eab3d62
  20. added a commit that references this issue on Aug 9, 2026
    4f3a81f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugE-needs-testI-hangT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions