Skip to content

rustdoc leaks ident macro meta-variables in non-literal const exprs in assoc tys & impl trait refs (excl. self ty) #127429

Open
@maia-s

Description

@maia-s

Minimal reproduction:

pub struct S<const A: bool>;

pub const TRUE: bool = true;

macro_rules! example {
    ($unexpanded:ident) => {
        impl core::ops::Add<S<$unexpanded>> for S<$unexpanded> {
            type Output = S<$unexpanded>;

            fn add(self, _: S<$unexpanded>) -> Self::Output {
                S::<$unexpanded>
            }
        }
    };
}

example!(TRUE);

When I generate documentation for this with cargo doc, the documentation for S's trait implementations says

impl Add<S<$unexpanded>> for S<TRUE>
type Output = S<$unexpanded>

I expected the documentation to document the expanded code. It happens with both 1.79 and nightly.

The problem persists if I change the macro to put {} around $unexpanded, but then if I pass true instead of TRUE to the macro, it's expanded and shown as impl Add for S<{ true }> and type Output = S<true> (without the {})

% rustc --version --verbose
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: aarch64-apple-darwin
release: 1.79.0
LLVM version: 18.1.7

% rustc +nightly --version --verbose
rustc 1.81.0-nightly (8337ba918 2024-06-12)
binary: rustc
commit-hash: 8337ba9189de188e2ed417018af2bf17a57d51ac
commit-date: 2024-06-12
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)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