Skip to content

Rust compiler hangs when using macro_rules! to generate code with complex type parameters #116681

Open
@iamanonymouscs

Description

@iamanonymouscs

Code

cat hang.rs

trait Trait {
    type Assoc;
}
impl Trait for () {
    type Assoc = ();
}
macro_rules! m {
    ([$($t:tt)*] [$($open:tt)*] [$($close:tt)*]) => {
        m!{[$($t)*][$($open)*$($open)*][$($close)*$($close)*]}
    };
    ([] [$($open:tt)*] [$($close:tt)*]) => {
        fn _f() -> $($open)*()$($close)* {}
    };
}
m! {[###########][impl Trait<Assoc =][>]}

Command

rustc -C opt-level=0 hang.rs

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

Instead, this happened: The code, as provided below, appears to encounter a hang

The same problem is reproduced on the nightly version(1.75.0-nightly (d627cf0 2023-10-10)) as well.

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

There is not any error information or stack information when using export RUST_BACKTRACE=1

Backtrace

<backtrace>

what's more, to get more information,I also tried '-Z time-passes' (using nighly version):
time:   0.000; rss:   30MB ->   32MB (   +2MB)  parse_crate

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)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