1use std::borrow::Cow;
2use std::collections::{HashMap, HashSet};
3use std::ffi::OsString;
4use std::fs::{self, File, create_dir_all};
5use std::hash::{DefaultHasher, Hash, Hasher};
6use std::io::prelude::*;
7use std::io::{self, BufReader};
8use std::process::{Child, Command, ExitStatus, Output, Stdio};
9use std::sync::Arc;
10use std::{env, iter, str};
11
12use build_helper::fs::remove_and_create_dir_all;
13use camino::{Utf8Path, Utf8PathBuf};
14use colored::Colorize;
15use regex::{Captures, Regex};
16use tracing::*;
17
18use crate::common::{
19 Assembly, Codegen, CodegenUnits, CompareMode, Config, CoverageMap, CoverageRun, Crashes,
20 DebugInfo, Debugger, FailMode, Incremental, MirOpt, PassMode, Pretty, RunMake, Rustdoc,
21 RustdocJs, RustdocJson, TestPaths, UI_EXTENSIONS, UI_FIXED, UI_RUN_STDERR, UI_RUN_STDOUT,
22 UI_STDERR, UI_STDOUT, UI_SVG, UI_WINDOWS_SVG, Ui, expected_output_path, incremental_dir,
23 output_base_dir, output_base_name, output_testname_unique,
24};
25use crate::compute_diff::{DiffLine, make_diff, write_diff, write_filtered_diff};
26use crate::errors::{Error, ErrorKind, load_errors};
27use crate::header::TestProps;
28use crate::read2::{Truncated, read2_abbreviated};
29use crate::util::{Utf8PathBufExt, add_dylib_path, logv, static_regex};
30use crate::{ColorConfig, json, stamp_file_path};
31
32mod debugger;
33
34mod assembly;
37mod codegen;
38mod codegen_units;
39mod coverage;
40mod crashes;
41mod debuginfo;
42mod incremental;
43mod js_doc;
44mod mir_opt;
45mod pretty;
46mod run_make;
47mod rustdoc;
48mod rustdoc_json;
49mod ui;
50#[cfg(test)]
53mod tests;
54
55const FAKE_SRC_BASE: &str = "fake-test-src-base";
56
57#[cfg(windows)]
58fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
59 use std::sync::Mutex;
60
61 use windows::Win32::System::Diagnostics::Debug::{
62 SEM_FAILCRITICALERRORS, SEM_NOGPFAULTERRORBOX, SetErrorMode,
63 };
64
65 static LOCK: Mutex<()> = Mutex::new(());
66
67 let _lock = LOCK.lock().unwrap();
69
70 unsafe {
81 let old_mode = SetErrorMode(SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS);
83 SetErrorMode(old_mode | SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS);
84 let r = f();
85 SetErrorMode(old_mode);
86 r
87 }
88}
89
90#[cfg(not(windows))]
91fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
92 f()
93}
94
95fn get_lib_name(name: &str, aux_type: AuxType) -> Option<String> {
97 match aux_type {
98 AuxType::Bin => None,
99 AuxType::Lib => Some(format!("lib{name}.rlib")),
104 AuxType::Dylib | AuxType::ProcMacro => Some(dylib_name(name)),
105 }
106}
107
108fn dylib_name(name: &str) -> String {
109 format!("{}{name}.{}", std::env::consts::DLL_PREFIX, std::env::consts::DLL_EXTENSION)
110}
111
112pub fn run(config: Arc<Config>, testpaths: &TestPaths, revision: Option<&str>) {
113 match &*config.target {
114 "arm-linux-androideabi"
115 | "armv7-linux-androideabi"
116 | "thumbv7neon-linux-androideabi"
117 | "aarch64-linux-android" => {
118 if !config.adb_device_status {
119 panic!("android device not available");
120 }
121 }
122
123 _ => {
124 if config.debugger == Some(Debugger::Gdb) && config.gdb.is_none() {
126 panic!("gdb not available but debuginfo gdb debuginfo test requested");
127 }
128 }
129 }
130
131 if config.verbose {
132 print!("\n\n");
134 }
135 debug!("running {}", testpaths.file);
136 let mut props = TestProps::from_file(&testpaths.file, revision, &config);
137
138 if props.incremental {
142 props.incremental_dir = Some(incremental_dir(&config, testpaths, revision));
143 }
144
145 let cx = TestCx { config: &config, props: &props, testpaths, revision };
146
147 if let Err(e) = create_dir_all(&cx.output_base_dir()) {
148 panic!("failed to create output base directory {}: {e}", cx.output_base_dir());
149 }
150
151 if props.incremental {
152 cx.init_incremental_test();
153 }
154
155 if config.mode == Incremental {
156 assert!(!props.revisions.is_empty(), "Incremental tests require revisions.");
159 for revision in &props.revisions {
160 let mut revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
161 revision_props.incremental_dir = props.incremental_dir.clone();
162 let rev_cx = TestCx {
163 config: &config,
164 props: &revision_props,
165 testpaths,
166 revision: Some(revision),
167 };
168 rev_cx.run_revision();
169 }
170 } else {
171 cx.run_revision();
172 }
173
174 cx.create_stamp();
175}
176
177pub fn compute_stamp_hash(config: &Config) -> String {
178 let mut hash = DefaultHasher::new();
179 config.stage_id.hash(&mut hash);
180 config.run.hash(&mut hash);
181 config.edition.hash(&mut hash);
182
183 match config.debugger {
184 Some(Debugger::Cdb) => {
185 config.cdb.hash(&mut hash);
186 }
187
188 Some(Debugger::Gdb) => {
189 config.gdb.hash(&mut hash);
190 env::var_os("PATH").hash(&mut hash);
191 env::var_os("PYTHONPATH").hash(&mut hash);
192 }
193
194 Some(Debugger::Lldb) => {
195 config.python.hash(&mut hash);
196 config.lldb_python_dir.hash(&mut hash);
197 env::var_os("PATH").hash(&mut hash);
198 env::var_os("PYTHONPATH").hash(&mut hash);
199 }
200
201 None => {}
202 }
203
204 if let Ui = config.mode {
205 config.force_pass_mode.hash(&mut hash);
206 }
207
208 format!("{:x}", hash.finish())
209}
210
211#[derive(Copy, Clone, Debug)]
212struct TestCx<'test> {
213 config: &'test Config,
214 props: &'test TestProps,
215 testpaths: &'test TestPaths,
216 revision: Option<&'test str>,
217}
218
219enum ReadFrom {
220 Path,
221 Stdin(String),
222}
223
224enum TestOutput {
225 Compile,
226 Run,
227}
228
229#[derive(Copy, Clone, PartialEq)]
231enum WillExecute {
232 Yes,
233 No,
234 Disabled,
235}
236
237#[derive(Copy, Clone)]
239enum Emit {
240 None,
241 Metadata,
242 LlvmIr,
243 Mir,
244 Asm,
245 LinkArgsAsm,
246}
247
248impl<'test> TestCx<'test> {
249 fn run_revision(&self) {
252 if self.props.should_ice && self.config.mode != Incremental && self.config.mode != Crashes {
253 self.fatal("cannot use should-ice in a test that is not cfail");
254 }
255 match self.config.mode {
256 Pretty => self.run_pretty_test(),
257 DebugInfo => self.run_debuginfo_test(),
258 Codegen => self.run_codegen_test(),
259 Rustdoc => self.run_rustdoc_test(),
260 RustdocJson => self.run_rustdoc_json_test(),
261 CodegenUnits => self.run_codegen_units_test(),
262 Incremental => self.run_incremental_test(),
263 RunMake => self.run_rmake_test(),
264 Ui => self.run_ui_test(),
265 MirOpt => self.run_mir_opt_test(),
266 Assembly => self.run_assembly_test(),
267 RustdocJs => self.run_rustdoc_js_test(),
268 CoverageMap => self.run_coverage_map_test(), CoverageRun => self.run_coverage_run_test(), Crashes => self.run_crash_test(),
271 }
272 }
273
274 fn pass_mode(&self) -> Option<PassMode> {
275 self.props.pass_mode(self.config)
276 }
277
278 fn should_run(&self, pm: Option<PassMode>) -> WillExecute {
279 let test_should_run = match self.config.mode {
280 Ui if pm == Some(PassMode::Run) || self.props.fail_mode == Some(FailMode::Run) => true,
281 MirOpt if pm == Some(PassMode::Run) => true,
282 Ui | MirOpt => false,
283 mode => panic!("unimplemented for mode {:?}", mode),
284 };
285 if test_should_run { self.run_if_enabled() } else { WillExecute::No }
286 }
287
288 fn run_if_enabled(&self) -> WillExecute {
289 if self.config.run_enabled() { WillExecute::Yes } else { WillExecute::Disabled }
290 }
291
292 fn should_run_successfully(&self, pm: Option<PassMode>) -> bool {
293 match self.config.mode {
294 Ui | MirOpt => pm == Some(PassMode::Run),
295 mode => panic!("unimplemented for mode {:?}", mode),
296 }
297 }
298
299 fn should_compile_successfully(&self, pm: Option<PassMode>) -> bool {
300 match self.config.mode {
301 RustdocJs => true,
302 Ui => pm.is_some() || self.props.fail_mode > Some(FailMode::Build),
303 Crashes => false,
304 Incremental => {
305 let revision =
306 self.revision.expect("incremental tests require a list of revisions");
307 if revision.starts_with("cpass")
308 || revision.starts_with("rpass")
309 || revision.starts_with("rfail")
310 {
311 true
312 } else if revision.starts_with("cfail") {
313 pm.is_some()
314 } else {
315 panic!("revision name must begin with cpass, rpass, rfail, or cfail");
316 }
317 }
318 mode => panic!("unimplemented for mode {:?}", mode),
319 }
320 }
321
322 fn check_if_test_should_compile(
323 &self,
324 fail_mode: Option<FailMode>,
325 pass_mode: Option<PassMode>,
326 proc_res: &ProcRes,
327 ) {
328 if self.should_compile_successfully(pass_mode) {
329 if !proc_res.status.success() {
330 match (fail_mode, pass_mode) {
331 (Some(FailMode::Build), Some(PassMode::Check)) => {
332 self.fatal_proc_rec(
334 "`build-fail` test is required to pass check build, but check build failed",
335 proc_res,
336 );
337 }
338 _ => {
339 self.fatal_proc_rec(
340 "test compilation failed although it shouldn't!",
341 proc_res,
342 );
343 }
344 }
345 }
346 } else {
347 if proc_res.status.success() {
348 {
349 self.error(&format!("{} test did not emit an error", self.config.mode));
350 if self.config.mode == crate::common::Mode::Ui {
351 println!("note: by default, ui tests are expected not to compile");
352 }
353 proc_res.fatal(None, || ());
354 };
355 }
356
357 if !self.props.dont_check_failure_status {
358 self.check_correct_failure_status(proc_res);
359 }
360 }
361 }
362
363 fn get_output(&self, proc_res: &ProcRes) -> String {
364 if self.props.check_stdout {
365 format!("{}{}", proc_res.stdout, proc_res.stderr)
366 } else {
367 proc_res.stderr.clone()
368 }
369 }
370
371 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
372 let expected_status = Some(self.props.failure_status.unwrap_or(1));
373 let received_status = proc_res.status.code();
374
375 if expected_status != received_status {
376 self.fatal_proc_rec(
377 &format!(
378 "Error: expected failure status ({:?}) but received status {:?}.",
379 expected_status, received_status
380 ),
381 proc_res,
382 );
383 }
384 }
385
386 #[must_use = "caller should check whether the command succeeded"]
396 fn run_command_to_procres(&self, cmd: &mut Command) -> ProcRes {
397 let output = cmd
398 .output()
399 .unwrap_or_else(|e| self.fatal(&format!("failed to exec `{cmd:?}` because: {e}")));
400
401 let proc_res = ProcRes {
402 status: output.status,
403 stdout: String::from_utf8(output.stdout).unwrap(),
404 stderr: String::from_utf8(output.stderr).unwrap(),
405 truncated: Truncated::No,
406 cmdline: format!("{cmd:?}"),
407 };
408 self.dump_output(
409 self.config.verbose,
410 &cmd.get_program().to_string_lossy(),
411 &proc_res.stdout,
412 &proc_res.stderr,
413 );
414
415 proc_res
416 }
417
418 fn print_source(&self, read_from: ReadFrom, pretty_type: &str) -> ProcRes {
419 let aux_dir = self.aux_output_dir_name();
420 let input: &str = match read_from {
421 ReadFrom::Stdin(_) => "-",
422 ReadFrom::Path => self.testpaths.file.as_str(),
423 };
424
425 let mut rustc = Command::new(&self.config.rustc_path);
426 rustc
427 .arg(input)
428 .args(&["-Z", &format!("unpretty={}", pretty_type)])
429 .args(&["--target", &self.config.target])
430 .arg("-L")
431 .arg(&aux_dir)
432 .arg("-A")
433 .arg("internal_features")
434 .args(&self.props.compile_flags)
435 .envs(self.props.rustc_env.clone());
436 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
437
438 let src = match read_from {
439 ReadFrom::Stdin(src) => Some(src),
440 ReadFrom::Path => None,
441 };
442
443 self.compose_and_run(
444 rustc,
445 self.config.compile_lib_path.as_path(),
446 Some(aux_dir.as_path()),
447 src,
448 )
449 }
450
451 fn compare_source(&self, expected: &str, actual: &str) {
452 if expected != actual {
453 self.fatal(&format!(
454 "pretty-printed source does not match expected source\n\
455 expected:\n\
456 ------------------------------------------\n\
457 {}\n\
458 ------------------------------------------\n\
459 actual:\n\
460 ------------------------------------------\n\
461 {}\n\
462 ------------------------------------------\n\
463 diff:\n\
464 ------------------------------------------\n\
465 {}\n",
466 expected,
467 actual,
468 write_diff(expected, actual, 3),
469 ));
470 }
471 }
472
473 fn set_revision_flags(&self, cmd: &mut Command) {
474 let normalize_revision = |revision: &str| revision.to_lowercase().replace("-", "_");
477
478 if let Some(revision) = self.revision {
479 let normalized_revision = normalize_revision(revision);
480 let cfg_arg = ["--cfg", &normalized_revision];
481 let arg = format!("--cfg={normalized_revision}");
482 if self
483 .props
484 .compile_flags
485 .windows(2)
486 .any(|args| args == cfg_arg || args[0] == arg || args[1] == arg)
487 {
488 panic!(
489 "error: redundant cfg argument `{normalized_revision}` is already created by the revision"
490 );
491 }
492 if self.config.builtin_cfg_names().contains(&normalized_revision) {
493 panic!("error: revision `{normalized_revision}` collides with a builtin cfg");
494 }
495 cmd.args(cfg_arg);
496 }
497
498 if !self.props.no_auto_check_cfg {
499 let mut check_cfg = String::with_capacity(25);
500
501 check_cfg.push_str("cfg(test,FALSE");
507 for revision in &self.props.revisions {
508 check_cfg.push(',');
509 check_cfg.push_str(&normalize_revision(revision));
510 }
511 check_cfg.push(')');
512
513 cmd.args(&["--check-cfg", &check_cfg]);
514 }
515 }
516
517 fn typecheck_source(&self, src: String) -> ProcRes {
518 let mut rustc = Command::new(&self.config.rustc_path);
519
520 let out_dir = self.output_base_name().with_extension("pretty-out");
521 remove_and_create_dir_all(&out_dir).unwrap_or_else(|e| {
522 panic!("failed to remove and recreate output directory `{out_dir}`: {e}")
523 });
524
525 let target = if self.props.force_host { &*self.config.host } else { &*self.config.target };
526
527 let aux_dir = self.aux_output_dir_name();
528
529 rustc
530 .arg("-")
531 .arg("-Zno-codegen")
532 .arg("--out-dir")
533 .arg(&out_dir)
534 .arg(&format!("--target={}", target))
535 .arg("-L")
536 .arg(&self.config.build_test_suite_root)
539 .arg("-L")
540 .arg(aux_dir)
541 .arg("-A")
542 .arg("internal_features");
543 self.set_revision_flags(&mut rustc);
544 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
545 rustc.args(&self.props.compile_flags);
546
547 self.compose_and_run_compiler(rustc, Some(src), self.testpaths)
548 }
549
550 fn maybe_add_external_args(&self, cmd: &mut Command, args: &Vec<String>) {
551 const OPT_FLAGS: &[&str] = &["-O", "-Copt-level=", "opt-level="];
556 const DEBUG_FLAGS: &[&str] = &["-g", "-Cdebuginfo=", "debuginfo="];
557
558 let have_opt_flag =
562 self.props.compile_flags.iter().any(|arg| OPT_FLAGS.iter().any(|f| arg.starts_with(f)));
563 let have_debug_flag = self
564 .props
565 .compile_flags
566 .iter()
567 .any(|arg| DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)));
568
569 for arg in args {
570 if OPT_FLAGS.iter().any(|f| arg.starts_with(f)) && have_opt_flag {
571 continue;
572 }
573 if DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)) && have_debug_flag {
574 continue;
575 }
576 cmd.arg(arg);
577 }
578 }
579
580 fn check_all_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
582 let mut missing_patterns: Vec<String> = Vec::new();
583 self.check_error_patterns(output_to_check, &mut missing_patterns);
584 self.check_regex_error_patterns(output_to_check, proc_res, &mut missing_patterns);
585
586 if missing_patterns.is_empty() {
587 return;
588 }
589
590 if missing_patterns.len() == 1 {
591 self.fatal_proc_rec(
592 &format!("error pattern '{}' not found!", missing_patterns[0]),
593 proc_res,
594 );
595 } else {
596 for pattern in missing_patterns {
597 self.error(&format!("error pattern '{}' not found!", pattern));
598 }
599 self.fatal_proc_rec("multiple error patterns not found", proc_res);
600 }
601 }
602
603 fn check_error_patterns(&self, output_to_check: &str, missing_patterns: &mut Vec<String>) {
604 debug!("check_error_patterns");
605 for pattern in &self.props.error_patterns {
606 if output_to_check.contains(pattern.trim()) {
607 debug!("found error pattern {}", pattern);
608 } else {
609 missing_patterns.push(pattern.to_string());
610 }
611 }
612 }
613
614 fn check_regex_error_patterns(
615 &self,
616 output_to_check: &str,
617 proc_res: &ProcRes,
618 missing_patterns: &mut Vec<String>,
619 ) {
620 debug!("check_regex_error_patterns");
621
622 for pattern in &self.props.regex_error_patterns {
623 let pattern = pattern.trim();
624 let re = match Regex::new(pattern) {
625 Ok(re) => re,
626 Err(err) => {
627 self.fatal_proc_rec(
628 &format!("invalid regex error pattern '{}': {:?}", pattern, err),
629 proc_res,
630 );
631 }
632 };
633 if re.is_match(output_to_check) {
634 debug!("found regex error pattern {}", pattern);
635 } else {
636 missing_patterns.push(pattern.to_string());
637 }
638 }
639 }
640
641 fn check_no_compiler_crash(&self, proc_res: &ProcRes, should_ice: bool) {
642 match proc_res.status.code() {
643 Some(101) if !should_ice => {
644 self.fatal_proc_rec("compiler encountered internal error", proc_res)
645 }
646 None => self.fatal_proc_rec("compiler terminated by signal", proc_res),
647 _ => (),
648 }
649 }
650
651 fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
652 for pat in &self.props.forbid_output {
653 if output_to_check.contains(pat) {
654 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
655 }
656 }
657 }
658
659 fn check_expected_errors(&self, proc_res: &ProcRes) {
661 let expected_errors = load_errors(&self.testpaths.file, self.revision);
662 debug!(
663 "check_expected_errors: expected_errors={:?} proc_res.status={:?}",
664 expected_errors, proc_res.status
665 );
666 if proc_res.status.success() && expected_errors.iter().any(|x| x.kind == ErrorKind::Error) {
667 self.fatal_proc_rec("process did not return an error status", proc_res);
668 }
669
670 if self.props.known_bug {
671 if !expected_errors.is_empty() {
672 self.fatal_proc_rec(
673 "`known_bug` tests should not have an expected error",
674 proc_res,
675 );
676 }
677 return;
678 }
679
680 let file_name = self.testpaths.file.to_string().replace(r"\", "/");
682
683 let diagnostic_file_name = if self.props.remap_src_base {
686 let mut p = Utf8PathBuf::from(FAKE_SRC_BASE);
687 p.push(&self.testpaths.relative_dir);
688 p.push(self.testpaths.file.file_name().unwrap());
689 p.to_string()
690 } else {
691 self.testpaths.file.to_string()
692 };
693
694 let expected_kinds: HashSet<_> = [ErrorKind::Error, ErrorKind::Warning]
697 .into_iter()
698 .chain(expected_errors.iter().map(|e| e.kind))
699 .collect();
700
701 let actual_errors = json::parse_output(&diagnostic_file_name, &self.get_output(proc_res))
703 .into_iter()
704 .map(|e| Error { msg: self.normalize_output(&e.msg, &[]), ..e });
705
706 let mut unexpected = Vec::new();
707 let mut found = vec![false; expected_errors.len()];
708 for actual_error in actual_errors {
709 for pattern in &self.props.error_patterns {
710 let pattern = pattern.trim();
711 if actual_error.msg.contains(pattern) {
712 let q = if actual_error.line_num.is_none() { "?" } else { "" };
713 self.fatal(&format!(
714 "error pattern '{pattern}' is found in structured \
715 diagnostics, use `//~{q} {} {pattern}` instead",
716 actual_error.kind,
717 ));
718 }
719 }
720
721 let opt_index =
722 expected_errors.iter().enumerate().position(|(index, expected_error)| {
723 !found[index]
724 && actual_error.line_num == expected_error.line_num
725 && actual_error.kind == expected_error.kind
726 && actual_error.msg.contains(&expected_error.msg)
727 });
728
729 match opt_index {
730 Some(index) => {
731 assert!(!found[index]);
733 found[index] = true;
734 }
735
736 None => {
737 if actual_error.require_annotation
738 && expected_kinds.contains(&actual_error.kind)
739 && !self.props.dont_require_annotations.contains(&actual_error.kind)
740 {
741 self.error(&format!(
742 "{}:{}: unexpected {}: '{}'",
743 file_name,
744 actual_error.line_num_str(),
745 actual_error.kind,
746 actual_error.msg
747 ));
748 unexpected.push(actual_error);
749 }
750 }
751 }
752 }
753
754 let mut not_found = Vec::new();
755 for (index, expected_error) in expected_errors.iter().enumerate() {
757 if !found[index] {
758 self.error(&format!(
759 "{}:{}: expected {} not found: {}",
760 file_name,
761 expected_error.line_num_str(),
762 expected_error.kind,
763 expected_error.msg
764 ));
765 not_found.push(expected_error);
766 }
767 }
768
769 if !unexpected.is_empty() || !not_found.is_empty() {
770 self.error(&format!(
771 "{} unexpected errors found, {} expected errors not found",
772 unexpected.len(),
773 not_found.len()
774 ));
775 println!("status: {}\ncommand: {}\n", proc_res.status, proc_res.cmdline);
776 if !unexpected.is_empty() {
777 println!("{}", "--- unexpected errors (from JSON output) ---".green());
778 for error in &unexpected {
779 println!("{}", error.render_for_expected());
780 }
781 println!("{}", "---".green());
782 }
783 if !not_found.is_empty() {
784 println!("{}", "--- not found errors (from test file) ---".red());
785 for error in ¬_found {
786 println!("{}", error.render_for_expected());
787 }
788 println!("{}", "---\n".red());
789 }
790 panic!("errors differ from expected");
791 }
792 }
793
794 fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {
795 match (pm, self.props.fail_mode, self.config.mode) {
796 (Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => Emit::Metadata,
797 _ => Emit::None,
798 }
799 }
800
801 fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
802 self.compile_test_general(will_execute, emit, self.props.local_pass_mode(), Vec::new())
803 }
804
805 fn compile_test_with_passes(
806 &self,
807 will_execute: WillExecute,
808 emit: Emit,
809 passes: Vec<String>,
810 ) -> ProcRes {
811 self.compile_test_general(will_execute, emit, self.props.local_pass_mode(), passes)
812 }
813
814 fn compile_test_general(
815 &self,
816 will_execute: WillExecute,
817 emit: Emit,
818 local_pm: Option<PassMode>,
819 passes: Vec<String>,
820 ) -> ProcRes {
821 let output_file = match will_execute {
823 WillExecute::Yes => TargetLocation::ThisFile(self.make_exe_name()),
824 WillExecute::No | WillExecute::Disabled => {
825 TargetLocation::ThisDirectory(self.output_base_dir())
826 }
827 };
828
829 let allow_unused = match self.config.mode {
830 Ui => {
831 if !self.is_rustdoc()
837 && local_pm != Some(PassMode::Run)
841 {
842 AllowUnused::Yes
843 } else {
844 AllowUnused::No
845 }
846 }
847 _ => AllowUnused::No,
848 };
849
850 let rustc = self.make_compile_args(
851 &self.testpaths.file,
852 output_file,
853 emit,
854 allow_unused,
855 LinkToAux::Yes,
856 passes,
857 );
858
859 self.compose_and_run_compiler(rustc, None, self.testpaths)
860 }
861
862 fn document(&self, root_out_dir: &Utf8Path, root_testpaths: &TestPaths) -> ProcRes {
865 if self.props.build_aux_docs {
866 for rel_ab in &self.props.aux.builds {
867 let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab);
868 let props_for_aux =
869 self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
870 let aux_cx = TestCx {
871 config: self.config,
872 props: &props_for_aux,
873 testpaths: &aux_testpaths,
874 revision: self.revision,
875 };
876 create_dir_all(aux_cx.output_base_dir()).unwrap();
878 let auxres = aux_cx.document(&root_out_dir, root_testpaths);
881 if !auxres.status.success() {
882 return auxres;
883 }
884 }
885 }
886
887 let aux_dir = self.aux_output_dir_name();
888
889 let rustdoc_path = self.config.rustdoc_path.as_ref().expect("--rustdoc-path not passed");
890
891 let out_dir: Cow<'_, Utf8Path> = if self.props.unique_doc_out_dir {
894 let file_name = self.testpaths.file.file_stem().expect("file name should not be empty");
895 let out_dir = Utf8PathBuf::from_iter([
896 root_out_dir,
897 Utf8Path::new("docs"),
898 Utf8Path::new(file_name),
899 Utf8Path::new("doc"),
900 ]);
901 create_dir_all(&out_dir).unwrap();
902 Cow::Owned(out_dir)
903 } else {
904 Cow::Borrowed(root_out_dir)
905 };
906
907 let mut rustdoc = Command::new(rustdoc_path);
908 let current_dir = output_base_dir(self.config, root_testpaths, self.safe_revision());
909 rustdoc.current_dir(current_dir);
910 rustdoc
911 .arg("-L")
912 .arg(self.config.run_lib_path.as_path())
913 .arg("-L")
914 .arg(aux_dir)
915 .arg("-o")
916 .arg(out_dir.as_ref())
917 .arg("--deny")
918 .arg("warnings")
919 .arg(&self.testpaths.file)
920 .arg("-A")
921 .arg("internal_features")
922 .args(&self.props.compile_flags)
923 .args(&self.props.doc_flags);
924
925 if self.config.mode == RustdocJson {
926 rustdoc.arg("--output-format").arg("json").arg("-Zunstable-options");
927 }
928
929 if let Some(ref linker) = self.config.target_linker {
930 rustdoc.arg(format!("-Clinker={}", linker));
931 }
932
933 self.compose_and_run_compiler(rustdoc, None, root_testpaths)
934 }
935
936 fn exec_compiled_test(&self) -> ProcRes {
937 self.exec_compiled_test_general(&[], true)
938 }
939
940 fn exec_compiled_test_general(
941 &self,
942 env_extra: &[(&str, &str)],
943 delete_after_success: bool,
944 ) -> ProcRes {
945 let prepare_env = |cmd: &mut Command| {
946 for (key, val) in &self.props.exec_env {
947 cmd.env(key, val);
948 }
949 for (key, val) in env_extra {
950 cmd.env(key, val);
951 }
952
953 for key in &self.props.unset_exec_env {
954 cmd.env_remove(key);
955 }
956 };
957
958 let proc_res = match &*self.config.target {
959 _ if self.config.remote_test_client.is_some() => {
974 let aux_dir = self.aux_output_dir_name();
975 let ProcArgs { prog, args } = self.make_run_args();
976 let mut support_libs = Vec::new();
977 if let Ok(entries) = aux_dir.read_dir() {
978 for entry in entries {
979 let entry = entry.unwrap();
980 if !entry.path().is_file() {
981 continue;
982 }
983 support_libs.push(entry.path());
984 }
985 }
986 let mut test_client =
987 Command::new(self.config.remote_test_client.as_ref().unwrap());
988 test_client
989 .args(&["run", &support_libs.len().to_string()])
990 .arg(&prog)
991 .args(support_libs)
992 .args(args);
993
994 prepare_env(&mut test_client);
995
996 self.compose_and_run(
997 test_client,
998 self.config.run_lib_path.as_path(),
999 Some(aux_dir.as_path()),
1000 None,
1001 )
1002 }
1003 _ if self.config.target.contains("vxworks") => {
1004 let aux_dir = self.aux_output_dir_name();
1005 let ProcArgs { prog, args } = self.make_run_args();
1006 let mut wr_run = Command::new("wr-run");
1007 wr_run.args(&[&prog]).args(args);
1008
1009 prepare_env(&mut wr_run);
1010
1011 self.compose_and_run(
1012 wr_run,
1013 self.config.run_lib_path.as_path(),
1014 Some(aux_dir.as_path()),
1015 None,
1016 )
1017 }
1018 _ => {
1019 let aux_dir = self.aux_output_dir_name();
1020 let ProcArgs { prog, args } = self.make_run_args();
1021 let mut program = Command::new(&prog);
1022 program.args(args).current_dir(&self.output_base_dir());
1023
1024 prepare_env(&mut program);
1025
1026 self.compose_and_run(
1027 program,
1028 self.config.run_lib_path.as_path(),
1029 Some(aux_dir.as_path()),
1030 None,
1031 )
1032 }
1033 };
1034
1035 if delete_after_success && proc_res.status.success() {
1036 let _ = fs::remove_file(self.make_exe_name());
1039 }
1040
1041 proc_res
1042 }
1043
1044 fn compute_aux_test_paths(&self, of: &TestPaths, rel_ab: &str) -> TestPaths {
1047 let test_ab =
1048 of.file.parent().expect("test file path has no parent").join("auxiliary").join(rel_ab);
1049 if !test_ab.exists() {
1050 self.fatal(&format!("aux-build `{}` source not found", test_ab))
1051 }
1052
1053 TestPaths {
1054 file: test_ab,
1055 relative_dir: of
1056 .relative_dir
1057 .join(self.output_testname_unique())
1058 .join("auxiliary")
1059 .join(rel_ab)
1060 .parent()
1061 .expect("aux-build path has no parent")
1062 .to_path_buf(),
1063 }
1064 }
1065
1066 fn is_vxworks_pure_static(&self) -> bool {
1067 if self.config.target.contains("vxworks") {
1068 match env::var("RUST_VXWORKS_TEST_DYLINK") {
1069 Ok(s) => s != "1",
1070 _ => true,
1071 }
1072 } else {
1073 false
1074 }
1075 }
1076
1077 fn is_vxworks_pure_dynamic(&self) -> bool {
1078 self.config.target.contains("vxworks") && !self.is_vxworks_pure_static()
1079 }
1080
1081 fn has_aux_dir(&self) -> bool {
1082 !self.props.aux.builds.is_empty()
1083 || !self.props.aux.crates.is_empty()
1084 || !self.props.aux.proc_macros.is_empty()
1085 }
1086
1087 fn aux_output_dir(&self) -> Utf8PathBuf {
1088 let aux_dir = self.aux_output_dir_name();
1089
1090 if !self.props.aux.builds.is_empty() {
1091 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1092 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1093 });
1094 }
1095
1096 if !self.props.aux.bins.is_empty() {
1097 let aux_bin_dir = self.aux_bin_output_dir_name();
1098 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1099 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1100 });
1101 remove_and_create_dir_all(&aux_bin_dir).unwrap_or_else(|e| {
1102 panic!("failed to remove and recreate output directory `{aux_bin_dir}`: {e}")
1103 });
1104 }
1105
1106 aux_dir
1107 }
1108
1109 fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Utf8Path, rustc: &mut Command) {
1110 for rel_ab in &self.props.aux.builds {
1111 self.build_auxiliary(of, rel_ab, &aux_dir, None);
1112 }
1113
1114 for rel_ab in &self.props.aux.bins {
1115 self.build_auxiliary(of, rel_ab, &aux_dir, Some(AuxType::Bin));
1116 }
1117
1118 let path_to_crate_name = |path: &str| -> String {
1119 path.rsplit_once('/')
1120 .map_or(path, |(_, tail)| tail)
1121 .trim_end_matches(".rs")
1122 .replace('-', "_")
1123 };
1124
1125 let add_extern =
1126 |rustc: &mut Command, aux_name: &str, aux_path: &str, aux_type: AuxType| {
1127 let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1128 if let Some(lib_name) = lib_name {
1129 rustc.arg("--extern").arg(format!("{}={}/{}", aux_name, aux_dir, lib_name));
1130 }
1131 };
1132
1133 for (aux_name, aux_path) in &self.props.aux.crates {
1134 let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, None);
1135 add_extern(rustc, aux_name, aux_path, aux_type);
1136 }
1137
1138 for proc_macro in &self.props.aux.proc_macros {
1139 self.build_auxiliary(of, proc_macro, &aux_dir, Some(AuxType::ProcMacro));
1140 let crate_name = path_to_crate_name(proc_macro);
1141 add_extern(rustc, &crate_name, proc_macro, AuxType::ProcMacro);
1142 }
1143
1144 if let Some(aux_file) = &self.props.aux.codegen_backend {
1147 let aux_type = self.build_auxiliary(of, aux_file, aux_dir, None);
1148 if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
1149 let lib_path = aux_dir.join(&lib_name);
1150 rustc.arg(format!("-Zcodegen-backend={}", lib_path));
1151 }
1152 }
1153 }
1154
1155 fn compose_and_run_compiler(
1158 &self,
1159 mut rustc: Command,
1160 input: Option<String>,
1161 root_testpaths: &TestPaths,
1162 ) -> ProcRes {
1163 if self.props.add_core_stubs {
1164 let minicore_path = self.build_minicore();
1165 rustc.arg("--extern");
1166 rustc.arg(&format!("minicore={}", minicore_path));
1167 }
1168
1169 let aux_dir = self.aux_output_dir();
1170 self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc);
1171
1172 rustc.envs(self.props.rustc_env.clone());
1173 self.props.unset_rustc_env.iter().fold(&mut rustc, Command::env_remove);
1174 self.compose_and_run(
1175 rustc,
1176 self.config.compile_lib_path.as_path(),
1177 Some(aux_dir.as_path()),
1178 input,
1179 )
1180 }
1181
1182 fn build_minicore(&self) -> Utf8PathBuf {
1185 let output_file_path = self.output_base_dir().join("libminicore.rlib");
1186 let mut rustc = self.make_compile_args(
1187 &self.config.minicore_path,
1188 TargetLocation::ThisFile(output_file_path.clone()),
1189 Emit::None,
1190 AllowUnused::Yes,
1191 LinkToAux::No,
1192 vec![],
1193 );
1194
1195 rustc.args(&["--crate-type", "rlib"]);
1196 rustc.arg("-Cpanic=abort");
1197
1198 let res = self.compose_and_run(rustc, self.config.compile_lib_path.as_path(), None, None);
1199 if !res.status.success() {
1200 self.fatal_proc_rec(
1201 &format!("auxiliary build of {} failed to compile: ", self.config.minicore_path),
1202 &res,
1203 );
1204 }
1205
1206 output_file_path
1207 }
1208
1209 fn build_auxiliary(
1213 &self,
1214 of: &TestPaths,
1215 source_path: &str,
1216 aux_dir: &Utf8Path,
1217 aux_type: Option<AuxType>,
1218 ) -> AuxType {
1219 let aux_testpaths = self.compute_aux_test_paths(of, source_path);
1220 let mut aux_props =
1221 self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
1222 if aux_type == Some(AuxType::ProcMacro) {
1223 aux_props.force_host = true;
1224 }
1225 let mut aux_dir = aux_dir.to_path_buf();
1226 if aux_type == Some(AuxType::Bin) {
1227 aux_dir.push("bin");
1231 }
1232 let aux_output = TargetLocation::ThisDirectory(aux_dir.clone());
1233 let aux_cx = TestCx {
1234 config: self.config,
1235 props: &aux_props,
1236 testpaths: &aux_testpaths,
1237 revision: self.revision,
1238 };
1239 create_dir_all(aux_cx.output_base_dir()).unwrap();
1241 let input_file = &aux_testpaths.file;
1242 let mut aux_rustc = aux_cx.make_compile_args(
1243 input_file,
1244 aux_output,
1245 Emit::None,
1246 AllowUnused::No,
1247 LinkToAux::No,
1248 Vec::new(),
1249 );
1250 aux_cx.build_all_auxiliary(of, &aux_dir, &mut aux_rustc);
1251
1252 aux_rustc.envs(aux_props.rustc_env.clone());
1253 for key in &aux_props.unset_rustc_env {
1254 aux_rustc.env_remove(key);
1255 }
1256
1257 let (aux_type, crate_type) = if aux_type == Some(AuxType::Bin) {
1258 (AuxType::Bin, Some("bin"))
1259 } else if aux_type == Some(AuxType::ProcMacro) {
1260 (AuxType::ProcMacro, Some("proc-macro"))
1261 } else if aux_type.is_some() {
1262 panic!("aux_type {aux_type:?} not expected");
1263 } else if aux_props.no_prefer_dynamic {
1264 (AuxType::Dylib, None)
1265 } else if self.config.target.contains("emscripten")
1266 || (self.config.target.contains("musl")
1267 && !aux_props.force_host
1268 && !self.config.host.contains("musl"))
1269 || self.config.target.contains("wasm32")
1270 || self.config.target.contains("nvptx")
1271 || self.is_vxworks_pure_static()
1272 || self.config.target.contains("bpf")
1273 || !self.config.target_cfg().dynamic_linking
1274 || matches!(self.config.mode, CoverageMap | CoverageRun)
1275 {
1276 (AuxType::Lib, Some("lib"))
1290 } else {
1291 (AuxType::Dylib, Some("dylib"))
1292 };
1293
1294 if let Some(crate_type) = crate_type {
1295 aux_rustc.args(&["--crate-type", crate_type]);
1296 }
1297
1298 if aux_type == AuxType::ProcMacro {
1299 aux_rustc.args(&["--extern", "proc_macro"]);
1301 }
1302
1303 aux_rustc.arg("-L").arg(&aux_dir);
1304
1305 let auxres = aux_cx.compose_and_run(
1306 aux_rustc,
1307 aux_cx.config.compile_lib_path.as_path(),
1308 Some(aux_dir.as_path()),
1309 None,
1310 );
1311 if !auxres.status.success() {
1312 self.fatal_proc_rec(
1313 &format!("auxiliary build of {} failed to compile: ", aux_testpaths.file),
1314 &auxres,
1315 );
1316 }
1317 aux_type
1318 }
1319
1320 fn read2_abbreviated(&self, child: Child) -> (Output, Truncated) {
1321 let mut filter_paths_from_len = Vec::new();
1322 let mut add_path = |path: &Utf8Path| {
1323 let path = path.to_string();
1324 let windows = path.replace("\\", "\\\\");
1325 if windows != path {
1326 filter_paths_from_len.push(windows);
1327 }
1328 filter_paths_from_len.push(path);
1329 };
1330
1331 add_path(&self.config.src_test_suite_root);
1337 add_path(&self.config.build_test_suite_root);
1338
1339 read2_abbreviated(child, &filter_paths_from_len).expect("failed to read output")
1340 }
1341
1342 fn compose_and_run(
1343 &self,
1344 mut command: Command,
1345 lib_path: &Utf8Path,
1346 aux_path: Option<&Utf8Path>,
1347 input: Option<String>,
1348 ) -> ProcRes {
1349 let cmdline = {
1350 let cmdline = self.make_cmdline(&command, lib_path);
1351 logv(self.config, format!("executing {}", cmdline));
1352 cmdline
1353 };
1354
1355 command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::piped());
1356
1357 add_dylib_path(&mut command, iter::once(lib_path).chain(aux_path));
1360
1361 let mut child = disable_error_reporting(|| command.spawn())
1362 .unwrap_or_else(|e| panic!("failed to exec `{command:?}`: {e:?}"));
1363 if let Some(input) = input {
1364 child.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
1365 }
1366
1367 let (Output { status, stdout, stderr }, truncated) = self.read2_abbreviated(child);
1368
1369 let result = ProcRes {
1370 status,
1371 stdout: String::from_utf8_lossy(&stdout).into_owned(),
1372 stderr: String::from_utf8_lossy(&stderr).into_owned(),
1373 truncated,
1374 cmdline,
1375 };
1376
1377 self.dump_output(
1378 self.config.verbose,
1379 &command.get_program().to_string_lossy(),
1380 &result.stdout,
1381 &result.stderr,
1382 );
1383
1384 result
1385 }
1386
1387 fn is_rustdoc(&self) -> bool {
1388 matches!(self.config.suite.as_str(), "rustdoc-ui" | "rustdoc-js" | "rustdoc-json")
1389 }
1390
1391 fn make_compile_args(
1392 &self,
1393 input_file: &Utf8Path,
1394 output_file: TargetLocation,
1395 emit: Emit,
1396 allow_unused: AllowUnused,
1397 link_to_aux: LinkToAux,
1398 passes: Vec<String>, ) -> Command {
1400 let is_aux = input_file.components().map(|c| c.as_os_str()).any(|c| c == "auxiliary");
1401 let is_rustdoc = self.is_rustdoc() && !is_aux;
1402 let mut rustc = if !is_rustdoc {
1403 Command::new(&self.config.rustc_path)
1404 } else {
1405 Command::new(&self.config.rustdoc_path.clone().expect("no rustdoc built yet"))
1406 };
1407 rustc.arg(input_file);
1408
1409 rustc.arg("-Zthreads=1");
1411
1412 rustc.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
1421 rustc.arg("-Ztranslate-remapped-path-to-local-path=no");
1422
1423 rustc.arg("-Z").arg(format!(
1428 "ignore-directory-in-diagnostics-source-blocks={}",
1429 home::cargo_home().expect("failed to find cargo home").to_str().unwrap()
1430 ));
1431 rustc.arg("-Z").arg(format!(
1433 "ignore-directory-in-diagnostics-source-blocks={}",
1434 self.config.src_root.join("vendor"),
1435 ));
1436
1437 if !self.props.compile_flags.iter().any(|flag| flag.starts_with("--sysroot"))
1439 && !self.config.host_rustcflags.iter().any(|flag| flag == "--sysroot")
1440 {
1441 rustc.arg("--sysroot").arg(&self.config.sysroot_base);
1443 }
1444
1445 let custom_target = self.props.compile_flags.iter().any(|x| x.starts_with("--target"));
1447
1448 if !custom_target {
1449 let target =
1450 if self.props.force_host { &*self.config.host } else { &*self.config.target };
1451
1452 rustc.arg(&format!("--target={}", target));
1453 }
1454 self.set_revision_flags(&mut rustc);
1455
1456 if !is_rustdoc {
1457 if let Some(ref incremental_dir) = self.props.incremental_dir {
1458 rustc.args(&["-C", &format!("incremental={}", incremental_dir)]);
1459 rustc.args(&["-Z", "incremental-verify-ich"]);
1460 }
1461
1462 if self.config.mode == CodegenUnits {
1463 rustc.args(&["-Z", "human_readable_cgu_names"]);
1464 }
1465 }
1466
1467 if self.config.optimize_tests && !is_rustdoc {
1468 match self.config.mode {
1469 Ui => {
1470 if self.config.optimize_tests
1475 && self.props.pass_mode(&self.config) == Some(PassMode::Run)
1476 && !self
1477 .props
1478 .compile_flags
1479 .iter()
1480 .any(|arg| arg == "-O" || arg.contains("opt-level"))
1481 {
1482 rustc.arg("-O");
1483 }
1484 }
1485 DebugInfo => { }
1486 CoverageMap | CoverageRun => {
1487 }
1492 _ => {
1493 rustc.arg("-O");
1494 }
1495 }
1496 }
1497
1498 let set_mir_dump_dir = |rustc: &mut Command| {
1499 let mir_dump_dir = self.output_base_dir();
1500 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1501 dir_opt.push_str(mir_dump_dir.as_str());
1502 debug!("dir_opt: {:?}", dir_opt);
1503 rustc.arg(dir_opt);
1504 };
1505
1506 match self.config.mode {
1507 Incremental => {
1508 if self.props.error_patterns.is_empty()
1512 && self.props.regex_error_patterns.is_empty()
1513 {
1514 rustc.args(&["--error-format", "json"]);
1515 rustc.args(&["--json", "future-incompat"]);
1516 }
1517 rustc.arg("-Zui-testing");
1518 rustc.arg("-Zdeduplicate-diagnostics=no");
1519 }
1520 Ui => {
1521 if !self.props.compile_flags.iter().any(|s| s.starts_with("--error-format")) {
1522 rustc.args(&["--error-format", "json"]);
1523 rustc.args(&["--json", "future-incompat"]);
1524 }
1525 rustc.arg("-Ccodegen-units=1");
1526 rustc.arg("-Zui-testing");
1528 rustc.arg("-Zdeduplicate-diagnostics=no");
1529 rustc.arg("-Zwrite-long-types-to-disk=no");
1530 rustc.arg("-Cstrip=debuginfo");
1532 }
1533 MirOpt => {
1534 let zdump_arg = if !passes.is_empty() {
1538 format!("-Zdump-mir={}", passes.join(" | "))
1539 } else {
1540 "-Zdump-mir=all".to_string()
1541 };
1542
1543 rustc.args(&[
1544 "-Copt-level=1",
1545 &zdump_arg,
1546 "-Zvalidate-mir",
1547 "-Zlint-mir",
1548 "-Zdump-mir-exclude-pass-number",
1549 "-Zmir-include-spans=false", "--crate-type=rlib",
1551 ]);
1552 if let Some(pass) = &self.props.mir_unit_test {
1553 rustc.args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]);
1554 } else {
1555 rustc.args(&[
1556 "-Zmir-opt-level=4",
1557 "-Zmir-enable-passes=+ReorderBasicBlocks,+ReorderLocals",
1558 ]);
1559 }
1560
1561 set_mir_dump_dir(&mut rustc);
1562 }
1563 CoverageMap => {
1564 rustc.arg("-Cinstrument-coverage");
1565 rustc.arg("-Zno-profiler-runtime");
1568 rustc.arg("-Copt-level=2");
1572 }
1573 CoverageRun => {
1574 rustc.arg("-Cinstrument-coverage");
1575 rustc.arg("-Copt-level=2");
1579 }
1580 Assembly | Codegen => {
1581 rustc.arg("-Cdebug-assertions=no");
1582 }
1583 Crashes => {
1584 set_mir_dump_dir(&mut rustc);
1585 }
1586 Pretty | DebugInfo | Rustdoc | RustdocJson | RunMake | CodegenUnits | RustdocJs => {
1587 }
1589 }
1590
1591 if self.props.remap_src_base {
1592 rustc.arg(format!(
1593 "--remap-path-prefix={}={}",
1594 self.config.src_test_suite_root, FAKE_SRC_BASE,
1595 ));
1596 }
1597
1598 match emit {
1599 Emit::None => {}
1600 Emit::Metadata if is_rustdoc => {}
1601 Emit::Metadata => {
1602 rustc.args(&["--emit", "metadata"]);
1603 }
1604 Emit::LlvmIr => {
1605 rustc.args(&["--emit", "llvm-ir"]);
1606 }
1607 Emit::Mir => {
1608 rustc.args(&["--emit", "mir"]);
1609 }
1610 Emit::Asm => {
1611 rustc.args(&["--emit", "asm"]);
1612 }
1613 Emit::LinkArgsAsm => {
1614 rustc.args(&["-Clink-args=--emit=asm"]);
1615 }
1616 }
1617
1618 if !is_rustdoc {
1619 if self.config.target == "wasm32-unknown-unknown" || self.is_vxworks_pure_static() {
1620 } else if !self.props.no_prefer_dynamic {
1622 rustc.args(&["-C", "prefer-dynamic"]);
1623 }
1624 }
1625
1626 match output_file {
1627 _ if self.props.compile_flags.iter().any(|flag| flag == "-o") => {}
1630 TargetLocation::ThisFile(path) => {
1631 rustc.arg("-o").arg(path);
1632 }
1633 TargetLocation::ThisDirectory(path) => {
1634 if is_rustdoc {
1635 rustc.arg("-o").arg(path);
1637 } else {
1638 rustc.arg("--out-dir").arg(path);
1639 }
1640 }
1641 }
1642
1643 match self.config.compare_mode {
1644 Some(CompareMode::Polonius) => {
1645 rustc.args(&["-Zpolonius"]);
1646 }
1647 Some(CompareMode::NextSolver) => {
1648 rustc.args(&["-Znext-solver"]);
1649 }
1650 Some(CompareMode::NextSolverCoherence) => {
1651 rustc.args(&["-Znext-solver=coherence"]);
1652 }
1653 Some(CompareMode::SplitDwarf) if self.config.target.contains("windows") => {
1654 rustc.args(&["-Csplit-debuginfo=unpacked", "-Zunstable-options"]);
1655 }
1656 Some(CompareMode::SplitDwarf) => {
1657 rustc.args(&["-Csplit-debuginfo=unpacked"]);
1658 }
1659 Some(CompareMode::SplitDwarfSingle) => {
1660 rustc.args(&["-Csplit-debuginfo=packed"]);
1661 }
1662 None => {}
1663 }
1664
1665 if let AllowUnused::Yes = allow_unused {
1668 rustc.args(&["-A", "unused"]);
1669 }
1670
1671 rustc.args(&["-A", "internal_features"]);
1673
1674 if self.props.force_host {
1675 self.maybe_add_external_args(&mut rustc, &self.config.host_rustcflags);
1676 if !is_rustdoc {
1677 if let Some(ref linker) = self.config.host_linker {
1678 rustc.arg(format!("-Clinker={}", linker));
1679 }
1680 }
1681 } else {
1682 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
1683 if !is_rustdoc {
1684 if let Some(ref linker) = self.config.target_linker {
1685 rustc.arg(format!("-Clinker={}", linker));
1686 }
1687 }
1688 }
1689
1690 if self.config.host.contains("musl") || self.is_vxworks_pure_dynamic() {
1692 rustc.arg("-Ctarget-feature=-crt-static");
1693 }
1694
1695 if let LinkToAux::Yes = link_to_aux {
1696 if self.has_aux_dir() {
1699 rustc.arg("-L").arg(self.aux_output_dir_name());
1700 }
1701 }
1702
1703 rustc.args(&self.props.compile_flags);
1704
1705 if self.props.add_core_stubs {
1714 rustc.arg("-Cpanic=abort");
1715 rustc.arg("-Cforce-unwind-tables=yes");
1716 }
1717
1718 rustc
1719 }
1720
1721 fn make_exe_name(&self) -> Utf8PathBuf {
1722 let mut f = self.output_base_dir().join("a");
1727 if self.config.target.contains("emscripten") {
1729 f = f.with_extra_extension("js");
1730 } else if self.config.target.starts_with("wasm") {
1731 f = f.with_extra_extension("wasm");
1732 } else if self.config.target.contains("spirv") {
1733 f = f.with_extra_extension("spv");
1734 } else if !env::consts::EXE_SUFFIX.is_empty() {
1735 f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1736 }
1737 f
1738 }
1739
1740 fn make_run_args(&self) -> ProcArgs {
1741 let mut args = self.split_maybe_args(&self.config.runner);
1744
1745 let exe_file = self.make_exe_name();
1746
1747 args.push(exe_file.into_os_string());
1748
1749 args.extend(self.props.run_flags.iter().map(OsString::from));
1751
1752 let prog = args.remove(0);
1753 ProcArgs { prog, args }
1754 }
1755
1756 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<OsString> {
1757 match *argstr {
1758 Some(ref s) => s
1759 .split(' ')
1760 .filter_map(|s| {
1761 if s.chars().all(|c| c.is_whitespace()) {
1762 None
1763 } else {
1764 Some(OsString::from(s))
1765 }
1766 })
1767 .collect(),
1768 None => Vec::new(),
1769 }
1770 }
1771
1772 fn make_cmdline(&self, command: &Command, libpath: &Utf8Path) -> String {
1773 use crate::util;
1774
1775 if cfg!(unix) {
1777 format!("{:?}", command)
1778 } else {
1779 fn lib_path_cmd_prefix(path: &str) -> String {
1782 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1783 }
1784
1785 format!("{} {:?}", lib_path_cmd_prefix(libpath.as_str()), command)
1786 }
1787 }
1788
1789 fn dump_output(&self, print_output: bool, proc_name: &str, out: &str, err: &str) {
1790 let revision = if let Some(r) = self.revision { format!("{}.", r) } else { String::new() };
1791
1792 self.dump_output_file(out, &format!("{}out", revision));
1793 self.dump_output_file(err, &format!("{}err", revision));
1794
1795 if !print_output {
1796 return;
1797 }
1798
1799 let path = Utf8Path::new(proc_name);
1800 let proc_name = if path.file_stem().is_some_and(|p| p == "rmake") {
1801 String::from_iter(
1802 path.parent()
1803 .unwrap()
1804 .file_name()
1805 .into_iter()
1806 .chain(Some("/"))
1807 .chain(path.file_name()),
1808 )
1809 } else {
1810 path.file_name().unwrap().into()
1811 };
1812 println!("------{proc_name} stdout------------------------------");
1813 println!("{}", out);
1814 println!("------{proc_name} stderr------------------------------");
1815 println!("{}", err);
1816 println!("------------------------------------------");
1817 }
1818
1819 fn dump_output_file(&self, out: &str, extension: &str) {
1820 let outfile = self.make_out_name(extension);
1821 fs::write(outfile.as_std_path(), out).unwrap();
1822 }
1823
1824 fn make_out_name(&self, extension: &str) -> Utf8PathBuf {
1827 self.output_base_name().with_extension(extension)
1828 }
1829
1830 fn aux_output_dir_name(&self) -> Utf8PathBuf {
1833 self.output_base_dir()
1834 .join("auxiliary")
1835 .with_extra_extension(self.config.mode.aux_dir_disambiguator())
1836 }
1837
1838 fn aux_bin_output_dir_name(&self) -> Utf8PathBuf {
1841 self.aux_output_dir_name().join("bin")
1842 }
1843
1844 fn output_testname_unique(&self) -> Utf8PathBuf {
1846 output_testname_unique(self.config, self.testpaths, self.safe_revision())
1847 }
1848
1849 fn safe_revision(&self) -> Option<&str> {
1852 if self.config.mode == Incremental { None } else { self.revision }
1853 }
1854
1855 fn output_base_dir(&self) -> Utf8PathBuf {
1859 output_base_dir(self.config, self.testpaths, self.safe_revision())
1860 }
1861
1862 fn output_base_name(&self) -> Utf8PathBuf {
1866 output_base_name(self.config, self.testpaths, self.safe_revision())
1867 }
1868
1869 fn error(&self, err: &str) {
1870 match self.revision {
1871 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1872 None => println!("\nerror: {}", err),
1873 }
1874 }
1875
1876 #[track_caller]
1877 fn fatal(&self, err: &str) -> ! {
1878 self.error(err);
1879 error!("fatal error, panic: {:?}", err);
1880 panic!("fatal error");
1881 }
1882
1883 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1884 self.error(err);
1885 proc_res.fatal(None, || ());
1886 }
1887
1888 fn fatal_proc_rec_with_ctx(
1889 &self,
1890 err: &str,
1891 proc_res: &ProcRes,
1892 on_failure: impl FnOnce(Self),
1893 ) -> ! {
1894 self.error(err);
1895 proc_res.fatal(None, || on_failure(*self));
1896 }
1897
1898 fn compile_test_and_save_ir(&self) -> (ProcRes, Utf8PathBuf) {
1901 let output_path = self.output_base_name().with_extension("ll");
1902 let input_file = &self.testpaths.file;
1903 let rustc = self.make_compile_args(
1904 input_file,
1905 TargetLocation::ThisFile(output_path.clone()),
1906 Emit::LlvmIr,
1907 AllowUnused::No,
1908 LinkToAux::Yes,
1909 Vec::new(),
1910 );
1911
1912 let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths);
1913 (proc_res, output_path)
1914 }
1915
1916 fn verify_with_filecheck(&self, output: &Utf8Path) -> ProcRes {
1917 let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
1918 filecheck.arg("--input-file").arg(output).arg(&self.testpaths.file);
1919
1920 filecheck.arg("--check-prefix=CHECK");
1922
1923 if let Some(rev) = self.revision {
1931 filecheck.arg("--check-prefix").arg(rev);
1932 }
1933
1934 filecheck.arg("--allow-unused-prefixes");
1938
1939 filecheck.args(&["--dump-input-context", "100"]);
1941
1942 filecheck.args(&self.props.filecheck_flags);
1944
1945 self.compose_and_run(filecheck, Utf8Path::new(""), None, None)
1947 }
1948
1949 fn charset() -> &'static str {
1950 if cfg!(target_os = "freebsd") { "ISO-8859-1" } else { "UTF-8" }
1952 }
1953
1954 fn compare_to_default_rustdoc(&mut self, out_dir: &Utf8Path) {
1955 if !self.config.has_html_tidy {
1956 return;
1957 }
1958 println!("info: generating a diff against nightly rustdoc");
1959
1960 let suffix =
1961 self.safe_revision().map_or("nightly".into(), |path| path.to_owned() + "-nightly");
1962 let compare_dir = output_base_dir(self.config, self.testpaths, Some(&suffix));
1963 remove_and_create_dir_all(&compare_dir).unwrap_or_else(|e| {
1964 panic!("failed to remove and recreate output directory `{compare_dir}`: {e}")
1965 });
1966
1967 let new_rustdoc = TestCx {
1969 config: &Config {
1970 rustdoc_path: Some("rustdoc".into()),
1973 rustc_path: "rustc".into(),
1975 ..self.config.clone()
1976 },
1977 ..*self
1978 };
1979
1980 let output_file = TargetLocation::ThisDirectory(new_rustdoc.aux_output_dir_name());
1981 let mut rustc = new_rustdoc.make_compile_args(
1982 &new_rustdoc.testpaths.file,
1983 output_file,
1984 Emit::None,
1985 AllowUnused::Yes,
1986 LinkToAux::Yes,
1987 Vec::new(),
1988 );
1989 let aux_dir = new_rustdoc.aux_output_dir();
1990 new_rustdoc.build_all_auxiliary(&new_rustdoc.testpaths, &aux_dir, &mut rustc);
1991
1992 let proc_res = new_rustdoc.document(&compare_dir, &new_rustdoc.testpaths);
1993 if !proc_res.status.success() {
1994 eprintln!("failed to run nightly rustdoc");
1995 return;
1996 }
1997
1998 #[rustfmt::skip]
1999 let tidy_args = [
2000 "--new-blocklevel-tags", "rustdoc-search,rustdoc-toolbar",
2001 "--indent", "yes",
2002 "--indent-spaces", "2",
2003 "--wrap", "0",
2004 "--show-warnings", "no",
2005 "--markup", "yes",
2006 "--quiet", "yes",
2007 "-modify",
2008 ];
2009 let tidy_dir = |dir| {
2010 for entry in walkdir::WalkDir::new(dir) {
2011 let entry = entry.expect("failed to read file");
2012 if entry.file_type().is_file()
2013 && entry.path().extension().and_then(|p| p.to_str()) == Some("html")
2014 {
2015 let status =
2016 Command::new("tidy").args(&tidy_args).arg(entry.path()).status().unwrap();
2017 assert!(status.success() || status.code() == Some(1));
2019 }
2020 }
2021 };
2022 tidy_dir(out_dir);
2023 tidy_dir(&compare_dir);
2024
2025 let pager = {
2026 let output = Command::new("git").args(&["config", "--get", "core.pager"]).output().ok();
2027 output.and_then(|out| {
2028 if out.status.success() {
2029 Some(String::from_utf8(out.stdout).expect("invalid UTF8 in git pager"))
2030 } else {
2031 None
2032 }
2033 })
2034 };
2035
2036 let diff_filename = format!("build/tmp/rustdoc-compare-{}.diff", std::process::id());
2037
2038 if !write_filtered_diff(
2039 &diff_filename,
2040 out_dir,
2041 &compare_dir,
2042 self.config.verbose,
2043 |file_type, extension| {
2044 file_type.is_file() && (extension == Some("html") || extension == Some("js"))
2045 },
2046 ) {
2047 return;
2048 }
2049
2050 match self.config.color {
2051 ColorConfig::AlwaysColor => colored::control::set_override(true),
2052 ColorConfig::NeverColor => colored::control::set_override(false),
2053 _ => {}
2054 }
2055
2056 if let Some(pager) = pager {
2057 let pager = pager.trim();
2058 if self.config.verbose {
2059 eprintln!("using pager {}", pager);
2060 }
2061 let output = Command::new(pager)
2062 .env("PAGER", "")
2064 .stdin(File::open(&diff_filename).unwrap())
2065 .output()
2068 .unwrap();
2069 assert!(output.status.success());
2070 println!("{}", String::from_utf8_lossy(&output.stdout));
2071 eprintln!("{}", String::from_utf8_lossy(&output.stderr));
2072 } else {
2073 use colored::Colorize;
2074 eprintln!("warning: no pager configured, falling back to unified diff");
2075 eprintln!(
2076 "help: try configuring a git pager (e.g. `delta`) with `git config --global core.pager delta`"
2077 );
2078 let mut out = io::stdout();
2079 let mut diff = BufReader::new(File::open(&diff_filename).unwrap());
2080 let mut line = Vec::new();
2081 loop {
2082 line.truncate(0);
2083 match diff.read_until(b'\n', &mut line) {
2084 Ok(0) => break,
2085 Ok(_) => {}
2086 Err(e) => eprintln!("ERROR: {:?}", e),
2087 }
2088 match String::from_utf8(line.clone()) {
2089 Ok(line) => {
2090 if line.starts_with('+') {
2091 write!(&mut out, "{}", line.green()).unwrap();
2092 } else if line.starts_with('-') {
2093 write!(&mut out, "{}", line.red()).unwrap();
2094 } else if line.starts_with('@') {
2095 write!(&mut out, "{}", line.blue()).unwrap();
2096 } else {
2097 out.write_all(line.as_bytes()).unwrap();
2098 }
2099 }
2100 Err(_) => {
2101 write!(&mut out, "{}", String::from_utf8_lossy(&line).reversed()).unwrap();
2102 }
2103 }
2104 }
2105 };
2106 }
2107
2108 fn get_lines(&self, path: &Utf8Path, mut other_files: Option<&mut Vec<String>>) -> Vec<usize> {
2109 let content = fs::read_to_string(path.as_std_path()).unwrap();
2110 let mut ignore = false;
2111 content
2112 .lines()
2113 .enumerate()
2114 .filter_map(|(line_nb, line)| {
2115 if (line.trim_start().starts_with("pub mod ")
2116 || line.trim_start().starts_with("mod "))
2117 && line.ends_with(';')
2118 {
2119 if let Some(ref mut other_files) = other_files {
2120 other_files.push(line.rsplit("mod ").next().unwrap().replace(';', ""));
2121 }
2122 None
2123 } else {
2124 let sline = line.rsplit("///").next().unwrap();
2125 let line = sline.trim_start();
2126 if line.starts_with("```") {
2127 if ignore {
2128 ignore = false;
2129 None
2130 } else {
2131 ignore = true;
2132 Some(line_nb + 1)
2133 }
2134 } else {
2135 None
2136 }
2137 }
2138 })
2139 .collect()
2140 }
2141
2142 fn check_rustdoc_test_option(&self, res: ProcRes) {
2147 let mut other_files = Vec::new();
2148 let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2149 let normalized = fs::canonicalize(&self.testpaths.file).expect("failed to canonicalize");
2150 let normalized = normalized.to_str().unwrap().replace('\\', "/");
2151 files.insert(normalized, self.get_lines(&self.testpaths.file, Some(&mut other_files)));
2152 for other_file in other_files {
2153 let mut path = self.testpaths.file.clone();
2154 path.set_file_name(&format!("{}.rs", other_file));
2155 let path = path.canonicalize_utf8().expect("failed to canonicalize");
2156 let normalized = path.as_str().replace('\\', "/");
2157 files.insert(normalized, self.get_lines(&path, None));
2158 }
2159
2160 let mut tested = 0;
2161 for _ in res.stdout.split('\n').filter(|s| s.starts_with("test ")).inspect(|s| {
2162 if let Some((left, right)) = s.split_once(" - ") {
2163 let path = left.rsplit("test ").next().unwrap();
2164 let path = fs::canonicalize(&path).expect("failed to canonicalize");
2165 let path = path.to_str().unwrap().replace('\\', "/");
2166 if let Some(ref mut v) = files.get_mut(&path) {
2167 tested += 1;
2168 let mut iter = right.split("(line ");
2169 iter.next();
2170 let line = iter
2171 .next()
2172 .unwrap_or(")")
2173 .split(')')
2174 .next()
2175 .unwrap_or("0")
2176 .parse()
2177 .unwrap_or(0);
2178 if let Ok(pos) = v.binary_search(&line) {
2179 v.remove(pos);
2180 } else {
2181 self.fatal_proc_rec(
2182 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2183 &res,
2184 );
2185 }
2186 }
2187 }
2188 }) {}
2189 if tested == 0 {
2190 self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2191 } else {
2192 for (entry, v) in &files {
2193 if !v.is_empty() {
2194 self.fatal_proc_rec(
2195 &format!(
2196 "Not found test at line{} \"{}\":{:?}",
2197 if v.len() > 1 { "s" } else { "" },
2198 entry,
2199 v
2200 ),
2201 &res,
2202 );
2203 }
2204 }
2205 }
2206 }
2207
2208 fn force_color_svg(&self) -> bool {
2209 self.props.compile_flags.iter().any(|s| s.contains("--color=always"))
2210 }
2211
2212 fn load_compare_outputs(
2213 &self,
2214 proc_res: &ProcRes,
2215 output_kind: TestOutput,
2216 explicit_format: bool,
2217 ) -> usize {
2218 let stderr_bits = format!("{}bit.stderr", self.config.get_pointer_width());
2219 let (stderr_kind, stdout_kind) = match output_kind {
2220 TestOutput::Compile => (
2221 if self.force_color_svg() {
2222 if self.config.target.contains("windows") {
2223 UI_WINDOWS_SVG
2226 } else {
2227 UI_SVG
2228 }
2229 } else if self.props.stderr_per_bitwidth {
2230 &stderr_bits
2231 } else {
2232 UI_STDERR
2233 },
2234 UI_STDOUT,
2235 ),
2236 TestOutput::Run => (UI_RUN_STDERR, UI_RUN_STDOUT),
2237 };
2238
2239 let expected_stderr = self.load_expected_output(stderr_kind);
2240 let expected_stdout = self.load_expected_output(stdout_kind);
2241
2242 let mut normalized_stdout =
2243 self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2244 match output_kind {
2245 TestOutput::Run if self.config.remote_test_client.is_some() => {
2246 normalized_stdout = static_regex!(
2251 "^uploaded \"\\$TEST_BUILD_DIR(/[[:alnum:]_\\-.]+)+\", waiting for result\n"
2252 )
2253 .replace(&normalized_stdout, "")
2254 .to_string();
2255 normalized_stdout = static_regex!("^died due to signal [0-9]+\n")
2258 .replace(&normalized_stdout, "")
2259 .to_string();
2260 }
2263 _ => {}
2264 };
2265
2266 let stderr = if self.force_color_svg() {
2267 anstyle_svg::Term::new().render_svg(&proc_res.stderr)
2268 } else if explicit_format {
2269 proc_res.stderr.clone()
2270 } else {
2271 json::extract_rendered(&proc_res.stderr)
2272 };
2273
2274 let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2275 let mut errors = 0;
2276 match output_kind {
2277 TestOutput::Compile => {
2278 if !self.props.dont_check_compiler_stdout {
2279 if self
2280 .compare_output(
2281 stdout_kind,
2282 &normalized_stdout,
2283 &proc_res.stdout,
2284 &expected_stdout,
2285 )
2286 .should_error()
2287 {
2288 errors += 1;
2289 }
2290 }
2291 if !self.props.dont_check_compiler_stderr {
2292 if self
2293 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2294 .should_error()
2295 {
2296 errors += 1;
2297 }
2298 }
2299 }
2300 TestOutput::Run => {
2301 if self
2302 .compare_output(
2303 stdout_kind,
2304 &normalized_stdout,
2305 &proc_res.stdout,
2306 &expected_stdout,
2307 )
2308 .should_error()
2309 {
2310 errors += 1;
2311 }
2312
2313 if self
2314 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2315 .should_error()
2316 {
2317 errors += 1;
2318 }
2319 }
2320 }
2321 errors
2322 }
2323
2324 fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2325 let rflags = self.props.run_flags.join(" ");
2328 let cflags = self.props.compile_flags.join(" ");
2329 let json = rflags.contains("--format json")
2330 || rflags.contains("--format=json")
2331 || cflags.contains("--error-format json")
2332 || cflags.contains("--error-format pretty-json")
2333 || cflags.contains("--error-format=json")
2334 || cflags.contains("--error-format=pretty-json")
2335 || cflags.contains("--output-format json")
2336 || cflags.contains("--output-format=json");
2337
2338 let mut normalized = output.to_string();
2339
2340 let mut normalize_path = |from: &Utf8Path, to: &str| {
2341 let from = if json { &from.as_str().replace("\\", "\\\\") } else { from.as_str() };
2342
2343 normalized = normalized.replace(from, to);
2344 };
2345
2346 let parent_dir = self.testpaths.file.parent().unwrap();
2347 normalize_path(parent_dir, "$DIR");
2348
2349 if self.props.remap_src_base {
2350 let mut remapped_parent_dir = Utf8PathBuf::from(FAKE_SRC_BASE);
2351 if self.testpaths.relative_dir != Utf8Path::new("") {
2352 remapped_parent_dir.push(&self.testpaths.relative_dir);
2353 }
2354 normalize_path(&remapped_parent_dir, "$DIR");
2355 }
2356
2357 let base_dir = Utf8Path::new("/rustc/FAKE_PREFIX");
2358 normalize_path(&base_dir.join("library"), "$SRC_DIR");
2360 normalize_path(&base_dir.join("compiler"), "$COMPILER_DIR");
2364
2365 let rust_src_dir = &self.config.sysroot_base.join("lib/rustlib/src/rust");
2367 rust_src_dir.try_exists().expect(&*format!("{} should exists", rust_src_dir));
2368 let rust_src_dir = rust_src_dir.read_link_utf8().unwrap_or(rust_src_dir.to_path_buf());
2369 normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
2370
2371 normalize_path(&self.output_base_dir(), "$TEST_BUILD_DIR");
2374 normalize_path(&self.output_base_dir().canonicalize_utf8().unwrap(), "$TEST_BUILD_DIR");
2381 normalize_path(&self.config.build_root, "$BUILD_DIR");
2383
2384 if json {
2385 normalized = normalized.replace("\\n", "\n");
2390 }
2391
2392 normalized = static_regex!("SRC_DIR(.+):\\d+:\\d+(: \\d+:\\d+)?")
2397 .replace_all(&normalized, "SRC_DIR$1:LL:COL")
2398 .into_owned();
2399
2400 normalized = Self::normalize_platform_differences(&normalized);
2401
2402 normalized =
2404 static_regex!(r"\$TEST_BUILD_DIR/(?P<filename>[^\.]+).long-type-(?P<hash>\d+).txt")
2405 .replace_all(&normalized, |caps: &Captures<'_>| {
2406 format!(
2407 "$TEST_BUILD_DIR/{filename}.long-type-$LONG_TYPE_HASH.txt",
2408 filename = &caps["filename"]
2409 )
2410 })
2411 .into_owned();
2412
2413 normalized = normalized.replace("\t", "\\t"); normalized =
2420 static_regex!("\\s*//(\\[.*\\])?~.*").replace_all(&normalized, "").into_owned();
2421
2422 let v0_crate_hash_prefix_re = static_regex!(r"_R.*?Cs[0-9a-zA-Z]+_");
2425 let v0_crate_hash_re = static_regex!(r"Cs[0-9a-zA-Z]+_");
2426
2427 const V0_CRATE_HASH_PLACEHOLDER: &str = r"CsCRATE_HASH_";
2428 if v0_crate_hash_prefix_re.is_match(&normalized) {
2429 normalized =
2431 v0_crate_hash_re.replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER).into_owned();
2432 }
2433
2434 let v0_back_ref_prefix_re = static_regex!(r"\(_R.*?B[0-9a-zA-Z]_");
2435 let v0_back_ref_re = static_regex!(r"B[0-9a-zA-Z]_");
2436
2437 const V0_BACK_REF_PLACEHOLDER: &str = r"B<REF>_";
2438 if v0_back_ref_prefix_re.is_match(&normalized) {
2439 normalized =
2441 v0_back_ref_re.replace_all(&normalized, V0_BACK_REF_PLACEHOLDER).into_owned();
2442 }
2443
2444 {
2451 let mut seen_allocs = indexmap::IndexSet::new();
2452
2453 normalized = static_regex!(
2455 r"╾─*a(lloc)?([0-9]+)(\+0x[0-9]+)?(<imm>)?( \([0-9]+ ptr bytes\))?─*╼"
2456 )
2457 .replace_all(&normalized, |caps: &Captures<'_>| {
2458 let index = caps.get(2).unwrap().as_str().to_string();
2460 let (index, _) = seen_allocs.insert_full(index);
2461 let offset = caps.get(3).map_or("", |c| c.as_str());
2462 let imm = caps.get(4).map_or("", |c| c.as_str());
2463 format!("╾ALLOC{index}{offset}{imm}╼")
2465 })
2466 .into_owned();
2467
2468 normalized = static_regex!(r"\balloc([0-9]+)\b")
2470 .replace_all(&normalized, |caps: &Captures<'_>| {
2471 let index = caps.get(1).unwrap().as_str().to_string();
2472 let (index, _) = seen_allocs.insert_full(index);
2473 format!("ALLOC{index}")
2474 })
2475 .into_owned();
2476 }
2477
2478 for rule in custom_rules {
2480 let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
2481 normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
2482 }
2483 normalized
2484 }
2485
2486 fn normalize_platform_differences(output: &str) -> String {
2492 let output = output.replace(r"\\", r"\");
2493
2494 static_regex!(
2499 r#"(?x)
2500 (?:
2501 # Match paths that don't include spaces.
2502 (?:\\[\pL\pN\.\-_']+)+\.\pL+
2503 |
2504 # If the path starts with a well-known root, then allow spaces and no file extension.
2505 \$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_'\ ]+)+
2506 )"#
2507 )
2508 .replace_all(&output, |caps: &Captures<'_>| {
2509 println!("{}", &caps[0]);
2510 caps[0].replace(r"\", "/")
2511 })
2512 .replace("\r\n", "\n")
2513 }
2514
2515 fn expected_output_path(&self, kind: &str) -> Utf8PathBuf {
2516 let mut path =
2517 expected_output_path(&self.testpaths, self.revision, &self.config.compare_mode, kind);
2518
2519 if !path.exists() {
2520 if let Some(CompareMode::Polonius) = self.config.compare_mode {
2521 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2522 }
2523 }
2524
2525 if !path.exists() {
2526 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2527 }
2528
2529 path
2530 }
2531
2532 fn load_expected_output(&self, kind: &str) -> String {
2533 let path = self.expected_output_path(kind);
2534 if path.exists() {
2535 match self.load_expected_output_from_path(&path) {
2536 Ok(x) => x,
2537 Err(x) => self.fatal(&x),
2538 }
2539 } else {
2540 String::new()
2541 }
2542 }
2543
2544 fn load_expected_output_from_path(&self, path: &Utf8Path) -> Result<String, String> {
2545 fs::read_to_string(path)
2546 .map_err(|err| format!("failed to load expected output from `{}`: {}", path, err))
2547 }
2548
2549 fn delete_file(&self, file: &Utf8Path) {
2550 if !file.exists() {
2551 return;
2553 }
2554 if let Err(e) = fs::remove_file(file.as_std_path()) {
2555 self.fatal(&format!("failed to delete `{}`: {}", file, e,));
2556 }
2557 }
2558
2559 fn compare_output(
2560 &self,
2561 stream: &str,
2562 actual: &str,
2563 actual_unnormalized: &str,
2564 expected: &str,
2565 ) -> CompareOutcome {
2566 let expected_path =
2567 expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2568
2569 if self.config.bless && actual.is_empty() && expected_path.exists() {
2570 self.delete_file(&expected_path);
2571 }
2572
2573 let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
2574 (true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
2577 _ => expected != actual,
2578 };
2579 if !are_different {
2580 return CompareOutcome::Same;
2581 }
2582
2583 let compare_output_by_lines = self.config.runner.is_some();
2587
2588 let tmp;
2589 let (expected, actual): (&str, &str) = if compare_output_by_lines {
2590 let actual_lines: HashSet<_> = actual.lines().collect();
2591 let expected_lines: Vec<_> = expected.lines().collect();
2592 let mut used = expected_lines.clone();
2593 used.retain(|line| actual_lines.contains(line));
2594 if used.len() == expected_lines.len() && (expected.is_empty() == actual.is_empty()) {
2596 return CompareOutcome::Same;
2597 }
2598 if expected_lines.is_empty() {
2599 ("", actual)
2601 } else {
2602 tmp = (expected_lines.join("\n"), used.join("\n"));
2603 (&tmp.0, &tmp.1)
2604 }
2605 } else {
2606 (expected, actual)
2607 };
2608
2609 let test_name = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
2611 let actual_path = self
2612 .output_base_name()
2613 .with_extra_extension(self.revision.unwrap_or(""))
2614 .with_extra_extension(test_name)
2615 .with_extra_extension(stream);
2616
2617 if let Err(err) = fs::write(&actual_path, &actual) {
2618 self.fatal(&format!("failed to write {stream} to `{actual_path:?}`: {err}",));
2619 }
2620 println!("Saved the actual {stream} to {actual_path:?}");
2621
2622 if !self.config.bless {
2623 if expected.is_empty() {
2624 println!("normalized {}:\n{}\n", stream, actual);
2625 } else {
2626 self.show_diff(
2627 stream,
2628 &expected_path,
2629 &actual_path,
2630 expected,
2631 actual,
2632 actual_unnormalized,
2633 );
2634 }
2635 } else {
2636 if self.revision.is_some() {
2639 let old =
2640 expected_output_path(self.testpaths, None, &self.config.compare_mode, stream);
2641 self.delete_file(&old);
2642 }
2643
2644 if !actual.is_empty() {
2645 if let Err(err) = fs::write(&expected_path, &actual) {
2646 self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
2647 }
2648 println!("Blessing the {stream} of {test_name} in {expected_path:?}");
2649 }
2650 }
2651
2652 println!("\nThe actual {0} differed from the expected {0}.", stream);
2653
2654 if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
2655 }
2656
2657 fn show_diff(
2659 &self,
2660 stream: &str,
2661 expected_path: &Utf8Path,
2662 actual_path: &Utf8Path,
2663 expected: &str,
2664 actual: &str,
2665 actual_unnormalized: &str,
2666 ) {
2667 eprintln!("diff of {stream}:\n");
2668 if let Some(diff_command) = self.config.diff_command.as_deref() {
2669 let mut args = diff_command.split_whitespace();
2670 let name = args.next().unwrap();
2671 match Command::new(name).args(args).args([expected_path, actual_path]).output() {
2672 Err(err) => {
2673 self.fatal(&format!(
2674 "failed to call custom diff command `{diff_command}`: {err}"
2675 ));
2676 }
2677 Ok(output) => {
2678 let output = String::from_utf8_lossy(&output.stdout);
2679 eprint!("{output}");
2680 }
2681 }
2682 } else {
2683 eprint!("{}", write_diff(expected, actual, 3));
2684 }
2685
2686 let diff_results = make_diff(actual, expected, 0);
2688
2689 let (mut mismatches_normalized, mut mismatch_line_nos) = (String::new(), vec![]);
2690 for hunk in diff_results {
2691 let mut line_no = hunk.line_number;
2692 for line in hunk.lines {
2693 if let DiffLine::Expected(normalized) = line {
2695 mismatches_normalized += &normalized;
2696 mismatches_normalized += "\n";
2697 mismatch_line_nos.push(line_no);
2698 line_no += 1;
2699 }
2700 }
2701 }
2702 let mut mismatches_unnormalized = String::new();
2703 let diff_normalized = make_diff(actual, actual_unnormalized, 0);
2704 for hunk in diff_normalized {
2705 if mismatch_line_nos.contains(&hunk.line_number) {
2706 for line in hunk.lines {
2707 if let DiffLine::Resulting(unnormalized) = line {
2708 mismatches_unnormalized += &unnormalized;
2709 mismatches_unnormalized += "\n";
2710 }
2711 }
2712 }
2713 }
2714
2715 let normalized_diff = make_diff(&mismatches_normalized, &mismatches_unnormalized, 0);
2716 if !normalized_diff.is_empty()
2718 && !mismatches_unnormalized.is_empty()
2719 && !mismatches_normalized.is_empty()
2720 {
2721 eprintln!("Note: some mismatched output was normalized before being compared");
2722 eprint!("{}", write_diff(&mismatches_unnormalized, &mismatches_normalized, 0));
2724 }
2725 }
2726
2727 fn check_and_prune_duplicate_outputs(
2728 &self,
2729 proc_res: &ProcRes,
2730 modes: &[CompareMode],
2731 require_same_modes: &[CompareMode],
2732 ) {
2733 for kind in UI_EXTENSIONS {
2734 let canon_comparison_path =
2735 expected_output_path(&self.testpaths, self.revision, &None, kind);
2736
2737 let canon = match self.load_expected_output_from_path(&canon_comparison_path) {
2738 Ok(canon) => canon,
2739 _ => continue,
2740 };
2741 let bless = self.config.bless;
2742 let check_and_prune_duplicate_outputs = |mode: &CompareMode, require_same: bool| {
2743 let examined_path =
2744 expected_output_path(&self.testpaths, self.revision, &Some(mode.clone()), kind);
2745
2746 let examined_content = match self.load_expected_output_from_path(&examined_path) {
2748 Ok(content) => content,
2749 _ => return,
2750 };
2751
2752 let is_duplicate = canon == examined_content;
2753
2754 match (bless, require_same, is_duplicate) {
2755 (true, _, true) => {
2757 self.delete_file(&examined_path);
2758 }
2759 (_, true, false) => {
2762 self.fatal_proc_rec(
2763 &format!("`{}` should not have different output from base test!", kind),
2764 proc_res,
2765 );
2766 }
2767 _ => {}
2768 }
2769 };
2770 for mode in modes {
2771 check_and_prune_duplicate_outputs(mode, false);
2772 }
2773 for mode in require_same_modes {
2774 check_and_prune_duplicate_outputs(mode, true);
2775 }
2776 }
2777 }
2778
2779 fn create_stamp(&self) {
2780 let stamp_file_path = stamp_file_path(&self.config, self.testpaths, self.revision);
2781 fs::write(&stamp_file_path, compute_stamp_hash(&self.config)).unwrap();
2782 }
2783
2784 fn init_incremental_test(&self) {
2785 let incremental_dir = self.props.incremental_dir.as_ref().unwrap();
2792 if incremental_dir.exists() {
2793 let canonicalized = incremental_dir.canonicalize().unwrap();
2796 fs::remove_dir_all(canonicalized).unwrap();
2797 }
2798 fs::create_dir_all(&incremental_dir).unwrap();
2799
2800 if self.config.verbose {
2801 println!("init_incremental_test: incremental_dir={incremental_dir}");
2802 }
2803 }
2804}
2805
2806struct ProcArgs {
2807 prog: OsString,
2808 args: Vec<OsString>,
2809}
2810
2811pub struct ProcRes {
2812 status: ExitStatus,
2813 stdout: String,
2814 stderr: String,
2815 truncated: Truncated,
2816 cmdline: String,
2817}
2818
2819impl ProcRes {
2820 pub fn print_info(&self) {
2821 fn render(name: &str, contents: &str) -> String {
2822 let contents = json::extract_rendered(contents);
2823 let contents = contents.trim_end();
2824 if contents.is_empty() {
2825 format!("{name}: none")
2826 } else {
2827 format!(
2828 "\
2829 --- {name} -------------------------------\n\
2830 {contents}\n\
2831 ------------------------------------------",
2832 )
2833 }
2834 }
2835
2836 println!(
2837 "status: {}\ncommand: {}\n{}\n{}\n",
2838 self.status,
2839 self.cmdline,
2840 render("stdout", &self.stdout),
2841 render("stderr", &self.stderr),
2842 );
2843 }
2844
2845 pub fn fatal(&self, err: Option<&str>, on_failure: impl FnOnce()) -> ! {
2846 if let Some(e) = err {
2847 println!("\nerror: {}", e);
2848 }
2849 self.print_info();
2850 on_failure();
2851 std::panic::resume_unwind(Box::new(()));
2854 }
2855}
2856
2857#[derive(Debug)]
2858enum TargetLocation {
2859 ThisFile(Utf8PathBuf),
2860 ThisDirectory(Utf8PathBuf),
2861}
2862
2863enum AllowUnused {
2864 Yes,
2865 No,
2866}
2867
2868enum LinkToAux {
2869 Yes,
2870 No,
2871}
2872
2873#[derive(Debug, PartialEq)]
2874enum AuxType {
2875 Bin,
2876 Lib,
2877 Dylib,
2878 ProcMacro,
2879}
2880
2881#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2884enum CompareOutcome {
2885 Same,
2887 Blessed,
2889 Differed,
2891}
2892
2893impl CompareOutcome {
2894 fn should_error(&self) -> bool {
2895 matches!(self, CompareOutcome::Differed)
2896 }
2897}