Skip to content

does not finish when trying to format a program with a ridiculous number of nested match #6079

Open
@cscherrNT

Description

@cscherrNT

When trying to format a program with a ridiculously nested datatype that is to be matched, rustfmt does not finish. It keeps a cpu core at 100%, but will not finish formatting even after 15 minutes. This might potentially break applications of rustfmt in CI/CD scripts and waste ressources.

Expected behavior
rustfmt finishes formatting quickly or returns some kind of error, explaining that it gives up on formatting ridiculously nested types/match statements.

Current behavior
rustfmt just keeps going, presumably trying to format the document, using up a full cpu core. This seems to start at a depth of about 16 match statements.

Reproduction
I know this source file is completely ridiculous, but bear with me. Note that compiling and running is near instantaneous.

See here for the full source and here for an example on how cpu time is wasted in context of CI/CD.

fn main() {
    let deep_type = Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(Option::Some(Option::Some(
        Option::Some(Option::Some(Option::Some(1337))),
    )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
;
    match match match match match match match match match match match match match match match match match match
    match match match match match match match match match match match match match match match match match match
    match match match match match match match match match match match match match match match match match match
    match match match match match match match match match match match match match match match match match match
    match match match match match match match match match match match match match match match match
        deep_type {
        Some(inner) => inner,
        None => unreachable!(),
    } {
        Some(inner) => inner,
        None => unreachable!(),
    } {
        Some(inner) => inner,
        None => unreachable!(),
    } {
        Some(inner) => inner,
        None => unreachable!(),
    } {
        Some(inner) => inner,
        None => unreachable!(),
/* it goes on a lot longer */

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-matchesmatch arms, patterns, blocks, etcp-low

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions