Skip to content

macro expansion for the doc attribute must occur during the rustdoc pass #144295

@fidelicura

Description

@fidelicura

Code

macro_rules! doc_literal {
    () => {
        "Welcome!"
    };
}

struct Foo {
    #[doc = doc_literal!()]
    bar: usize,
}

Reproduction Steps

cargo build and that's it.

Expected Outcome

This documentation comment is expanded only after the rustdoc has finished it's work, as far as I can see. So, neither LSP can't see this comment, nor clap. This would be really useful when you have multiple places that should have the similar documentation, but stored in a centralized manner.

Actual Output

$ cargo expand
    Checking doc_comment_literal_macro v0.1.0 (/home/me/Desktop/doc_comment_literal_macro)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

#![feature(prelude_import)]
#![allow(unused)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
extern crate std;
struct Foo {
    ///Welcome!
    bar: usize,
}
fn main() {}

Version

$ rustdoc --version
rustdoc 1.87.0 (17067e9ac 2025-05-09)

$ cargo -V
cargo 1.87.0 (99624be96 2025-05-06)

$ rustc -V
rustc 1.87.0 (17067e9ac 2025-05-09)

Additional Details

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions