@@ -206,7 +206,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<()
206
206
test_args,
207
207
nocapture,
208
208
opts,
209
- rustdoc_options,
209
+ & rustdoc_options,
210
210
& unused_extern_reports,
211
211
standalone_tests,
212
212
mergeable_tests,
@@ -259,39 +259,43 @@ pub(crate) fn run_tests(
259
259
mut test_args : Vec < String > ,
260
260
nocapture : bool ,
261
261
opts : GlobalTestOptions ,
262
- rustdoc_options : RustdocOptions ,
262
+ rustdoc_options : & Arc < RustdocOptions > ,
263
263
unused_extern_reports : & Arc < Mutex < Vec < UnusedExterns > > > ,
264
264
mut standalone_tests : Vec < test:: TestDescAndFn > ,
265
- mut mergeable_tests : FxHashMap < Edition , Vec < ( DocTest , ScrapedDoctest ) > > ,
265
+ mergeable_tests : FxHashMap < Edition , Vec < ( DocTest , ScrapedDoctest ) > > ,
266
266
) {
267
267
test_args. insert ( 0 , "rustdoctest" . to_string ( ) ) ;
268
268
if nocapture {
269
269
test_args. push ( "--nocapture" . to_string ( ) ) ;
270
270
}
271
271
272
272
let mut nb_errors = 0 ;
273
+ let target_str = rustdoc_options. target . to_string ( ) ;
273
274
274
275
for ( edition, mut doctests) in mergeable_tests {
275
276
if doctests. is_empty ( ) {
276
277
continue ;
277
278
}
278
279
doctests. sort_by ( |( _, a) , ( _, b) | a. name . cmp ( & b. name ) ) ;
279
- let outdir = Arc :: clone ( & doctests[ 0 ] . outdir ) ;
280
280
281
281
let mut tests_runner = runner:: DocTestRunner :: new ( ) ;
282
282
283
283
let rustdoc_test_options = IndividualTestOptions :: new (
284
284
& rustdoc_options,
285
- format ! ( "merged_doctest " ) ,
286
- PathBuf :: from ( r"doctest .rs") ,
285
+ & format ! ( "merged_doctest_{edition} " ) ,
286
+ PathBuf :: from ( format ! ( "doctest_{edition} .rs") ) ,
287
287
) ;
288
288
289
289
for ( doctest, scraped_test) in & doctests {
290
- tests_runner. add_test ( doctest, scraped_test) ;
290
+ tests_runner. add_test ( doctest, scraped_test, & target_str ) ;
291
291
}
292
- if let Ok ( success) =
293
- tests_runner. run_tests ( rustdoc_test_options, edition, & opts, & test_args, & outdir)
294
- {
292
+ if let Ok ( success) = tests_runner. run_tests (
293
+ rustdoc_test_options,
294
+ edition,
295
+ & opts,
296
+ & test_args,
297
+ rustdoc_options,
298
+ ) {
295
299
if !success {
296
300
nb_errors += 1 ;
297
301
}
@@ -311,7 +315,7 @@ pub(crate) fn run_tests(
311
315
doctest,
312
316
scraped_test,
313
317
opts. clone ( ) ,
314
- rustdoc_test_options . clone ( ) ,
318
+ Arc :: clone ( & rustdoc_options ) ,
315
319
unused_extern_reports. clone ( ) ,
316
320
) ) ;
317
321
}
@@ -406,7 +410,7 @@ impl DirState {
406
410
// We could unify this struct the one in rustc but they have different
407
411
// ownership semantics, so doing so would create wasteful allocations.
408
412
#[ derive( serde:: Serialize , serde:: Deserialize ) ]
409
- struct UnusedExterns {
413
+ pub ( crate ) struct UnusedExterns {
410
414
/// Lint level of the unused_crate_dependencies lint
411
415
lint_level : String ,
412
416
/// List of unused externs by their names.
@@ -642,12 +646,11 @@ fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
642
646
}
643
647
struct IndividualTestOptions {
644
648
outdir : DirState ,
645
- test_id : String ,
646
649
path : PathBuf ,
647
650
}
648
651
649
652
impl IndividualTestOptions {
650
- fn new ( options : & RustdocOptions , test_id : String , test_path : PathBuf ) -> Self {
653
+ fn new ( options : & RustdocOptions , test_id : & str , test_path : PathBuf ) -> Self {
651
654
let outdir = if let Some ( ref path) = options. persist_doctests {
652
655
let mut path = path. clone ( ) ;
653
656
path. push ( & test_id) ;
@@ -662,15 +665,14 @@ impl IndividualTestOptions {
662
665
DirState :: Temp ( get_doctest_dir ( ) . expect ( "rustdoc needs a tempdir" ) )
663
666
} ;
664
667
665
- Self { outdir, test_id , path : test_path }
668
+ Self { outdir, path : test_path }
666
669
}
667
670
}
668
671
669
672
/// A doctest scraped from the code, ready to be turned into a runnable test.
670
- struct ScrapedDoctest {
673
+ pub ( crate ) struct ScrapedDoctest {
671
674
filename : FileName ,
672
675
line : usize ,
673
- logical_path : Vec < String > ,
674
676
langstr : LangString ,
675
677
text : String ,
676
678
name : String ,
@@ -692,7 +694,7 @@ impl ScrapedDoctest {
692
694
let name =
693
695
format ! ( "{} - {item_path}(line {line})" , filename. prefer_remapped_unconditionaly( ) ) ;
694
696
695
- Self { filename, line, logical_path , langstr, text, name }
697
+ Self { filename, line, langstr, text, name }
696
698
}
697
699
fn edition ( & self , opts : & RustdocOptions ) -> Edition {
698
700
self . langstr . edition . unwrap_or ( opts. edition )
@@ -701,6 +703,19 @@ impl ScrapedDoctest {
701
703
fn no_run ( & self , opts : & RustdocOptions ) -> bool {
702
704
self . langstr . no_run || opts. no_run
703
705
}
706
+ fn path ( & self ) -> PathBuf {
707
+ match & self . filename {
708
+ FileName :: Real ( path) => {
709
+ if let Some ( local_path) = path. local_path ( ) {
710
+ local_path. to_path_buf ( )
711
+ } else {
712
+ // Somehow we got the filename from the metadata of another crate, should never happen
713
+ unreachable ! ( "doctest from a different crate" ) ;
714
+ }
715
+ }
716
+ _ => PathBuf :: from ( r"doctest.rs" ) ,
717
+ }
718
+ }
704
719
}
705
720
706
721
pub ( crate ) trait DoctestVisitor {
@@ -757,7 +772,7 @@ impl CreateRunnableDoctests {
757
772
758
773
let edition = scraped_test. edition ( & self . rustdoc_options ) ;
759
774
let doctest =
760
- DocTest :: new ( & scraped_test. text , Some ( & self . opts . crate_name ) , edition, test_id) ;
775
+ DocTest :: new ( & scraped_test. text , Some ( & self . opts . crate_name ) , edition, Some ( test_id) ) ;
761
776
let is_standalone = scraped_test. langstr . compile_fail
762
777
|| scraped_test. langstr . test_harness
763
778
|| self . rustdoc_options . nocapture
@@ -784,7 +799,7 @@ impl CreateRunnableDoctests {
784
799
test,
785
800
scraped_test,
786
801
self . opts . clone ( ) ,
787
- self . rustdoc_options . clone ( ) ,
802
+ Arc :: clone ( & self . rustdoc_options ) ,
788
803
self . unused_extern_reports . clone ( ) ,
789
804
)
790
805
}
@@ -794,32 +809,20 @@ fn generate_test_desc_and_fn(
794
809
test : DocTest ,
795
810
scraped_test : ScrapedDoctest ,
796
811
opts : GlobalTestOptions ,
797
- rustdoc_options : IndividualTestOptions ,
812
+ rustdoc_options : Arc < RustdocOptions > ,
798
813
unused_externs : Arc < Mutex < Vec < UnusedExterns > > > ,
799
814
) -> test:: TestDescAndFn {
800
815
let target_str = rustdoc_options. target . to_string ( ) ;
816
+ let rustdoc_test_options = IndividualTestOptions :: new (
817
+ & rustdoc_options,
818
+ test. test_id . as_deref ( ) . unwrap_or_else ( || "<doctest>" ) ,
819
+ scraped_test. path ( ) ,
820
+ ) ;
801
821
802
- let path = match & scraped_test. filename {
803
- FileName :: Real ( path) => {
804
- if let Some ( local_path) = path. local_path ( ) {
805
- local_path. to_path_buf ( )
806
- } else {
807
- // Somehow we got the filename from the metadata of another crate, should never happen
808
- unreachable ! ( "doctest from a different crate" ) ;
809
- }
810
- }
811
- _ => PathBuf :: from ( r"doctest.rs" ) ,
812
- } ;
813
-
814
- let name = & test. name ;
815
- let rustdoc_test_options =
816
- IndividualTestOptions :: new ( & rustdoc_options, test. test_id . clone ( ) , path) ;
817
- // let rustdoc_options_clone = rustdoc_options.clone();
818
-
819
- debug ! ( "creating test {name}: {}" , scraped_test. text) ;
822
+ debug ! ( "creating test {}: {}" , scraped_test. name, scraped_test. text) ;
820
823
test:: TestDescAndFn {
821
824
desc : test:: TestDesc {
822
- name : test:: DynTestName ( name) ,
825
+ name : test:: DynTestName ( scraped_test . name . clone ( ) ) ,
823
826
ignore : match scraped_test. langstr . ignore {
824
827
Ignore :: All => true ,
825
828
Ignore :: None => false ,
0 commit comments