Skip to content

Commit 07d0b57

Browse files
authored
Rollup merge of #61279 - llogiq:implicit-option-main-doctests, 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
2 parents 1b66a13 + 6bb6c00 commit 07d0b57

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ conversion, so type inference fails because the type is not unique. Please note
253253
that you must write the `(())` in one sequence without intermediate whitespace
254254
so that rustdoc understands you want an implicit `Result`-returning function.
255255

256+
As of version 1.37.0, this simplification also works with `Option`s, which can
257+
be handy to test e.g. iterators or checked arithmetic, for example:
258+
259+
```ignore
260+
/// ```
261+
/// let _ = &[].iter().next()?;
262+
///# Some(())
263+
/// ```
264+
```
265+
266+
Note that the result must be a `Some(())` and this has to be written in one go.
267+
In this case disambiguating the result isn't required.
268+
256269
## Documenting macros
257270

258271
Here’s an example of documenting a macro:

src/librustdoc/test.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,13 @@ pub fn make_test(s: &str,
530530
prog.push_str(everything_else);
531531
} else {
532532
let returns_result = everything_else.trim_end().ends_with("(())");
533+
let returns_option = everything_else.trim_end().ends_with("Some(())");
533534
let (main_pre, main_post) = if returns_result {
534-
("fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {",
535+
(if returns_option {
536+
"fn main() { fn _inner() -> Option<()> {"
537+
} else {
538+
"fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {"
539+
},
535540
"}\n_inner().unwrap() }")
536541
} else {
537542
("fn main() {\n", "\n}")

src/test/rustdoc/process-termination.rs

+12
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@
2121
/// Err("This is returned from `main`, leading to panic")?;
2222
/// Ok::<(), &'static str>(())
2323
/// ```
24+
///
25+
/// This also works with `Option<()>`s now:
26+
///
27+
/// ```rust
28+
/// Some(())
29+
/// ```
30+
///
31+
/// ```rust,should_panic
32+
/// let x: &[u32] = &[];
33+
/// let _ = x.iter().next()?;
34+
/// Some(())
35+
/// ```
2436
pub fn check_process_termination() {}

0 commit comments

Comments
 (0)