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

Comments for macro expansions #37277

Closed
Robbepop opened this issue Oct 19, 2016 · 3 comments
Closed

Comments for macro expansions #37277

Robbepop opened this issue Oct 19, 2016 · 3 comments

Comments

@Robbepop
Copy link
Contributor

macro_rules! foo_macro {
    ($name:ident) => {
        pub struct $name;
    }
}

/// Doc comment for Foo struct.
foo_macro!(Foo);

/// Doc comment for Bar struct.
foo_macro!(Bar);

In this trivial code sample the user wants to create a struct via macro expansion and write a unique comment for every different macro invokation.
This means a unique comment for struct Foo and Bar which is currently not possible in the rust compiler when enabling:

#![warn(missing_docs)]

With the following warnings:

warning: missing documentation for a struct
  --> src/activation_fn.rs:9:3
   |
9  |        pub struct $name;
   |        ^^^^^^^^^^^^^^^^^
...
14 | foo_macro!(Foo);
   | ---------------- in this macro invocation
   |
note: lint level defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(missing_docs)]
   |         ^^^^^^^^^^^^

warning: missing documentation for a struct
  --> src/activation_fn.rs:9:3
   |
9  |        pub struct $name;
   |        ^^^^^^^^^^^^^^^^^
...
17 | foo_macro!(Bar);
   | ---------------- in this macro invocation
   |
note: lint level defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(missing_docs)]
   |         ^^^^^^^^^^^^

While this obviously does work:

macro_rules! foo_macro {
    /// Unified comment for any macro expansion which is bad!
    ($name:ident) => {
        pub struct $name;
    }
}

foo_macro!(Foo);
foo_macro!(Bar);
@bluss
Copy link
Member

bluss commented Oct 19, 2016

You could just forward all attributes (doc comments are #[doc] attributes), but since the repetition of attributes can't be followed directly by an ident, it requires changing the syntax a bit.

Example (playground)

macro_rules! foo_macro {
    ($(#[$attr:meta])* struct $name:ident) => {
        $(#[$attr])*
        pub struct $name;
    }
}

foo_macro!(
    /// Doc comment for Foo struct.
    struct Foo
);

foo_macro!(
    /// Doc comment for Bar struct.
    struct Bar
);

fn main() {
}

The user forum is a good place to discuss questions like this. https://users.rust-lang.org/

@bluss
Copy link
Member

bluss commented Oct 19, 2016

Concrete bug reports are very welcome here, but I'll close this as it has been answered and futher discussion is best on the user forum.

@bluss bluss closed this as completed Oct 19, 2016
@lovasoa
Copy link
Contributor

lovasoa commented Jan 31, 2019

The issue here does not seem to be solved. How to write doc comments for macros that start with an ident ?

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

3 participants