Rollup merge of #62213 - QuietMisdreavus:cfg-doctest, r=GuillaumeGomez

rustdoc: set cfg(doctest) when collecting doctests

Note: This PR builds on top of https://github.com/rust-lang/rust/pull/61199; only the last commit is specific to this PR.

As discussed in https://github.com/rust-lang/rust/pull/61199, we want the ability to isolate items to only when rustdoc is collecting doctests, but we can't use `cfg(test)` because of libcore's `#![cfg(not(test))]`. This PR proposes a new cfg flag, `cfg(doctest)`, specific to this situation, rather than reusing an existing flag. I've isolated it behind a feature gate so that we can contain the effects to nightly only. (A stable workaround that can be used in lieu of `#[cfg(doctest)]` is `#[cfg(rustdoc)] #[doc(hidden)]`, at least once https://github.com/rust-lang/rust/pull/61351 lands.)

Tracking issue: https://github.com/rust-lang/rust/issues/62210
This commit is contained in:
Mazdak Farrokhzad 2019-07-07 17:00:18 +02:00 committed by GitHub
commit fe807fcf3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 78 additions and 4 deletions

View file

@ -1,10 +1,12 @@
// build-pass (FIXME(62277): could be check-pass?)
// compile-flags:--test
// compile-flags:--test --test-args --test-threads=1
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// Crates like core have doctests gated on `cfg(not(test))` so we need to make
// sure `cfg(test)` is not active when running `rustdoc --test`.
#![feature(cfg_doctest)]
/// this doctest will be ignored:
///
/// ```
@ -20,3 +22,11 @@ pub struct Foo;
/// ```
#[cfg(not(test))]
pub struct Foo;
/// this doctest will be tested, but will not appear in documentation:
///
/// ```
/// assert!(true)
/// ```
#[cfg(doctest)]
pub struct Bar;

View file

@ -1,6 +1,7 @@
running 1 test
test $DIR/cfg-test.rs - Foo (line 18) ... ok
running 2 tests
test $DIR/cfg-test.rs - Bar (line 28) ... ok
test $DIR/cfg-test.rs - Foo (line 20) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

View file

@ -0,0 +1,8 @@
#![feature(cfg_doctest)]
// @!has cfg_doctest/struct.SomeStruct.html
// @!has cfg_doctest/index.html '//a/@href' 'struct.SomeStruct.html'
/// Sneaky, this isn't actually part of docs.
#[cfg(doctest)]
pub struct SomeStruct;

View file

@ -0,0 +1,4 @@
#[cfg(doctest)] //~ ERROR
pub struct SomeStruct;
fn main() {}

View file

@ -0,0 +1,12 @@
error[E0658]: `cfg(doctest)` is experimental and subject to change
--> $DIR/feature-gate-cfg_doctest.rs:1:7
|
LL | #[cfg(doctest)]
| ^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/62210
= help: add #![feature(cfg_doctest)] to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.