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

#[doc] does not concat if a macro produces a literal string #99

Open
chanced opened this issue Sep 23, 2023 · 1 comment
Open

#[doc] does not concat if a macro produces a literal string #99

chanced opened this issue Sep 23, 2023 · 1 comment

Comments

@chanced
Copy link

chanced commented Sep 23, 2023

I'm incredibly new to macros so I don't know if this is even possible to address.

If a macro that produces a string literal is used in #[doc], paste does not concat.

macro_rules! create_string {
    () => {
        "example"
    };
}
macro_rules! example {
    () => {
        paste::paste! {
            #[doc = create_string!() "example"]
            fn example(){}
        }
    };
}
error: expected one of `.`, `?`, `]`, or an operator, found `"example"`
  --> sandbox/src/lib.rs:20:38
   |
20 |             #[doc = create_string!() "example"]
   |                                      ^^^^^^^^^ expected one of `.`, `?`, `]`, or an operator
...
26 | example!();
@Dantsz
Copy link

Dantsz commented Apr 2, 2024

I don't think paste fits here, afaik the doc comment expects a &str, and the result of paste is an identifier.
I was wrong and it seems this is related to proc_macros expansion, and there is an unstable feature to expand macros that resolve to literals: rust-lang/rust#90765
You can just use concat! here, which does eager macro expansion:

macro_rules! create_string {
    () => {
        "example"
    };
}
macro_rules! example {
    () => {
            #[doc = concat!(create_string!(), "a")]
            fn example(){}
    }
}

You also would use [< >] if you wanted to create a new identifier, but I'm not sure if paste can expand macros.

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

2 participants