Skip to content

Commit a212960

Browse files
stabilize cfg(doctest)
1 parent eb5ef81 commit a212960

File tree

10 files changed

+35
-56
lines changed

10 files changed

+35
-56
lines changed

src/doc/rustdoc/src/documentation-tests.md

+31
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,34 @@ However, it's preferable to use fenced code blocks over indented code blocks.
379379
Not only are fenced code blocks considered more idiomatic for Rust code,
380380
but there is no way to use directives such as `ignore` or `should_panic` with
381381
indented code blocks.
382+
383+
### Include items only when collecting doctests
384+
385+
Rustdoc's [documentation tests] can do some things that regular unit tests can't, so it can
386+
sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in
387+
documentation. To this end, Rustdoc allows you to have certain items only appear when it's
388+
collecting doctests, so you can utilize doctest functionality without forcing the test to appear in
389+
docs, or to find an arbitrary private item to include it on.
390+
391+
When compiling a crate for use in doctests (with `--test` option), rustdoc will set `cfg(doctest)`.
392+
Note that they will still link against only the public items of your crate; if you need to
393+
test private items, either make items conditionally public via `cfg(doctest)` or write a unit test.
394+
395+
In this example, we're adding doctests that we know won't compile, to verify that our struct can
396+
only take in valid data:
397+
398+
```rust
399+
/// We have a struct here. Remember it doesn't accept negative numbers!
400+
pub struct MyStruct(usize);
401+
402+
/// ```compile_fail
403+
/// let x = my_crate::MyStruct(-5);
404+
/// ```
405+
#[cfg(doctest)]
406+
pub struct MyStructOnlyTakesUsize;
407+
```
408+
409+
Please note that the struct `MyStructOnlyTakesUsize` will not appear in documentation or in the
410+
public API considering it only exists when running rustdoc with the `--test` option.
411+
412+
[documentation tests]: documentation-tests.html

src/doc/rustdoc/src/unstable-features.md

-30
Original file line numberDiff line numberDiff line change
@@ -211,36 +211,6 @@ pub struct BigX;
211211
Then, when looking for it through the `rustdoc` search, if you enter "x" or
212212
"big", search will show the `BigX` struct first.
213213

214-
### Include items only when collecting doctests
215-
216-
Rustdoc's [documentation tests] can do some things that regular unit tests can't, so it can
217-
sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in
218-
documentation. To this end, Rustdoc allows you to have certain items only appear when it's
219-
collecting doctests, so you can utilize doctest functionality without forcing the test to appear in
220-
docs, or to find an arbitrary private item to include it on.
221-
222-
If you add `#![feature(cfg_doctest)]` to your crate, Rustdoc will set `cfg(doctest)` when collecting
223-
doctests. Note that they will still link against only the public items of your crate; if you need to
224-
test private items, unit tests are still the way to go.
225-
226-
In this example, we're adding doctests that we know won't compile, to verify that our struct can
227-
only take in valid data:
228-
229-
```rust
230-
#![feature(cfg_doctest)]
231-
232-
/// We have a struct here. Remember it doesn't accept negative numbers!
233-
pub struct MyStruct(usize);
234-
235-
/// ```compile_fail
236-
/// let x = my_crate::MyStruct(-5);
237-
/// ```
238-
#[cfg(doctest)]
239-
pub struct MyStructOnlyTakesUsize;
240-
```
241-
242-
[documentation tests]: documentation-tests.html
243-
244214
## Unstable command-line arguments
245215

246216
These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are

src/libsyntax/feature_gate/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ declare_features! (
251251
(accepted, non_exhaustive, "1.40.0", Some(44109), None),
252252
/// Allows calling constructor functions in `const fn`.
253253
(accepted, const_constructor, "1.40.0", Some(61456), None),
254+
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.
255+
(accepted, cfg_doctest, "1.40.0", Some(62210), None),
254256

255257
// -------------------------------------------------------------------------
256258
// feature-group-end: accepted features

src/libsyntax/feature_gate/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,6 @@ declare_features! (
506506
/// Allows `async || body` closures.
507507
(active, async_closure, "1.37.0", Some(62290), None),
508508

509-
/// Allows the use of `#[cfg(doctest)]`; set when rustdoc is collecting doctests.
510-
(active, cfg_doctest, "1.37.0", Some(62210), None),
511-
512509
/// Allows `[x; N]` where `x` is a constant (RFC 2203).
513510
(active, const_in_array_repeat_expressions, "1.37.0", Some(49147), None),
514511

src/libsyntax/feature_gate/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[
3131
(sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3232
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3333
(sym::rustdoc, sym::doc_cfg, cfg_fn!(doc_cfg)),
34-
(sym::doctest, sym::cfg_doctest, cfg_fn!(cfg_doctest)),
3534
];
3635

3736
#[derive(Debug)]

src/test/rustdoc-ui/cfg-test.rs

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Crates like core have doctests gated on `cfg(not(test))` so we need to make
66
// sure `cfg(test)` is not active when running `rustdoc --test`.
77

8-
#![feature(cfg_doctest)]
9-
108
/// this doctest will be ignored:
119
///
1210
/// ```

src/test/rustdoc-ui/cfg-test.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
running 2 tests
3-
test $DIR/cfg-test.rs - Bar (line 28) ... ok
4-
test $DIR/cfg-test.rs - Foo (line 20) ... ok
3+
test $DIR/cfg-test.rs - Bar (line 26) ... ok
4+
test $DIR/cfg-test.rs - Foo (line 18) ... ok
55

66
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
77

src/test/rustdoc/cfg-doctest.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(cfg_doctest)]
2-
31
// @!has cfg_doctest/struct.SomeStruct.html
42
// @!has cfg_doctest/index.html '//a/@href' 'struct.SomeStruct.html'
53

src/test/ui/feature-gate/feature-gate-cfg_doctest.rs

-4
This file was deleted.

src/test/ui/feature-gate/feature-gate-cfg_doctest.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)