-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2024: Add rustdoc combined doctests #320
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
233d37f
2024: Add rustdoc combined doctests
ehuss 2dc4b85
Remove `main` function mention.
ehuss 53ac97c
Use an attribute example that actually prevents combining
ehuss 91a2bf7
Add sentence emphasizing that it is unlikely to have issues
ehuss f5dc745
Add link to implementation description
ehuss d1a6a44
Make some editorial changes
traviscross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Rustdoc combined tests | ||
|
||
🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". | ||
More information may be found in the tracking issue at <https://github.com/rust-lang/rust/issues/124853>. | ||
|
||
## Summary | ||
|
||
- [Doctests] are now combined into a single binary which should result in a significant performance improvement. | ||
|
||
## Details | ||
|
||
Prior the the 2024 Edition, rustdoc's "test" mode would compile each code block in your documentation as a separate executable. Although this was relatively simple to implement, it resulted in a significant performance burden when there were a large number of documentation tests. Starting with the 2024 Edition, rustdoc will attempt to combine documentation tests into a single binary, significantly reducing the overhead for compiling doctests. | ||
|
||
```rust | ||
/// Adds two numbers | ||
/// | ||
/// ``` | ||
/// assert_eq!(add(1, 1), 2); | ||
/// ``` | ||
pub fn add(left: u64, right: u64) -> u64 { | ||
left + right | ||
} | ||
|
||
/// Subtracts two numbers | ||
/// | ||
/// ``` | ||
/// assert_eq!(subtract(2, 1), 1); | ||
/// ``` | ||
pub fn subtract(left: u64, right: u64) -> u64 { | ||
left - right | ||
} | ||
``` | ||
|
||
In this example, the two doctests will now be compiled into a single executable. Rustdoc will essentially place each example in a separate function within a single binary. The tests still run in independent processes as they did before, so any global state (like global statics) should still continue to work correctly.[^implementation] | ||
|
||
This change is only available in the 2024 Edition to avoid potential incompatibilities with existing doctests which may not work in a combined executable. However, these incompatibilities are expected to be extremely rare. | ||
|
||
[doctests]: ../../rustdoc/write-documentation/documentation-tests.html | ||
[libtest harness]: ../../rustc/tests/index.html | ||
|
||
[^implementation]: For more information on the details of how this work, see ["Doctests - How were they improved?"](https://blog.guillaume-gomez.fr/articles/2024-08-17+Doctests+-+How+were+they+improved%3F). | ||
|
||
### Standalone tag | ||
|
||
In some situations it is not possible for rustdoc to combine examples into a single executable. Rustdoc will attempt to automatically detect if this is not possible. For example, a test will not be combined with others if it: | ||
|
||
* Uses the [`compile_fail`][tags] tag, which indicates that the example should fail to compile. | ||
* Uses an [`edition`][tags] tag, which indicates the edition of the example.[^edition-tag] | ||
* Uses global attributes, like the [`global_allocator`] attribute, which could potentially interfere with other tests. | ||
* Defines any crate-wide attributes (like `#![feature(...)]`). | ||
* Defines a macro that uses `$crate`, because the `$crate` path will not work correctly. | ||
|
||
However, rustdoc is not able to automatically determine *all* situations where an example cannot be combined with other examples. In these situations, you can add the `standalone` language tag to indicate that the example should be built as a separate executable. For example: | ||
|
||
```rust | ||
//! ``` | ||
//! let location = std::panic::Location::caller(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect example. I think it's the only case where code wasn't working in merged doctest. |
||
//! assert_eq!(location.line(), 5); | ||
//! ``` | ||
``` | ||
|
||
This is sensitive to the code structure of how the example is compiled and won't work with the "combined" approach because the line numbers will shift depending on how the doctests are combined. In these situations, you can add the `standalone` tag to force the example to be built separately just as it was in previous editions. E.g.: | ||
|
||
```rust | ||
//! ```standalone | ||
//! let location = std::panic::Location::caller(); | ||
//! assert_eq!(location.line(), 5); | ||
//! ``` | ||
``` | ||
|
||
[tags]: ../../rustdoc/write-documentation/documentation-tests.html#attributes | ||
[`global_allocator`]: ../../std/alloc/trait.GlobalAlloc.html | ||
|
||
[^edition-tag]: Note that rustdoc will only combine tests if the entire crate is Edition 2024 or greater. Using the `edition2024` tag in older editions will not result in those tests being combined. | ||
|
||
## Migration | ||
|
||
There is no automatic migration to determine which doctests need to be annotated with the `standalone` tag. It's very unlikely that any given doctest will not work correctly when migrated. We suggest that you update your crate to the 2024 Edition and then run your documentation tests and see if any fail. If one does, you will need to analyze whether it can be rewritten to be compatible with the combined approach, or alternatively, add the `standalone` tag to retain the previous behavior. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍