Skip to content

Commit 9f0de65

Browse files
authored
Merge pull request #1194 from mattheww/2022-04_termination
Document the Termination trait for main() and test functions
2 parents b5f6c23 + e172ea5 commit 9f0de65

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/attributes/testing.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ enables the [`test` conditional compilation option].
99

1010
The *`test` attribute* marks a function to be executed as a test. These
1111
functions are only compiled when in test mode. Test functions must be free,
12-
monomorphic functions that take no arguments, and the return type must be one
13-
of the following:
12+
monomorphic functions that take no arguments, and the return type must implement the [`Termination`] trait, for example:
1413

1514
* `()`
16-
* `Result<(), E> where E: Error`
17-
<!-- * `!` -->
18-
<!-- * Result<!, E> where E: Error` -->
19-
20-
> Note: The implementation of which return types are allowed is determined by
21-
> the unstable [`Termination`] trait.
15+
* `Result<(), E> where E: Debug`
16+
* `!`
17+
<!-- * Result<!, E> where E: Debug` -->
2218

2319
<!-- If the previous section needs updating (from "must take no arguments"
2420
onwards, also update it in the crates-and-source-files.md file -->
2521

2622
> Note: The test mode is enabled by passing the `--test` argument to `rustc`
2723
> or using `cargo test`.
2824
29-
Tests that return `()` pass as long as they terminate and do not panic. Tests
30-
that return a `Result<(), E>` pass as long as they return `Ok(())`. Tests that
31-
do not terminate neither pass nor fail.
25+
The test harness calls the returned value's [`report`] method, and classifies the test as passed or failed depending on whether the resulting [`ExitCode`] represents successful termination.
26+
In particular:
27+
* Tests that return `()` pass as long as they terminate and do not panic.
28+
* Tests that return a `Result<(), E>` pass as long as they return `Ok(())`.
29+
* Tests that return `ExitCode::SUCCESS` pass, and tests that return `ExitCode::FAILURE` fail.
30+
* Tests that do not terminate neither pass nor fail.
3231

3332
```rust
3433
# use std::io;
@@ -85,5 +84,7 @@ fn mytest() {
8584
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
8685
[_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
8786
[`Termination`]: ../../std/process/trait.Termination.html
87+
[`report`]: ../../std/process/trait.Termination.html#tymethod.report
8888
[`test` conditional compilation option]: ../conditional-compilation.md#test
8989
[attributes]: ../attributes.md
90+
[`ExitCode`]: ../../std/process/struct.ExitCode.html

src/crates-and-source-files.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,30 @@ This section has been moved to the [Preludes chapter](names/preludes.md).
103103
A crate that contains a `main` [function] can be compiled to an executable. If a
104104
`main` function is present, it must take no arguments, must not declare any
105105
[trait or lifetime bounds], must not have any [where clauses], and its return
106-
type must be one of the following:
106+
type must implement the [`Termination`] trait.
107107

108-
* `()`
109-
* `Result<(), E> where E: Error`
110-
<!-- * `!` -->
111-
<!-- * Result<!, E> where E: Error` -->
108+
```rust
109+
fn main() {}
110+
```
111+
```rust
112+
fn main() -> ! {
113+
std::process::exit(0);
114+
}
115+
```
116+
```rust
117+
fn main() -> impl std::process::Termination {
118+
std::process::ExitCode::SUCCESS
119+
}
120+
```
112121

113-
> Note: The implementation of which return types are allowed is determined by
114-
> the unstable [`Termination`] trait.
122+
> **Note**: Types with implementations of [`Termination`] in the standard library include:
123+
>
124+
> * `()`
125+
> * [`!`]
126+
> * [`ExitCode`]
127+
> * `Result<(), E> where E: Debug`
128+
> * `Result<Infallible, E> where E: Debug`
129+
<!-- > * Result<!, E> where E: Debug` -->
115130
116131
<!-- If the previous section needs updating (from "must take no arguments"
117132
onwards, also update it in the testing.md file -->
@@ -143,11 +158,13 @@ or `_` (U+005F) characters.
143158
in the Owens and Flatt module system, or a *configuration* in Mesa.
144159

145160
[Unicode alphanumeric]: ../std/primitive.char.html#method.is_alphanumeric
161+
[`!`]: types/never.md
146162
[_InnerAttribute_]: attributes.md
147163
[_Item_]: items.md
148164
[_MetaNameValueStr_]: attributes.md#meta-item-attribute-syntax
149165
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
150166
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
167+
[`ExitCode`]: ../std/process/struct.ExitCode.html
151168
[`Termination`]: ../std/process/trait.Termination.html
152169
[attribute]: attributes.md
153170
[attributes]: attributes.md

src/special-types-and-traits.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ The [`Sync`] trait indicates that a value of this type is safe to share between
9292
multiple threads. This trait must be implemented for all types used in
9393
immutable [`static` items].
9494

95+
## `Termination`
96+
97+
The [`Termination`] trait indicates the acceptable return types for the [main function] and [test functions].
98+
9599
## Auto traits
96100

97101
The [`Send`], [`Sync`], [`Unpin`], [`UnwindSafe`], and [`RefUnwindSafe`] traits are _auto
@@ -151,6 +155,7 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
151155
[`std::cmp`]: ../std/cmp/index.html
152156
[`std::marker::PhantomData<T>`]: ../std/marker/struct.PhantomData.html
153157
[`std::ops`]: ../std/ops/index.html
158+
[`Termination`]: ../std/process/trait.Termination.html
154159
[`UnwindSafe`]: ../std/panic/trait.UnwindSafe.html
155160
[`Sync`]: ../std/marker/trait.Sync.html
156161
[`Unpin`]: ../std/marker/trait.Unpin.html
@@ -168,11 +173,13 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
168173
[implementation items]: items/implementations.md
169174
[indexing expressions]: expressions/array-expr.md#array-and-slice-indexing-expressions
170175
[interior mutability]: interior-mutability.md
176+
[main function]: crates-and-source-files.md#main-functions
171177
[Methods]: items/associated-items.md#associated-functions-and-methods
172178
[method resolution]: expressions/method-call-expr.md
173179
[operators]: expressions/operator-expr.md
174180
[orphan rules]: items/implementations.md#trait-implementation-coherence
175181
[`static` items]: items/static-items.md
182+
[test functions]: attributes/testing.md#the-test-attribute
176183
[the standard library]: ../std/index.html
177184
[trait object]: types/trait-object.md
178185
[Tuples]: types/tuple.md

0 commit comments

Comments
 (0)