Skip to content

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

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

Closed
apoelstra opened this issue Jul 28, 2014 · 5 comments
Closed
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@apoelstra
Copy link
Contributor

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.

@apoelstra apoelstra changed the title macros: it is possible to pass most anything to macro_rules! as a tt macros: it is possible to downcast a tt to most anything using nested macros Jul 28, 2014
@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jan 27, 2015
@huonw
Copy link
Member

huonw commented Oct 27, 2015

I'm not sure I understand

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

Could you clarify? (The doc comment is an attribute that ends up attached to the fn.)

@bltavares
Copy link
Contributor

Triaging: I was not able to compile on nightly. The code needed some ; added, but now it fails with an error on parsing.

<anon>:19:3: 19:28 error: no rules expected the token `[`
<anon>:19   /// This is a doc comment
            ^~~~~~~~~~~~~~~~~~~~~~~~~
#![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();
}

@Mark-Simulacrum
Copy link
Member

The original and updated code no longer compile, and I suspect it should be closed, since the original issue is, and I cannot reproduce.

@jseyfried Can you confirm?

@Mark-Simulacrum
Copy link
Member

Closing. Please reopen if that's not the correct decision here.

@jseyfried
Copy link
Contributor

Yeah, this should be closed -- we can now downcast tts anywhere.

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 11, 2023
…mode, r=lnicola

make ParamLoweringMode accessible

In `hir-ty`, the `TyLoweringContext` has functions `pub fn with_impl_trait_mode` and `pub fn with_type_param_mode`, which can be used to fine-tune certain lowering properties.
Each of these takes one enum (either `pub enum ImplTraitLoweringMode`, or `pub enum ParamLoweringMode`), which encodes the possible configuration options.

To then make this usable for other crates, `TyLoweringContext` and `ImplTraitLoweringMode` are exported. Unfortuntely, `ParamLoweringMode` is not. This means that while the method can be called, there are no useful values to call it with.

Presumably this is an oversight. It would be great if this was made actually public.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
Projects
None yet
Development

No branches or pull requests

6 participants