Skip to content

Commit

Permalink
Rollup merge of rust-lang#61279 - llogiq:implicit-option-main-doctest…
Browse files Browse the repository at this point in the history
…s, r=GuillaumeGomez

implicit `Option`-returning doctests

This distinguishes `Option` and `Result`-returning doctests with implicit `main` method, where the former tests must end with `Some(())`.

Open question: Does this need a feature gate?

r? @GuillaumeGomez
  • Loading branch information
Centril authored May 30, 2019
2 parents 1b66a13 + 6bb6c00 commit 07d0b57
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/doc/rustdoc/src/documentation-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ conversion, so type inference fails because the type is not unique. Please note
that you must write the `(())` in one sequence without intermediate whitespace
so that rustdoc understands you want an implicit `Result`-returning function.

As of version 1.37.0, this simplification also works with `Option`s, which can
be handy to test e.g. iterators or checked arithmetic, for example:

```ignore
/// ```
/// let _ = &[].iter().next()?;
///# Some(())
/// ```
```

Note that the result must be a `Some(())` and this has to be written in one go.
In this case disambiguating the result isn't required.

## Documenting macros

Here’s an example of documenting a macro:
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,13 @@ pub fn make_test(s: &str,
prog.push_str(everything_else);
} else {
let returns_result = everything_else.trim_end().ends_with("(())");
let returns_option = everything_else.trim_end().ends_with("Some(())");
let (main_pre, main_post) = if returns_result {
("fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {",
(if returns_option {
"fn main() { fn _inner() -> Option<()> {"
} else {
"fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {"
},
"}\n_inner().unwrap() }")
} else {
("fn main() {\n", "\n}")
Expand Down
12 changes: 12 additions & 0 deletions src/test/rustdoc/process-termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@
/// Err("This is returned from `main`, leading to panic")?;
/// Ok::<(), &'static str>(())
/// ```
///
/// This also works with `Option<()>`s now:
///
/// ```rust
/// Some(())
/// ```
///
/// ```rust,should_panic
/// let x: &[u32] = &[];
/// let _ = x.iter().next()?;
/// Some(())
/// ```
pub fn check_process_termination() {}

0 comments on commit 07d0b57

Please sign in to comment.