Skip to content

macros: it is possible to downcast a tt to most anything using nested macros #16036

Closed
@apoelstra

Description

@apoelstra

The following code compiles:

#![feature(macro_rules)]

macro_rules! print_doccomment(
  (
    $doc:tt
    fn $name:ident()
  ) => (
    macro_rules! dummy( () => (
      $doc
      fn $name() {
        println!("The doc for this function is `{}`", stringify!($doc));
      }
    ))
    dummy!()
  )
)

print_doccomment!(
  /// This is a doc comment
  fn function_to_doc()
)

fn main() {
  function_to_doc();
}

And prints "/// This is a doc comment". However, the token /// This is a doc comment is not an item, which is the only thing that should parse for the first $doc when dummy! is expanded (according to @chris-morgan on IRC).

Note that I am using (a variation on) this in some code to transmit the doc comments on RPC-handling functions in response to the RPC "help". It would be great if there were a sanctioned way to parse or construct doccomments in macros.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions