Skip to content

Inconsistent rustdoc constant expansion from macro #115295

Closed
@DaniPopes

Description

@DaniPopes

I tried this code:

pub struct WithConst<const N: usize>;

pub trait Trait<T> {}

macro_rules! spans_from_macro {
    () => {
        impl Trait<WithConst<42>> for WithConst<42> {}
        impl WithConst<42> {
            pub fn new() -> Self {
                Self
            }
        }
        pub struct Other {
            pub field: WithConst<42>,
        }
    };
}

// doc: impl Trait<WithConst<41>> for WithConst<41>
impl Trait<WithConst<41>> for WithConst<41> {}

// doc: impl Trait<WithConst<42>> for WithConst<{ _ }>
//      impl WithConst<{ _ }>
//      struct Other { pub field: WithConst<{ _ }> }
spans_from_macro!();

I expected to see this happen: constants are formatted as either all { _ }, or all 42.

Instead, this happened: the one in the trait generic is formatted as the literal 42, while everywhere else it's formatted as { _ }.

I assume it's this:

if classification == Literal
&& !value.span.from_expansion()
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) {

which can be expanded on to also format the literal if it's coming from a macro span by using the pretty printer.

Meta

rustc --version --verbose:

rustc 1.74.0-nightly (5c6a7e71c 2023-08-20)
binary: rustc
commit-hash: 5c6a7e71cd66705c31c9af94077901a220f0870c
commit-date: 2023-08-20
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0
Backtrace

N/A

@rustbot label A-rustdoc-ui T-rustdoc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustdoc-uiArea: Rustdoc UI (generated HTML)C-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions