Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a0c4a77

Browse files
Improve code readability
1 parent 211fde6 commit a0c4a77

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/librustdoc/doctest.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ struct RunnableDoctest {
448448
no_run: bool,
449449
}
450450

451+
impl RunnableDoctest {
452+
fn path_for_merged_doctest(&self) -> PathBuf {
453+
self.test_opts.outdir.path().join(&format!("doctest_{}.rs", self.edition))
454+
}
455+
}
456+
451457
fn run_test(
452458
doctest: RunnableDoctest,
453459
rustdoc_options: &RustdocOptions,
@@ -528,8 +534,7 @@ fn run_test(
528534
if is_multiple_tests {
529535
// It makes the compilation failure much faster if it is for a combined doctest.
530536
compiler.arg("--error-format=short");
531-
let input_file =
532-
doctest.test_opts.outdir.path().join(&format!("doctest_{}.rs", doctest.edition));
537+
let input_file = doctest.path_for_merged_doctest();
533538
if std::fs::write(&input_file, &doctest.full_test_code).is_err() {
534539
// If we cannot write this file for any reason, we leave. All combined tests will be
535540
// tested as standalone tests.
@@ -809,9 +814,9 @@ impl CreateRunnableDoctests {
809814
edition,
810815
self.can_merge_doctests,
811816
Some(test_id),
817+
Some(&scraped_test.langstr),
812818
);
813-
let is_standalone = !self.can_merge_doctests
814-
|| !doctest.can_be_merged
819+
let is_standalone = !doctest.can_be_merged
815820
|| scraped_test.langstr.compile_fail
816821
|| scraped_test.langstr.test_harness
817822
|| scraped_test.langstr.standalone

src/librustdoc/doctest/make.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Logic for transforming the raw code given by the user into something actually
22
//! runnable, e.g. by adding a `main` function if it doesn't already exist.
33
4+
use crate::html::markdown::LangString;
5+
46
use std::io;
57

68
use rustc_ast as ast;
@@ -42,7 +44,13 @@ impl DocTestBuilder {
4244
can_merge_doctests: bool,
4345
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
4446
test_id: Option<String>,
47+
lang_str: Option<&LangString>,
4548
) -> Self {
49+
let can_merge_doctests = can_merge_doctests
50+
&& lang_str.is_some_and(|lang_str| {
51+
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone
52+
});
53+
4654
let SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } =
4755
partition_source(source, edition);
4856

src/librustdoc/doctest/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn make_test(
1616
DEFAULT_EDITION,
1717
false,
1818
test_id.map(|s| s.to_string()),
19+
None,
1920
);
2021
let (code, line_offset) =
2122
doctest.generate_unique_doctest(test_code, dont_insert_main, opts, crate_name);

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
297297
attrs: vec![],
298298
args_file: PathBuf::new(),
299299
};
300-
let doctest = doctest::DocTestBuilder::new(&test, krate, edition, false, None);
300+
let doctest = doctest::DocTestBuilder::new(&test, krate, edition, false, None, None);
301301
let (test, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
302302
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
303303

0 commit comments

Comments
 (0)