-
Notifications
You must be signed in to change notification settings - Fork 14.1k
rustdoc: Add unstable --merge-doctests=yes/no/auto flag
#149565
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
Open
jyn514
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
jyn514:force-doctest-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−33
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ use rustc_span::{DUMMY_SP, FileName, Span, kw}; | |
| use tracing::debug; | ||
|
|
||
| use super::GlobalTestOptions; | ||
| use crate::config::MergeDoctests; | ||
| use crate::display::Joined as _; | ||
| use crate::html::markdown::LangString; | ||
|
|
||
|
|
@@ -41,7 +42,7 @@ pub(crate) struct BuildDocTestBuilder<'a> { | |
| source: &'a str, | ||
| crate_name: Option<&'a str>, | ||
| edition: Edition, | ||
| can_merge_doctests: bool, | ||
| can_merge_doctests: MergeDoctests, | ||
| // If `test_id` is `None`, it means we're generating code for a code example "run" link. | ||
| test_id: Option<String>, | ||
| lang_str: Option<&'a LangString>, | ||
|
|
@@ -55,7 +56,7 @@ impl<'a> BuildDocTestBuilder<'a> { | |
| source, | ||
| crate_name: None, | ||
| edition: DEFAULT_EDITION, | ||
| can_merge_doctests: false, | ||
| can_merge_doctests: MergeDoctests::Never, | ||
| test_id: None, | ||
| lang_str: None, | ||
| span: DUMMY_SP, | ||
|
|
@@ -70,7 +71,7 @@ impl<'a> BuildDocTestBuilder<'a> { | |
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) fn can_merge_doctests(mut self, can_merge_doctests: bool) -> Self { | ||
| pub(crate) fn can_merge_doctests(mut self, can_merge_doctests: MergeDoctests) -> Self { | ||
| self.can_merge_doctests = can_merge_doctests; | ||
| self | ||
| } | ||
|
|
@@ -117,10 +118,6 @@ impl<'a> BuildDocTestBuilder<'a> { | |
| span, | ||
| global_crate_attrs, | ||
| } = self; | ||
| let can_merge_doctests = can_merge_doctests | ||
| && lang_str.is_some_and(|lang_str| { | ||
| !lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone_crate | ||
| }); | ||
|
|
||
| let result = rustc_driver::catch_fatal_errors(|| { | ||
| rustc_span::create_session_if_not_set_then(edition, |_| { | ||
|
|
@@ -155,14 +152,27 @@ impl<'a> BuildDocTestBuilder<'a> { | |
| debug!("crate_attrs:\n{crate_attrs}{maybe_crate_attrs}"); | ||
| debug!("crates:\n{crates}"); | ||
| debug!("after:\n{everything_else}"); | ||
|
|
||
| // If it contains `#[feature]` or `#[no_std]`, we don't want it to be merged either. | ||
| let can_be_merged = can_merge_doctests | ||
| && !has_global_allocator | ||
| && crate_attrs.is_empty() | ||
| // If this is a merged doctest and a defined macro uses `$crate`, then the path will | ||
| // not work, so better not put it into merged doctests. | ||
| && !(has_macro_def && everything_else.contains("$crate")); | ||
| debug!("merge-doctests: {can_merge_doctests:?}"); | ||
|
|
||
| // Up until now, we've been dealing with settings for the whole crate. | ||
| // Now, infer settings for this particular test. | ||
| // | ||
| // Avoid tests with incompatible attributes. | ||
| let opt_out = lang_str.is_some_and(|lang_str| { | ||
| lang_str.compile_fail || lang_str.test_harness || lang_str.standalone_crate | ||
| }); | ||
| let can_be_merged = if can_merge_doctests == MergeDoctests::Auto { | ||
| // We try to look at the contents of the test to detect whether it should be merged. | ||
| // This is not a complete list of possible failures, but it catches many cases. | ||
| let will_probably_fail = has_global_allocator | ||
|
Contributor
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. Don't know why I'm getting a chuckle at that variable name :) |
||
| || !crate_attrs.is_empty() | ||
| // If this is a merged doctest and a defined macro uses `$crate`, then the path will | ||
| // not work, so better not put it into merged doctests. | ||
| || (has_macro_def && everything_else.contains("$crate")); | ||
| !opt_out && !will_probably_fail | ||
| } else { | ||
| can_merge_doctests != MergeDoctests::Never && !opt_out | ||
| }; | ||
| DocTestBuilder { | ||
| supports_color, | ||
| has_main_fn, | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
25 changes: 25 additions & 0 deletions
25
tests/rustdoc-ui/doctest/force-merge-default-not-override.rs
This file contains hidden or 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,25 @@ | ||
| //@ check-pass | ||
| //@ edition: 2024 | ||
| //@ compile-flags: --test --test-args=--test-threads=1 --merge-doctests=yes -Z unstable-options | ||
| //@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR" | ||
| //@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" | ||
| //@ normalize-stdout: "ran in \d+\.\d+s" -> "ran in $$TIME" | ||
| //@ normalize-stdout: "compilation took \d+\.\d+s" -> "compilation took $$TIME" | ||
| //@ normalize-stdout: ".rs:\d+:\d+" -> ".rs:$$LINE:$$COL" | ||
|
|
||
| // FIXME: compiletest doesn't support `// RAW` for doctests because the progress messages aren't | ||
| // emitted as JSON. Instead the .stderr file tests that this doesn't contains a | ||
| // "merged compilation took ..." message. | ||
|
|
||
| /// ```standalone_crate | ||
| /// let x = 12; | ||
| /// ``` | ||
| /// | ||
| /// These two doctests should be not be merged, even though this passes `--merge-doctests=yes`. | ||
| /// | ||
| /// ```standalone_crate | ||
| /// fn main() { | ||
| /// println!("owo"); | ||
| /// } | ||
| /// ``` | ||
| pub struct Foo; |
7 changes: 7 additions & 0 deletions
7
tests/rustdoc-ui/doctest/force-merge-default-not-override.stdout
This file contains hidden or 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,7 @@ | ||
|
|
||
| running 2 tests | ||
| test $DIR/force-merge-default-not-override.rs - Foo (line 14) ... ok | ||
| test $DIR/force-merge-default-not-override.rs - Foo (line 20) ... ok | ||
|
|
||
| test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.