rust-lang/rust

Conditionally derived traits using `cfg_attr` are not documented as being behind a feature flag.

cobward opened this issue · 3 comments

When conditionally deriving a trait using cfg_attr, the doc_cfg feature gate is not documented. A minimal example can be found here, but is summarised below:

Example:

 #![feature(doc_auto_cfg)]
 #[cfg_attr(feature = "debug", derive(Debug))]
 pub struct Test();

Generates:

generated

Expectation:

expected

It's because the cfg information isn't kept. When I use macro expansion, here's what we get:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
pub struct Test();
#[automatically_derived]
impl ::core::fmt::Debug for Test {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "Test")
    }
}

It's much more complicated than what I originally imagined: when the function to generate the implementation is called, the provided item doesn't have the attribute around the derive at all. I'm investigating to see if there is a way to do it without breaking everything but the deeper I go, the less hope for that I have...

There is no way to work around this manually with #[doc(cfg(...))] in affected code currently, is there?

Thank you so much for your work!