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

Support macro expansion inside attribute #8092

Open
edwin0cheng opened this issue Mar 18, 2021 · 6 comments
Open

Support macro expansion inside attribute #8092

edwin0cheng opened this issue Mar 18, 2021 · 6 comments
Labels
A-macro macro expansion C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) S-actionable Someone could pick this issue up and work on it right now

Comments

@edwin0cheng
Copy link
Member

e.g :

#[doc = concat!("Hello", " world")]
struct Bar;

It is unstable feature and it is used in rustc master already.

@edwin0cheng edwin0cheng added A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now labels Mar 18, 2021
@jonas-schievink
Copy link
Contributor

cc #7779

@jonas-schievink
Copy link
Contributor

I guess this is related to #7049

@mvforell
Copy link

This would be very useful for generating doc attributs in macros, e.g. like described at https://stackoverflow.com/a/43353854:

macro_rules! impl_foo {
    ($name:ident, $sname:expr) => {
        #[doc = "Returns a new `"]
        #[doc = $sname]
        #[doc = "`."]
        pub fn myfoo() -> $name {
            42
        }
    };

    ($name:tt) => {
        impl_foo!($name, stringify!($name));
    };
}

impl_foo!(u32);

Currently, the above doc gets rendered as

Returns a new ``.

instead of

Returns a new u32.

@sanbox-irl
Copy link

To add to this discussion -- since the std uses this macro, many examples in the std do not read correctly right now:

@Veykril Veykril added the C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) label May 11, 2023
@peter-lyons-kehl
Copy link

peter-lyons-kehl commented May 25, 2023

A workaround: Have a proc_macro that generates the string, injects it in #[doc = "..." ] (escaping any backslashes \ into \\ and any quotes " into \\\") and generates your desired item (along with that #[doc = "..." ]).

Tedious, and it requires an extra proc_macro crate (unless you're generating proc macros already). But, chances are that you're already generating your desired code either with macro_rules! or proc macros, so it's not much more science. (The proc macro itself would be simple and is feasible without syn and quote, potentially generating proc_macro::TokenStream with my_generated_code_string.parse().unwrap(), or with proc_macro API, hence lightweight.)

@DianaNites
Copy link
Contributor

Since this is still an issue and I havent seen it mentioned anywhere, this depended on the now stable extended_key_value_attributes, which had a tracking issue on this side which was closed in favor of this issue.

extended_key_value_attributes was stabilized a few months after these issues were opened back in 2021, but when it got stabilized little further seems to have been done beyond newer issues being closed as duplicates of this.

This has been a stable feature for years, std and many other crates rely on it for their documentation, and more continue to as time goes on, and rust-analyzer doesn't render docs using it correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

7 participants