Skip to content

rustdoc replaces ## with # even if it is in a code block #118027

@mamekoro

Description

@mamekoro

I tried this code:

/// ```
/// define_component!(
///     #[derive(Debug)]
///     ##[min(0.0))]
///     ##[max(100.0))]
///     ##[default(50.0)]
///     pub Fuel(pub f32);
/// );
/// ```
macro_rules! define_component {
    (
        $(#[$attr:meta])*
        ##[min($min:expr)]
        ##[max($max:expr)]
        ##[default($default:expr)]
        $self_vis:vis $self:ident($inner_vis:vis $inner:ty);
    ) => {};
}

The define_component macro uses a single hash sign # to represent a normal attribute (e.g. #[derive(Debug)]), and uses a double hash sign ## to represent an argument specific to that macro (e.g. ##[min(0.0)]).

I ran cargo doc --document-private-items --open and opened the generated define_component page.

I expected to see this happen:

define_component!(
    #[derive(Debug)]
    ##[min(0.0))]
    ##[max(100.0))]
    ##[default(50.0)]
    pub Fuel(pub f32);
);

Instead, this happened:

define_component!(
    #[derive(Debug)]
    #[min(0.0))]
    #[max(100.0))]
    #[default(50.0)]
    pub Fuel(pub f32);
);

## were replaced by #. (e.g. ##[min(0.0)] became #[min(0.0)])

I think rustdoc should keep code blocks as written (except for coloring, etc.). This is because code blocks are generally expected to be enclosed in the HTML <pre> tag, which defines preformatted text.

In other words, code blocks are preformatted and should be kept as written.

Perhaps the following code is related:

fn map_line(s: &str) -> Line<'_> {
let trimmed = s.trim();
if trimmed.starts_with("##") {
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))

Workaround

If the doc comment starts with ```text instead of ```, this problem does not occur.

Meta

rustdoc --version --verbose:

rustdoc 1.74.0 (79e9716c9 2023-11-13)
binary: rustdoc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    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