rust-lang / rust-lang/rust

Compilation Hang and Memory Exhaustion When Compiling Code with Associated Type and Trait Objects

Open
#117,275 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-dyn-trait C-bug F-impl_trait_in_assoc_type fixed-by-next-solver I-hang T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code:

pub trait Sampler<D> {
    type Iter<'a, F: FnMut(Self::Iter<'_, F>) -> f64>: Iterator<Item = D>
    where
        Self: 'a;
    fn sample<F: FnMut(&D) -> f64>(&self, pdf: F) -> Self::Iter<'_, F>;
}
pub struct Metropolis<D: 'static, S: Sampler<D>>(Vec<D>, S);
impl<D: 'static, S: Sampler<D>> Sampler<Vec<D>> for Metropolis<D, S> {
    type Iter<'a, F: FnMut(&Vec<D>) -> f64>
    where
        S: 'a,
    = impl Iterator<Item = Vec<D>> + 'a;
    fn sample<F: FnMut(&Vec<D>) -> f64>(&self, mut pdf: F) -> Self::Iter<'_, F> {
        || {
            self.1.sample(|_| 0.0);
        };
        std::iter::empty()
    }
}

I expected to see this happen: The compiler compiles successfully or outputs an error message.

Instead, this happened: The Rust compiler hangs indefinitely and exhausts memory during compilation, leading to an abnormal exit (return code 137).

Meta

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

The same problem is reproduced on the nightly version(1.75.0-nightly aa1a71e9e 2023-10-26)

what's more, to get more information,I also tried '-Z time-passes' (using nighly version):

Output

$ rustc code.rs -Z time-passes
time:   0.003; rss:   30MB ->   32MB (   +2MB)	parse_crate
time:   0.001; rss:   33MB ->   34MB (   +1MB)	setup_global_ctxt
time:   0.024; rss:   34MB ->   47MB (  +13MB)	expand_crate
time:   0.024; rss:   34MB ->   47MB (  +13MB)	macro_expand_crate
time:   0.002; rss:   47MB ->   49MB (   +2MB)	finalize_macro_resolutions
time:   0.003; rss:   49MB ->   52MB (   +3MB)	late_resolve_crate
time:   0.006; rss:   47MB ->   52MB (   +5MB)	resolve_crate
error[E0658]: `impl Trait` in associated types is unstable
  --> code.rs:12:7
   |
12 |     = impl Iterator<Item = Vec<D>> + 'a;
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
   = help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable

time:   0.007; rss:   52MB ->   58MB (   +5MB)	complete_gated_feature_checking
warning: where clause not allowed here
  --> code.rs:10:5
   |
10 | /     where
11 | |         S: 'a,
   | |______________^
   |
   = note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
   = note: `#[warn(deprecated_where_clause_location)]` on by default
help: move it to the end of the type declaration
   |
10 ~     
11 ~     = impl Iterator<Item = Vec<D>> + 'a where S: 'a;
   |

error[E0601]: `main` function not found in crate `code`
  --> code.rs:19:2
   |
19 | }
   |  ^ consider adding a `main` function to `code.rs`

time:   0.011; rss:   52MB ->   59MB (   +7MB)	looking_for_entry_point
time:   0.013; rss:   52MB ->   59MB (   +7MB)	misc_checking_1
time:   0.006; rss:   59MB ->   63MB (   +4MB)	type_collecting

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 by compiling the reproducer in code.rs with the nightly rustc command and compare the reported time-passes stages, especially type_collecting. Investigate why this associated type and trait-object combination hangs and exhausts memory; done means rustc terminates normally with a diagnostic or successful compilation instead of exiting with code 137.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.