nrc / derive-new

derive simple constructor functions for Rust structs
MIT License
525 stars 35 forks source link

Doesn't work with #[deny(missing_docs)] #7

Closed Arnavion closed 7 years ago

Arnavion commented 7 years ago

It could add #[allow(missing_docs)] on the generated impl.

Or it could unilaterally add a generic doc comment /// Constructs a #struct_name (not sure if quote supports this).

nrc commented 7 years ago

I think we should generate the doc string, should be no problem with quote (I hope).

(The real solution here is that Rust should be hygienic wrt to lints and macros, but that's something of a long-term fix...)

dtolnay commented 7 years ago

/// is tricky but here is an example of generating doc comments with quote! in svd2rust:

let comment = format!("0x{:02x} - {}",
                      register.address_offset,
                      respace(&register.description));
quote! {
    #[doc = #comment]
    pub #field_name : #field_ty
}
nrc commented 7 years ago

@dtolnay why is /// tricky?

dtolnay commented 7 years ago

@nrc because macro_rules macros like quote! are not allowed to look inside them at compile time. They just receive a string containing the text of the comment, which is useless because at runtime we can no longer look in the string and interpolate the #variables it refers to.

fn main() {
    println!("{}", stringify! {
        /// Constructs a #struct_name
        fn new() {}
    });
}
# [ doc = r" Constructs a #struct_name" ] fn new (  ) {  }

If I ever rewrite quote! as a procedural macro then it can support interpolating doc comments.