Skip to content

rustdoc: support #![cfg(feature)] that disables doc tests #133886

Open
@kornelski

Description

@kornelski

Code in doc comments may require specific Cargo features or platforms, but currently the syntax for disabling doctests is non-obvious, verbose, and by mixing languages and syntaxes, it doesn't play well with Markdown syntax highlighting:

#![cfg_attr(feature = "alloc", doc = " ```")]
#![cfg_attr(not(feature = "alloc"), doc = " ```ignore")]
//! code
//! ```

I suggest supporting #![cfg(…)] inside doctests, injected into the test code in a way that disables a block of code containing the test. Currently #![cfg(feature = …)] doesn't work at all in doctests, because it gets hoisted to be a real crate attribute, and ends up disabling entire the test module.

//! ```rust
//! #![cfg(feature = "alloc")] // proposed syntax
//! code
//! ```

In the implementation I think it would require wrapping "everything_else" code in extra { }, and keeping #![cfg(feature = …)] attrs in the code, instead of extracting them and hoisting them to the top level of the doctest crate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cfgArea: `cfg` conditional compilationA-doctestsArea: Documentation tests, run by rustdocC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions