Skip to content

Rust compiler hangs when compiling code with complex struct #117443

Open
@iamanonymouscs

Description

@iamanonymouscs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions