Skip to content

Fix O(tests) stack usage in edition 2024 mergeable doctests #138281

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

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/librustdoc/doctest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ impl DocTestRunner {
self.crate_attrs.insert(line.to_string());
}
}
if !self.ids.is_empty() {
self.ids.push(',');
}
self.ids.push_str(&format!(
"{}::TEST",
"tests.push({}::TEST);\n",
generate_mergeable_doctest(
doctest,
scraped_test,
Expand Down Expand Up @@ -136,19 +133,23 @@ mod __doctest_mod {{

#[rustc_main]
fn main() -> std::process::ExitCode {{
const TESTS: [test::TestDescAndFn; {nb_tests}] = [{ids}];
let tests = {{
let mut tests = Vec::with_capacity({nb_tests});
{ids}
tests
}};
let test_marker = std::ffi::OsStr::new(__doctest_mod::RUN_OPTION);
let test_args = &[{test_args}];
const ENV_BIN: &'static str = \"RUSTDOC_DOCTEST_BIN_PATH\";

if let Ok(binary) = std::env::var(ENV_BIN) {{
let _ = crate::__doctest_mod::BINARY_PATH.set(binary.into());
unsafe {{ std::env::remove_var(ENV_BIN); }}
return std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None));
return std::process::Termination::report(test::test_main(test_args, tests, None));
}} else if let Ok(nb_test) = std::env::var(__doctest_mod::RUN_OPTION) {{
if let Ok(nb_test) = nb_test.parse::<usize>() {{
if let Some(test) = TESTS.get(nb_test) {{
if let test::StaticTestFn(f) = test.testfn {{
if let Some(test) = tests.get(nb_test) {{
if let test::StaticTestFn(f) = &test.testfn {{
return std::process::Termination::report(f());
}}
}}
Expand All @@ -158,7 +159,7 @@ if let Ok(binary) = std::env::var(ENV_BIN) {{

eprintln!(\"WARNING: No rustdoc doctest environment variable provided so doctests will be run in \
the same process\");
std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None))
std::process::Termination::report(test::test_main(test_args, tests, None))
}}",
nb_tests = self.nb_tests,
output = self.output,
Expand Down
Loading