Skip to content

Commit 72aeffa

Browse files
Remove need for unsafe code in merged doctests
1 parent a3bc2c7 commit 72aeffa

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/librustdoc/doctest/runner.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ impl DocTestRunner {
107107
{output}
108108
109109
mod __doctest_mod {{
110-
pub static mut BINARY_PATH: Option<std::path::PathBuf> = None;
110+
use std::sync::OnceLock;
111+
use std::path::PathBuf;
112+
113+
pub static BINARY_PATH: OnceLock<PathBuf> = OnceLock::new();
111114
pub const RUN_OPTION: &str = \"*doctest-inner-test\";
112115
pub const BIN_OPTION: &str = \"*doctest-bin-path\";
113116
114117
#[allow(unused)]
115-
pub fn get_doctest_path() -> Option<&'static std::path::Path> {{
116-
unsafe {{ self::BINARY_PATH.as_deref() }}
118+
pub fn doctest_path() -> Option<&'static PathBuf> {{
119+
self::BINARY_PATH.get()
117120
}}
118121
119122
#[allow(unused)]
@@ -145,7 +148,9 @@ while let Some(arg) = args.next() {{
145148
let Some(binary) = args.next() else {{
146149
panic!(\"missing argument after `{{}}`\", __doctest_mod::BIN_OPTION);
147150
}};
148-
unsafe {{ crate::__doctest_mod::BINARY_PATH = Some(binary.into()); }}
151+
if crate::__doctest_mod::BINARY_PATH.set(binary.into()).is_err() {{
152+
panic!(\"`{{}}` option was used more than once\", bin_marker.to_string_lossy());
153+
}}
149154
return std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None));
150155
}} else if arg == test_marker {{
151156
let Some(nb_test) = args.next() else {{
@@ -254,7 +259,7 @@ test::StaticTestFn(
254259
} else {
255260
format!(
256261
"
257-
if let Some(bin_path) = crate::__doctest_mod::get_doctest_path() {{
262+
if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
258263
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}))
259264
}} else {{
260265
test::assert_test_result(self::main())

0 commit comments

Comments
 (0)