rust-lang / rust-lang/rust

Rust compiler hangs when compiling code with complex struct

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

Nobody has claimed this yet.

C-bug I-hang T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code with rustc -C opt-level=1 code.rs

#![recursion_limit = "1024"]
#![allow(dead_code)]
use std::mem;
pub struct S0<T>(T, T);
pub struct S1<T>(Option<Box<S0<S0<T>>>>, Option<Box<S0<S0<T>>>>);
pub struct S2<T>(Option<Box<S1<S1<T>>>>, Option<Box<S1<S1<T>>>>);
pub struct S3<T>(Option<Box<S2<S2<T>>>>, Option<Box<S2<S2<T>>>>);
pub struct S4<T>(Option<Box<S3<S3<T>>>>, Option<Box<S3<S3<T>>>>);
pub struct S5<T>(Option<Box<S4<S4<T>>>>, Option<Box<S4<S4<T>>>>, Option<T>);
trait Foo {
    fn xxx(&self) {}
}
impl<T> Foo for T
where
    T: Foo,
    T: Sync,
{
    fn xxx(&self) {}
}
impl Foo for S5<u8> {
    fn xxx(&self) {}
}
trait Bar {}
impl<T> Foo for T
where
    T: Bar,
    T: Sync,
{
    fn xxx(&self) {}
}
impl Foo for S5<u8> {
    fn xxx(&self) {}
}

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

Instead, this happened: The Rust compiler hangs indefinitely.

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 31bc7e2c4 2023-10-30)

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

Output

time:   0.001; rss:   29MB ->   31MB (   +2MB)	parse_crate
time:   0.006; rss:   34MB ->   48MB (  +13MB)	expand_crate
time:   0.006; rss:   34MB ->   48MB (  +13MB)	macro_expand_crate
warning: unused import: `std::mem`
 --> code.rs:3:5
  |
3 | use std::mem;
  |     ^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

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

time:   0.014; rss:   51MB ->   56MB (   +6MB)	looking_for_entry_point
time:   0.014; rss:   51MB ->   57MB (   +6MB)	misc_checking_1
time:   0.001; rss:   57MB ->   60MB (   +3MB)	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 with the reproducer in code.rs and run it with rustc -C opt-level=1, then compare the -Z time-passes output that stops at type_collecting. The fix is complete when this code no longer hangs indefinitely and instead compiles successfully or reports an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.