]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctest.rs
Rollup merge of #82802 - jyn514:build-rustdoc-fullmake, r=Mark-Simulacrum
[rust.git] / src / librustdoc / doctest.rs
1 use rustc_ast as ast;
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_data_structures::sync::Lrc;
4 use rustc_errors::{ColorConfig, ErrorReported};
5 use rustc_hir as hir;
6 use rustc_hir::intravisit;
7 use rustc_hir::{HirId, CRATE_HIR_ID};
8 use rustc_interface::interface;
9 use rustc_middle::hir::map::Map;
10 use rustc_middle::ty::TyCtxt;
11 use rustc_session::config::{self, CrateType, ErrorOutputType};
12 use rustc_session::{lint, DiagnosticOutput, Session};
13 use rustc_span::edition::Edition;
14 use rustc_span::source_map::SourceMap;
15 use rustc_span::symbol::sym;
16 use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
17 use rustc_target::spec::TargetTriple;
18 use tempfile::Builder as TempFileBuilder;
19
20 use std::env;
21 use std::io::{self, Write};
22 use std::panic;
23 use std::path::PathBuf;
24 use std::process::{self, Command, Stdio};
25 use std::str;
26
27 use crate::clean::Attributes;
28 use crate::config::Options;
29 use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
30 use crate::lint::init_lints;
31 use crate::passes::span_of_attrs;
32
33 #[derive(Clone, Default)]
34 crate struct TestOptions {
35     /// Whether to disable the default `extern crate my_crate;` when creating doctests.
36     crate no_crate_inject: bool,
37     /// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
38     /// the default `#![allow(unused)]`.
39     crate display_warnings: bool,
40     /// Additional crate-level attributes to add to doctests.
41     crate attrs: Vec<String>,
42 }
43
44 crate fn run(options: Options) -> Result<(), ErrorReported> {
45     let input = config::Input::File(options.input.clone());
46
47     let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
48
49     // See core::create_config for what's going on here.
50     let allowed_lints = vec![
51         invalid_codeblock_attributes_name.to_owned(),
52         lint::builtin::UNKNOWN_LINTS.name.to_owned(),
53         lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_owned(),
54     ];
55
56     let (lint_opts, lint_caps) = init_lints(allowed_lints, options.lint_opts.clone(), |lint| {
57         if lint.name == invalid_codeblock_attributes_name {
58             None
59         } else {
60             Some((lint.name_lower(), lint::Allow))
61         }
62     });
63
64     let crate_types =
65         if options.proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
66
67     let sessopts = config::Options {
68         maybe_sysroot: options.maybe_sysroot.clone(),
69         search_paths: options.libs.clone(),
70         crate_types,
71         lint_opts: if !options.display_warnings { lint_opts } else { vec![] },
72         lint_cap: Some(options.lint_cap.clone().unwrap_or_else(|| lint::Forbid)),
73         cg: options.codegen_options.clone(),
74         externs: options.externs.clone(),
75         unstable_features: options.render_options.unstable_features,
76         actually_rustdoc: true,
77         debugging_opts: config::DebuggingOptions { ..config::basic_debugging_options() },
78         edition: options.edition,
79         target_triple: options.target.clone(),
80         crate_name: options.crate_name.clone(),
81         ..config::Options::default()
82     };
83
84     let mut cfgs = options.cfgs.clone();
85     cfgs.push("doc".to_owned());
86     cfgs.push("doctest".to_owned());
87     let config = interface::Config {
88         opts: sessopts,
89         crate_cfg: interface::parse_cfgspecs(cfgs),
90         input,
91         input_path: None,
92         output_file: None,
93         output_dir: None,
94         file_loader: None,
95         diagnostic_output: DiagnosticOutput::Default,
96         stderr: None,
97         lint_caps,
98         register_lints: Some(box crate::lint::register_lints),
99         override_queries: None,
100         make_codegen_backend: None,
101         registry: rustc_driver::diagnostics_registry(),
102     };
103
104     let mut test_args = options.test_args.clone();
105     let display_warnings = options.display_warnings;
106
107     let tests = interface::run_compiler(config, |compiler| {
108         compiler.enter(|queries| {
109             let lower_to_hir = queries.lower_to_hir()?;
110
111             let mut opts = scrape_test_config(lower_to_hir.peek().0);
112             opts.display_warnings |= options.display_warnings;
113             let enable_per_target_ignores = options.enable_per_target_ignores;
114             let mut collector = Collector::new(
115                 queries.crate_name()?.peek().to_string(),
116                 options,
117                 false,
118                 opts,
119                 Some(compiler.session().parse_sess.clone_source_map()),
120                 None,
121                 enable_per_target_ignores,
122             );
123
124             let mut global_ctxt = queries.global_ctxt()?.take();
125
126             global_ctxt.enter(|tcx| {
127                 let krate = tcx.hir().krate();
128
129                 let mut hir_collector = HirCollector {
130                     sess: compiler.session(),
131                     collector: &mut collector,
132                     map: tcx.hir(),
133                     codes: ErrorCodes::from(
134                         compiler.session().opts.unstable_features.is_nightly_build(),
135                     ),
136                     tcx,
137                 };
138                 hir_collector.visit_testable(
139                     "".to_string(),
140                     &krate.item.attrs,
141                     CRATE_HIR_ID,
142                     krate.item.span,
143                     |this| {
144                         intravisit::walk_crate(this, krate);
145                     },
146                 );
147             });
148             compiler.session().abort_if_errors();
149
150             let ret: Result<_, ErrorReported> = Ok(collector.tests);
151             ret
152         })
153     });
154     let tests = match tests {
155         Ok(tests) => tests,
156         Err(ErrorReported) => return Err(ErrorReported),
157     };
158
159     test_args.insert(0, "rustdoctest".to_string());
160
161     testing::test_main(
162         &test_args,
163         tests,
164         Some(testing::Options::new().display_output(display_warnings)),
165     );
166
167     Ok(())
168 }
169
170 // Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
171 fn scrape_test_config(krate: &::rustc_hir::Crate<'_>) -> TestOptions {
172     use rustc_ast_pretty::pprust;
173
174     let mut opts =
175         TestOptions { no_crate_inject: false, display_warnings: false, attrs: Vec::new() };
176
177     let test_attrs: Vec<_> = krate
178         .item
179         .attrs
180         .iter()
181         .filter(|a| a.has_name(sym::doc))
182         .flat_map(|a| a.meta_item_list().unwrap_or_else(Vec::new))
183         .filter(|a| a.has_name(sym::test))
184         .collect();
185     let attrs = test_attrs.iter().flat_map(|a| a.meta_item_list().unwrap_or(&[]));
186
187     for attr in attrs {
188         if attr.has_name(sym::no_crate_inject) {
189             opts.no_crate_inject = true;
190         }
191         if attr.has_name(sym::attr) {
192             if let Some(l) = attr.meta_item_list() {
193                 for item in l {
194                     opts.attrs.push(pprust::meta_list_item_to_string(item));
195                 }
196             }
197         }
198     }
199
200     opts
201 }
202
203 /// Documentation test failure modes.
204 enum TestFailure {
205     /// The test failed to compile.
206     CompileError,
207     /// The test is marked `compile_fail` but compiled successfully.
208     UnexpectedCompilePass,
209     /// The test failed to compile (as expected) but the compiler output did not contain all
210     /// expected error codes.
211     MissingErrorCodes(Vec<String>),
212     /// The test binary was unable to be executed.
213     ExecutionError(io::Error),
214     /// The test binary exited with a non-zero exit code.
215     ///
216     /// This typically means an assertion in the test failed or another form of panic occurred.
217     ExecutionFailure(process::Output),
218     /// The test is marked `should_panic` but the test binary executed successfully.
219     UnexpectedRunPass,
220 }
221
222 enum DirState {
223     Temp(tempfile::TempDir),
224     Perm(PathBuf),
225 }
226
227 impl DirState {
228     fn path(&self) -> &std::path::Path {
229         match self {
230             DirState::Temp(t) => t.path(),
231             DirState::Perm(p) => p.as_path(),
232         }
233     }
234 }
235
236 fn run_test(
237     test: &str,
238     cratename: &str,
239     line: usize,
240     options: Options,
241     should_panic: bool,
242     no_run: bool,
243     as_test_harness: bool,
244     runtool: Option<String>,
245     runtool_args: Vec<String>,
246     target: TargetTriple,
247     compile_fail: bool,
248     mut error_codes: Vec<String>,
249     opts: &TestOptions,
250     edition: Edition,
251     outdir: DirState,
252     path: PathBuf,
253     test_id: &str,
254 ) -> Result<(), TestFailure> {
255     let (test, line_offset, supports_color) =
256         make_test(test, Some(cratename), as_test_harness, opts, edition, Some(test_id));
257
258     let output_file = outdir.path().join("rust_out");
259
260     let rustc_binary = options
261         .test_builder
262         .as_deref()
263         .unwrap_or_else(|| rustc_interface::util::rustc_path().expect("found rustc"));
264     let mut compiler = Command::new(&rustc_binary);
265     compiler.arg("--crate-type").arg("bin");
266     for cfg in &options.cfgs {
267         compiler.arg("--cfg").arg(&cfg);
268     }
269     if let Some(sysroot) = options.maybe_sysroot {
270         compiler.arg("--sysroot").arg(sysroot);
271     }
272     compiler.arg("--edition").arg(&edition.to_string());
273     compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", path);
274     compiler.env("UNSTABLE_RUSTDOC_TEST_LINE", format!("{}", line as isize - line_offset as isize));
275     compiler.arg("-o").arg(&output_file);
276     if as_test_harness {
277         compiler.arg("--test");
278     }
279     for lib_str in &options.lib_strs {
280         compiler.arg("-L").arg(&lib_str);
281     }
282     for extern_str in &options.extern_strs {
283         compiler.arg("--extern").arg(&extern_str);
284     }
285     compiler.arg("-Ccodegen-units=1");
286     for codegen_options_str in &options.codegen_options_strs {
287         compiler.arg("-C").arg(&codegen_options_str);
288     }
289     for debugging_option_str in &options.debugging_opts_strs {
290         compiler.arg("-Z").arg(&debugging_option_str);
291     }
292     if no_run && !compile_fail {
293         compiler.arg("--emit=metadata");
294     }
295     compiler.arg("--target").arg(match target {
296         TargetTriple::TargetTriple(s) => s,
297         TargetTriple::TargetPath(path) => {
298             path.to_str().expect("target path must be valid unicode").to_string()
299         }
300     });
301     if let ErrorOutputType::HumanReadable(kind) = options.error_format {
302         let (short, color_config) = kind.unzip();
303
304         if short {
305             compiler.arg("--error-format").arg("short");
306         }
307
308         match color_config {
309             ColorConfig::Never => {
310                 compiler.arg("--color").arg("never");
311             }
312             ColorConfig::Always => {
313                 compiler.arg("--color").arg("always");
314             }
315             ColorConfig::Auto => {
316                 compiler.arg("--color").arg(if supports_color { "always" } else { "never" });
317             }
318         }
319     }
320
321     compiler.arg("-");
322     compiler.stdin(Stdio::piped());
323     compiler.stderr(Stdio::piped());
324
325     let mut child = compiler.spawn().expect("Failed to spawn rustc process");
326     {
327         let stdin = child.stdin.as_mut().expect("Failed to open stdin");
328         stdin.write_all(test.as_bytes()).expect("could write out test sources");
329     }
330     let output = child.wait_with_output().expect("Failed to read stdout");
331
332     struct Bomb<'a>(&'a str);
333     impl Drop for Bomb<'_> {
334         fn drop(&mut self) {
335             eprint!("{}", self.0);
336         }
337     }
338     let out = str::from_utf8(&output.stderr).unwrap();
339     let _bomb = Bomb(&out);
340     match (output.status.success(), compile_fail) {
341         (true, true) => {
342             return Err(TestFailure::UnexpectedCompilePass);
343         }
344         (true, false) => {}
345         (false, true) => {
346             if !error_codes.is_empty() {
347                 // We used to check if the output contained "error[{}]: " but since we added the
348                 // colored output, we can't anymore because of the color escape characters before
349                 // the ":".
350                 error_codes.retain(|err| !out.contains(&format!("error[{}]", err)));
351
352                 if !error_codes.is_empty() {
353                     return Err(TestFailure::MissingErrorCodes(error_codes));
354                 }
355             }
356         }
357         (false, false) => {
358             return Err(TestFailure::CompileError);
359         }
360     }
361
362     if no_run {
363         return Ok(());
364     }
365
366     // Run the code!
367     let mut cmd;
368
369     if let Some(tool) = runtool {
370         cmd = Command::new(tool);
371         cmd.args(runtool_args);
372         cmd.arg(output_file);
373     } else {
374         cmd = Command::new(output_file);
375     }
376     if let Some(run_directory) = options.test_run_directory {
377         cmd.current_dir(run_directory);
378     }
379
380     match cmd.output() {
381         Err(e) => return Err(TestFailure::ExecutionError(e)),
382         Ok(out) => {
383             if should_panic && out.status.success() {
384                 return Err(TestFailure::UnexpectedRunPass);
385             } else if !should_panic && !out.status.success() {
386                 return Err(TestFailure::ExecutionFailure(out));
387             }
388         }
389     }
390
391     Ok(())
392 }
393
394 /// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
395 /// lines before the test code begins as well as if the output stream supports colors or not.
396 crate fn make_test(
397     s: &str,
398     cratename: Option<&str>,
399     dont_insert_main: bool,
400     opts: &TestOptions,
401     edition: Edition,
402     test_id: Option<&str>,
403 ) -> (String, usize, bool) {
404     let (crate_attrs, everything_else, crates) = partition_source(s);
405     let everything_else = everything_else.trim();
406     let mut line_offset = 0;
407     let mut prog = String::new();
408     let mut supports_color = false;
409
410     if opts.attrs.is_empty() && !opts.display_warnings {
411         // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
412         // lints that are commonly triggered in doctests. The crate-level test attributes are
413         // commonly used to make tests fail in case they trigger warnings, so having this there in
414         // that case may cause some tests to pass when they shouldn't have.
415         prog.push_str("#![allow(unused)]\n");
416         line_offset += 1;
417     }
418
419     // Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
420     for attr in &opts.attrs {
421         prog.push_str(&format!("#![{}]\n", attr));
422         line_offset += 1;
423     }
424
425     // Now push any outer attributes from the example, assuming they
426     // are intended to be crate attributes.
427     prog.push_str(&crate_attrs);
428     prog.push_str(&crates);
429
430     // Uses librustc_ast to parse the doctest and find if there's a main fn and the extern
431     // crate already is included.
432     let result = rustc_driver::catch_fatal_errors(|| {
433         rustc_span::with_session_globals(edition, || {
434             use rustc_errors::emitter::{Emitter, EmitterWriter};
435             use rustc_errors::Handler;
436             use rustc_parse::maybe_new_parser_from_source_str;
437             use rustc_parse::parser::ForceCollect;
438             use rustc_session::parse::ParseSess;
439             use rustc_span::source_map::FilePathMapping;
440
441             let filename = FileName::anon_source_code(s);
442             let source = crates + everything_else;
443
444             // Any errors in parsing should also appear when the doctest is compiled for real, so just
445             // send all the errors that librustc_ast emits directly into a `Sink` instead of stderr.
446             let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
447             supports_color =
448                 EmitterWriter::stderr(ColorConfig::Auto, None, false, false, Some(80), false)
449                     .supports_color();
450
451             let emitter =
452                 EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
453
454             // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
455             let handler = Handler::with_emitter(false, None, box emitter);
456             let sess = ParseSess::with_span_handler(handler, sm);
457
458             let mut found_main = false;
459             let mut found_extern_crate = cratename.is_none();
460             let mut found_macro = false;
461
462             let mut parser = match maybe_new_parser_from_source_str(&sess, filename, source) {
463                 Ok(p) => p,
464                 Err(errs) => {
465                     for mut err in errs {
466                         err.cancel();
467                     }
468
469                     return (found_main, found_extern_crate, found_macro);
470                 }
471             };
472
473             loop {
474                 match parser.parse_item(ForceCollect::No) {
475                     Ok(Some(item)) => {
476                         if !found_main {
477                             if let ast::ItemKind::Fn(..) = item.kind {
478                                 if item.ident.name == sym::main {
479                                     found_main = true;
480                                 }
481                             }
482                         }
483
484                         if !found_extern_crate {
485                             if let ast::ItemKind::ExternCrate(original) = item.kind {
486                                 // This code will never be reached if `cratename` is none because
487                                 // `found_extern_crate` is initialized to `true` if it is none.
488                                 let cratename = cratename.unwrap();
489
490                                 match original {
491                                     Some(name) => found_extern_crate = name.as_str() == cratename,
492                                     None => found_extern_crate = item.ident.as_str() == cratename,
493                                 }
494                             }
495                         }
496
497                         if !found_macro {
498                             if let ast::ItemKind::MacCall(..) = item.kind {
499                                 found_macro = true;
500                             }
501                         }
502
503                         if found_main && found_extern_crate {
504                             break;
505                         }
506                     }
507                     Ok(None) => break,
508                     Err(mut e) => {
509                         e.cancel();
510                         break;
511                     }
512                 }
513             }
514
515             // Reset errors so that they won't be reported as compiler bugs when dropping the
516             // handler. Any errors in the tests will be reported when the test file is compiled,
517             // Note that we still need to cancel the errors above otherwise `DiagnosticBuilder`
518             // will panic on drop.
519             sess.span_diagnostic.reset_err_count();
520
521             (found_main, found_extern_crate, found_macro)
522         })
523     });
524     let (already_has_main, already_has_extern_crate, found_macro) = match result {
525         Ok(result) => result,
526         Err(ErrorReported) => {
527             // If the parser panicked due to a fatal error, pass the test code through unchanged.
528             // The error will be reported during compilation.
529             return (s.to_owned(), 0, false);
530         }
531     };
532
533     // If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't
534     // see it. In that case, run the old text-based scan to see if they at least have a main
535     // function written inside a macro invocation. See
536     // https://github.com/rust-lang/rust/issues/56898
537     let already_has_main = if found_macro && !already_has_main {
538         s.lines()
539             .map(|line| {
540                 let comment = line.find("//");
541                 if let Some(comment_begins) = comment { &line[0..comment_begins] } else { line }
542             })
543             .any(|code| code.contains("fn main"))
544     } else {
545         already_has_main
546     };
547
548     // Don't inject `extern crate std` because it's already injected by the
549     // compiler.
550     if !already_has_extern_crate && !opts.no_crate_inject && cratename != Some("std") {
551         if let Some(cratename) = cratename {
552             // Don't inject `extern crate` if the crate is never used.
553             // NOTE: this is terribly inaccurate because it doesn't actually
554             // parse the source, but only has false positives, not false
555             // negatives.
556             if s.contains(cratename) {
557                 prog.push_str(&format!("extern crate r#{};\n", cratename));
558                 line_offset += 1;
559             }
560         }
561     }
562
563     // FIXME: This code cannot yet handle no_std test cases yet
564     if dont_insert_main || already_has_main || prog.contains("![no_std]") {
565         prog.push_str(everything_else);
566     } else {
567         let returns_result = everything_else.trim_end().ends_with("(())");
568         // Give each doctest main function a unique name.
569         // This is for example needed for the tooling around `-Z instrument-coverage`.
570         let inner_fn_name = if let Some(test_id) = test_id {
571             format!("_doctest_main_{}", test_id)
572         } else {
573             "_inner".into()
574         };
575         let inner_attr = if test_id.is_some() { "#[allow(non_snake_case)] " } else { "" };
576         let (main_pre, main_post) = if returns_result {
577             (
578                 format!(
579                     "fn main() {{ {}fn {}() -> Result<(), impl core::fmt::Debug> {{\n",
580                     inner_attr, inner_fn_name
581                 ),
582                 format!("\n}} {}().unwrap() }}", inner_fn_name),
583             )
584         } else if test_id.is_some() {
585             (
586                 format!("fn main() {{ {}fn {}() {{\n", inner_attr, inner_fn_name),
587                 format!("\n}} {}() }}", inner_fn_name),
588             )
589         } else {
590             ("fn main() {\n".into(), "\n}".into())
591         };
592         // Note on newlines: We insert a line/newline *before*, and *after*
593         // the doctest and adjust the `line_offset` accordingly.
594         // In the case of `-Z instrument-coverage`, this means that the generated
595         // inner `main` function spans from the doctest opening codeblock to the
596         // closing one. For example
597         // /// ``` <- start of the inner main
598         // /// <- code under doctest
599         // /// ``` <- end of the inner main
600         line_offset += 1;
601
602         prog.extend([&main_pre, everything_else, &main_post].iter().cloned());
603     }
604
605     debug!("final doctest:\n{}", prog);
606
607     (prog, line_offset, supports_color)
608 }
609
610 // FIXME(aburka): use a real parser to deal with multiline attributes
611 fn partition_source(s: &str) -> (String, String, String) {
612     #[derive(Copy, Clone, PartialEq)]
613     enum PartitionState {
614         Attrs,
615         Crates,
616         Other,
617     }
618     let mut state = PartitionState::Attrs;
619     let mut before = String::new();
620     let mut crates = String::new();
621     let mut after = String::new();
622
623     for line in s.lines() {
624         let trimline = line.trim();
625
626         // FIXME(misdreavus): if a doc comment is placed on an extern crate statement, it will be
627         // shunted into "everything else"
628         match state {
629             PartitionState::Attrs => {
630                 state = if trimline.starts_with("#![")
631                     || trimline.chars().all(|c| c.is_whitespace())
632                     || (trimline.starts_with("//") && !trimline.starts_with("///"))
633                 {
634                     PartitionState::Attrs
635                 } else if trimline.starts_with("extern crate")
636                     || trimline.starts_with("#[macro_use] extern crate")
637                 {
638                     PartitionState::Crates
639                 } else {
640                     PartitionState::Other
641                 };
642             }
643             PartitionState::Crates => {
644                 state = if trimline.starts_with("extern crate")
645                     || trimline.starts_with("#[macro_use] extern crate")
646                     || trimline.chars().all(|c| c.is_whitespace())
647                     || (trimline.starts_with("//") && !trimline.starts_with("///"))
648                 {
649                     PartitionState::Crates
650                 } else {
651                     PartitionState::Other
652                 };
653             }
654             PartitionState::Other => {}
655         }
656
657         match state {
658             PartitionState::Attrs => {
659                 before.push_str(line);
660                 before.push('\n');
661             }
662             PartitionState::Crates => {
663                 crates.push_str(line);
664                 crates.push('\n');
665             }
666             PartitionState::Other => {
667                 after.push_str(line);
668                 after.push('\n');
669             }
670         }
671     }
672
673     debug!("before:\n{}", before);
674     debug!("crates:\n{}", crates);
675     debug!("after:\n{}", after);
676
677     (before, after, crates)
678 }
679
680 crate trait Tester {
681     fn add_test(&mut self, test: String, config: LangString, line: usize);
682     fn get_line(&self) -> usize {
683         0
684     }
685     fn register_header(&mut self, _name: &str, _level: u32) {}
686 }
687
688 crate struct Collector {
689     crate tests: Vec<testing::TestDescAndFn>,
690
691     // The name of the test displayed to the user, separated by `::`.
692     //
693     // In tests from Rust source, this is the path to the item
694     // e.g., `["std", "vec", "Vec", "push"]`.
695     //
696     // In tests from a markdown file, this is the titles of all headers (h1~h6)
697     // of the sections that contain the code block, e.g., if the markdown file is
698     // written as:
699     //
700     // ``````markdown
701     // # Title
702     //
703     // ## Subtitle
704     //
705     // ```rust
706     // assert!(true);
707     // ```
708     // ``````
709     //
710     // the `names` vector of that test will be `["Title", "Subtitle"]`.
711     names: Vec<String>,
712
713     options: Options,
714     use_headers: bool,
715     enable_per_target_ignores: bool,
716     cratename: String,
717     opts: TestOptions,
718     position: Span,
719     source_map: Option<Lrc<SourceMap>>,
720     filename: Option<PathBuf>,
721     visited_tests: FxHashMap<(String, usize), usize>,
722 }
723
724 impl Collector {
725     crate fn new(
726         cratename: String,
727         options: Options,
728         use_headers: bool,
729         opts: TestOptions,
730         source_map: Option<Lrc<SourceMap>>,
731         filename: Option<PathBuf>,
732         enable_per_target_ignores: bool,
733     ) -> Collector {
734         Collector {
735             tests: Vec::new(),
736             names: Vec::new(),
737             options,
738             use_headers,
739             enable_per_target_ignores,
740             cratename,
741             opts,
742             position: DUMMY_SP,
743             source_map,
744             filename,
745             visited_tests: FxHashMap::default(),
746         }
747     }
748
749     fn generate_name(&self, line: usize, filename: &FileName) -> String {
750         let mut item_path = self.names.join("::");
751         if !item_path.is_empty() {
752             item_path.push(' ');
753         }
754         format!("{} - {}(line {})", filename, item_path, line)
755     }
756
757     crate fn set_position(&mut self, position: Span) {
758         self.position = position;
759     }
760
761     fn get_filename(&self) -> FileName {
762         if let Some(ref source_map) = self.source_map {
763             let filename = source_map.span_to_filename(self.position);
764             if let FileName::Real(ref filename) = filename {
765                 if let Ok(cur_dir) = env::current_dir() {
766                     if let Ok(path) = filename.local_path().strip_prefix(&cur_dir) {
767                         return path.to_owned().into();
768                     }
769                 }
770             }
771             filename
772         } else if let Some(ref filename) = self.filename {
773             filename.clone().into()
774         } else {
775             FileName::Custom("input".to_owned())
776         }
777     }
778 }
779
780 impl Tester for Collector {
781     fn add_test(&mut self, test: String, config: LangString, line: usize) {
782         let filename = self.get_filename();
783         let name = self.generate_name(line, &filename);
784         let cratename = self.cratename.to_string();
785         let opts = self.opts.clone();
786         let edition = config.edition.unwrap_or(self.options.edition);
787         let options = self.options.clone();
788         let runtool = self.options.runtool.clone();
789         let runtool_args = self.options.runtool_args.clone();
790         let target = self.options.target.clone();
791         let target_str = target.to_string();
792
793         // FIXME(#44940): if doctests ever support path remapping, then this filename
794         // needs to be the result of `SourceMap::span_to_unmapped_path`.
795         let path = match &filename {
796             FileName::Real(path) => path.local_path().to_path_buf(),
797             _ => PathBuf::from(r"doctest.rs"),
798         };
799
800         // For example `module/file.rs` would become `module_file_rs`
801         let file = filename
802             .to_string()
803             .chars()
804             .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
805             .collect::<String>();
806         let test_id = format!(
807             "{file}_{line}_{number}",
808             file = file,
809             line = line,
810             number = {
811                 // Increases the current test number, if this file already
812                 // exists or it creates a new entry with a test number of 0.
813                 self.visited_tests.entry((file.clone(), line)).and_modify(|v| *v += 1).or_insert(0)
814             },
815         );
816         let outdir = if let Some(mut path) = options.persist_doctests.clone() {
817             path.push(&test_id);
818
819             std::fs::create_dir_all(&path)
820                 .expect("Couldn't create directory for doctest executables");
821
822             DirState::Perm(path)
823         } else {
824             DirState::Temp(
825                 TempFileBuilder::new()
826                     .prefix("rustdoctest")
827                     .tempdir()
828                     .expect("rustdoc needs a tempdir"),
829             )
830         };
831
832         debug!("creating test {}: {}", name, test);
833         self.tests.push(testing::TestDescAndFn {
834             desc: testing::TestDesc {
835                 name: testing::DynTestName(name),
836                 ignore: match config.ignore {
837                     Ignore::All => true,
838                     Ignore::None => false,
839                     Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
840                 },
841                 // compiler failures are test failures
842                 should_panic: testing::ShouldPanic::No,
843                 allow_fail: config.allow_fail,
844                 test_type: testing::TestType::DocTest,
845             },
846             testfn: testing::DynTestFn(box move || {
847                 let res = run_test(
848                     &test,
849                     &cratename,
850                     line,
851                     options,
852                     config.should_panic,
853                     config.no_run,
854                     config.test_harness,
855                     runtool,
856                     runtool_args,
857                     target,
858                     config.compile_fail,
859                     config.error_codes,
860                     &opts,
861                     edition,
862                     outdir,
863                     path,
864                     &test_id,
865                 );
866
867                 if let Err(err) = res {
868                     match err {
869                         TestFailure::CompileError => {
870                             eprint!("Couldn't compile the test.");
871                         }
872                         TestFailure::UnexpectedCompilePass => {
873                             eprint!("Test compiled successfully, but it's marked `compile_fail`.");
874                         }
875                         TestFailure::UnexpectedRunPass => {
876                             eprint!("Test executable succeeded, but it's marked `should_panic`.");
877                         }
878                         TestFailure::MissingErrorCodes(codes) => {
879                             eprint!("Some expected error codes were not found: {:?}", codes);
880                         }
881                         TestFailure::ExecutionError(err) => {
882                             eprint!("Couldn't run the test: {}", err);
883                             if err.kind() == io::ErrorKind::PermissionDenied {
884                                 eprint!(" - maybe your tempdir is mounted with noexec?");
885                             }
886                         }
887                         TestFailure::ExecutionFailure(out) => {
888                             let reason = if let Some(code) = out.status.code() {
889                                 format!("exit code {}", code)
890                             } else {
891                                 String::from("terminated by signal")
892                             };
893
894                             eprintln!("Test executable failed ({}).", reason);
895
896                             // FIXME(#12309): An unfortunate side-effect of capturing the test
897                             // executable's output is that the relative ordering between the test's
898                             // stdout and stderr is lost. However, this is better than the
899                             // alternative: if the test executable inherited the parent's I/O
900                             // handles the output wouldn't be captured at all, even on success.
901                             //
902                             // The ordering could be preserved if the test process' stderr was
903                             // redirected to stdout, but that functionality does not exist in the
904                             // standard library, so it may not be portable enough.
905                             let stdout = str::from_utf8(&out.stdout).unwrap_or_default();
906                             let stderr = str::from_utf8(&out.stderr).unwrap_or_default();
907
908                             if !stdout.is_empty() || !stderr.is_empty() {
909                                 eprintln!();
910
911                                 if !stdout.is_empty() {
912                                     eprintln!("stdout:\n{}", stdout);
913                                 }
914
915                                 if !stderr.is_empty() {
916                                     eprintln!("stderr:\n{}", stderr);
917                                 }
918                             }
919                         }
920                     }
921
922                     panic::resume_unwind(box ());
923                 }
924             }),
925         });
926     }
927
928     fn get_line(&self) -> usize {
929         if let Some(ref source_map) = self.source_map {
930             let line = self.position.lo().to_usize();
931             let line = source_map.lookup_char_pos(BytePos(line as u32)).line;
932             if line > 0 { line - 1 } else { line }
933         } else {
934             0
935         }
936     }
937
938     fn register_header(&mut self, name: &str, level: u32) {
939         if self.use_headers {
940             // We use these headings as test names, so it's good if
941             // they're valid identifiers.
942             let name = name
943                 .chars()
944                 .enumerate()
945                 .map(|(i, c)| {
946                     if (i == 0 && rustc_lexer::is_id_start(c))
947                         || (i != 0 && rustc_lexer::is_id_continue(c))
948                     {
949                         c
950                     } else {
951                         '_'
952                     }
953                 })
954                 .collect::<String>();
955
956             // Here we try to efficiently assemble the header titles into the
957             // test name in the form of `h1::h2::h3::h4::h5::h6`.
958             //
959             // Suppose that originally `self.names` contains `[h1, h2, h3]`...
960             let level = level as usize;
961             if level <= self.names.len() {
962                 // ... Consider `level == 2`. All headers in the lower levels
963                 // are irrelevant in this new level. So we should reset
964                 // `self.names` to contain headers until <h2>, and replace that
965                 // slot with the new name: `[h1, name]`.
966                 self.names.truncate(level);
967                 self.names[level - 1] = name;
968             } else {
969                 // ... On the other hand, consider `level == 5`. This means we
970                 // need to extend `self.names` to contain five headers. We fill
971                 // in the missing level (<h4>) with `_`. Thus `self.names` will
972                 // become `[h1, h2, h3, "_", name]`.
973                 if level - 1 > self.names.len() {
974                     self.names.resize(level - 1, "_".to_owned());
975                 }
976                 self.names.push(name);
977             }
978         }
979     }
980 }
981
982 struct HirCollector<'a, 'hir, 'tcx> {
983     sess: &'a Session,
984     collector: &'a mut Collector,
985     map: Map<'hir>,
986     codes: ErrorCodes,
987     tcx: TyCtxt<'tcx>,
988 }
989
990 impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
991     fn visit_testable<F: FnOnce(&mut Self)>(
992         &mut self,
993         name: String,
994         attrs: &[ast::Attribute],
995         hir_id: HirId,
996         sp: Span,
997         nested: F,
998     ) {
999         let mut attrs = Attributes::from_ast(self.sess.diagnostic(), attrs, None);
1000         if let Some(ref cfg) = attrs.cfg {
1001             if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
1002                 return;
1003             }
1004         }
1005
1006         let has_name = !name.is_empty();
1007         if has_name {
1008             self.collector.names.push(name);
1009         }
1010
1011         attrs.unindent_doc_comments();
1012         // The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
1013         // anything else, this will combine them for us.
1014         if let Some(doc) = attrs.collapsed_doc_value() {
1015             // Use the outermost invocation, so that doctest names come from where the docs were written.
1016             let span = attrs
1017                 .span
1018                 .map(|span| span.ctxt().outer_expn().expansion_cause().unwrap_or(span))
1019                 .unwrap_or(DUMMY_SP);
1020             self.collector.set_position(span);
1021             markdown::find_testable_code(
1022                 &doc,
1023                 self.collector,
1024                 self.codes,
1025                 self.collector.enable_per_target_ignores,
1026                 Some(&crate::html::markdown::ExtraInfo::new(
1027                     self.tcx,
1028                     hir_id,
1029                     span_of_attrs(&attrs).unwrap_or(sp),
1030                 )),
1031             );
1032         }
1033
1034         nested(self);
1035
1036         if has_name {
1037             self.collector.names.pop();
1038         }
1039     }
1040 }
1041
1042 impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> {
1043     type Map = Map<'hir>;
1044
1045     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1046         intravisit::NestedVisitorMap::All(self.map)
1047     }
1048
1049     fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
1050         let name = if let hir::ItemKind::Impl(impl_) = &item.kind {
1051             rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
1052         } else {
1053             item.ident.to_string()
1054         };
1055
1056         self.visit_testable(name, &item.attrs, item.hir_id(), item.span, |this| {
1057             intravisit::walk_item(this, item);
1058         });
1059     }
1060
1061     fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
1062         self.visit_testable(
1063             item.ident.to_string(),
1064             &item.attrs,
1065             item.hir_id(),
1066             item.span,
1067             |this| {
1068                 intravisit::walk_trait_item(this, item);
1069             },
1070         );
1071     }
1072
1073     fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
1074         self.visit_testable(
1075             item.ident.to_string(),
1076             &item.attrs,
1077             item.hir_id(),
1078             item.span,
1079             |this| {
1080                 intravisit::walk_impl_item(this, item);
1081             },
1082         );
1083     }
1084
1085     fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
1086         self.visit_testable(
1087             item.ident.to_string(),
1088             &item.attrs,
1089             item.hir_id(),
1090             item.span,
1091             |this| {
1092                 intravisit::walk_foreign_item(this, item);
1093             },
1094         );
1095     }
1096
1097     fn visit_variant(
1098         &mut self,
1099         v: &'hir hir::Variant<'_>,
1100         g: &'hir hir::Generics<'_>,
1101         item_id: hir::HirId,
1102     ) {
1103         self.visit_testable(v.ident.to_string(), &v.attrs, v.id, v.span, |this| {
1104             intravisit::walk_variant(this, v, g, item_id);
1105         });
1106     }
1107
1108     fn visit_struct_field(&mut self, f: &'hir hir::StructField<'_>) {
1109         self.visit_testable(f.ident.to_string(), &f.attrs, f.hir_id, f.span, |this| {
1110             intravisit::walk_struct_field(this, f);
1111         });
1112     }
1113
1114     fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef<'_>) {
1115         self.visit_testable(
1116             macro_def.ident.to_string(),
1117             &macro_def.attrs,
1118             macro_def.hir_id(),
1119             macro_def.span,
1120             |_| (),
1121         );
1122     }
1123 }
1124
1125 #[cfg(test)]
1126 mod tests;