Including doc comments in named! macro (Not complete) #317
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I am making a bunch of parsers with the
named!
macro and I want to be able to include doc comments for them so I can make doc tests. I tried putting them directly before the macro but they don't seem to be picked up by the compiler. So I decided to try to modify thenamed!
macro to support them myself. I knew nothing about macros when I started this little experiment, so please be gentle. Hopefully there is a simple solution that I missed.After a bit of research I found out that doc comments are actually converted to attributes during compilation, so
/// This is a doc comment
becomes#[doc = "This is a doc comment"]
. Since we can match meta attributes in macros, it should be possible to grab them and stick them before the output function. It seems to work with a simple example(runningcargo rustc --lib -- -Z unstable-options --pretty=expanded
shows the#[doc]
attributes directly above the print function). Unfortunately when I tried to modify thenamed!
macro(with the changes in the commit in this pull request) to do the same thing, I ran info some ambiguity issues that are a bit above my skill level.I found an issue about this exact problem, and it does have a work-around, but it seems like a hack, and I don't really understand it enough to try it. Plus I think it would require some pretty big changes to the implementation of the
named!
macro, which might not be such a good idea.I am wondering what your thoughts on this are @Geal? Is it even good idea at all. Is the solution worth the changes?