Skip to content

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

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 13, 2023 · 2 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. 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

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
@iamanonymouscs iamanonymouscs added the C-bug Category: This is a bug. label Oct 13, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 13, 2023
@Noratrieb Noratrieb added I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. T-types Relevant to the types team, which will review and decide on the PR/issue. A-trait-system Area: Trait system and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 14, 2023
@clubby789
Copy link
Contributor

Debugging this quickly it looks like it hangs somewhere in macro expansion

@clubby789 clubby789 added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed A-trait-system Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. labels Oct 14, 2023
@Noratrieb
Copy link
Member

Without investigating it, I suspect this macro has an infinite loop that isn't detected by the compiler.
#103029 may help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. 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

4 participants