@@ -40,15 +40,19 @@ use crate::lint::init_lints;
4040use self :: rust:: HirCollector ;
4141
4242/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
43- #[ derive( Clone , Default ) ]
43+ #[ derive( Clone ) ]
4444pub ( crate ) struct GlobalTestOptions {
45+ /// Name of the crate (for regular `rustdoc`) or Markdown file (for `rustdoc foo.md`).
46+ pub ( crate ) crate_name : String ,
4547 /// Whether to disable the default `extern crate my_crate;` when creating doctests.
4648 pub ( crate ) no_crate_inject : bool ,
4749 /// Whether inserting extra indent spaces in code block,
4850 /// default is `false`, only `true` for generating code link of Rust playground
4951 pub ( crate ) insert_indent_space : bool ,
5052 /// Additional crate-level attributes to add to doctests.
5153 pub ( crate ) attrs : Vec < String > ,
54+ /// Path to file containing arguments for the invocation of rustc.
55+ pub ( crate ) args_file : PathBuf ,
5256}
5357
5458pub ( crate ) fn generate_args_file ( file_path : & Path , options : & RustdocOptions ) -> Result < ( ) , String > {
@@ -167,24 +171,19 @@ pub(crate) fn run(
167171 Ok ( temp_dir) => temp_dir,
168172 Err ( error) => return crate :: wrap_return ( dcx, Err ( error) ) ,
169173 } ;
170- let file_path = temp_dir. path ( ) . join ( "rustdoc-cfgs" ) ;
171- crate :: wrap_return ( dcx, generate_args_file ( & file_path , & options) ) ?;
174+ let args_path = temp_dir. path ( ) . join ( "rustdoc-cfgs" ) ;
175+ crate :: wrap_return ( dcx, generate_args_file ( & args_path , & options) ) ?;
172176
173177 let ( tests, unused_extern_reports, compiling_test_count) =
174178 interface:: run_compiler ( config, |compiler| {
175179 compiler. enter ( |queries| {
176180 let collector = queries. global_ctxt ( ) ?. enter ( |tcx| {
181+ let crate_name = tcx. crate_name ( LOCAL_CRATE ) . to_string ( ) ;
177182 let crate_attrs = tcx. hir ( ) . attrs ( CRATE_HIR_ID ) ;
178-
179- let opts = scrape_test_config ( crate_attrs) ;
183+ let opts = scrape_test_config ( crate_name, crate_attrs, args_path) ;
180184 let enable_per_target_ignores = options. enable_per_target_ignores ;
181- let mut collector = CreateRunnableDoctests :: new (
182- tcx. crate_name ( LOCAL_CRATE ) . to_string ( ) ,
183- options,
184- opts,
185- file_path,
186- ) ;
187185
186+ let mut collector = CreateRunnableDoctests :: new ( options, opts) ;
188187 let hir_collector = HirCollector :: new (
189188 & compiler. sess ,
190189 tcx. hir ( ) ,
@@ -264,11 +263,20 @@ pub(crate) fn run_tests(
264263}
265264
266265// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
267- fn scrape_test_config ( attrs : & [ ast:: Attribute ] ) -> GlobalTestOptions {
266+ fn scrape_test_config (
267+ crate_name : String ,
268+ attrs : & [ ast:: Attribute ] ,
269+ args_file : PathBuf ,
270+ ) -> GlobalTestOptions {
268271 use rustc_ast_pretty:: pprust;
269272
270- let mut opts =
271- GlobalTestOptions { no_crate_inject : false , attrs : Vec :: new ( ) , insert_indent_space : false } ;
273+ let mut opts = GlobalTestOptions {
274+ crate_name,
275+ no_crate_inject : false ,
276+ attrs : Vec :: new ( ) ,
277+ insert_indent_space : false ,
278+ args_file,
279+ } ;
272280
273281 let test_attrs: Vec < _ > = attrs
274282 . iter ( )
@@ -363,20 +371,18 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com
363371
364372fn run_test (
365373 test : & str ,
366- crate_name : & str ,
367374 line : usize ,
368375 rustdoc_options : & RustdocOptions ,
369376 test_options : IndividualTestOptions ,
370377 mut lang_string : LangString ,
371378 no_run : bool ,
372379 opts : & GlobalTestOptions ,
373380 edition : Edition ,
374- path : PathBuf ,
375381 report_unused_externs : impl Fn ( UnusedExterns ) ,
376382) -> Result < ( ) , TestFailure > {
377383 let ( test, line_offset, supports_color) = make_test (
378384 test,
379- Some ( crate_name) ,
385+ Some ( & opts . crate_name ) ,
380386 lang_string. test_harness ,
381387 opts,
382388 edition,
@@ -393,14 +399,14 @@ fn run_test(
393399 . unwrap_or_else ( || rustc_interface:: util:: rustc_path ( ) . expect ( "found rustc" ) ) ;
394400 let mut compiler = wrapped_rustc_command ( & rustdoc_options. test_builder_wrappers , rustc_binary) ;
395401
396- compiler. arg ( & format ! ( "@{}" , test_options . arg_file . display( ) ) ) ;
402+ compiler. arg ( & format ! ( "@{}" , opts . args_file . display( ) ) ) ;
397403
398404 if let Some ( sysroot) = & rustdoc_options. maybe_sysroot {
399405 compiler. arg ( format ! ( "--sysroot={}" , sysroot. display( ) ) ) ;
400406 }
401407
402408 compiler. arg ( "--edition" ) . arg ( & edition. to_string ( ) ) ;
403- compiler. env ( "UNSTABLE_RUSTDOC_TEST_PATH" , path) ;
409+ compiler. env ( "UNSTABLE_RUSTDOC_TEST_PATH" , & test_options . path ) ;
404410 compiler. env ( "UNSTABLE_RUSTDOC_TEST_LINE" , format ! ( "{}" , line as isize - line_offset as isize ) ) ;
405411 compiler. arg ( "-o" ) . arg ( & output_file) ;
406412 if lang_string. test_harness {
@@ -927,13 +933,13 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
927933}
928934
929935pub ( crate ) struct IndividualTestOptions {
930- arg_file : PathBuf ,
931936 outdir : DirState ,
932937 test_id : String ,
938+ path : PathBuf ,
933939}
934940
935941impl IndividualTestOptions {
936- fn new ( options : & RustdocOptions , arg_file : & Path , test_id : String ) -> Self {
942+ fn new ( options : & RustdocOptions , test_id : String , test_path : PathBuf ) -> Self {
937943 let outdir = if let Some ( ref path) = options. persist_doctests {
938944 let mut path = path. clone ( ) ;
939945 path. push ( & test_id) ;
@@ -948,7 +954,7 @@ impl IndividualTestOptions {
948954 DirState :: Temp ( get_doctest_dir ( ) . expect ( "rustdoc needs a tempdir" ) )
949955 } ;
950956
951- Self { arg_file : arg_file . into ( ) , outdir, test_id }
957+ Self { outdir, test_id, path : test_path }
952958 }
953959}
954960
@@ -980,30 +986,24 @@ pub(crate) struct CreateRunnableDoctests {
980986 pub ( crate ) tests : Vec < test:: TestDescAndFn > ,
981987
982988 rustdoc_options : Arc < RustdocOptions > ,
983- crate_name : String ,
984989 opts : GlobalTestOptions ,
985990 visited_tests : FxHashMap < ( String , usize ) , usize > ,
986991 unused_extern_reports : Arc < Mutex < Vec < UnusedExterns > > > ,
987992 compiling_test_count : AtomicUsize ,
988- arg_file : PathBuf ,
989993}
990994
991995impl CreateRunnableDoctests {
992996 pub ( crate ) fn new (
993- crate_name : String ,
994997 rustdoc_options : RustdocOptions ,
995998 opts : GlobalTestOptions ,
996- arg_file : PathBuf ,
997999 ) -> CreateRunnableDoctests {
9981000 CreateRunnableDoctests {
9991001 tests : Vec :: new ( ) ,
10001002 rustdoc_options : Arc :: new ( rustdoc_options) ,
1001- crate_name,
10021003 opts,
10031004 visited_tests : FxHashMap :: default ( ) ,
10041005 unused_extern_reports : Default :: default ( ) ,
10051006 compiling_test_count : AtomicUsize :: new ( 0 ) ,
1006- arg_file,
10071007 }
10081008 }
10091009
@@ -1018,7 +1018,6 @@ impl CreateRunnableDoctests {
10181018
10191019 fn add_test ( & mut self , test : ScrapedDoctest ) {
10201020 let name = self . generate_name ( & test. filename , test. line , & test. logical_path ) ;
1021- let crate_name = self . crate_name . clone ( ) ;
10221021 let opts = self . opts . clone ( ) ;
10231022 let target_str = self . rustdoc_options . target . to_string ( ) ;
10241023 let unused_externs = self . unused_extern_reports . clone ( ) ;
@@ -1061,8 +1060,7 @@ impl CreateRunnableDoctests {
10611060 ) ;
10621061
10631062 let rustdoc_options = self . rustdoc_options . clone ( ) ;
1064- let rustdoc_test_options =
1065- IndividualTestOptions :: new ( & self . rustdoc_options , & self . arg_file , test_id) ;
1063+ let rustdoc_test_options = IndividualTestOptions :: new ( & self . rustdoc_options , test_id, path) ;
10661064
10671065 debug ! ( "creating test {name}: {}" , test. text) ;
10681066 self . tests . push ( test:: TestDescAndFn {
@@ -1086,25 +1084,15 @@ impl CreateRunnableDoctests {
10861084 test_type : test:: TestType :: DocTest ,
10871085 } ,
10881086 testfn : test:: DynTestFn ( Box :: new ( move || {
1089- doctest_run_fn (
1090- crate_name,
1091- rustdoc_test_options,
1092- opts,
1093- path,
1094- test,
1095- rustdoc_options,
1096- unused_externs,
1097- )
1087+ doctest_run_fn ( rustdoc_test_options, opts, test, rustdoc_options, unused_externs)
10981088 } ) ) ,
10991089 } ) ;
11001090 }
11011091}
11021092
11031093fn doctest_run_fn (
1104- crate_name : String ,
11051094 test_opts : IndividualTestOptions ,
11061095 global_opts : GlobalTestOptions ,
1107- path : PathBuf ,
11081096 scraped_test : ScrapedDoctest ,
11091097 rustdoc_options : Arc < RustdocOptions > ,
11101098 unused_externs : Arc < Mutex < Vec < UnusedExterns > > > ,
@@ -1116,15 +1104,13 @@ fn doctest_run_fn(
11161104 let edition = scraped_test. edition ( & rustdoc_options) ;
11171105 let res = run_test (
11181106 & scraped_test. text ,
1119- & crate_name,
11201107 scraped_test. line ,
11211108 & rustdoc_options,
11221109 test_opts,
11231110 scraped_test. langstr ,
11241111 no_run,
11251112 & global_opts,
11261113 edition,
1127- path,
11281114 report_unused_externs,
11291115 ) ;
11301116
0 commit comments