Skip to content
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

Semi-complex arg! macro invocation causes stack overflow and huge slowdown with build, check, or clippy #4670

Closed
2 tasks done
TED-996 opened this issue Jan 24, 2023 · 4 comments
Labels
C-bug Category: Updating dependencies

Comments

@TED-996
Copy link

TED-996 commented Jan 24, 2023

Please complete the following tasks

Rust Version

rustc 1.66.1 (90743e729 2023-01-10)

Clap Version

4.1.3

Minimal reproducible code

fn main() {
    let _ = clap::arg!(--add-b-alias <ALIAS> <NAME> ...);
}

Steps to reproduce the bug with the above code

cargo check

Actual Behaviour

The check command takes an unreasonably long amount of time to complete (around 10 seconds on my machine for the repro code alone). cargo build and cargo clippy similarly take an unreasonably long time to complete.

When running, the program overflows the stack.

Expected Behaviour

The check command should complete in a very short amount of time (less than one second). cargo build and cargo clippy should take a relatively short time as well.

When running, the program should execute to completion.

Additional Context

It seems like the combination of a three-word option name, num_args/value_names and multiple values (...) is relevant. The following examples all take a more reasonable amount of time:

    let _ = clap::arg!(--add-alias <ALIAS> <NAME> ...);  // this is better (~1s on my machine)
    let _ = clap::arg!(--add-b-alias <ALIAS> <NAME>);    // this is much better (.7s on my machine)
    let _ = clap::arg!(--add-b-alias ...);               // this is also much better (.5s on my machine)

I have worked around the problem by building the Arg struct manually. However this makes the arg! macro unusable for me - the editor experience is destroyed because with checking slowed down so much, even syntax higlighting is broken.

Debug Output

thread 'main' has overflowed its stack

@TED-996 TED-996 added the C-bug Category: Updating dependencies label Jan 24, 2023
@TED-996 TED-996 changed the title Semi-complex arg! macro invocation causes huge slowdown with build, check, or clippy Semi-complex arg! macro invocation causes stack overflow and huge slowdown with build, check, or clippy Jan 24, 2023
@TED-996
Copy link
Author

TED-996 commented Jan 24, 2023

Updated title, the repro code causes a stack overflow without the debug feature as well, at least on Windows.

epage added a commit to epage/clap that referenced this issue Jan 24, 2023
To "append" calls, we were passing in a more and more complex expression
on each recursive invocation.  We now pass in a fixed amount of data on
each iteration, reducing how much the macro machinery needs to parse,
speeding up builds.

Fresh builds went from 39s to 32s on my machine.

Inspired by clap-rs#4670
@epage
Copy link
Member

epage commented Jan 24, 2023

#4673 should hopefully speed up compilation,.

#3524 is our main stack overflow issue for arg!

One thing to note is that --add-b-alias is actually parsed as --add -b -alias by the macro machinery. You need to do --"add-b-alias" to make the macro machinery understand it correctly.

@TED-996
Copy link
Author

TED-996 commented Jan 25, 2023

Thank you! I've missed that part of the documentation, sorry! The quotes do indeed fix the stack overflow and compile time issues.

@epage
Copy link
Member

epage commented Jan 31, 2023

As we have other issues for stack overfows, I'm going to close this out

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

2 participants