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

derive(Subcommand) does not respect attributes on fields of enum variants #5007

Closed
2 tasks done
SlayerOfTheBad opened this issue Jul 14, 2023 · 5 comments · Fixed by #5009
Closed
2 tasks done

derive(Subcommand) does not respect attributes on fields of enum variants #5007

SlayerOfTheBad opened this issue Jul 14, 2023 · 5 comments · Fixed by #5009
Labels
C-bug Category: Updating dependencies

Comments

@SlayerOfTheBad
Copy link

Please complete the following tasks

Rust Version

rustc 1.70.0 (90c541806 2023-05-31)

Clap Version

4.3.11

Minimal reproducible code

#[derive(Args)]
pub struct ListCommand {
    dir: PathBuf,
    limit: u64
}

#[derive(ExternalDerive, Subcommand)]
pub enum Commands {
    #[id(b"LS")]
    List(
        #[id(b"")]
        ListArgs
    )
}

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}


fn main() {
    Cli::parse();
}

Where ExternalDerive is some derive macro that takes id as an attribute. In a private project currently, so I can not point to a crate with this derive macro.

Steps to reproduce the bug with the above code

cargo run list ~ 100

Actual Behaviour

Compilation fails due to the Subcommand derive macro.

error: expected type, found `#`
  --> audio_lib/src/actions/mod.rs:46:9
   |
46 |         #[id(b"")]
   |         ^ expected type

error: proc-macro derive produced unparsable tokens
  --> audio_lib/src/actions/mod.rs:42:33
   |
42 | #[derive(Debug, Compose, Parse, Subcommand)]
   |                                 ^^^^^^^^^^

error: could not compile `audio_lib` (lib) due to 2 previous errors

Expected Behaviour

I expect the code to compile, and to run, and then exit without doing anything.

Additional Context

No response

Debug Output

No response

@SlayerOfTheBad SlayerOfTheBad added the C-bug Category: Updating dependencies label Jul 14, 2023
@epage
Copy link
Member

epage commented Jul 14, 2023

Could you include a full reproduction case? Without access to ExternalDerive, I can't reproduce this to determine root cause.

@SlayerOfTheBad
Copy link
Author

Of course, I have made a minimal reproduction repository here.

It's reproduced simply by running cargo run --bin bin

@epage
Copy link
Member

epage commented Jul 14, 2023

Examples of bad generated code:

                       {
                           return :: std :: result :: Result ::
                           Ok(Self ::
                           List(< #[id(b"")] ListArgs as clap :: FromArgMatches > ::
                           from_arg_matches_mut(__clap_arg_matches) ?))
                       }
// ...
               fn augment_subcommands < 'b > (__clap_app : clap :: Command) -> clap ::
               Command
               {
                   ; let __clap_app = __clap_app ; let __clap_app =
                   __clap_app.subcommand({
                       let __clap_subcommand = clap :: Command :: new("list") ; let
                       __clap_subcommand = __clap_subcommand ; let __clap_subcommand =
                       {
                           < #[id(b"")] ListArgs as clap :: Args > ::
                           augment_args(__clap_subcommand)
                       } ; __clap_subcommand
                   }) ; ; __clap_app
               } fn augment_subcommands_for_update < 'b > (__clap_app : clap :: Command)
               -> clap :: Command
               {
                   ; let __clap_app = __clap_app ; let __clap_app =
                   __clap_app.subcommand({
                       let __clap_subcommand = clap :: Command :: new("list") ; let
                       __clap_subcommand = __clap_subcommand ; let __clap_subcommand =
                       {
                           < #[id(b"")] ListArgs as clap :: Args > ::
                           augment_args_for_update(__clap_subcommand)
                       } ; __clap_subcommand
                   }) ; ; __clap_app
// ...

epage added a commit to epage/clap that referenced this issue Jul 14, 2023
Well, thats annoying how easy it is to get this wrong but tests can only
help with regressions; it would be hard to prevent future failures.

Fixes clap-rs#5007
@epage
Copy link
Member

epage commented Jul 14, 2023

Thanks! That was a big help to see the exact reproduction case you were dealing with

@SlayerOfTheBad
Copy link
Author

Of course!
Glad I could help in resolving this issue. I'm surprised it'd never come up before honestly.

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

Successfully merging a pull request may close this issue.

2 participants