Repository navigation
Closed
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.Category: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.Relevant to the rustdoc team, which will review and decide on the PR/issue.
Description
I tried this code:
/// You are supposed to call it like this
///
/// # Example
///
/// ```
/// let example = Example::new()
/// .first("hello")
#[cfg_attr(not(feature = "one"), doc = " .second(\"hello\")\n")]
/// .third("hello")
/// .build();
/// ```
pub struct Example {}
fn main() {}with cargo doc --open
I expected to see this:
Instead, this happened:
Meta
rustc --version --verbose:
rustc 1.43.0-nightly (564758c4c 2020-03-08)
binary: rustc
commit-hash: 564758c4c329e89722454dd2fbb35f1ac0b8b47c
commit-date: 2020-03-08
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0
running cargo expand over the code gives me
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
/// You are supposed to call it like this
///
/// # Example
///
/// ```
/// let example = Example::new()
/// .first("hello")
/// .second("hello")
/// .third("hello")
/// .build();
/// ```
pub struct Example {}Metadata
Metadata
Assignees
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.Category: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.Relevant to the rustdoc team, which will review and decide on the PR/issue.


Activity
GuillaumeGomez commentedon Apr 6, 2020
As explained on discord,
the attributes aren't applied to doc comments(EDIT: they are applied, the problem seems to be that it results in two doc comments for rustdoc). However, your doc comment is split in two so the second half isn't considered as being inside a code block, explaining why you have this display. Even ifcargo expandshows it as one doc comment, rustdoc sees two of them.Maybe we should add a warning or something that attributes aren't applied to doc comments... What do you think @rust-lang/rustdoc ?Mark-Simulacrum commentedon Apr 6, 2020
How can an attribute get applied to a doc comment? Both are, under the hood, attributes, no? I would expect that to be equivalent to expecting
#[cfg(foo)] #[derive(Debug)] struct Barto only derive Debug if--cfg foois passed, which is not how that works.I'm guessing what is true is that
doc =attributes directly written don't actually merge with///-style comments. Potentially relevant: #65750, #60935, #60936Nemo157 commentedon Apr 6, 2020
Simplified reproducer
Checking with 1.31.0 it still ignores it (so likely not #65750 related), but checking with 1.21.0 (and 1.0.0) it worked as expected, so somewhere within that range (but I don't have any other versions installed at the moment to check).
Luro02 commentedon Apr 6, 2020
Compilation broke somewhere between
I tested it with
Luro02 commentedon Apr 7, 2020
They are not supposed to be applied to a doc comment
I do not think that. Your example would make sense for
which I also consider to be wrong, but I am talking about
cfg_attrwhich should insert the attribute conditionallybut that is not the point.
It might help to illustrate my thought process;
We have some kind of documented item:
and each of those
///is turned into a doc attributehttps://doc.rust-lang.org/reference/comments.html#doc-comments
In a later step those attributes would then be concatenated and parsed as markdown or whatever.
I want to use this behavior to document items conditionally, depending on which features the crate user has enabled.
One way to do this would be to do
but that would blow up the source code with many duplicate lines, where only some minor things were changed.
So I thought that it might make more sense to conditionally include a doc comment like this:
which would turn into
or
depending on whether the feature is enabled. So the attribute should not be inserted and not applied to the following doc comment.
The weird thing is that the manually expanded version is working as expected.
but this is very unhandy to work with :/
Nemo157 commentedon Apr 7, 2020
I was randomly looking at passes for another reason and noticed what is likely the source of this issue. There are two relevant passes:
collapse-docsruns first and merges together neighboring doc attributes, but it distinguishes whether the doc was aSugaredDoc(///) orRawDoc(#[doc]) and only merges the same kind together.unindent-commentsruns second and removes leading columns of whitespace from the docs. This runs on a single "doc fragment" at a time, because of merging that normally means the entire documentation string, but since sugared and raw docs aren't merged they remain as separate fragments and use their own widths of leading whitespace to strip.Nemo157 commentedon Apr 7, 2020
That also matches up with #44781 first released in 1.24 which introduced this distinction between doc kinds in order to fix #42760, which IMO was user error in not matching the indent depth.
Rollup merge of rust-lang#78400 - GuillaumeGomez:fix-unindent, r=jyn514
Rollup merge of rust-lang#78400 - GuillaumeGomez:fix-unindent, r=jyn514