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

Strange compilation error "error: expected ," in arg derive #5119

Open
2 tasks done
apbr opened this issue Sep 11, 2023 · 4 comments
Open
2 tasks done

Strange compilation error "error: expected ," in arg derive #5119

apbr opened this issue Sep 11, 2023 · 4 comments
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies

Comments

@apbr
Copy link

apbr commented Sep 11, 2023

Please complete the following tasks

Rust Version

rustc 1.71.0 (8ede3aae2 2023-07-12)

Clap Version

4.4.2

Minimal reproducible code

use clap::Parser;

#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Args {
    #[arg(long, short, default_value_t = "foo".to_string())]
    pub some_value: String,
}

fn main() {
    let args = Args::parse();
    println!("Hello, world!");
}

Steps to reproduce the bug with the above code

cargo run

Actual Behaviour

I do get this error:

error: expected `,`
 --> src/main.rs:6:47
  |
6 |     #[arg(long, short, default_value_t = "foo".to_string())]
  |                                               ^

This message is not really helpful.

Expected Behaviour

I don't see why this should not just compile. At least there should be a more conclusive error message.

Additional Context

I know I can work around this by using #[arg(long, short, default_value_t = String::from("foo"))] or #[arg(long, short, default_value_t = ("foo".to_string()))].
Though the latter option gives the following warning when compiling:

warning: unnecessary parentheses around assigned value
 --> src/main.rs:6:42
  |
6 |     #[arg(long, short, default_value_t = ("foo".to_string()))]
  |                                          ^                 ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
6 -     #[arg(long, short, default_value_t = ("foo".to_string()))]
6 +     #[arg(long, short, default_value_t = "foo".to_string())]
  |

Debug Output

No response

@apbr apbr added the C-bug Category: Updating dependencies label Sep 11, 2023
@epage epage added the A-derive Area: #[derive]` macro API label Sep 11, 2023
@epage
Copy link
Member

epage commented Sep 11, 2023

Not quite sure what is going on. If I comment out the code that generates code for default_value_t, it still happens.

@Sinfaen
Copy link

Sinfaen commented Mar 23, 2024

also seeing this

@epage
Copy link
Member

epage commented Mar 25, 2024

For anyone blocked on this, there are two workarounds for this, depending on your situation

  • Use default_value
  • Move the call out to a function

See

#!/usr/bin/env nargo
---
[dependencies]
clap = { version = "4", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Args {
    //#[arg(long, short, default_value = "foo")]
    #[arg(long, short, default_value_t = "foo".to_string())]
    //#[arg(long, short, default_value_t = default())]
    pub some_value: String,
}

fn default() -> String {
    "foo".to_string()
}

fn main() {
    let args = Args::parse();
    println!("Hello, world!");
}

@lolbinarycat
Copy link

there's another workaround i've found: surround it in parenthesies: default_value_t = ("foo".to_string()).

unfortunately rustc deems these "unnecessary" and will output a warning about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

4 participants