@@ -40,15 +40,19 @@ use crate::lint::init_lints;
40
40
use self :: rust:: HirCollector ;
41
41
42
42
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
43
- #[ derive( Clone , Default ) ]
43
+ #[ derive( Clone ) ]
44
44
pub ( crate ) struct GlobalTestOptions {
45
+ /// Name of the crate (for regular `rustdoc`) or Markdown file (for `rustdoc foo.md`).
46
+ pub ( crate ) crate_name : String ,
45
47
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
46
48
pub ( crate ) no_crate_inject : bool ,
47
49
/// Whether inserting extra indent spaces in code block,
48
50
/// default is `false`, only `true` for generating code link of Rust playground
49
51
pub ( crate ) insert_indent_space : bool ,
50
52
/// Additional crate-level attributes to add to doctests.
51
53
pub ( crate ) attrs : Vec < String > ,
54
+ /// Path to file containing arguments for the invocation of rustc.
55
+ pub ( crate ) args_file : PathBuf ,
52
56
}
53
57
54
58
pub ( crate ) fn generate_args_file ( file_path : & Path , options : & RustdocOptions ) -> Result < ( ) , String > {
@@ -167,24 +171,19 @@ pub(crate) fn run(
167
171
Ok ( temp_dir) => temp_dir,
168
172
Err ( error) => return crate :: wrap_return ( dcx, Err ( error) ) ,
169
173
} ;
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) ) ?;
172
176
173
177
let ( tests, unused_extern_reports, compiling_test_count) =
174
178
interface:: run_compiler ( config, |compiler| {
175
179
compiler. enter ( |queries| {
176
180
let collector = queries. global_ctxt ( ) ?. enter ( |tcx| {
181
+ let crate_name = tcx. crate_name ( LOCAL_CRATE ) . to_string ( ) ;
177
182
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) ;
180
184
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
- ) ;
187
185
186
+ let mut collector = CreateRunnableDoctests :: new ( options, opts) ;
188
187
let hir_collector = HirCollector :: new (
189
188
& compiler. sess ,
190
189
tcx. hir ( ) ,
@@ -264,11 +263,20 @@ pub(crate) fn run_tests(
264
263
}
265
264
266
265
// 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 {
268
271
use rustc_ast_pretty:: pprust;
269
272
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
+ } ;
272
280
273
281
let test_attrs: Vec < _ > = attrs
274
282
. iter ( )
@@ -363,20 +371,18 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com
363
371
364
372
fn run_test (
365
373
test : & str ,
366
- crate_name : & str ,
367
374
line : usize ,
368
375
rustdoc_options : & RustdocOptions ,
369
376
test_options : IndividualTestOptions ,
370
377
mut lang_string : LangString ,
371
378
no_run : bool ,
372
379
opts : & GlobalTestOptions ,
373
380
edition : Edition ,
374
- path : PathBuf ,
375
381
report_unused_externs : impl Fn ( UnusedExterns ) ,
376
382
) -> Result < ( ) , TestFailure > {
377
383
let ( test, line_offset, supports_color) = make_test (
378
384
test,
379
- Some ( crate_name) ,
385
+ Some ( & opts . crate_name ) ,
380
386
lang_string. test_harness ,
381
387
opts,
382
388
edition,
@@ -393,14 +399,14 @@ fn run_test(
393
399
. unwrap_or_else ( || rustc_interface:: util:: rustc_path ( ) . expect ( "found rustc" ) ) ;
394
400
let mut compiler = wrapped_rustc_command ( & rustdoc_options. test_builder_wrappers , rustc_binary) ;
395
401
396
- compiler. arg ( & format ! ( "@{}" , test_options . arg_file . display( ) ) ) ;
402
+ compiler. arg ( & format ! ( "@{}" , opts . args_file . display( ) ) ) ;
397
403
398
404
if let Some ( sysroot) = & rustdoc_options. maybe_sysroot {
399
405
compiler. arg ( format ! ( "--sysroot={}" , sysroot. display( ) ) ) ;
400
406
}
401
407
402
408
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 ) ;
404
410
compiler. env ( "UNSTABLE_RUSTDOC_TEST_LINE" , format ! ( "{}" , line as isize - line_offset as isize ) ) ;
405
411
compiler. arg ( "-o" ) . arg ( & output_file) ;
406
412
if lang_string. test_harness {
@@ -926,13 +932,13 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
926
932
}
927
933
928
934
pub ( crate ) struct IndividualTestOptions {
929
- arg_file : PathBuf ,
930
935
outdir : DirState ,
931
936
test_id : String ,
937
+ path : PathBuf ,
932
938
}
933
939
934
940
impl IndividualTestOptions {
935
- fn new ( options : & RustdocOptions , arg_file : & Path , test_id : String ) -> Self {
941
+ fn new ( options : & RustdocOptions , test_id : String , test_path : PathBuf ) -> Self {
936
942
let outdir = if let Some ( ref path) = options. persist_doctests {
937
943
let mut path = path. clone ( ) ;
938
944
path. push ( & test_id) ;
@@ -947,7 +953,7 @@ impl IndividualTestOptions {
947
953
DirState :: Temp ( get_doctest_dir ( ) . expect ( "rustdoc needs a tempdir" ) )
948
954
} ;
949
955
950
- Self { arg_file : arg_file . into ( ) , outdir, test_id }
956
+ Self { outdir, test_id, path : test_path }
951
957
}
952
958
}
953
959
@@ -979,30 +985,24 @@ pub(crate) struct CreateRunnableDoctests {
979
985
pub ( crate ) tests : Vec < test:: TestDescAndFn > ,
980
986
981
987
rustdoc_options : Arc < RustdocOptions > ,
982
- crate_name : String ,
983
988
opts : GlobalTestOptions ,
984
989
visited_tests : FxHashMap < ( String , usize ) , usize > ,
985
990
unused_extern_reports : Arc < Mutex < Vec < UnusedExterns > > > ,
986
991
compiling_test_count : AtomicUsize ,
987
- arg_file : PathBuf ,
988
992
}
989
993
990
994
impl CreateRunnableDoctests {
991
995
pub ( crate ) fn new (
992
- crate_name : String ,
993
996
rustdoc_options : RustdocOptions ,
994
997
opts : GlobalTestOptions ,
995
- arg_file : PathBuf ,
996
998
) -> CreateRunnableDoctests {
997
999
CreateRunnableDoctests {
998
1000
tests : Vec :: new ( ) ,
999
1001
rustdoc_options : Arc :: new ( rustdoc_options) ,
1000
- crate_name,
1001
1002
opts,
1002
1003
visited_tests : FxHashMap :: default ( ) ,
1003
1004
unused_extern_reports : Default :: default ( ) ,
1004
1005
compiling_test_count : AtomicUsize :: new ( 0 ) ,
1005
- arg_file,
1006
1006
}
1007
1007
}
1008
1008
@@ -1017,7 +1017,6 @@ impl CreateRunnableDoctests {
1017
1017
1018
1018
fn add_test ( & mut self , test : ScrapedDoctest ) {
1019
1019
let name = self . generate_name ( & test. filename , test. line , & test. logical_path ) ;
1020
- let crate_name = self . crate_name . clone ( ) ;
1021
1020
let opts = self . opts . clone ( ) ;
1022
1021
let target_str = self . rustdoc_options . target . to_string ( ) ;
1023
1022
let unused_externs = self . unused_extern_reports . clone ( ) ;
@@ -1060,8 +1059,7 @@ impl CreateRunnableDoctests {
1060
1059
) ;
1061
1060
1062
1061
let rustdoc_options = self . rustdoc_options . clone ( ) ;
1063
- let rustdoc_test_options =
1064
- IndividualTestOptions :: new ( & self . rustdoc_options , & self . arg_file , test_id) ;
1062
+ let rustdoc_test_options = IndividualTestOptions :: new ( & self . rustdoc_options , test_id, path) ;
1065
1063
1066
1064
debug ! ( "creating test {name}: {}" , test. text) ;
1067
1065
self . tests . push ( test:: TestDescAndFn {
@@ -1085,25 +1083,15 @@ impl CreateRunnableDoctests {
1085
1083
test_type : test:: TestType :: DocTest ,
1086
1084
} ,
1087
1085
testfn : test:: DynTestFn ( Box :: new ( move || {
1088
- doctest_run_fn (
1089
- crate_name,
1090
- rustdoc_test_options,
1091
- opts,
1092
- path,
1093
- test,
1094
- rustdoc_options,
1095
- unused_externs,
1096
- )
1086
+ doctest_run_fn ( rustdoc_test_options, opts, test, rustdoc_options, unused_externs)
1097
1087
} ) ) ,
1098
1088
} ) ;
1099
1089
}
1100
1090
}
1101
1091
1102
1092
fn doctest_run_fn (
1103
- crate_name : String ,
1104
1093
test_opts : IndividualTestOptions ,
1105
1094
global_opts : GlobalTestOptions ,
1106
- path : PathBuf ,
1107
1095
scraped_test : ScrapedDoctest ,
1108
1096
rustdoc_options : Arc < RustdocOptions > ,
1109
1097
unused_externs : Arc < Mutex < Vec < UnusedExterns > > > ,
@@ -1115,15 +1103,13 @@ fn doctest_run_fn(
1115
1103
let edition = scraped_test. edition ( & rustdoc_options) ;
1116
1104
let res = run_test (
1117
1105
& scraped_test. text ,
1118
- & crate_name,
1119
1106
scraped_test. line ,
1120
1107
& rustdoc_options,
1121
1108
test_opts,
1122
1109
scraped_test. langstr ,
1123
1110
no_run,
1124
1111
& global_opts,
1125
1112
edition,
1126
- path,
1127
1113
report_unused_externs,
1128
1114
) ;
1129
1115
0 commit comments