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

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

Open
mamekoro opened this issue Nov 17, 2023 · 1 comment · May be fixed by #118711
Open

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

mamekoro opened this issue Nov 17, 2023 · 1 comment · May be fixed by #118711
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@mamekoro
Copy link

mamekoro commented Nov 17, 2023

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
@mamekoro mamekoro added the C-bug Category: This is a bug. label Nov 17, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 17, 2023
@saethlin saethlin added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 18, 2023
@GuillaumeGomez
Copy link
Member

It's because you can hide lines of code if you prepend them with #. There is some "intelligence" by using the parser to know when the # is actually used in the code or not. In the current example, the parser doesn't know and rustdoc simply removes it. I don't really see a way to fix it. An idea could be to introduce a new codeblock tag which would allow to say "keep the code as is".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

4 participants