Skip to content

Support macro expansion inside attribute #8092

Closed

Description

@edwin0cheng

e.g :

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

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

Activity

  1. added
    A-macromacro expansion
    S-actionableSomeone could pick this issue up and work on it right now
    on Mar 18, 2021
  2. jonas-schievink commented on Mar 18, 2021

    @jonas-schievink
    Contributor
  3. jonas-schievink commented on Mar 21, 2021

    @jonas-schievink
    Contributor

    I guess this is related to #7049

  4. mvforell commented on Mar 17, 2022

    @mvforell

    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.

  5. sanbox-irl commented on May 3, 2023

    @sanbox-irl

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

  6. added
    C-ArchitectureBig architectural things which we need to figure up-front (or suggestions for rewrites :0) )
    on May 11, 2023
  7. peter-lyons-kehl commented on May 25, 2023

    @peter-lyons-kehl

    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.)

  8. DianaNites commented on Jul 10, 2024

    @DianaNites
    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

  9. so1ve commented on Apr 1, 2026

    @so1ve
    Contributor

    @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-macromacro expansionC-ArchitectureBig architectural things which we need to figure up-front (or suggestions for rewrites :0) )S-actionableSomeone could pick this issue up and work on it right now

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions