Skip to content

MBE macro expansion stuck #14074

@oxalica

Description

@oxalica

To reproduce, write a src/lib.rs with contents

macro_rules! ty {
    (string) => { "string" };
    ($arg:tt -> $($ret:tt)+) => { (ty!($arg), ty!($($ret)+)) };
    ({ $($key:literal : $ty:tt),* }) => {
        [
            $(
                ($key, ty!($ty)),
            )*
        ]
    };
}

pub fn foo() {
    ty!({
        "description": string,
    });
}

Run rust-analyzer diagnostics src/lib.rs, and it stuck with 100% CPU usage.

Backtrace: https://gist.github.com/oxalica/0088ef4eed00478a7c886576fbc905b8

rust-analyzer version: Both 2023-01-23 and current master 7acc434 can reproduce.

rustc version:

rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6

extra information
The macro input has an unexpected trailing , thus fails to match. cargo check on the code above will terminate with these errors. But there is no infinite macro recursion.

    Checking testt v0.1.0 (/home/oxa/tmp/testt)
error: unexpected end of macro invocation
  --> src/lib.rs:16:6
   |
1  | macro_rules! ty {
   | --------------- when calling this macro
...
16 |     });
   |      ^ missing tokens in macro arguments
   |
note: while trying to match `->`
  --> src/lib.rs:3:14
   |
3  |     ($arg:tt -> $($ret:tt)+) => { (ty!($arg), ty!($($ret)+)) };
   |              ^^

error: could not compile `testt` due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macromacro expansionC-bugCategory: bugI-hangIssue: The analysis never terminates, due to infinite loops, deadlock, livelock, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions