Skip to content

Doc example with feature dependency does not compile #85239

Open
@mordak

Description

@mordak

A doc example which depends on a feature does not compile if the entire example is conditionally excluded.

Having the example conditionally compiled with a feature does not work if the feature is not enabled:

/// ```
/// #![cfg(feature = "foo")]
/// assert_eq!(1, 1);
/// ```
pub struct Foo;

Fails with:

error[E0601]: main function not found in crate rust_out
--> src/lib.rs:8:1
|
1 | / #![allow(unused)]
2 | | #![cfg(feature = "foo")]
3 | | fn main() { #[allow(non_snake_case)] fn doctest_main_src_lib_rs_9_0() {
4 | | assert_eq!(1, 1);
5 | | } doctest_main_src_lib_rs_9_0() }
| |
________________________________^ consider adding a main function at the crate level

Adding a main() as suggested also does not work:

/// ```
/// #![cfg(feature = "foo")]
/// fn main() {
/// assert_eq!(1, 1);
/// }
/// ```
pub struct Foo;

Couldn't compile the test.
---- src/lib.rs - Foo (line 1) stdout ----
error[E0601]: main function not found in crate rust_out
--> src/lib.rs:1:1
|
1 | / #![allow(unused)]
2 | | #![cfg(feature = "foo")]
3 | | fn main() {
4 | | assert_eq!(1, 1);
5 | | }
| |_^ consider adding a main function at the crate level

I expected the example to compile like an empty example, which does work:

/// ```
/// ```
pub struct Baz;

Meta

Affects stable and nightly:

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 12.0.0
rustc 1.54.0-nightly (5c0292654 2021-05-11)
binary: rustc
commit-hash: 5c029265465301fe9cb3960ce2a5da6c99b8dcf2
commit-date: 2021-05-11
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-discussionCategory: Discussion or questions that doesn't represent real issues.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