Compilation Hang and Memory Exhaustion When Compiling Code with Associated Type and Trait Objects #117275
Open
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
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: trait objects, vtable layoutCategory: This is a bug.`#![feature(impl_trait_in_assoc_type)]`Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.Relevant to the compiler team, which will review and decide on the PR/issue.Fixed by the next-generation trait solver, `-Znext-solver`.
Activity