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, fmt, iter, str};
11
12use build_helper::fs::remove_and_create_dir_all;
13use camino::{Utf8Path, Utf8PathBuf};
14use colored::{Color, Colorize};
15use regex::{Captures, Regex};
16use tracing::*;
17
18use crate::common::{
19 CompareMode, Config, Debugger, FailMode, PassMode, RunFailMode, RunResult, TestMode, TestPaths,
20 TestSuite, UI_EXTENSIONS, UI_FIXED, UI_RUN_STDERR, UI_RUN_STDOUT, UI_STDERR, UI_STDOUT, UI_SVG,
21 UI_WINDOWS_SVG, expected_output_path, incremental_dir, output_base_dir, output_base_name,
22 output_testname_unique,
23};
24use crate::directives::TestProps;
25use crate::errors::{Error, ErrorKind, load_errors};
26use crate::output_capture::ConsoleOut;
27use crate::read2::{Truncated, read2_abbreviated};
28use crate::runtest::compute_diff::{DiffLine, make_diff, write_diff, write_filtered_diff};
29use crate::util::{Utf8PathBufExt, add_dylib_path, static_regex};
30use crate::{ColorConfig, help, json, stamp_file_path, warning};
31
32mod assembly;
35mod codegen;
36mod codegen_units;
37mod coverage;
38mod crashes;
39mod debuginfo;
40mod incremental;
41mod js_doc;
42mod mir_opt;
43mod pretty;
44mod run_make;
45mod rustdoc;
46mod rustdoc_json;
47mod ui;
48mod compute_diff;
51mod debugger;
52#[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(
113 config: Arc<Config>,
114 stdout: &dyn ConsoleOut,
115 stderr: &dyn ConsoleOut,
116 testpaths: &TestPaths,
117 revision: Option<&str>,
118) {
119 match &*config.target {
120 "arm-linux-androideabi"
121 | "armv7-linux-androideabi"
122 | "thumbv7neon-linux-androideabi"
123 | "aarch64-linux-android" => {
124 if !config.adb_device_status {
125 panic!("android device not available");
126 }
127 }
128
129 _ => {
130 if config.debugger == Some(Debugger::Gdb) && config.gdb.is_none() {
134 panic!("gdb not available but debuginfo gdb debuginfo test requested");
135 }
136 }
137 }
138
139 if config.verbose {
140 write!(stdout, "\n\n");
142 }
143 debug!("running {}", testpaths.file);
144 let mut props = TestProps::from_file(&testpaths.file, revision, &config);
145
146 if props.incremental {
150 props.incremental_dir = Some(incremental_dir(&config, testpaths, revision));
151 }
152
153 let cx = TestCx { config: &config, stdout, stderr, props: &props, testpaths, revision };
154
155 if let Err(e) = create_dir_all(&cx.output_base_dir()) {
156 panic!("failed to create output base directory {}: {e}", cx.output_base_dir());
157 }
158
159 if props.incremental {
160 cx.init_incremental_test();
161 }
162
163 if config.mode == TestMode::Incremental {
164 assert!(!props.revisions.is_empty(), "Incremental tests require revisions.");
167 for revision in &props.revisions {
168 let mut revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
169 revision_props.incremental_dir = props.incremental_dir.clone();
170 let rev_cx = TestCx {
171 config: &config,
172 stdout,
173 stderr,
174 props: &revision_props,
175 testpaths,
176 revision: Some(revision),
177 };
178 rev_cx.run_revision();
179 }
180 } else {
181 cx.run_revision();
182 }
183
184 cx.create_stamp();
185}
186
187pub fn compute_stamp_hash(config: &Config) -> String {
188 let mut hash = DefaultHasher::new();
189 config.stage_id.hash(&mut hash);
190 config.run.hash(&mut hash);
191 config.edition.hash(&mut hash);
192
193 match config.debugger {
194 Some(Debugger::Cdb) => {
195 config.cdb.hash(&mut hash);
196 }
197
198 Some(Debugger::Gdb) => {
199 config.gdb.hash(&mut hash);
200 env::var_os("PATH").hash(&mut hash);
201 env::var_os("PYTHONPATH").hash(&mut hash);
202 }
203
204 Some(Debugger::Lldb) => {
205 config.python.hash(&mut hash);
206 config.lldb_python_dir.hash(&mut hash);
207 env::var_os("PATH").hash(&mut hash);
208 env::var_os("PYTHONPATH").hash(&mut hash);
209 }
210
211 None => {}
212 }
213
214 if config.mode == TestMode::Ui {
215 config.force_pass_mode.hash(&mut hash);
216 }
217
218 format!("{:x}", hash.finish())
219}
220
221#[derive(Copy, Clone, Debug)]
222struct TestCx<'test> {
223 config: &'test Config,
224 stdout: &'test dyn ConsoleOut,
225 stderr: &'test dyn ConsoleOut,
226 props: &'test TestProps,
227 testpaths: &'test TestPaths,
228 revision: Option<&'test str>,
229}
230
231enum ReadFrom {
232 Path,
233 Stdin(String),
234}
235
236enum TestOutput {
237 Compile,
238 Run,
239}
240
241#[derive(Copy, Clone, PartialEq)]
243enum WillExecute {
244 Yes,
245 No,
246 Disabled,
247}
248
249#[derive(Copy, Clone)]
251enum Emit {
252 None,
253 Metadata,
254 LlvmIr,
255 Mir,
256 Asm,
257 LinkArgsAsm,
258}
259
260impl<'test> TestCx<'test> {
261 fn run_revision(&self) {
264 if self.props.should_ice
265 && self.config.mode != TestMode::Incremental
266 && self.config.mode != TestMode::Crashes
267 {
268 self.fatal("cannot use should-ice in a test that is not cfail");
269 }
270 match self.config.mode {
271 TestMode::Pretty => self.run_pretty_test(),
272 TestMode::DebugInfo => self.run_debuginfo_test(),
273 TestMode::Codegen => self.run_codegen_test(),
274 TestMode::Rustdoc => self.run_rustdoc_test(),
275 TestMode::RustdocJson => self.run_rustdoc_json_test(),
276 TestMode::CodegenUnits => self.run_codegen_units_test(),
277 TestMode::Incremental => self.run_incremental_test(),
278 TestMode::RunMake => self.run_rmake_test(),
279 TestMode::Ui => self.run_ui_test(),
280 TestMode::MirOpt => self.run_mir_opt_test(),
281 TestMode::Assembly => self.run_assembly_test(),
282 TestMode::RustdocJs => self.run_rustdoc_js_test(),
283 TestMode::CoverageMap => self.run_coverage_map_test(), TestMode::CoverageRun => self.run_coverage_run_test(), TestMode::Crashes => self.run_crash_test(),
286 }
287 }
288
289 fn pass_mode(&self) -> Option<PassMode> {
290 self.props.pass_mode(self.config)
291 }
292
293 fn should_run(&self, pm: Option<PassMode>) -> WillExecute {
294 let test_should_run = match self.config.mode {
295 TestMode::Ui
296 if pm == Some(PassMode::Run)
297 || matches!(self.props.fail_mode, Some(FailMode::Run(_))) =>
298 {
299 true
300 }
301 TestMode::MirOpt if pm == Some(PassMode::Run) => true,
302 TestMode::Ui | TestMode::MirOpt => false,
303 mode => panic!("unimplemented for mode {:?}", mode),
304 };
305 if test_should_run { self.run_if_enabled() } else { WillExecute::No }
306 }
307
308 fn run_if_enabled(&self) -> WillExecute {
309 if self.config.run_enabled() { WillExecute::Yes } else { WillExecute::Disabled }
310 }
311
312 fn should_run_successfully(&self, pm: Option<PassMode>) -> bool {
313 match self.config.mode {
314 TestMode::Ui | TestMode::MirOpt => pm == Some(PassMode::Run),
315 mode => panic!("unimplemented for mode {:?}", mode),
316 }
317 }
318
319 fn should_compile_successfully(&self, pm: Option<PassMode>) -> bool {
320 match self.config.mode {
321 TestMode::RustdocJs => true,
322 TestMode::Ui => pm.is_some() || self.props.fail_mode > Some(FailMode::Build),
323 TestMode::Crashes => false,
324 TestMode::Incremental => {
325 let revision =
326 self.revision.expect("incremental tests require a list of revisions");
327 if revision.starts_with("cpass")
328 || revision.starts_with("rpass")
329 || revision.starts_with("rfail")
330 {
331 true
332 } else if revision.starts_with("cfail") {
333 pm.is_some()
334 } else {
335 panic!("revision name must begin with cpass, rpass, rfail, or cfail");
336 }
337 }
338 mode => panic!("unimplemented for mode {:?}", mode),
339 }
340 }
341
342 fn check_if_test_should_compile(
343 &self,
344 fail_mode: Option<FailMode>,
345 pass_mode: Option<PassMode>,
346 proc_res: &ProcRes,
347 ) {
348 if self.should_compile_successfully(pass_mode) {
349 if !proc_res.status.success() {
350 match (fail_mode, pass_mode) {
351 (Some(FailMode::Build), Some(PassMode::Check)) => {
352 self.fatal_proc_rec(
354 "`build-fail` test is required to pass check build, but check build failed",
355 proc_res,
356 );
357 }
358 _ => {
359 self.fatal_proc_rec(
360 "test compilation failed although it shouldn't!",
361 proc_res,
362 );
363 }
364 }
365 }
366 } else {
367 if proc_res.status.success() {
368 let err = &format!("{} test did not emit an error", self.config.mode);
369 let extra_note = (self.config.mode == crate::common::TestMode::Ui)
370 .then_some("note: by default, ui tests are expected not to compile.\nhint: use check-pass, build-pass, or run-pass directive to change this behavior.");
371 self.fatal_proc_rec_general(err, extra_note, proc_res, || ());
372 }
373
374 if !self.props.dont_check_failure_status {
375 self.check_correct_failure_status(proc_res);
376 }
377 }
378 }
379
380 fn get_output(&self, proc_res: &ProcRes) -> String {
381 if self.props.check_stdout {
382 format!("{}{}", proc_res.stdout, proc_res.stderr)
383 } else {
384 proc_res.stderr.clone()
385 }
386 }
387
388 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
389 let expected_status = Some(self.props.failure_status.unwrap_or(1));
390 let received_status = proc_res.status.code();
391
392 if expected_status != received_status {
393 self.fatal_proc_rec(
394 &format!(
395 "Error: expected failure status ({:?}) but received status {:?}.",
396 expected_status, received_status
397 ),
398 proc_res,
399 );
400 }
401 }
402
403 #[must_use = "caller should check whether the command succeeded"]
413 fn run_command_to_procres(&self, cmd: &mut Command) -> ProcRes {
414 let output = cmd
415 .output()
416 .unwrap_or_else(|e| self.fatal(&format!("failed to exec `{cmd:?}` because: {e}")));
417
418 let proc_res = ProcRes {
419 status: output.status,
420 stdout: String::from_utf8(output.stdout).unwrap(),
421 stderr: String::from_utf8(output.stderr).unwrap(),
422 truncated: Truncated::No,
423 cmdline: format!("{cmd:?}"),
424 };
425 self.dump_output(
426 self.config.verbose || !proc_res.status.success(),
427 &cmd.get_program().to_string_lossy(),
428 &proc_res.stdout,
429 &proc_res.stderr,
430 );
431
432 proc_res
433 }
434
435 fn print_source(&self, read_from: ReadFrom, pretty_type: &str) -> ProcRes {
436 let aux_dir = self.aux_output_dir_name();
437 let input: &str = match read_from {
438 ReadFrom::Stdin(_) => "-",
439 ReadFrom::Path => self.testpaths.file.as_str(),
440 };
441
442 let mut rustc = Command::new(&self.config.rustc_path);
443 rustc
444 .arg(input)
445 .args(&["-Z", &format!("unpretty={}", pretty_type)])
446 .args(&["--target", &self.config.target])
447 .arg("-L")
448 .arg(&aux_dir)
449 .arg("-A")
450 .arg("internal_features")
451 .args(&self.props.compile_flags)
452 .envs(self.props.rustc_env.clone());
453 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
454
455 let src = match read_from {
456 ReadFrom::Stdin(src) => Some(src),
457 ReadFrom::Path => None,
458 };
459
460 self.compose_and_run(
461 rustc,
462 self.config.compile_lib_path.as_path(),
463 Some(aux_dir.as_path()),
464 src,
465 )
466 }
467
468 fn compare_source(&self, expected: &str, actual: &str) {
469 if expected != actual {
470 self.fatal(&format!(
471 "pretty-printed source does not match expected source\n\
472 expected:\n\
473 ------------------------------------------\n\
474 {}\n\
475 ------------------------------------------\n\
476 actual:\n\
477 ------------------------------------------\n\
478 {}\n\
479 ------------------------------------------\n\
480 diff:\n\
481 ------------------------------------------\n\
482 {}\n",
483 expected,
484 actual,
485 write_diff(expected, actual, 3),
486 ));
487 }
488 }
489
490 fn set_revision_flags(&self, cmd: &mut Command) {
491 let normalize_revision = |revision: &str| revision.to_lowercase().replace("-", "_");
494
495 if let Some(revision) = self.revision {
496 let normalized_revision = normalize_revision(revision);
497 let cfg_arg = ["--cfg", &normalized_revision];
498 let arg = format!("--cfg={normalized_revision}");
499 if self
500 .props
501 .compile_flags
502 .windows(2)
503 .any(|args| args == cfg_arg || args[0] == arg || args[1] == arg)
504 {
505 error!(
506 "redundant cfg argument `{normalized_revision}` is already created by the \
507 revision"
508 );
509 panic!("redundant cfg argument");
510 }
511 if self.config.builtin_cfg_names().contains(&normalized_revision) {
512 error!("revision `{normalized_revision}` collides with a built-in cfg");
513 panic!("revision collides with built-in cfg");
514 }
515 cmd.args(cfg_arg);
516 }
517
518 if !self.props.no_auto_check_cfg {
519 let mut check_cfg = String::with_capacity(25);
520
521 check_cfg.push_str("cfg(test,FALSE");
527 for revision in &self.props.revisions {
528 check_cfg.push(',');
529 check_cfg.push_str(&normalize_revision(revision));
530 }
531 check_cfg.push(')');
532
533 cmd.args(&["--check-cfg", &check_cfg]);
534 }
535 }
536
537 fn typecheck_source(&self, src: String) -> ProcRes {
538 let mut rustc = Command::new(&self.config.rustc_path);
539
540 let out_dir = self.output_base_name().with_extension("pretty-out");
541 remove_and_create_dir_all(&out_dir).unwrap_or_else(|e| {
542 panic!("failed to remove and recreate output directory `{out_dir}`: {e}")
543 });
544
545 let target = if self.props.force_host { &*self.config.host } else { &*self.config.target };
546
547 let aux_dir = self.aux_output_dir_name();
548
549 rustc
550 .arg("-")
551 .arg("-Zno-codegen")
552 .arg("--out-dir")
553 .arg(&out_dir)
554 .arg(&format!("--target={}", target))
555 .arg("-L")
556 .arg(&self.config.build_test_suite_root)
559 .arg("-L")
560 .arg(aux_dir)
561 .arg("-A")
562 .arg("internal_features");
563 self.set_revision_flags(&mut rustc);
564 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
565 rustc.args(&self.props.compile_flags);
566
567 self.compose_and_run_compiler(rustc, Some(src), self.testpaths)
568 }
569
570 fn maybe_add_external_args(&self, cmd: &mut Command, args: &Vec<String>) {
571 const OPT_FLAGS: &[&str] = &["-O", "-Copt-level=", "opt-level="];
576 const DEBUG_FLAGS: &[&str] = &["-g", "-Cdebuginfo=", "debuginfo="];
577
578 let have_opt_flag =
582 self.props.compile_flags.iter().any(|arg| OPT_FLAGS.iter().any(|f| arg.starts_with(f)));
583 let have_debug_flag = self
584 .props
585 .compile_flags
586 .iter()
587 .any(|arg| DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)));
588
589 for arg in args {
590 if OPT_FLAGS.iter().any(|f| arg.starts_with(f)) && have_opt_flag {
591 continue;
592 }
593 if DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)) && have_debug_flag {
594 continue;
595 }
596 cmd.arg(arg);
597 }
598 }
599
600 fn check_all_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
602 let mut missing_patterns: Vec<String> = Vec::new();
603 self.check_error_patterns(output_to_check, &mut missing_patterns);
604 self.check_regex_error_patterns(output_to_check, proc_res, &mut missing_patterns);
605
606 if missing_patterns.is_empty() {
607 return;
608 }
609
610 if missing_patterns.len() == 1 {
611 self.fatal_proc_rec(
612 &format!("error pattern '{}' not found!", missing_patterns[0]),
613 proc_res,
614 );
615 } else {
616 for pattern in missing_patterns {
617 writeln!(
618 self.stdout,
619 "\n{prefix}: error pattern '{pattern}' not found!",
620 prefix = self.error_prefix()
621 );
622 }
623 self.fatal_proc_rec("multiple error patterns not found", proc_res);
624 }
625 }
626
627 fn check_error_patterns(&self, output_to_check: &str, missing_patterns: &mut Vec<String>) {
628 debug!("check_error_patterns");
629 for pattern in &self.props.error_patterns {
630 if output_to_check.contains(pattern.trim()) {
631 debug!("found error pattern {}", pattern);
632 } else {
633 missing_patterns.push(pattern.to_string());
634 }
635 }
636 }
637
638 fn check_regex_error_patterns(
639 &self,
640 output_to_check: &str,
641 proc_res: &ProcRes,
642 missing_patterns: &mut Vec<String>,
643 ) {
644 debug!("check_regex_error_patterns");
645
646 for pattern in &self.props.regex_error_patterns {
647 let pattern = pattern.trim();
648 let re = match Regex::new(pattern) {
649 Ok(re) => re,
650 Err(err) => {
651 self.fatal_proc_rec(
652 &format!("invalid regex error pattern '{}': {:?}", pattern, err),
653 proc_res,
654 );
655 }
656 };
657 if re.is_match(output_to_check) {
658 debug!("found regex error pattern {}", pattern);
659 } else {
660 missing_patterns.push(pattern.to_string());
661 }
662 }
663 }
664
665 fn check_no_compiler_crash(&self, proc_res: &ProcRes, should_ice: bool) {
666 match proc_res.status.code() {
667 Some(101) if !should_ice => {
668 self.fatal_proc_rec("compiler encountered internal error", proc_res)
669 }
670 None => self.fatal_proc_rec("compiler terminated by signal", proc_res),
671 _ => (),
672 }
673 }
674
675 fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
676 for pat in &self.props.forbid_output {
677 if output_to_check.contains(pat) {
678 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
679 }
680 }
681 }
682
683 fn check_expected_errors(&self, proc_res: &ProcRes) {
685 let expected_errors = load_errors(&self.testpaths.file, self.revision);
686 debug!(
687 "check_expected_errors: expected_errors={:?} proc_res.status={:?}",
688 expected_errors, proc_res.status
689 );
690 if proc_res.status.success() && expected_errors.iter().any(|x| x.kind == ErrorKind::Error) {
691 self.fatal_proc_rec("process did not return an error status", proc_res);
692 }
693
694 if self.props.known_bug {
695 if !expected_errors.is_empty() {
696 self.fatal_proc_rec(
697 "`known_bug` tests should not have an expected error",
698 proc_res,
699 );
700 }
701 return;
702 }
703
704 let diagnostic_file_name = if self.props.remap_src_base {
707 let mut p = Utf8PathBuf::from(FAKE_SRC_BASE);
708 p.push(&self.testpaths.relative_dir);
709 p.push(self.testpaths.file.file_name().unwrap());
710 p.to_string()
711 } else {
712 self.testpaths.file.to_string()
713 };
714
715 let expected_kinds: HashSet<_> = [ErrorKind::Error, ErrorKind::Warning]
718 .into_iter()
719 .chain(expected_errors.iter().map(|e| e.kind))
720 .collect();
721
722 let actual_errors = json::parse_output(&diagnostic_file_name, &self.get_output(proc_res))
724 .into_iter()
725 .map(|e| Error { msg: self.normalize_output(&e.msg, &[]), ..e });
726
727 let mut unexpected = Vec::new();
728 let mut unimportant = Vec::new();
729 let mut found = vec![false; expected_errors.len()];
730 for actual_error in actual_errors {
731 for pattern in &self.props.error_patterns {
732 let pattern = pattern.trim();
733 if actual_error.msg.contains(pattern) {
734 let q = if actual_error.line_num.is_none() { "?" } else { "" };
735 self.fatal(&format!(
736 "error pattern '{pattern}' is found in structured \
737 diagnostics, use `//~{q} {} {pattern}` instead",
738 actual_error.kind,
739 ));
740 }
741 }
742
743 let opt_index =
744 expected_errors.iter().enumerate().position(|(index, expected_error)| {
745 !found[index]
746 && actual_error.line_num == expected_error.line_num
747 && actual_error.kind == expected_error.kind
748 && actual_error.msg.contains(&expected_error.msg)
749 });
750
751 match opt_index {
752 Some(index) => {
753 assert!(!found[index]);
755 found[index] = true;
756 }
757
758 None => {
759 if actual_error.require_annotation
760 && expected_kinds.contains(&actual_error.kind)
761 && !self.props.dont_require_annotations.contains(&actual_error.kind)
762 {
763 unexpected.push(actual_error);
764 } else {
765 unimportant.push(actual_error);
766 }
767 }
768 }
769 }
770
771 let mut not_found = Vec::new();
772 for (index, expected_error) in expected_errors.iter().enumerate() {
774 if !found[index] {
775 not_found.push(expected_error);
776 }
777 }
778
779 if !unexpected.is_empty() || !not_found.is_empty() {
780 let file_name = self
783 .testpaths
784 .file
785 .strip_prefix(self.config.src_root.as_str())
786 .unwrap_or(&self.testpaths.file)
787 .to_string()
788 .replace(r"\", "/");
789 let line_str = |e: &Error| {
790 let line_num = e.line_num.map_or("?".to_string(), |line_num| line_num.to_string());
791 let opt_col_num = match e.column_num {
793 Some(col_num) if line_num != "?" => format!(":{col_num}"),
794 _ => "".to_string(),
795 };
796 format!("{file_name}:{line_num}{opt_col_num}")
797 };
798 let print_error =
799 |e| writeln!(self.stdout, "{}: {}: {}", line_str(e), e.kind, e.msg.cyan());
800 let push_suggestion =
801 |suggestions: &mut Vec<_>, e: &Error, kind, line, msg, color, rank| {
802 let mut ret = String::new();
803 if kind {
804 ret += &format!("{} {}", "with different kind:".color(color), e.kind);
805 }
806 if line {
807 if !ret.is_empty() {
808 ret.push(' ');
809 }
810 ret += &format!("{} {}", "on different line:".color(color), line_str(e));
811 }
812 if msg {
813 if !ret.is_empty() {
814 ret.push(' ');
815 }
816 ret +=
817 &format!("{} {}", "with different message:".color(color), e.msg.cyan());
818 }
819 suggestions.push((ret, rank));
820 };
821 let show_suggestions = |mut suggestions: Vec<_>, prefix: &str, color| {
822 suggestions.sort_by_key(|(_, rank)| *rank);
824 if let Some(&(_, top_rank)) = suggestions.first() {
825 for (suggestion, rank) in suggestions {
826 if rank == top_rank {
827 writeln!(self.stdout, " {} {suggestion}", prefix.color(color));
828 }
829 }
830 }
831 };
832
833 if !unexpected.is_empty() {
840 writeln!(
841 self.stdout,
842 "\n{prefix}: {n} diagnostics reported in JSON output but not expected in test file",
843 prefix = self.error_prefix(),
844 n = unexpected.len(),
845 );
846 for error in &unexpected {
847 print_error(error);
848 let mut suggestions = Vec::new();
849 for candidate in ¬_found {
850 let kind_mismatch = candidate.kind != error.kind;
851 let mut push_red_suggestion = |line, msg, rank| {
852 push_suggestion(
853 &mut suggestions,
854 candidate,
855 kind_mismatch,
856 line,
857 msg,
858 Color::Red,
859 rank,
860 )
861 };
862 if error.msg.contains(&candidate.msg) {
863 push_red_suggestion(candidate.line_num != error.line_num, false, 0);
864 } else if candidate.line_num.is_some()
865 && candidate.line_num == error.line_num
866 {
867 push_red_suggestion(false, true, if kind_mismatch { 2 } else { 1 });
868 }
869 }
870
871 show_suggestions(suggestions, "expected", Color::Red);
872 }
873 }
874 if !not_found.is_empty() {
875 writeln!(
876 self.stdout,
877 "\n{prefix}: {n} diagnostics expected in test file but not reported in JSON output",
878 prefix = self.error_prefix(),
879 n = not_found.len(),
880 );
881 for error in ¬_found {
882 print_error(error);
883 let mut suggestions = Vec::new();
884 for candidate in unexpected.iter().chain(&unimportant) {
885 let kind_mismatch = candidate.kind != error.kind;
886 let mut push_green_suggestion = |line, msg, rank| {
887 push_suggestion(
888 &mut suggestions,
889 candidate,
890 kind_mismatch,
891 line,
892 msg,
893 Color::Green,
894 rank,
895 )
896 };
897 if candidate.msg.contains(&error.msg) {
898 push_green_suggestion(candidate.line_num != error.line_num, false, 0);
899 } else if candidate.line_num.is_some()
900 && candidate.line_num == error.line_num
901 {
902 push_green_suggestion(false, true, if kind_mismatch { 2 } else { 1 });
903 }
904 }
905
906 show_suggestions(suggestions, "reported", Color::Green);
907 }
908 }
909 panic!(
910 "errors differ from expected\nstatus: {}\ncommand: {}\n",
911 proc_res.status, proc_res.cmdline
912 );
913 }
914 }
915
916 fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {
917 match (pm, self.props.fail_mode, self.config.mode) {
918 (Some(PassMode::Check), ..) | (_, Some(FailMode::Check), TestMode::Ui) => {
919 Emit::Metadata
920 }
921 _ => Emit::None,
922 }
923 }
924
925 fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
926 self.compile_test_general(will_execute, emit, self.props.local_pass_mode(), Vec::new())
927 }
928
929 fn compile_test_with_passes(
930 &self,
931 will_execute: WillExecute,
932 emit: Emit,
933 passes: Vec<String>,
934 ) -> ProcRes {
935 self.compile_test_general(will_execute, emit, self.props.local_pass_mode(), passes)
936 }
937
938 fn compile_test_general(
939 &self,
940 will_execute: WillExecute,
941 emit: Emit,
942 local_pm: Option<PassMode>,
943 passes: Vec<String>,
944 ) -> ProcRes {
945 let output_file = match will_execute {
947 WillExecute::Yes => TargetLocation::ThisFile(self.make_exe_name()),
948 WillExecute::No | WillExecute::Disabled => {
949 TargetLocation::ThisDirectory(self.output_base_dir())
950 }
951 };
952
953 let allow_unused = match self.config.mode {
954 TestMode::Ui => {
955 if !self.is_rustdoc()
961 && local_pm != Some(PassMode::Run)
965 {
966 AllowUnused::Yes
967 } else {
968 AllowUnused::No
969 }
970 }
971 _ => AllowUnused::No,
972 };
973
974 let rustc = self.make_compile_args(
975 &self.testpaths.file,
976 output_file,
977 emit,
978 allow_unused,
979 LinkToAux::Yes,
980 passes,
981 );
982
983 self.compose_and_run_compiler(rustc, None, self.testpaths)
984 }
985
986 fn document(&self, root_out_dir: &Utf8Path, root_testpaths: &TestPaths) -> ProcRes {
989 if self.props.build_aux_docs {
990 for rel_ab in &self.props.aux.builds {
991 let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab);
992 let props_for_aux =
993 self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
994 let aux_cx = TestCx {
995 config: self.config,
996 stdout: self.stdout,
997 stderr: self.stderr,
998 props: &props_for_aux,
999 testpaths: &aux_testpaths,
1000 revision: self.revision,
1001 };
1002 create_dir_all(aux_cx.output_base_dir()).unwrap();
1004 let auxres = aux_cx.document(&root_out_dir, root_testpaths);
1007 if !auxres.status.success() {
1008 return auxres;
1009 }
1010 }
1011 }
1012
1013 let aux_dir = self.aux_output_dir_name();
1014
1015 let rustdoc_path = self.config.rustdoc_path.as_ref().expect("--rustdoc-path not passed");
1016
1017 let out_dir: Cow<'_, Utf8Path> = if self.props.unique_doc_out_dir {
1020 let file_name = self.testpaths.file.file_stem().expect("file name should not be empty");
1021 let out_dir = Utf8PathBuf::from_iter([
1022 root_out_dir,
1023 Utf8Path::new("docs"),
1024 Utf8Path::new(file_name),
1025 Utf8Path::new("doc"),
1026 ]);
1027 create_dir_all(&out_dir).unwrap();
1028 Cow::Owned(out_dir)
1029 } else {
1030 Cow::Borrowed(root_out_dir)
1031 };
1032
1033 let mut rustdoc = Command::new(rustdoc_path);
1034 let current_dir = output_base_dir(self.config, root_testpaths, self.safe_revision());
1035 rustdoc.current_dir(current_dir);
1036 rustdoc
1037 .arg("-L")
1038 .arg(self.config.run_lib_path.as_path())
1039 .arg("-L")
1040 .arg(aux_dir)
1041 .arg("-o")
1042 .arg(out_dir.as_ref())
1043 .arg("--deny")
1044 .arg("warnings")
1045 .arg(&self.testpaths.file)
1046 .arg("-A")
1047 .arg("internal_features")
1048 .args(&self.props.compile_flags)
1049 .args(&self.props.doc_flags);
1050
1051 if self.config.mode == TestMode::RustdocJson {
1052 rustdoc.arg("--output-format").arg("json").arg("-Zunstable-options");
1053 }
1054
1055 if let Some(ref linker) = self.config.target_linker {
1056 rustdoc.arg(format!("-Clinker={}", linker));
1057 }
1058
1059 self.compose_and_run_compiler(rustdoc, None, root_testpaths)
1060 }
1061
1062 fn exec_compiled_test(&self) -> ProcRes {
1063 self.exec_compiled_test_general(&[], true)
1064 }
1065
1066 fn exec_compiled_test_general(
1067 &self,
1068 env_extra: &[(&str, &str)],
1069 delete_after_success: bool,
1070 ) -> ProcRes {
1071 let prepare_env = |cmd: &mut Command| {
1072 for (key, val) in &self.props.exec_env {
1073 cmd.env(key, val);
1074 }
1075 for (key, val) in env_extra {
1076 cmd.env(key, val);
1077 }
1078
1079 for key in &self.props.unset_exec_env {
1080 cmd.env_remove(key);
1081 }
1082 };
1083
1084 let proc_res = match &*self.config.target {
1085 _ if self.config.remote_test_client.is_some() => {
1102 let aux_dir = self.aux_output_dir_name();
1103 let ProcArgs { prog, args } = self.make_run_args();
1104 let mut support_libs = Vec::new();
1105 if let Ok(entries) = aux_dir.read_dir() {
1106 for entry in entries {
1107 let entry = entry.unwrap();
1108 if !entry.path().is_file() {
1109 continue;
1110 }
1111 support_libs.push(entry.path());
1112 }
1113 }
1114 let mut test_client =
1115 Command::new(self.config.remote_test_client.as_ref().unwrap());
1116 test_client
1117 .args(&["run", &support_libs.len().to_string()])
1118 .arg(&prog)
1119 .args(support_libs)
1120 .args(args);
1121
1122 prepare_env(&mut test_client);
1123
1124 self.compose_and_run(
1125 test_client,
1126 self.config.run_lib_path.as_path(),
1127 Some(aux_dir.as_path()),
1128 None,
1129 )
1130 }
1131 _ if self.config.target.contains("vxworks") => {
1132 let aux_dir = self.aux_output_dir_name();
1133 let ProcArgs { prog, args } = self.make_run_args();
1134 let mut wr_run = Command::new("wr-run");
1135 wr_run.args(&[&prog]).args(args);
1136
1137 prepare_env(&mut wr_run);
1138
1139 self.compose_and_run(
1140 wr_run,
1141 self.config.run_lib_path.as_path(),
1142 Some(aux_dir.as_path()),
1143 None,
1144 )
1145 }
1146 _ => {
1147 let aux_dir = self.aux_output_dir_name();
1148 let ProcArgs { prog, args } = self.make_run_args();
1149 let mut program = Command::new(&prog);
1150 program.args(args).current_dir(&self.output_base_dir());
1151
1152 prepare_env(&mut program);
1153
1154 self.compose_and_run(
1155 program,
1156 self.config.run_lib_path.as_path(),
1157 Some(aux_dir.as_path()),
1158 None,
1159 )
1160 }
1161 };
1162
1163 if delete_after_success && proc_res.status.success() {
1164 let _ = fs::remove_file(self.make_exe_name());
1167 }
1168
1169 proc_res
1170 }
1171
1172 fn compute_aux_test_paths(&self, of: &TestPaths, rel_ab: &str) -> TestPaths {
1175 let test_ab =
1176 of.file.parent().expect("test file path has no parent").join("auxiliary").join(rel_ab);
1177 if !test_ab.exists() {
1178 self.fatal(&format!("aux-build `{}` source not found", test_ab))
1179 }
1180
1181 TestPaths {
1182 file: test_ab,
1183 relative_dir: of
1184 .relative_dir
1185 .join(self.output_testname_unique())
1186 .join("auxiliary")
1187 .join(rel_ab)
1188 .parent()
1189 .expect("aux-build path has no parent")
1190 .to_path_buf(),
1191 }
1192 }
1193
1194 fn is_vxworks_pure_static(&self) -> bool {
1195 if self.config.target.contains("vxworks") {
1196 match env::var("RUST_VXWORKS_TEST_DYLINK") {
1197 Ok(s) => s != "1",
1198 _ => true,
1199 }
1200 } else {
1201 false
1202 }
1203 }
1204
1205 fn is_vxworks_pure_dynamic(&self) -> bool {
1206 self.config.target.contains("vxworks") && !self.is_vxworks_pure_static()
1207 }
1208
1209 fn has_aux_dir(&self) -> bool {
1210 !self.props.aux.builds.is_empty()
1211 || !self.props.aux.crates.is_empty()
1212 || !self.props.aux.proc_macros.is_empty()
1213 }
1214
1215 fn aux_output_dir(&self) -> Utf8PathBuf {
1216 let aux_dir = self.aux_output_dir_name();
1217
1218 if !self.props.aux.builds.is_empty() {
1219 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1220 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1221 });
1222 }
1223
1224 if !self.props.aux.bins.is_empty() {
1225 let aux_bin_dir = self.aux_bin_output_dir_name();
1226 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1227 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1228 });
1229 remove_and_create_dir_all(&aux_bin_dir).unwrap_or_else(|e| {
1230 panic!("failed to remove and recreate output directory `{aux_bin_dir}`: {e}")
1231 });
1232 }
1233
1234 aux_dir
1235 }
1236
1237 fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Utf8Path, rustc: &mut Command) {
1238 for rel_ab in &self.props.aux.builds {
1239 self.build_auxiliary(of, rel_ab, &aux_dir, None);
1240 }
1241
1242 for rel_ab in &self.props.aux.bins {
1243 self.build_auxiliary(of, rel_ab, &aux_dir, Some(AuxType::Bin));
1244 }
1245
1246 let path_to_crate_name = |path: &str| -> String {
1247 path.rsplit_once('/')
1248 .map_or(path, |(_, tail)| tail)
1249 .trim_end_matches(".rs")
1250 .replace('-', "_")
1251 };
1252
1253 let add_extern =
1254 |rustc: &mut Command, aux_name: &str, aux_path: &str, aux_type: AuxType| {
1255 let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1256 if let Some(lib_name) = lib_name {
1257 rustc.arg("--extern").arg(format!("{}={}/{}", aux_name, aux_dir, lib_name));
1258 }
1259 };
1260
1261 for (aux_name, aux_path) in &self.props.aux.crates {
1262 let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, None);
1263 add_extern(rustc, aux_name, aux_path, aux_type);
1264 }
1265
1266 for proc_macro in &self.props.aux.proc_macros {
1267 self.build_auxiliary(of, proc_macro, &aux_dir, Some(AuxType::ProcMacro));
1268 let crate_name = path_to_crate_name(proc_macro);
1269 add_extern(rustc, &crate_name, proc_macro, AuxType::ProcMacro);
1270 }
1271
1272 if let Some(aux_file) = &self.props.aux.codegen_backend {
1275 let aux_type = self.build_auxiliary(of, aux_file, aux_dir, None);
1276 if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
1277 let lib_path = aux_dir.join(&lib_name);
1278 rustc.arg(format!("-Zcodegen-backend={}", lib_path));
1279 }
1280 }
1281 }
1282
1283 fn compose_and_run_compiler(
1286 &self,
1287 mut rustc: Command,
1288 input: Option<String>,
1289 root_testpaths: &TestPaths,
1290 ) -> ProcRes {
1291 if self.props.add_core_stubs {
1292 let minicore_path = self.build_minicore();
1293 rustc.arg("--extern");
1294 rustc.arg(&format!("minicore={}", minicore_path));
1295 }
1296
1297 let aux_dir = self.aux_output_dir();
1298 self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc);
1299
1300 rustc.envs(self.props.rustc_env.clone());
1301 self.props.unset_rustc_env.iter().fold(&mut rustc, Command::env_remove);
1302 self.compose_and_run(
1303 rustc,
1304 self.config.compile_lib_path.as_path(),
1305 Some(aux_dir.as_path()),
1306 input,
1307 )
1308 }
1309
1310 fn build_minicore(&self) -> Utf8PathBuf {
1313 let output_file_path = self.output_base_dir().join("libminicore.rlib");
1314 let mut rustc = self.make_compile_args(
1315 &self.config.minicore_path,
1316 TargetLocation::ThisFile(output_file_path.clone()),
1317 Emit::None,
1318 AllowUnused::Yes,
1319 LinkToAux::No,
1320 vec![],
1321 );
1322
1323 rustc.args(&["--crate-type", "rlib"]);
1324 rustc.arg("-Cpanic=abort");
1325
1326 let res = self.compose_and_run(rustc, self.config.compile_lib_path.as_path(), None, None);
1327 if !res.status.success() {
1328 self.fatal_proc_rec(
1329 &format!("auxiliary build of {} failed to compile: ", self.config.minicore_path),
1330 &res,
1331 );
1332 }
1333
1334 output_file_path
1335 }
1336
1337 fn build_auxiliary(
1341 &self,
1342 of: &TestPaths,
1343 source_path: &str,
1344 aux_dir: &Utf8Path,
1345 aux_type: Option<AuxType>,
1346 ) -> AuxType {
1347 let aux_testpaths = self.compute_aux_test_paths(of, source_path);
1348 let mut aux_props =
1349 self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
1350 if aux_type == Some(AuxType::ProcMacro) {
1351 aux_props.force_host = true;
1352 }
1353 let mut aux_dir = aux_dir.to_path_buf();
1354 if aux_type == Some(AuxType::Bin) {
1355 aux_dir.push("bin");
1359 }
1360 let aux_output = TargetLocation::ThisDirectory(aux_dir.clone());
1361 let aux_cx = TestCx {
1362 config: self.config,
1363 stdout: self.stdout,
1364 stderr: self.stderr,
1365 props: &aux_props,
1366 testpaths: &aux_testpaths,
1367 revision: self.revision,
1368 };
1369 create_dir_all(aux_cx.output_base_dir()).unwrap();
1371 let input_file = &aux_testpaths.file;
1372 let mut aux_rustc = aux_cx.make_compile_args(
1373 input_file,
1374 aux_output,
1375 Emit::None,
1376 AllowUnused::No,
1377 LinkToAux::No,
1378 Vec::new(),
1379 );
1380 aux_cx.build_all_auxiliary(of, &aux_dir, &mut aux_rustc);
1381
1382 aux_rustc.envs(aux_props.rustc_env.clone());
1383 for key in &aux_props.unset_rustc_env {
1384 aux_rustc.env_remove(key);
1385 }
1386
1387 let (aux_type, crate_type) = if aux_type == Some(AuxType::Bin) {
1388 (AuxType::Bin, Some("bin"))
1389 } else if aux_type == Some(AuxType::ProcMacro) {
1390 (AuxType::ProcMacro, Some("proc-macro"))
1391 } else if aux_type.is_some() {
1392 panic!("aux_type {aux_type:?} not expected");
1393 } else if aux_props.no_prefer_dynamic {
1394 (AuxType::Dylib, None)
1395 } else if self.config.target.contains("emscripten")
1396 || (self.config.target.contains("musl")
1397 && !aux_props.force_host
1398 && !self.config.host.contains("musl"))
1399 || self.config.target.contains("wasm32")
1400 || self.config.target.contains("nvptx")
1401 || self.is_vxworks_pure_static()
1402 || self.config.target.contains("bpf")
1403 || !self.config.target_cfg().dynamic_linking
1404 || matches!(self.config.mode, TestMode::CoverageMap | TestMode::CoverageRun)
1405 {
1406 (AuxType::Lib, Some("lib"))
1420 } else {
1421 (AuxType::Dylib, Some("dylib"))
1422 };
1423
1424 if let Some(crate_type) = crate_type {
1425 aux_rustc.args(&["--crate-type", crate_type]);
1426 }
1427
1428 if aux_type == AuxType::ProcMacro {
1429 aux_rustc.args(&["--extern", "proc_macro"]);
1431 }
1432
1433 aux_rustc.arg("-L").arg(&aux_dir);
1434
1435 let auxres = aux_cx.compose_and_run(
1436 aux_rustc,
1437 aux_cx.config.compile_lib_path.as_path(),
1438 Some(aux_dir.as_path()),
1439 None,
1440 );
1441 if !auxres.status.success() {
1442 self.fatal_proc_rec(
1443 &format!("auxiliary build of {} failed to compile: ", aux_testpaths.file),
1444 &auxres,
1445 );
1446 }
1447 aux_type
1448 }
1449
1450 fn read2_abbreviated(&self, child: Child) -> (Output, Truncated) {
1451 let mut filter_paths_from_len = Vec::new();
1452 let mut add_path = |path: &Utf8Path| {
1453 let path = path.to_string();
1454 let windows = path.replace("\\", "\\\\");
1455 if windows != path {
1456 filter_paths_from_len.push(windows);
1457 }
1458 filter_paths_from_len.push(path);
1459 };
1460
1461 add_path(&self.config.src_test_suite_root);
1467 add_path(&self.config.build_test_suite_root);
1468
1469 read2_abbreviated(child, &filter_paths_from_len).expect("failed to read output")
1470 }
1471
1472 fn compose_and_run(
1473 &self,
1474 mut command: Command,
1475 lib_path: &Utf8Path,
1476 aux_path: Option<&Utf8Path>,
1477 input: Option<String>,
1478 ) -> ProcRes {
1479 let cmdline = {
1480 let cmdline = self.make_cmdline(&command, lib_path);
1481 self.logv(format_args!("executing {cmdline}"));
1482 cmdline
1483 };
1484
1485 command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::piped());
1486
1487 add_dylib_path(&mut command, iter::once(lib_path).chain(aux_path));
1490
1491 let mut child = disable_error_reporting(|| command.spawn())
1492 .unwrap_or_else(|e| panic!("failed to exec `{command:?}`: {e:?}"));
1493 if let Some(input) = input {
1494 child.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
1495 }
1496
1497 let (Output { status, stdout, stderr }, truncated) = self.read2_abbreviated(child);
1498
1499 let result = ProcRes {
1500 status,
1501 stdout: String::from_utf8_lossy(&stdout).into_owned(),
1502 stderr: String::from_utf8_lossy(&stderr).into_owned(),
1503 truncated,
1504 cmdline,
1505 };
1506
1507 self.dump_output(
1508 self.config.verbose || (!result.status.success() && self.config.mode != TestMode::Ui),
1509 &command.get_program().to_string_lossy(),
1510 &result.stdout,
1511 &result.stderr,
1512 );
1513
1514 result
1515 }
1516
1517 fn is_rustdoc(&self) -> bool {
1518 matches!(
1519 self.config.suite,
1520 TestSuite::RustdocUi | TestSuite::RustdocJs | TestSuite::RustdocJson
1521 )
1522 }
1523
1524 fn make_compile_args(
1525 &self,
1526 input_file: &Utf8Path,
1527 output_file: TargetLocation,
1528 emit: Emit,
1529 allow_unused: AllowUnused,
1530 link_to_aux: LinkToAux,
1531 passes: Vec<String>, ) -> Command {
1533 let is_aux = input_file.components().map(|c| c.as_os_str()).any(|c| c == "auxiliary");
1534 let is_rustdoc = self.is_rustdoc() && !is_aux;
1535 let mut rustc = if !is_rustdoc {
1536 Command::new(&self.config.rustc_path)
1537 } else {
1538 Command::new(&self.config.rustdoc_path.clone().expect("no rustdoc built yet"))
1539 };
1540 rustc.arg(input_file);
1541
1542 rustc.arg("-Zthreads=1");
1544
1545 rustc.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
1554 rustc.arg("-Ztranslate-remapped-path-to-local-path=no");
1555
1556 rustc.arg("-Z").arg(format!(
1561 "ignore-directory-in-diagnostics-source-blocks={}",
1562 home::cargo_home().expect("failed to find cargo home").to_str().unwrap()
1563 ));
1564 rustc.arg("-Z").arg(format!(
1566 "ignore-directory-in-diagnostics-source-blocks={}",
1567 self.config.src_root.join("vendor"),
1568 ));
1569
1570 if !self.props.compile_flags.iter().any(|flag| flag.starts_with("--sysroot"))
1574 && !self.config.host_rustcflags.iter().any(|flag| flag == "--sysroot")
1575 {
1576 rustc.arg("--sysroot").arg(&self.config.sysroot_base);
1578 }
1579
1580 if let Some(ref backend) = self.config.override_codegen_backend {
1582 rustc.arg(format!("-Zcodegen-backend={}", backend));
1583 }
1584
1585 let custom_target = self.props.compile_flags.iter().any(|x| x.starts_with("--target"));
1587
1588 if !custom_target {
1589 let target =
1590 if self.props.force_host { &*self.config.host } else { &*self.config.target };
1591
1592 rustc.arg(&format!("--target={}", target));
1593 }
1594 self.set_revision_flags(&mut rustc);
1595
1596 if !is_rustdoc {
1597 if let Some(ref incremental_dir) = self.props.incremental_dir {
1598 rustc.args(&["-C", &format!("incremental={}", incremental_dir)]);
1599 rustc.args(&["-Z", "incremental-verify-ich"]);
1600 }
1601
1602 if self.config.mode == TestMode::CodegenUnits {
1603 rustc.args(&["-Z", "human_readable_cgu_names"]);
1604 }
1605 }
1606
1607 if self.config.optimize_tests && !is_rustdoc {
1608 match self.config.mode {
1609 TestMode::Ui => {
1610 if self.config.optimize_tests
1615 && self.props.pass_mode(&self.config) == Some(PassMode::Run)
1616 && !self
1617 .props
1618 .compile_flags
1619 .iter()
1620 .any(|arg| arg == "-O" || arg.contains("opt-level"))
1621 {
1622 rustc.arg("-O");
1623 }
1624 }
1625 TestMode::DebugInfo => { }
1626 TestMode::CoverageMap | TestMode::CoverageRun => {
1627 }
1632 _ => {
1633 rustc.arg("-O");
1634 }
1635 }
1636 }
1637
1638 let set_mir_dump_dir = |rustc: &mut Command| {
1639 let mir_dump_dir = self.output_base_dir();
1640 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1641 dir_opt.push_str(mir_dump_dir.as_str());
1642 debug!("dir_opt: {:?}", dir_opt);
1643 rustc.arg(dir_opt);
1644 };
1645
1646 match self.config.mode {
1647 TestMode::Incremental => {
1648 if self.props.error_patterns.is_empty()
1652 && self.props.regex_error_patterns.is_empty()
1653 {
1654 rustc.args(&["--error-format", "json"]);
1655 rustc.args(&["--json", "future-incompat"]);
1656 }
1657 rustc.arg("-Zui-testing");
1658 rustc.arg("-Zdeduplicate-diagnostics=no");
1659 }
1660 TestMode::Ui => {
1661 if !self.props.compile_flags.iter().any(|s| s.starts_with("--error-format")) {
1662 rustc.args(&["--error-format", "json"]);
1663 rustc.args(&["--json", "future-incompat"]);
1664 }
1665 rustc.arg("-Ccodegen-units=1");
1666 rustc.arg("-Zui-testing");
1668 rustc.arg("-Zdeduplicate-diagnostics=no");
1669 rustc.arg("-Zwrite-long-types-to-disk=no");
1670 rustc.arg("-Cstrip=debuginfo");
1672 }
1673 TestMode::MirOpt => {
1674 let zdump_arg = if !passes.is_empty() {
1678 format!("-Zdump-mir={}", passes.join(" | "))
1679 } else {
1680 "-Zdump-mir=all".to_string()
1681 };
1682
1683 rustc.args(&[
1684 "-Copt-level=1",
1685 &zdump_arg,
1686 "-Zvalidate-mir",
1687 "-Zlint-mir",
1688 "-Zdump-mir-exclude-pass-number",
1689 "-Zmir-include-spans=false", "--crate-type=rlib",
1691 ]);
1692 if let Some(pass) = &self.props.mir_unit_test {
1693 rustc.args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]);
1694 } else {
1695 rustc.args(&[
1696 "-Zmir-opt-level=4",
1697 "-Zmir-enable-passes=+ReorderBasicBlocks,+ReorderLocals",
1698 ]);
1699 }
1700
1701 set_mir_dump_dir(&mut rustc);
1702 }
1703 TestMode::CoverageMap => {
1704 rustc.arg("-Cinstrument-coverage");
1705 rustc.arg("-Zno-profiler-runtime");
1708 rustc.arg("-Copt-level=2");
1712 }
1713 TestMode::CoverageRun => {
1714 rustc.arg("-Cinstrument-coverage");
1715 rustc.arg("-Copt-level=2");
1719 }
1720 TestMode::Assembly | TestMode::Codegen => {
1721 rustc.arg("-Cdebug-assertions=no");
1722 rustc.arg("-Zcodegen-source-order");
1726 }
1727 TestMode::Crashes => {
1728 set_mir_dump_dir(&mut rustc);
1729 }
1730 TestMode::CodegenUnits => {
1731 rustc.arg("-Zprint-mono-items");
1732 }
1733 TestMode::Pretty
1734 | TestMode::DebugInfo
1735 | TestMode::Rustdoc
1736 | TestMode::RustdocJson
1737 | TestMode::RunMake
1738 | TestMode::RustdocJs => {
1739 }
1741 }
1742
1743 if self.props.remap_src_base {
1744 rustc.arg(format!(
1745 "--remap-path-prefix={}={}",
1746 self.config.src_test_suite_root, FAKE_SRC_BASE,
1747 ));
1748 }
1749
1750 match emit {
1751 Emit::None => {}
1752 Emit::Metadata if is_rustdoc => {}
1753 Emit::Metadata => {
1754 rustc.args(&["--emit", "metadata"]);
1755 }
1756 Emit::LlvmIr => {
1757 rustc.args(&["--emit", "llvm-ir"]);
1758 }
1759 Emit::Mir => {
1760 rustc.args(&["--emit", "mir"]);
1761 }
1762 Emit::Asm => {
1763 rustc.args(&["--emit", "asm"]);
1764 }
1765 Emit::LinkArgsAsm => {
1766 rustc.args(&["-Clink-args=--emit=asm"]);
1767 }
1768 }
1769
1770 if !is_rustdoc {
1771 if self.config.target == "wasm32-unknown-unknown" || self.is_vxworks_pure_static() {
1772 } else if !self.props.no_prefer_dynamic {
1774 rustc.args(&["-C", "prefer-dynamic"]);
1775 }
1776 }
1777
1778 match output_file {
1779 _ if self.props.compile_flags.iter().any(|flag| flag == "-o") => {}
1782 TargetLocation::ThisFile(path) => {
1783 rustc.arg("-o").arg(path);
1784 }
1785 TargetLocation::ThisDirectory(path) => {
1786 if is_rustdoc {
1787 rustc.arg("-o").arg(path);
1789 } else {
1790 rustc.arg("--out-dir").arg(path);
1791 }
1792 }
1793 }
1794
1795 match self.config.compare_mode {
1796 Some(CompareMode::Polonius) => {
1797 rustc.args(&["-Zpolonius=next"]);
1798 }
1799 Some(CompareMode::NextSolver) => {
1800 rustc.args(&["-Znext-solver"]);
1801 }
1802 Some(CompareMode::NextSolverCoherence) => {
1803 rustc.args(&["-Znext-solver=coherence"]);
1804 }
1805 Some(CompareMode::SplitDwarf) if self.config.target.contains("windows") => {
1806 rustc.args(&["-Csplit-debuginfo=unpacked", "-Zunstable-options"]);
1807 }
1808 Some(CompareMode::SplitDwarf) => {
1809 rustc.args(&["-Csplit-debuginfo=unpacked"]);
1810 }
1811 Some(CompareMode::SplitDwarfSingle) => {
1812 rustc.args(&["-Csplit-debuginfo=packed"]);
1813 }
1814 None => {}
1815 }
1816
1817 if let AllowUnused::Yes = allow_unused {
1820 rustc.args(&["-A", "unused"]);
1821 }
1822
1823 rustc.args(&["-A", "internal_features"]);
1825
1826 rustc.args(&["-A", "unused_parens"]);
1830 rustc.args(&["-A", "unused_braces"]);
1831
1832 if self.props.force_host {
1833 self.maybe_add_external_args(&mut rustc, &self.config.host_rustcflags);
1834 if !is_rustdoc {
1835 if let Some(ref linker) = self.config.host_linker {
1836 rustc.arg(format!("-Clinker={}", linker));
1837 }
1838 }
1839 } else {
1840 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
1841 if !is_rustdoc {
1842 if let Some(ref linker) = self.config.target_linker {
1843 rustc.arg(format!("-Clinker={}", linker));
1844 }
1845 }
1846 }
1847
1848 if self.config.host.contains("musl") || self.is_vxworks_pure_dynamic() {
1850 rustc.arg("-Ctarget-feature=-crt-static");
1851 }
1852
1853 if let LinkToAux::Yes = link_to_aux {
1854 if self.has_aux_dir() {
1857 rustc.arg("-L").arg(self.aux_output_dir_name());
1858 }
1859 }
1860
1861 rustc.args(&self.props.compile_flags);
1862
1863 if self.props.add_core_stubs {
1872 rustc.arg("-Cpanic=abort");
1873 rustc.arg("-Cforce-unwind-tables=yes");
1874 }
1875
1876 rustc
1877 }
1878
1879 fn make_exe_name(&self) -> Utf8PathBuf {
1880 let mut f = self.output_base_dir().join("a");
1885 if self.config.target.contains("emscripten") {
1887 f = f.with_extra_extension("js");
1888 } else if self.config.target.starts_with("wasm") {
1889 f = f.with_extra_extension("wasm");
1890 } else if self.config.target.contains("spirv") {
1891 f = f.with_extra_extension("spv");
1892 } else if !env::consts::EXE_SUFFIX.is_empty() {
1893 f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1894 }
1895 f
1896 }
1897
1898 fn make_run_args(&self) -> ProcArgs {
1899 let mut args = self.split_maybe_args(&self.config.runner);
1902
1903 let exe_file = self.make_exe_name();
1904
1905 args.push(exe_file.into_os_string());
1906
1907 args.extend(self.props.run_flags.iter().map(OsString::from));
1909
1910 let prog = args.remove(0);
1911 ProcArgs { prog, args }
1912 }
1913
1914 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<OsString> {
1915 match *argstr {
1916 Some(ref s) => s
1917 .split(' ')
1918 .filter_map(|s| {
1919 if s.chars().all(|c| c.is_whitespace()) {
1920 None
1921 } else {
1922 Some(OsString::from(s))
1923 }
1924 })
1925 .collect(),
1926 None => Vec::new(),
1927 }
1928 }
1929
1930 fn make_cmdline(&self, command: &Command, libpath: &Utf8Path) -> String {
1931 use crate::util;
1932
1933 if cfg!(unix) {
1935 format!("{:?}", command)
1936 } else {
1937 fn lib_path_cmd_prefix(path: &str) -> String {
1940 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1941 }
1942
1943 format!("{} {:?}", lib_path_cmd_prefix(libpath.as_str()), command)
1944 }
1945 }
1946
1947 fn dump_output(&self, print_output: bool, proc_name: &str, out: &str, err: &str) {
1948 let revision = if let Some(r) = self.revision { format!("{}.", r) } else { String::new() };
1949
1950 self.dump_output_file(out, &format!("{}out", revision));
1951 self.dump_output_file(err, &format!("{}err", revision));
1952
1953 if !print_output {
1954 return;
1955 }
1956
1957 let path = Utf8Path::new(proc_name);
1958 let proc_name = if path.file_stem().is_some_and(|p| p == "rmake") {
1959 String::from_iter(
1960 path.parent()
1961 .unwrap()
1962 .file_name()
1963 .into_iter()
1964 .chain(Some("/"))
1965 .chain(path.file_name()),
1966 )
1967 } else {
1968 path.file_name().unwrap().into()
1969 };
1970 writeln!(self.stdout, "------{proc_name} stdout------------------------------");
1971 writeln!(self.stdout, "{}", out);
1972 writeln!(self.stdout, "------{proc_name} stderr------------------------------");
1973 writeln!(self.stdout, "{}", err);
1974 writeln!(self.stdout, "------------------------------------------");
1975 }
1976
1977 fn dump_output_file(&self, out: &str, extension: &str) {
1978 let outfile = self.make_out_name(extension);
1979 fs::write(outfile.as_std_path(), out)
1980 .unwrap_or_else(|err| panic!("failed to write {outfile}: {err:?}"));
1981 }
1982
1983 fn make_out_name(&self, extension: &str) -> Utf8PathBuf {
1986 self.output_base_name().with_extension(extension)
1987 }
1988
1989 fn aux_output_dir_name(&self) -> Utf8PathBuf {
1992 self.output_base_dir()
1993 .join("auxiliary")
1994 .with_extra_extension(self.config.mode.aux_dir_disambiguator())
1995 }
1996
1997 fn aux_bin_output_dir_name(&self) -> Utf8PathBuf {
2000 self.aux_output_dir_name().join("bin")
2001 }
2002
2003 fn output_testname_unique(&self) -> Utf8PathBuf {
2005 output_testname_unique(self.config, self.testpaths, self.safe_revision())
2006 }
2007
2008 fn safe_revision(&self) -> Option<&str> {
2011 if self.config.mode == TestMode::Incremental { None } else { self.revision }
2012 }
2013
2014 fn output_base_dir(&self) -> Utf8PathBuf {
2018 output_base_dir(self.config, self.testpaths, self.safe_revision())
2019 }
2020
2021 fn output_base_name(&self) -> Utf8PathBuf {
2025 output_base_name(self.config, self.testpaths, self.safe_revision())
2026 }
2027
2028 fn logv(&self, message: impl fmt::Display) {
2033 debug!("{message}");
2034 if self.config.verbose {
2035 writeln!(self.stdout, "{message}");
2037 }
2038 }
2039
2040 #[must_use]
2043 fn error_prefix(&self) -> String {
2044 match self.revision {
2045 Some(rev) => format!("error in revision `{rev}`"),
2046 None => format!("error"),
2047 }
2048 }
2049
2050 #[track_caller]
2051 fn fatal(&self, err: &str) -> ! {
2052 writeln!(self.stdout, "\n{prefix}: {err}", prefix = self.error_prefix());
2053 error!("fatal error, panic: {:?}", err);
2054 panic!("fatal error");
2055 }
2056
2057 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
2058 self.fatal_proc_rec_general(err, None, proc_res, || ());
2059 }
2060
2061 fn fatal_proc_rec_general(
2064 &self,
2065 err: &str,
2066 extra_note: Option<&str>,
2067 proc_res: &ProcRes,
2068 callback_before_unwind: impl FnOnce(),
2069 ) -> ! {
2070 writeln!(self.stdout, "\n{prefix}: {err}", prefix = self.error_prefix());
2071
2072 if let Some(note) = extra_note {
2074 writeln!(self.stdout, "{note}");
2075 }
2076
2077 writeln!(self.stdout, "{}", proc_res.format_info());
2079
2080 callback_before_unwind();
2082
2083 std::panic::resume_unwind(Box::new(()));
2086 }
2087
2088 fn compile_test_and_save_ir(&self) -> (ProcRes, Utf8PathBuf) {
2091 let output_path = self.output_base_name().with_extension("ll");
2092 let input_file = &self.testpaths.file;
2093 let rustc = self.make_compile_args(
2094 input_file,
2095 TargetLocation::ThisFile(output_path.clone()),
2096 Emit::LlvmIr,
2097 AllowUnused::No,
2098 LinkToAux::Yes,
2099 Vec::new(),
2100 );
2101
2102 let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths);
2103 (proc_res, output_path)
2104 }
2105
2106 fn verify_with_filecheck(&self, output: &Utf8Path) -> ProcRes {
2107 let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
2108 filecheck.arg("--input-file").arg(output).arg(&self.testpaths.file);
2109
2110 filecheck.arg("--check-prefix=CHECK");
2112
2113 if let Some(rev) = self.revision {
2121 filecheck.arg("--check-prefix").arg(rev);
2122 }
2123
2124 filecheck.arg("--allow-unused-prefixes");
2128
2129 filecheck.args(&["--dump-input-context", "100"]);
2131
2132 filecheck.args(&self.props.filecheck_flags);
2134
2135 self.compose_and_run(filecheck, Utf8Path::new(""), None, None)
2137 }
2138
2139 fn charset() -> &'static str {
2140 if cfg!(target_os = "freebsd") { "ISO-8859-1" } else { "UTF-8" }
2142 }
2143
2144 fn compare_to_default_rustdoc(&self, out_dir: &Utf8Path) {
2145 if !self.config.has_html_tidy {
2146 return;
2147 }
2148 writeln!(self.stdout, "info: generating a diff against nightly rustdoc");
2149
2150 let suffix =
2151 self.safe_revision().map_or("nightly".into(), |path| path.to_owned() + "-nightly");
2152 let compare_dir = output_base_dir(self.config, self.testpaths, Some(&suffix));
2153 remove_and_create_dir_all(&compare_dir).unwrap_or_else(|e| {
2154 panic!("failed to remove and recreate output directory `{compare_dir}`: {e}")
2155 });
2156
2157 let new_rustdoc = TestCx {
2159 config: &Config {
2160 rustdoc_path: Some("rustdoc".into()),
2163 rustc_path: "rustc".into(),
2165 ..self.config.clone()
2166 },
2167 ..*self
2168 };
2169
2170 let output_file = TargetLocation::ThisDirectory(new_rustdoc.aux_output_dir_name());
2171 let mut rustc = new_rustdoc.make_compile_args(
2172 &new_rustdoc.testpaths.file,
2173 output_file,
2174 Emit::None,
2175 AllowUnused::Yes,
2176 LinkToAux::Yes,
2177 Vec::new(),
2178 );
2179 let aux_dir = new_rustdoc.aux_output_dir();
2180 new_rustdoc.build_all_auxiliary(&new_rustdoc.testpaths, &aux_dir, &mut rustc);
2181
2182 let proc_res = new_rustdoc.document(&compare_dir, &new_rustdoc.testpaths);
2183 if !proc_res.status.success() {
2184 writeln!(self.stderr, "failed to run nightly rustdoc");
2185 return;
2186 }
2187
2188 #[rustfmt::skip]
2189 let tidy_args = [
2190 "--new-blocklevel-tags", "rustdoc-search,rustdoc-toolbar,rustdoc-topbar",
2191 "--indent", "yes",
2192 "--indent-spaces", "2",
2193 "--wrap", "0",
2194 "--show-warnings", "no",
2195 "--markup", "yes",
2196 "--quiet", "yes",
2197 "-modify",
2198 ];
2199 let tidy_dir = |dir| {
2200 for entry in walkdir::WalkDir::new(dir) {
2201 let entry = entry.expect("failed to read file");
2202 if entry.file_type().is_file()
2203 && entry.path().extension().and_then(|p| p.to_str()) == Some("html")
2204 {
2205 let status =
2206 Command::new("tidy").args(&tidy_args).arg(entry.path()).status().unwrap();
2207 assert!(status.success() || status.code() == Some(1));
2209 }
2210 }
2211 };
2212 tidy_dir(out_dir);
2213 tidy_dir(&compare_dir);
2214
2215 let pager = {
2216 let output = Command::new("git").args(&["config", "--get", "core.pager"]).output().ok();
2217 output.and_then(|out| {
2218 if out.status.success() {
2219 Some(String::from_utf8(out.stdout).expect("invalid UTF8 in git pager"))
2220 } else {
2221 None
2222 }
2223 })
2224 };
2225
2226 let diff_filename = format!("build/tmp/rustdoc-compare-{}.diff", std::process::id());
2227
2228 if !write_filtered_diff(
2229 self,
2230 &diff_filename,
2231 out_dir,
2232 &compare_dir,
2233 self.config.verbose,
2234 |file_type, extension| {
2235 file_type.is_file() && (extension == Some("html") || extension == Some("js"))
2236 },
2237 ) {
2238 return;
2239 }
2240
2241 match self.config.color {
2242 ColorConfig::AlwaysColor => colored::control::set_override(true),
2243 ColorConfig::NeverColor => colored::control::set_override(false),
2244 _ => {}
2245 }
2246
2247 if let Some(pager) = pager {
2248 let pager = pager.trim();
2249 if self.config.verbose {
2250 writeln!(self.stderr, "using pager {}", pager);
2251 }
2252 let output = Command::new(pager)
2253 .env("PAGER", "")
2255 .stdin(File::open(&diff_filename).unwrap())
2256 .output()
2259 .unwrap();
2260 assert!(output.status.success());
2261 writeln!(self.stdout, "{}", String::from_utf8_lossy(&output.stdout));
2262 writeln!(self.stderr, "{}", String::from_utf8_lossy(&output.stderr));
2263 } else {
2264 warning!("no pager configured, falling back to unified diff");
2265 help!(
2266 "try configuring a git pager (e.g. `delta`) with \
2267 `git config --global core.pager delta`"
2268 );
2269 let mut out = io::stdout();
2270 let mut diff = BufReader::new(File::open(&diff_filename).unwrap());
2271 let mut line = Vec::new();
2272 loop {
2273 line.truncate(0);
2274 match diff.read_until(b'\n', &mut line) {
2275 Ok(0) => break,
2276 Ok(_) => {}
2277 Err(e) => writeln!(self.stderr, "ERROR: {:?}", e),
2278 }
2279 match String::from_utf8(line.clone()) {
2280 Ok(line) => {
2281 if line.starts_with('+') {
2282 write!(&mut out, "{}", line.green()).unwrap();
2283 } else if line.starts_with('-') {
2284 write!(&mut out, "{}", line.red()).unwrap();
2285 } else if line.starts_with('@') {
2286 write!(&mut out, "{}", line.blue()).unwrap();
2287 } else {
2288 out.write_all(line.as_bytes()).unwrap();
2289 }
2290 }
2291 Err(_) => {
2292 write!(&mut out, "{}", String::from_utf8_lossy(&line).reversed()).unwrap();
2293 }
2294 }
2295 }
2296 };
2297 }
2298
2299 fn get_lines(&self, path: &Utf8Path, mut other_files: Option<&mut Vec<String>>) -> Vec<usize> {
2300 let content = fs::read_to_string(path.as_std_path()).unwrap();
2301 let mut ignore = false;
2302 content
2303 .lines()
2304 .enumerate()
2305 .filter_map(|(line_nb, line)| {
2306 if (line.trim_start().starts_with("pub mod ")
2307 || line.trim_start().starts_with("mod "))
2308 && line.ends_with(';')
2309 {
2310 if let Some(ref mut other_files) = other_files {
2311 other_files.push(line.rsplit("mod ").next().unwrap().replace(';', ""));
2312 }
2313 None
2314 } else {
2315 let sline = line.rsplit("///").next().unwrap();
2316 let line = sline.trim_start();
2317 if line.starts_with("```") {
2318 if ignore {
2319 ignore = false;
2320 None
2321 } else {
2322 ignore = true;
2323 Some(line_nb + 1)
2324 }
2325 } else {
2326 None
2327 }
2328 }
2329 })
2330 .collect()
2331 }
2332
2333 fn check_rustdoc_test_option(&self, res: ProcRes) {
2338 let mut other_files = Vec::new();
2339 let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2340 let normalized = fs::canonicalize(&self.testpaths.file).expect("failed to canonicalize");
2341 let normalized = normalized.to_str().unwrap().replace('\\', "/");
2342 files.insert(normalized, self.get_lines(&self.testpaths.file, Some(&mut other_files)));
2343 for other_file in other_files {
2344 let mut path = self.testpaths.file.clone();
2345 path.set_file_name(&format!("{}.rs", other_file));
2346 let path = path.canonicalize_utf8().expect("failed to canonicalize");
2347 let normalized = path.as_str().replace('\\', "/");
2348 files.insert(normalized, self.get_lines(&path, None));
2349 }
2350
2351 let mut tested = 0;
2352 for _ in res.stdout.split('\n').filter(|s| s.starts_with("test ")).inspect(|s| {
2353 if let Some((left, right)) = s.split_once(" - ") {
2354 let path = left.rsplit("test ").next().unwrap();
2355 let path = fs::canonicalize(&path).expect("failed to canonicalize");
2356 let path = path.to_str().unwrap().replace('\\', "/");
2357 if let Some(ref mut v) = files.get_mut(&path) {
2358 tested += 1;
2359 let mut iter = right.split("(line ");
2360 iter.next();
2361 let line = iter
2362 .next()
2363 .unwrap_or(")")
2364 .split(')')
2365 .next()
2366 .unwrap_or("0")
2367 .parse()
2368 .unwrap_or(0);
2369 if let Ok(pos) = v.binary_search(&line) {
2370 v.remove(pos);
2371 } else {
2372 self.fatal_proc_rec(
2373 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2374 &res,
2375 );
2376 }
2377 }
2378 }
2379 }) {}
2380 if tested == 0 {
2381 self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2382 } else {
2383 for (entry, v) in &files {
2384 if !v.is_empty() {
2385 self.fatal_proc_rec(
2386 &format!(
2387 "Not found test at line{} \"{}\":{:?}",
2388 if v.len() > 1 { "s" } else { "" },
2389 entry,
2390 v
2391 ),
2392 &res,
2393 );
2394 }
2395 }
2396 }
2397 }
2398
2399 fn force_color_svg(&self) -> bool {
2400 self.props.compile_flags.iter().any(|s| s.contains("--color=always"))
2401 }
2402
2403 fn load_compare_outputs(
2404 &self,
2405 proc_res: &ProcRes,
2406 output_kind: TestOutput,
2407 explicit_format: bool,
2408 ) -> usize {
2409 let stderr_bits = format!("{}bit.stderr", self.config.get_pointer_width());
2410 let (stderr_kind, stdout_kind) = match output_kind {
2411 TestOutput::Compile => (
2412 if self.force_color_svg() {
2413 if self.config.target.contains("windows") {
2414 UI_WINDOWS_SVG
2417 } else {
2418 UI_SVG
2419 }
2420 } else if self.props.stderr_per_bitwidth {
2421 &stderr_bits
2422 } else {
2423 UI_STDERR
2424 },
2425 UI_STDOUT,
2426 ),
2427 TestOutput::Run => (UI_RUN_STDERR, UI_RUN_STDOUT),
2428 };
2429
2430 let expected_stderr = self.load_expected_output(stderr_kind);
2431 let expected_stdout = self.load_expected_output(stdout_kind);
2432
2433 let mut normalized_stdout =
2434 self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2435 match output_kind {
2436 TestOutput::Run if self.config.remote_test_client.is_some() => {
2437 normalized_stdout = static_regex!(
2442 "^uploaded \"\\$TEST_BUILD_DIR(/[[:alnum:]_\\-.]+)+\", waiting for result\n"
2443 )
2444 .replace(&normalized_stdout, "")
2445 .to_string();
2446 normalized_stdout = static_regex!("^died due to signal [0-9]+\n")
2449 .replace(&normalized_stdout, "")
2450 .to_string();
2451 }
2454 _ => {}
2455 };
2456
2457 let stderr = if self.force_color_svg() {
2458 anstyle_svg::Term::new().render_svg(&proc_res.stderr)
2459 } else if explicit_format {
2460 proc_res.stderr.clone()
2461 } else {
2462 json::extract_rendered(&proc_res.stderr)
2463 };
2464
2465 let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2466 let mut errors = 0;
2467 match output_kind {
2468 TestOutput::Compile => {
2469 if !self.props.dont_check_compiler_stdout {
2470 if self
2471 .compare_output(
2472 stdout_kind,
2473 &normalized_stdout,
2474 &proc_res.stdout,
2475 &expected_stdout,
2476 )
2477 .should_error()
2478 {
2479 errors += 1;
2480 }
2481 }
2482 if !self.props.dont_check_compiler_stderr {
2483 if self
2484 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2485 .should_error()
2486 {
2487 errors += 1;
2488 }
2489 }
2490 }
2491 TestOutput::Run => {
2492 if self
2493 .compare_output(
2494 stdout_kind,
2495 &normalized_stdout,
2496 &proc_res.stdout,
2497 &expected_stdout,
2498 )
2499 .should_error()
2500 {
2501 errors += 1;
2502 }
2503
2504 if self
2505 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2506 .should_error()
2507 {
2508 errors += 1;
2509 }
2510 }
2511 }
2512 errors
2513 }
2514
2515 fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2516 let rflags = self.props.run_flags.join(" ");
2519 let cflags = self.props.compile_flags.join(" ");
2520 let json = rflags.contains("--format json")
2521 || rflags.contains("--format=json")
2522 || cflags.contains("--error-format json")
2523 || cflags.contains("--error-format pretty-json")
2524 || cflags.contains("--error-format=json")
2525 || cflags.contains("--error-format=pretty-json")
2526 || cflags.contains("--output-format json")
2527 || cflags.contains("--output-format=json");
2528
2529 let mut normalized = output.to_string();
2530
2531 let mut normalize_path = |from: &Utf8Path, to: &str| {
2532 let from = if json { &from.as_str().replace("\\", "\\\\") } else { from.as_str() };
2533
2534 normalized = normalized.replace(from, to);
2535 };
2536
2537 let parent_dir = self.testpaths.file.parent().unwrap();
2538 normalize_path(parent_dir, "$DIR");
2539
2540 if self.props.remap_src_base {
2541 let mut remapped_parent_dir = Utf8PathBuf::from(FAKE_SRC_BASE);
2542 if self.testpaths.relative_dir != Utf8Path::new("") {
2543 remapped_parent_dir.push(&self.testpaths.relative_dir);
2544 }
2545 normalize_path(&remapped_parent_dir, "$DIR");
2546 }
2547
2548 let base_dir = Utf8Path::new("/rustc/FAKE_PREFIX");
2549 normalize_path(&base_dir.join("library"), "$SRC_DIR");
2551 normalize_path(&base_dir.join("compiler"), "$COMPILER_DIR");
2555
2556 let rust_src_dir = &self.config.sysroot_base.join("lib/rustlib/src/rust");
2558 rust_src_dir.try_exists().expect(&*format!("{} should exists", rust_src_dir));
2559 let rust_src_dir =
2560 rust_src_dir.read_link_utf8().unwrap_or_else(|_| rust_src_dir.to_path_buf());
2561 normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
2562
2563 let rustc_src_dir = &self.config.sysroot_base.join("lib/rustlib/rustc-src/rust");
2565 rustc_src_dir.try_exists().expect(&*format!("{} should exists", rustc_src_dir));
2566 let rustc_src_dir = rustc_src_dir.read_link_utf8().unwrap_or(rustc_src_dir.to_path_buf());
2567 normalize_path(&rustc_src_dir.join("compiler"), "$COMPILER_DIR_REAL");
2568
2569 normalize_path(&self.output_base_dir(), "$TEST_BUILD_DIR");
2572 normalize_path(&self.output_base_dir().canonicalize_utf8().unwrap(), "$TEST_BUILD_DIR");
2579 normalize_path(&self.config.build_root, "$BUILD_DIR");
2581
2582 if json {
2583 normalized = normalized.replace("\\n", "\n");
2588 }
2589
2590 normalized = static_regex!("SRC_DIR(.+):\\d+:\\d+(: \\d+:\\d+)?")
2595 .replace_all(&normalized, "SRC_DIR$1:LL:COL")
2596 .into_owned();
2597
2598 normalized = Self::normalize_platform_differences(&normalized);
2599
2600 normalized =
2602 static_regex!(r"\$TEST_BUILD_DIR/(?P<filename>[^\.]+).long-type-(?P<hash>\d+).txt")
2603 .replace_all(&normalized, |caps: &Captures<'_>| {
2604 format!(
2605 "$TEST_BUILD_DIR/{filename}.long-type-$LONG_TYPE_HASH.txt",
2606 filename = &caps["filename"]
2607 )
2608 })
2609 .into_owned();
2610
2611 normalized = static_regex!(r"thread '(?P<name>.*?)' \((rtid )?\d+\) panicked")
2613 .replace_all(&normalized, "thread '$name' ($$TID) panicked")
2614 .into_owned();
2615
2616 normalized = normalized.replace("\t", "\\t"); normalized =
2623 static_regex!("\\s*//(\\[.*\\])?~.*").replace_all(&normalized, "").into_owned();
2624
2625 let v0_crate_hash_prefix_re = static_regex!(r"_R.*?Cs[0-9a-zA-Z]+_");
2628 let v0_crate_hash_re = static_regex!(r"Cs[0-9a-zA-Z]+_");
2629
2630 const V0_CRATE_HASH_PLACEHOLDER: &str = r"CsCRATE_HASH_";
2631 if v0_crate_hash_prefix_re.is_match(&normalized) {
2632 normalized =
2634 v0_crate_hash_re.replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER).into_owned();
2635 }
2636
2637 let v0_back_ref_prefix_re = static_regex!(r"\(_R.*?B[0-9a-zA-Z]_");
2638 let v0_back_ref_re = static_regex!(r"B[0-9a-zA-Z]_");
2639
2640 const V0_BACK_REF_PLACEHOLDER: &str = r"B<REF>_";
2641 if v0_back_ref_prefix_re.is_match(&normalized) {
2642 normalized =
2644 v0_back_ref_re.replace_all(&normalized, V0_BACK_REF_PLACEHOLDER).into_owned();
2645 }
2646
2647 {
2654 let mut seen_allocs = indexmap::IndexSet::new();
2655
2656 normalized = static_regex!(
2658 r"╾─*a(lloc)?([0-9]+)(\+0x[0-9]+)?(<imm>)?( \([0-9]+ ptr bytes\))?─*╼"
2659 )
2660 .replace_all(&normalized, |caps: &Captures<'_>| {
2661 let index = caps.get(2).unwrap().as_str().to_string();
2663 let (index, _) = seen_allocs.insert_full(index);
2664 let offset = caps.get(3).map_or("", |c| c.as_str());
2665 let imm = caps.get(4).map_or("", |c| c.as_str());
2666 format!("╾ALLOC{index}{offset}{imm}╼")
2668 })
2669 .into_owned();
2670
2671 normalized = static_regex!(r"\balloc([0-9]+)\b")
2673 .replace_all(&normalized, |caps: &Captures<'_>| {
2674 let index = caps.get(1).unwrap().as_str().to_string();
2675 let (index, _) = seen_allocs.insert_full(index);
2676 format!("ALLOC{index}")
2677 })
2678 .into_owned();
2679 }
2680
2681 for rule in custom_rules {
2683 let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
2684 normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
2685 }
2686 normalized
2687 }
2688
2689 fn normalize_platform_differences(output: &str) -> String {
2695 let output = output.replace(r"\\", r"\");
2696
2697 let re = static_regex!(
2702 r#"(?x)
2703 (?:
2704 # Match paths that don't include spaces.
2705 (?:\\[\pL\pN\.\-_']+)+\.\pL+
2706 |
2707 # If the path starts with a well-known root, then allow spaces and no file extension.
2708 \$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_'\ ]+)+
2709 )"#
2710 );
2711 re.replace_all(&output, |caps: &Captures<'_>| caps[0].replace(r"\", "/"))
2712 .replace("\r\n", "\n")
2713 }
2714
2715 fn expected_output_path(&self, kind: &str) -> Utf8PathBuf {
2716 let mut path =
2717 expected_output_path(&self.testpaths, self.revision, &self.config.compare_mode, kind);
2718
2719 if !path.exists() {
2720 if let Some(CompareMode::Polonius) = self.config.compare_mode {
2721 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2722 }
2723 }
2724
2725 if !path.exists() {
2726 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2727 }
2728
2729 path
2730 }
2731
2732 fn load_expected_output(&self, kind: &str) -> String {
2733 let path = self.expected_output_path(kind);
2734 if path.exists() {
2735 match self.load_expected_output_from_path(&path) {
2736 Ok(x) => x,
2737 Err(x) => self.fatal(&x),
2738 }
2739 } else {
2740 String::new()
2741 }
2742 }
2743
2744 fn load_expected_output_from_path(&self, path: &Utf8Path) -> Result<String, String> {
2745 fs::read_to_string(path)
2746 .map_err(|err| format!("failed to load expected output from `{}`: {}", path, err))
2747 }
2748
2749 fn delete_file(&self, file: &Utf8Path) {
2750 if !file.exists() {
2751 return;
2753 }
2754 if let Err(e) = fs::remove_file(file.as_std_path()) {
2755 self.fatal(&format!("failed to delete `{}`: {}", file, e,));
2756 }
2757 }
2758
2759 fn compare_output(
2760 &self,
2761 stream: &str,
2762 actual: &str,
2763 actual_unnormalized: &str,
2764 expected: &str,
2765 ) -> CompareOutcome {
2766 let expected_path =
2767 expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2768
2769 if self.config.bless && actual.is_empty() && expected_path.exists() {
2770 self.delete_file(&expected_path);
2771 }
2772
2773 let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
2774 (true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
2777 _ => expected != actual,
2778 };
2779 if !are_different {
2780 return CompareOutcome::Same;
2781 }
2782
2783 let compare_output_by_lines =
2790 self.props.compare_output_by_lines || self.config.runner.is_some();
2791
2792 let tmp;
2793 let (expected, actual): (&str, &str) = if compare_output_by_lines {
2794 let actual_lines: HashSet<_> = actual.lines().collect();
2795 let expected_lines: Vec<_> = expected.lines().collect();
2796 let mut used = expected_lines.clone();
2797 used.retain(|line| actual_lines.contains(line));
2798 if used.len() == expected_lines.len() && (expected.is_empty() == actual.is_empty()) {
2800 return CompareOutcome::Same;
2801 }
2802 if expected_lines.is_empty() {
2803 ("", actual)
2805 } else {
2806 tmp = (expected_lines.join("\n"), used.join("\n"));
2807 (&tmp.0, &tmp.1)
2808 }
2809 } else {
2810 (expected, actual)
2811 };
2812
2813 let actual_path = self
2815 .output_base_name()
2816 .with_extra_extension(self.revision.unwrap_or(""))
2817 .with_extra_extension(
2818 self.config.compare_mode.as_ref().map(|cm| cm.to_str()).unwrap_or(""),
2819 )
2820 .with_extra_extension(stream);
2821
2822 if let Err(err) = fs::write(&actual_path, &actual) {
2823 self.fatal(&format!("failed to write {stream} to `{actual_path}`: {err}",));
2824 }
2825 writeln!(self.stdout, "Saved the actual {stream} to `{actual_path}`");
2826
2827 if !self.config.bless {
2828 if expected.is_empty() {
2829 writeln!(self.stdout, "normalized {}:\n{}\n", stream, actual);
2830 } else {
2831 self.show_diff(
2832 stream,
2833 &expected_path,
2834 &actual_path,
2835 expected,
2836 actual,
2837 actual_unnormalized,
2838 );
2839 }
2840 } else {
2841 if self.revision.is_some() {
2844 let old =
2845 expected_output_path(self.testpaths, None, &self.config.compare_mode, stream);
2846 self.delete_file(&old);
2847 }
2848
2849 if !actual.is_empty() {
2850 if let Err(err) = fs::write(&expected_path, &actual) {
2851 self.fatal(&format!("failed to write {stream} to `{expected_path}`: {err}"));
2852 }
2853 writeln!(
2854 self.stdout,
2855 "Blessing the {stream} of `{test_name}` as `{expected_path}`",
2856 test_name = self.testpaths.file
2857 );
2858 }
2859 }
2860
2861 writeln!(self.stdout, "\nThe actual {stream} differed from the expected {stream}");
2862
2863 if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
2864 }
2865
2866 fn show_diff(
2868 &self,
2869 stream: &str,
2870 expected_path: &Utf8Path,
2871 actual_path: &Utf8Path,
2872 expected: &str,
2873 actual: &str,
2874 actual_unnormalized: &str,
2875 ) {
2876 writeln!(self.stderr, "diff of {stream}:\n");
2877 if let Some(diff_command) = self.config.diff_command.as_deref() {
2878 let mut args = diff_command.split_whitespace();
2879 let name = args.next().unwrap();
2880 match Command::new(name).args(args).args([expected_path, actual_path]).output() {
2881 Err(err) => {
2882 self.fatal(&format!(
2883 "failed to call custom diff command `{diff_command}`: {err}"
2884 ));
2885 }
2886 Ok(output) => {
2887 let output = String::from_utf8_lossy(&output.stdout);
2888 write!(self.stderr, "{output}");
2889 }
2890 }
2891 } else {
2892 write!(self.stderr, "{}", write_diff(expected, actual, 3));
2893 }
2894
2895 let diff_results = make_diff(actual, expected, 0);
2897
2898 let (mut mismatches_normalized, mut mismatch_line_nos) = (String::new(), vec![]);
2899 for hunk in diff_results {
2900 let mut line_no = hunk.line_number;
2901 for line in hunk.lines {
2902 if let DiffLine::Expected(normalized) = line {
2904 mismatches_normalized += &normalized;
2905 mismatches_normalized += "\n";
2906 mismatch_line_nos.push(line_no);
2907 line_no += 1;
2908 }
2909 }
2910 }
2911 let mut mismatches_unnormalized = String::new();
2912 let diff_normalized = make_diff(actual, actual_unnormalized, 0);
2913 for hunk in diff_normalized {
2914 if mismatch_line_nos.contains(&hunk.line_number) {
2915 for line in hunk.lines {
2916 if let DiffLine::Resulting(unnormalized) = line {
2917 mismatches_unnormalized += &unnormalized;
2918 mismatches_unnormalized += "\n";
2919 }
2920 }
2921 }
2922 }
2923
2924 let normalized_diff = make_diff(&mismatches_normalized, &mismatches_unnormalized, 0);
2925 if !normalized_diff.is_empty()
2927 && !mismatches_unnormalized.is_empty()
2928 && !mismatches_normalized.is_empty()
2929 {
2930 writeln!(
2931 self.stderr,
2932 "Note: some mismatched output was normalized before being compared"
2933 );
2934 write!(
2936 self.stderr,
2937 "{}",
2938 write_diff(&mismatches_unnormalized, &mismatches_normalized, 0)
2939 );
2940 }
2941 }
2942
2943 fn check_and_prune_duplicate_outputs(
2944 &self,
2945 proc_res: &ProcRes,
2946 modes: &[CompareMode],
2947 require_same_modes: &[CompareMode],
2948 ) {
2949 for kind in UI_EXTENSIONS {
2950 let canon_comparison_path =
2951 expected_output_path(&self.testpaths, self.revision, &None, kind);
2952
2953 let canon = match self.load_expected_output_from_path(&canon_comparison_path) {
2954 Ok(canon) => canon,
2955 _ => continue,
2956 };
2957 let bless = self.config.bless;
2958 let check_and_prune_duplicate_outputs = |mode: &CompareMode, require_same: bool| {
2959 let examined_path =
2960 expected_output_path(&self.testpaths, self.revision, &Some(mode.clone()), kind);
2961
2962 let examined_content = match self.load_expected_output_from_path(&examined_path) {
2964 Ok(content) => content,
2965 _ => return,
2966 };
2967
2968 let is_duplicate = canon == examined_content;
2969
2970 match (bless, require_same, is_duplicate) {
2971 (true, _, true) => {
2973 self.delete_file(&examined_path);
2974 }
2975 (_, true, false) => {
2978 self.fatal_proc_rec(
2979 &format!("`{}` should not have different output from base test!", kind),
2980 proc_res,
2981 );
2982 }
2983 _ => {}
2984 }
2985 };
2986 for mode in modes {
2987 check_and_prune_duplicate_outputs(mode, false);
2988 }
2989 for mode in require_same_modes {
2990 check_and_prune_duplicate_outputs(mode, true);
2991 }
2992 }
2993 }
2994
2995 fn create_stamp(&self) {
2996 let stamp_file_path = stamp_file_path(&self.config, self.testpaths, self.revision);
2997 fs::write(&stamp_file_path, compute_stamp_hash(&self.config)).unwrap();
2998 }
2999
3000 fn init_incremental_test(&self) {
3001 let incremental_dir = self.props.incremental_dir.as_ref().unwrap();
3008 if incremental_dir.exists() {
3009 let canonicalized = incremental_dir.canonicalize().unwrap();
3012 fs::remove_dir_all(canonicalized).unwrap();
3013 }
3014 fs::create_dir_all(&incremental_dir).unwrap();
3015
3016 if self.config.verbose {
3017 writeln!(self.stdout, "init_incremental_test: incremental_dir={incremental_dir}");
3018 }
3019 }
3020}
3021
3022struct ProcArgs {
3023 prog: OsString,
3024 args: Vec<OsString>,
3025}
3026
3027#[derive(Debug)]
3028pub struct ProcRes {
3029 status: ExitStatus,
3030 stdout: String,
3031 stderr: String,
3032 truncated: Truncated,
3033 cmdline: String,
3034}
3035
3036impl ProcRes {
3037 #[must_use]
3038 pub fn format_info(&self) -> String {
3039 fn render(name: &str, contents: &str) -> String {
3040 let contents = json::extract_rendered(contents);
3041 let contents = contents.trim_end();
3042 if contents.is_empty() {
3043 format!("{name}: none")
3044 } else {
3045 format!(
3046 "\
3047 --- {name} -------------------------------\n\
3048 {contents}\n\
3049 ------------------------------------------",
3050 )
3051 }
3052 }
3053
3054 format!(
3055 "status: {}\ncommand: {}\n{}\n{}\n",
3056 self.status,
3057 self.cmdline,
3058 render("stdout", &self.stdout),
3059 render("stderr", &self.stderr),
3060 )
3061 }
3062}
3063
3064#[derive(Debug)]
3065enum TargetLocation {
3066 ThisFile(Utf8PathBuf),
3067 ThisDirectory(Utf8PathBuf),
3068}
3069
3070enum AllowUnused {
3071 Yes,
3072 No,
3073}
3074
3075enum LinkToAux {
3076 Yes,
3077 No,
3078}
3079
3080#[derive(Debug, PartialEq)]
3081enum AuxType {
3082 Bin,
3083 Lib,
3084 Dylib,
3085 ProcMacro,
3086}
3087
3088#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3091enum CompareOutcome {
3092 Same,
3094 Blessed,
3096 Differed,
3098}
3099
3100impl CompareOutcome {
3101 fn should_error(&self) -> bool {
3102 matches!(self, CompareOutcome::Differed)
3103 }
3104}