Strange error message shows comment not code

I was messing around with structopt. Thinking about if a library had command line options, how would you do that? Then the binary would have it's options and it would magically include the binaries options. So I thought.
I played a bit and made a library and a binary and got a strange error message....it includes the comment above the code in the error, not the code itself. I dont need to do this; include options in a library, but as normal I was just doing silly things. Anyway I extracted it all into one minimal file and wonder if the gurus here could maybe explain why I get the comment in my error, instead of the code.

use structopt::StructOpt;

/// This is a comment over the binaries struct
#[derive(StructOpt)]
struct ProgyCli {
    /// A string is required
    text: String,
    /// Why do I see this comment in the error not the following line?
    lib_cli: liby::LibCli,
}

fn main() {
    let args = ProgyCli::from_args();
    println!("{}", args.text);
}

mod liby {
    use structopt::StructOpt;

    /// This is a comment over the libraries struct
    #[derive(StructOpt)]
    pub struct LibCli {
        /// A string is required here also
        pub text: String,
    }
}

and the error is ...

   Compiling progy v0.1.0 (/home/js/learn/rust/erbug)
error[E0277]: the trait bound `LibCli: FromStr` is not satisfied
   --> src/main.rs:8:5
    |
8   |     /// Why do I see this comment in the error not the following line?
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromStr` is not implemented for `LibCli`
    |
note: required by `from_str`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `progy` due to previous error

When you write a macro, you have to associate each part of the generated code with a part of the input, which is what the compiler uses to display errors in the generated code without showing the actual generated code. The library has messed up those spans in this case, so the error points at the wrong thing.

1 Like

Hmmm....
As soon as I posted and walked away I thought maybe it isn't a mistake? I started to think that the macro or part of structopt/clap that turns that comment into a string. Is missing for the library structs comment and pointing at that location is correct.
Thanks for your answer. I think that is the same thing you said

Interestingly, it seems to be trying to highlight the entire field including the doc comment; the output differs between stable and nightly

$ cargo +stable check
    Checking crate_name v0.1.0 (path/to/crate)
error[E0277]: the trait bound `LibCli: FromStr` is not satisfied
   --> src/main.rs:9:5
    |
9   |     /// Why do I see this comment in the error not the following line?
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromStr` is not implemented for `LibCli`
    |
note: required by `from_str`
   --> path/to/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/traits.rs:554:5
    |
554 |     fn from_str(s: &str) -> Result<Self, Self::Err>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `crate_name` due to previous error
$ cargo +nightly check
    Checking crate_name v0.1.0 (path/to/crate)
error[E0277]: the trait bound `LibCli: FromStr` is not satisfied
   --> src/main.rs:9:5
    |
9   | /     /// Why do I see this comment in the error not the following line?
10  | |     lib_cli: liby::LibCli,
    | |_________________________^ the trait `FromStr` is not implemented for `LibCli`
    |
note: required by `from_str`
   --> path/to/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/traits.rs:554:5
    |
554 |     fn from_str(s: &str) -> Result<Self, Self::Err>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `crate_name` due to previous error
1 Like

Is there a setting so I get more verbose output, like yours showing the path to the toolchain file ...traits.rs that I am now reading in my terminal?

I think the extra info has to do with the fact that I have the rust-scr component installed.

$ rustup +stable component list --installed
cargo-x86_64-unknown-linux-gnu
clippy-x86_64-unknown-linux-gnu
rls-x86_64-unknown-linux-gnu
rust-analysis-x86_64-unknown-linux-gnu
rust-docs-x86_64-unknown-linux-gnu
rust-src
rust-std-x86_64-unknown-linux-gnu
rustc-x86_64-unknown-linux-gnu
rustfmt-x86_64-unknown-linux-gnu

You can install it yourself with

rustup +stable component add rust-src

and if you use nightly occasionally, too, you can also

rustup +nightly component add rust-src

Cool, thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.