]> git.lizzy.rs Git - rust.git/commitdiff
implicit `Option`-returning doctests
authorAndre Bogus <bogusandre@gmail.com>
Tue, 28 May 2019 17:10:39 +0000 (19:10 +0200)
committerAndre Bogus <bogusandre@gmail.com>
Tue, 28 May 2019 17:10:39 +0000 (19:10 +0200)
This distinguishes `Option` and `Result`-returning doctests with
implicit `main` method, where the former tests must end with
`Some(())`.

src/doc/rustdoc/src/documentation-tests.md
src/librustdoc/test.rs
src/test/rustdoc/process-termination.rs

index c9acd3c307b54690851393d1e5304d78cbcee2ad..a896ce819aeb7e389c03d6aa7987e18940727435 100644 (file)
@@ -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:
index d76d4380755f24b40b4d97e48f323663b53708c7..1b6460f52515419e8ec73753dd04b5dfaae0e415 100644 (file)
@@ -499,8 +499,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}")
index 32258792b6e8b88c601b50ac6d4a53b5e84d69be..31ae0143d4771aee9492a0333a3ad102f2bc6568 100644 (file)
 /// 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() {}