Skip to content

Commit 646667a

Browse files
committed
--merge-doctests is a default, not an override
1 parent 3aa4ece commit 646667a

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,22 @@ impl<'a> BuildDocTestBuilder<'a> {
156156

157157
// Up until now, we've been dealing with settings for the whole crate.
158158
// Now, infer settings for this particular test.
159+
//
160+
// Avoid tests with incompatible attributes.
161+
let opt_out = lang_str.is_some_and(|lang_str| {
162+
lang_str.compile_fail || lang_str.test_harness || lang_str.standalone_crate
163+
});
159164
let can_be_merged = if can_merge_doctests == MergeDoctests::Auto {
160-
let mut can_merge = false;
161-
// Avoid tests with incompatible attributes.
162-
can_merge |= lang_str.is_some_and(|lang_str| {
163-
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone_crate
164-
});
165-
// If it contains `#[feature]` or `#[no_std]`, we don't want it to be merged either.
166-
can_merge &= !has_global_allocator
167-
&& crate_attrs.is_empty()
165+
// We try to look at the contents of the test to detect whether it should be merged.
166+
// This is not a complete list of possible failures, but it catches many cases.
167+
let will_probably_fail = has_global_allocator
168+
|| !crate_attrs.is_empty()
168169
// If this is a merged doctest and a defined macro uses `$crate`, then the path will
169170
// not work, so better not put it into merged doctests.
170-
&& !(has_macro_def && everything_else.contains("$crate"));
171-
can_merge
171+
|| (has_macro_def && everything_else.contains("$crate"));
172+
!opt_out && !will_probably_fail
172173
} else {
173-
can_merge_doctests != MergeDoctests::Never
174+
can_merge_doctests != MergeDoctests::Never && !opt_out
174175
};
175176
DocTestBuilder {
176177
supports_color,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ check-pass
2+
//@ edition: 2024
3+
//@ compile-flags: --test --test-args=--test-threads=1 --merge-doctests=yes -Z unstable-options
4+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
5+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
//@ normalize-stdout: "ran in \d+\.\d+s" -> "ran in $$TIME"
7+
//@ normalize-stdout: "compilation took \d+\.\d+s" -> "compilation took $$TIME"
8+
//@ normalize-stdout: ".rs:\d+:\d+" -> ".rs:$$LINE:$$COL"
9+
10+
// FIXME: compiletest doesn't support `// RAW` for doctests because the progress messages aren't
11+
// emitted as JSON. Instead the .stderr file tests that this doesn't contains a
12+
// "merged compilation took ..." message.
13+
14+
/// ```standalone_crate
15+
/// let x = 12;
16+
/// ```
17+
///
18+
/// These two doctests should be not be merged, even though this passes `--merge-doctests=yes`.
19+
///
20+
/// ```standalone_crate
21+
/// fn main() {
22+
/// println!("owo");
23+
/// }
24+
/// ```
25+
pub struct Foo;

0 commit comments

Comments
 (0)