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

Inconsistent rustdoc constant expansion from macro #115295

Closed
DaniPopes opened this issue Aug 27, 2023 · 0 comments · Fixed by #115485
Closed

Inconsistent rustdoc constant expansion from macro #115295

DaniPopes opened this issue Aug 27, 2023 · 0 comments · Fixed by #115485
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@DaniPopes
Copy link
Contributor

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

@DaniPopes DaniPopes added the C-bug Category: This is a bug. label Aug 27, 2023
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-rustdoc-ui Area: Rustdoc UI (generated HTML) T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 27, 2023
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2023
@bors bors closed this as completed in cd5b5e0 Nov 7, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 7, 2023
Rollup merge of rust-lang#115485 - DaniPopes:rustdoc-macro-consts, r=jackh726,fmease

Format macro const literals with pretty printer

Fixes rust-lang#115295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants