Skip to content

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

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
iamanonymouscs opened this issue Oct 27, 2023 · 3 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-dyn-trait Area: trait objects, vtable layout C-bug Category: This is a bug. F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@iamanonymouscs
Copy link

iamanonymouscs commented Oct 27, 2023

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

@iamanonymouscs iamanonymouscs added the C-bug Category: This is a bug. label Oct 27, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 27, 2023
@krtab

This comment was marked as off-topic.

@rustbot

This comment was marked as off-topic.

@krtab

This comment was marked as off-topic.

@krtab krtab removed their assignment Oct 27, 2023
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 28, 2023
@saethlin saethlin added the F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` label Nov 8, 2023
@fmease fmease added A-associated-items Area: Associated items (types, constants & functions) A-dyn-trait Area: trait objects, vtable layout fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-dyn-trait Area: trait objects, vtable layout C-bug Category: This is a bug. F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants