Skip to content

Typo in #[arg()] leads to opaque error message #129

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

Open
BenWiederhake opened this issue Apr 12, 2025 · 1 comment
Open

Typo in #[arg()] leads to opaque error message #129

BenWiederhake opened this issue Apr 12, 2025 · 1 comment

Comments

@BenWiederhake
Copy link
Contributor

Some (somewhat reasonable?) mistakes lead to completely incomprehensible and unhelpful compiler errors. For example, writing

    #[arg("--time[X]")] // WRONG!
    IgnoreOptionArg(#[allow(dead_code)] Option<OsString>),

instead of:

    #[arg("--time[=X]")] // CORRECT!
    IgnoreOptionArg(#[allow(dead_code)] Option<OsString>),

leads to this unhelpful error message:

error: proc-macro derive panicked
  --> src/uu/du/src/du.rs:79:10
   |
79 | #[derive(Arguments)]
   |          ^^^^^^^^^
   |
   = help: message: called `Option::unwrap()` on a `None` value

How should we approach this? I suggest replacing every .unwrap within derive to a .expect. This way, we at least get some information on what's wrong. This affects the following places:

$ git grep -Pirn 'unwrap\(' derive/
derive/src/argument.rs:35:            return ArgumentsAttr::parse(attr).unwrap();
derive/src/argument.rs:43:    let attributes = get_arg_attributes(&v.attrs).unwrap();
derive/src/flags.rs:64:                    .unwrap();
derive/src/flags.rs:86:            let f = chars.next().unwrap();
derive/src/help.rs:88:        ).unwrap();
derive/src/help.rs:90:        writeln!(w, "{}", #summary).unwrap();
derive/src/help.rs:92:        writeln!(w, "\nUsage:\n  {}", format!(#usage, bin_name)).unwrap();
derive/src/help.rs:96:        writeln!(w, "{}", #after_options).unwrap();
derive/src/help.rs:103:    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
derive/src/help.rs:107:    let mut f = std::fs::File::open(location).unwrap();
derive/src/help.rs:108:    f.read_to_string(&mut contents).unwrap();
derive/src/help_parser.rs:117:            parse_section("some section", input).unwrap(),
derive/src/help_parser.rs:121:            parse_section("SOME SECTION", input).unwrap(),
derive/src/help_parser.rs:125:            parse_section("another section", input).unwrap(),
derive/src/help_parser.rs:146:            parse_section("after section", input).unwrap(),
derive/src/lib.rs:76:            let mut values = parser.raw_args().unwrap().collect::<Vec<OsString>>();
derive/src/lib.rs:155:            let ValueAttr { keys, value } = ValueAttr::parse(&attr).unwrap();

However, I'm not sure whether this would result in actually-useful error messages.

@tertsdiepraam
Copy link
Member

Good idea! I don't have time this weekend so feel free to take this up, otherwise I'll try to do this next weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants