1use std::borrow::Cow;
2use std::collections::{HashMap, HashSet};
3use std::ffi::OsString;
4use std::fs::{self, create_dir_all};
5use std::hash::{DefaultHasher, Hash, Hasher};
6use std::io::prelude::*;
7use std::process::{Child, Command, ExitStatus, Output, Stdio};
8use std::{env, fmt, io, iter, str};
9
10use build_helper::fs::remove_and_create_dir_all;
11use camino::{Utf8Path, Utf8PathBuf};
12use colored::{Color, Colorize};
13use regex::{Captures, Regex};
14use tracing::*;
15
16use crate::common::{
17 CompareMode, Config, Debugger, ForcePassMode, PassFailMode, RunResult, TestMode, TestPaths,
18 TestSuite, UI_EXTENSIONS, UI_FIXED, UI_RUN_STDERR, UI_RUN_STDOUT, UI_STDERR, UI_STDOUT, UI_SVG,
19 UI_WINDOWS_SVG, expected_output_path, incremental_dir, output_base_dir, output_base_name,
20};
21use crate::directives::{AuxCrate, TestProps};
22use crate::errors::{Error, ErrorKind, load_errors};
23use crate::output_capture::ConsoleOut;
24use crate::read2::{Truncated, read2_abbreviated};
25use crate::runtest::compute_diff::{DiffLine, diff_by_lines, make_diff, write_diff};
26use crate::util::{Utf8PathBufExt, add_dylib_path, static_regex};
27use crate::{json, stamp_file_path};
28
29mod assembly;
32mod codegen;
33mod codegen_units;
34mod coverage;
35mod crashes;
36mod debuginfo;
37mod incremental;
38mod js_doc;
39mod mir_opt;
40mod pretty;
41mod run_make;
42mod rustdoc;
43mod rustdoc_json;
44mod ui;
45mod compute_diff;
48mod debugger;
49#[cfg(test)]
50mod tests;
51
52const FAKE_SRC_BASE: &str = "fake-test-src-base";
53
54#[cfg(windows)]
55fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
56 use std::sync::Mutex;
57
58 use windows::Win32::System::Diagnostics::Debug::{
59 SEM_FAILCRITICALERRORS, SEM_NOGPFAULTERRORBOX, SetErrorMode,
60 };
61
62 static LOCK: Mutex<()> = Mutex::new(());
63
64 let _lock = LOCK.lock().unwrap();
66
67 unsafe {
78 let old_mode = SetErrorMode(SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS);
80 SetErrorMode(old_mode | SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS);
81 let r = f();
82 SetErrorMode(old_mode);
83 r
84 }
85}
86
87#[cfg(not(windows))]
88fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
89 f()
90}
91
92fn get_lib_name(name: &str, aux_type: AuxType) -> Option<String> {
94 match aux_type {
95 AuxType::Bin => None,
96 AuxType::Lib => Some(format!("lib{name}.rlib")),
101 AuxType::Dylib | AuxType::ProcMacro => Some(dylib_name(name)),
102 }
103}
104
105fn dylib_name(name: &str) -> String {
106 format!("{}{name}.{}", std::env::consts::DLL_PREFIX, std::env::consts::DLL_EXTENSION)
107}
108
109pub(crate) fn run(
110 config: &Config,
111 stdout: &dyn ConsoleOut,
112 stderr: &dyn ConsoleOut,
113 testpaths: &TestPaths,
114 revision: Option<&str>,
115) {
116 match &*config.target {
117 "arm-linux-androideabi"
118 | "armv7-linux-androideabi"
119 | "thumbv7neon-linux-androideabi"
120 | "aarch64-linux-android" => {
121 if !config.adb_device_status {
122 panic!("android device not available");
123 }
124 }
125
126 _ => {
127 if config.debugger == Some(Debugger::Gdb) && config.gdb.is_none() {
131 panic!("gdb not available but debuginfo gdb debuginfo test requested");
132 }
133 }
134 }
135
136 if config.verbose {
137 write!(stdout, "\n\n");
139 }
140 debug!("running {}", testpaths.file);
141 let mut props = TestProps::from_file(&testpaths.file, revision, &config);
142
143 if props.incremental {
147 props.incremental_dir = Some(incremental_dir(&config, testpaths, revision));
148 }
149
150 let cx = TestCx { config: &config, stdout, stderr, props: &props, testpaths, revision };
151
152 if let Err(e) = create_dir_all(&cx.output_base_dir()) {
153 panic!("failed to create output base directory {}: {e}", cx.output_base_dir());
154 }
155
156 if props.incremental {
157 cx.init_incremental_test();
158 }
159
160 if config.mode == TestMode::Incremental {
161 assert!(!props.revisions.is_empty(), "Incremental tests require revisions.");
164 for revision in &props.revisions {
165 let mut revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
166 revision_props.incremental_dir = props.incremental_dir.clone();
167 let rev_cx = TestCx {
168 config: &config,
169 stdout,
170 stderr,
171 props: &revision_props,
172 testpaths,
173 revision: Some(revision),
174 };
175 rev_cx.run_revision();
176 }
177 } else {
178 cx.run_revision();
179 }
180
181 cx.create_stamp();
182}
183
184pub(crate) fn compute_stamp_hash(config: &Config) -> String {
185 let mut hash = DefaultHasher::new();
186 config.stage_id.hash(&mut hash);
187 config.run.hash(&mut hash);
188 config.edition.hash(&mut hash);
189
190 match config.debugger {
191 Some(Debugger::Cdb) => {
192 config.cdb.hash(&mut hash);
193 }
194
195 Some(Debugger::Gdb) => {
196 config.gdb.hash(&mut hash);
197 env::var_os("PATH").hash(&mut hash);
198 env::var_os("PYTHONPATH").hash(&mut hash);
199 }
200
201 Some(Debugger::Lldb) => {
202 config.lldb.hash(&mut hash);
206 env::var_os("PATH").hash(&mut hash);
207 }
208
209 None => {}
210 }
211
212 if config.mode == TestMode::Ui {
213 config.force_pass_mode.hash(&mut hash);
214 }
215
216 format!("{:x}", hash.finish())
217}
218
219#[derive(Copy, Clone, Debug)]
220struct TestCx<'test> {
221 config: &'test Config,
222 stdout: &'test dyn ConsoleOut,
223 stderr: &'test dyn ConsoleOut,
224 props: &'test TestProps,
225 testpaths: &'test TestPaths,
226 revision: Option<&'test str>,
227}
228
229enum ReadFrom {
230 Path,
231 Stdin(String),
232}
233
234enum TestOutput {
235 Compile,
236 Run,
237}
238
239#[derive(Copy, Clone, PartialEq)]
241enum WillExecute {
242 Yes,
243 No,
244 Disabled,
245}
246
247#[derive(Copy, Clone)]
249enum Emit {
250 None,
251 Metadata,
252 LlvmIr,
253 Mir,
254 Asm,
255 LinkArgsAsm,
256}
257
258#[derive(Clone, Copy, Debug, PartialEq, Eq)]
260enum CompilerKind {
261 Rustc,
262 Rustdoc,
263}
264
265impl<'test> TestCx<'test> {
266 fn run_revision(&self) {
269 for _ in 0..self.config.iteration_count {
272 match self.config.mode {
273 TestMode::Pretty => self.run_pretty_test(),
274 TestMode::DebugInfo => self.run_debuginfo_test(),
275 TestMode::Codegen => self.run_codegen_test(),
276 TestMode::RustdocHtml => self.run_rustdoc_html_test(),
277 TestMode::RustdocJson => self.run_rustdoc_json_test(),
278 TestMode::CodegenUnits => self.run_codegen_units_test(),
279 TestMode::Incremental => self.run_incremental_test(),
280 TestMode::RunMake => self.run_rmake_test(),
281 TestMode::Ui => self.run_ui_test(),
282 TestMode::MirOpt => self.run_mir_opt_test(),
283 TestMode::Assembly => self.run_assembly_test(),
284 TestMode::RustdocJs => self.run_rustdoc_js_test(),
285 TestMode::CoverageMap => self.run_coverage_map_test(), TestMode::CoverageRun => self.run_coverage_run_test(), TestMode::Crashes => self.run_crash_test(),
288 }
289 }
290 }
291
292 fn effective_pass_fail_mode(&self) -> Option<PassFailMode> {
296 assert_eq!(self.config.mode, TestMode::Ui);
297 let declared = self.props.pass_fail_mode?;
299
300 if let Some(force_pass_mode) = self.config.force_pass_mode
303 && !self.props.no_pass_override
304 && declared.is_pass()
305 {
306 match force_pass_mode {
307 ForcePassMode::Check => Some(PassFailMode::CheckPass),
308 ForcePassMode::Build => Some(PassFailMode::BuildPass),
309 ForcePassMode::Run => Some(PassFailMode::RunPass),
310 }
311 } else {
312 Some(declared)
313 }
314 }
315
316 fn run_if_enabled(&self) -> WillExecute {
317 if self.config.run_enabled() { WillExecute::Yes } else { WillExecute::Disabled }
318 }
319
320 fn check_if_test_should_compile(&self, pass_fail: PassFailMode, proc_res: &ProcRes) {
321 assert_eq!(self.config.mode, TestMode::Ui);
322
323 let should_compile_successfully = match pass_fail {
324 PassFailMode::CheckFail | PassFailMode::BuildFail => false,
325
326 PassFailMode::CheckPass
327 | PassFailMode::BuildPass
328 | PassFailMode::RunFail
329 | PassFailMode::RunCrash
330 | PassFailMode::RunFailOrCrash
331 | PassFailMode::RunPass => true,
332 };
333
334 if should_compile_successfully {
335 if !proc_res.status.success() {
336 if pass_fail == PassFailMode::CheckPass
337 && self.effective_pass_fail_mode() == Some(PassFailMode::BuildFail)
338 {
339 self.fatal_proc_rec(
341 "`build-fail` test is required to pass check build, but check build failed",
342 proc_res,
343 );
344 } else {
345 self.fatal_proc_rec("test compilation failed although it shouldn't!", proc_res);
346 }
347 }
348 } else {
349 if proc_res.status.success() {
350 let err = &format!("{} test did not emit an error", self.config.mode);
351 let extra_note = Some(
352 "note: by default, ui tests are expected not to compile.\nhint: use check-pass, build-pass, or run-pass directive to change this behavior.",
353 );
354 self.fatal_proc_rec_general(err, extra_note, proc_res, || ());
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 || !proc_res.status.success(),
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
427 self.build_all_auxiliary(&self.aux_output_dir(), &mut rustc);
428
429 rustc
430 .arg(input)
431 .args(&["-Z", &format!("unpretty={}", pretty_type)])
432 .arg("-Zunstable-options")
433 .args(&["--target", &self.config.target])
434 .arg("-L")
435 .arg(&aux_dir)
436 .arg("-A")
437 .arg("internal_features")
438 .args(&self.props.compile_flags)
439 .envs(self.props.rustc_env.clone());
440 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
441
442 let src = match read_from {
443 ReadFrom::Stdin(src) => Some(src),
444 ReadFrom::Path => None,
445 };
446
447 self.compose_and_run(
448 rustc,
449 self.config.host_compile_lib_path.as_path(),
450 Some(aux_dir.as_path()),
451 src,
452 )
453 }
454
455 fn compare_source(&self, expected: &str, actual: &str) {
456 if expected != actual {
457 self.fatal(&format!(
458 "pretty-printed source does not match expected source\n\
459 expected:\n\
460 ------------------------------------------\n\
461 {}\n\
462 ------------------------------------------\n\
463 actual:\n\
464 ------------------------------------------\n\
465 {}\n\
466 ------------------------------------------\n\
467 diff:\n\
468 ------------------------------------------\n\
469 {}\n",
470 expected,
471 actual,
472 write_diff(expected, actual, 3),
473 ));
474 }
475 }
476
477 fn set_revision_flags(&self, cmd: &mut Command) {
478 let normalize_revision = |revision: &str| revision.to_lowercase().replace("-", "_");
481
482 if let Some(revision) = self.revision {
483 let normalized_revision = normalize_revision(revision);
484 let cfg_arg = ["--cfg", &normalized_revision];
485 let arg = format!("--cfg={normalized_revision}");
486 let contains_arg =
488 self.props.compile_flags.iter().any(|considered_arg| *considered_arg == arg);
489 let contains_cfg_arg = self.props.compile_flags.windows(2).any(|args| args == cfg_arg);
490 if contains_arg || contains_cfg_arg {
491 error!(
492 "redundant cfg argument `{normalized_revision}` is already created by the \
493 revision"
494 );
495 panic!("redundant cfg argument");
496 }
497 if self.config.builtin_cfg_names().contains(&normalized_revision) {
498 error!("revision `{normalized_revision}` collides with a built-in cfg");
499 panic!("revision collides with built-in cfg");
500 }
501 cmd.args(cfg_arg);
502 }
503
504 if !self.props.no_auto_check_cfg {
505 let mut check_cfg = String::with_capacity(25);
506
507 check_cfg.push_str("cfg(test,FALSE");
513 for revision in &self.props.revisions {
514 check_cfg.push(',');
515 check_cfg.push_str(&normalize_revision(revision));
516 }
517 check_cfg.push(')');
518
519 cmd.args(&["--check-cfg", &check_cfg]);
520 }
521 }
522
523 fn typecheck_source(&self, src: String) -> ProcRes {
524 let mut rustc = Command::new(&self.config.rustc_path);
525
526 let out_dir = self.output_base_name().with_extension("pretty-out");
527 remove_and_create_dir_all(&out_dir).unwrap_or_else(|e| {
528 panic!("failed to remove and recreate output directory `{out_dir}`: {e}")
529 });
530
531 let target = if self.props.force_host { &*self.config.host } else { &*self.config.target };
532
533 let aux_dir = self.aux_output_dir_name();
534
535 rustc
536 .arg("-")
537 .arg("-Zno-codegen")
538 .arg("-Zunstable-options")
539 .arg("--out-dir")
540 .arg(&out_dir)
541 .arg(&format!("--target={}", target))
542 .arg("-L")
543 .arg(&self.config.build_test_suite_root)
546 .arg("-L")
547 .arg(aux_dir)
548 .arg("-A")
549 .arg("internal_features");
550 self.set_revision_flags(&mut rustc);
551 self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
552 rustc.args(&self.props.compile_flags);
553
554 self.compose_and_run_compiler(rustc, Some(src))
555 }
556
557 fn maybe_add_external_args(&self, cmd: &mut Command, args: &Vec<String>) {
558 const OPT_FLAGS: &[&str] = &["-O", "-Copt-level=", "opt-level="];
563 const DEBUG_FLAGS: &[&str] = &["-g", "-Cdebuginfo=", "debuginfo="];
564
565 let have_opt_flag =
569 self.props.compile_flags.iter().any(|arg| OPT_FLAGS.iter().any(|f| arg.starts_with(f)));
570 let have_debug_flag = self
571 .props
572 .compile_flags
573 .iter()
574 .any(|arg| DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)));
575
576 for arg in args {
577 if OPT_FLAGS.iter().any(|f| arg.starts_with(f)) && have_opt_flag {
578 continue;
579 }
580 if DEBUG_FLAGS.iter().any(|f| arg.starts_with(f)) && have_debug_flag {
581 continue;
582 }
583 cmd.arg(arg);
584 }
585 }
586
587 fn check_all_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
589 let mut missing_patterns: Vec<String> = Vec::new();
590 self.check_error_patterns(output_to_check, &mut missing_patterns);
591 self.check_regex_error_patterns(output_to_check, proc_res, &mut missing_patterns);
592
593 if missing_patterns.is_empty() {
594 return;
595 }
596
597 if missing_patterns.len() == 1 {
598 self.fatal_proc_rec(
599 &format!("error pattern '{}' not found!", missing_patterns[0]),
600 proc_res,
601 );
602 } else {
603 for pattern in missing_patterns {
604 writeln!(
605 self.stdout,
606 "\n{prefix}: error pattern '{pattern}' not found!",
607 prefix = self.error_prefix()
608 );
609 }
610 self.fatal_proc_rec("multiple error patterns not found", proc_res);
611 }
612 }
613
614 fn check_error_patterns(&self, output_to_check: &str, missing_patterns: &mut Vec<String>) {
615 debug!("check_error_patterns");
616 for pattern in &self.props.error_patterns {
617 if output_to_check.contains(pattern.trim()) {
618 debug!("found error pattern {}", pattern);
619 } else {
620 missing_patterns.push(pattern.to_string());
621 }
622 }
623 }
624
625 fn check_regex_error_patterns(
626 &self,
627 output_to_check: &str,
628 proc_res: &ProcRes,
629 missing_patterns: &mut Vec<String>,
630 ) {
631 debug!("check_regex_error_patterns");
632
633 for pattern in &self.props.regex_error_patterns {
634 let pattern = pattern.trim();
635 let re = match Regex::new(pattern) {
636 Ok(re) => re,
637 Err(err) => {
638 self.fatal_proc_rec(
639 &format!("invalid regex error pattern '{}': {:?}", pattern, err),
640 proc_res,
641 );
642 }
643 };
644 if re.is_match(output_to_check) {
645 debug!("found regex error pattern {}", pattern);
646 } else {
647 missing_patterns.push(pattern.to_string());
648 }
649 }
650 }
651
652 fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
653 for pat in &self.props.forbid_output {
654 if output_to_check.contains(pat) {
655 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
656 }
657 }
658 }
659
660 fn check_expected_errors(&self, proc_res: &ProcRes) {
662 let expected_errors = load_errors(&self.testpaths.file, self.revision);
663 debug!(
664 "check_expected_errors: expected_errors={:?} proc_res.status={:?}",
665 expected_errors, proc_res.status
666 );
667 if proc_res.status.success() && expected_errors.iter().any(|x| x.kind == ErrorKind::Error) {
668 self.fatal_proc_rec("process did not return an error status", proc_res);
669 }
670
671 if self.props.known_bug {
672 if !expected_errors.is_empty() {
673 self.fatal_proc_rec(
674 "`known_bug` tests should not have an expected error",
675 proc_res,
676 );
677 }
678 return;
679 }
680
681 let diagnostic_file_name = if self.props.remap_src_base {
684 let mut p = Utf8PathBuf::from(FAKE_SRC_BASE);
685 p.push(&self.testpaths.relative_dir);
686 p.push(self.testpaths.file.file_name().unwrap());
687 p.to_string()
688 } else {
689 self.testpaths.file.to_string()
690 };
691
692 let expected_kinds: HashSet<_> = [ErrorKind::Error, ErrorKind::Warning]
695 .into_iter()
696 .chain(expected_errors.iter().map(|e| e.kind))
697 .collect();
698
699 let actual_errors = json::parse_output(&diagnostic_file_name, &self.get_output(proc_res))
701 .into_iter()
702 .map(|e| Error { msg: self.normalize_output(&e.msg, &[]), ..e });
703
704 let mut unexpected = Vec::new();
705 let mut unimportant = Vec::new();
706 let mut found = vec![false; expected_errors.len()];
707 for actual_error in actual_errors {
708 for pattern in &self.props.error_patterns {
709 let pattern = pattern.trim();
710 if actual_error.msg.contains(pattern) {
711 let q = if actual_error.line_num.is_none() { "?" } else { "" };
712 self.fatal(&format!(
713 "error pattern '{pattern}' is found in structured \
714 diagnostics, use `//~{q} {} {pattern}` instead",
715 actual_error.kind,
716 ));
717 }
718 }
719
720 let opt_index =
721 expected_errors.iter().enumerate().position(|(index, expected_error)| {
722 !found[index]
723 && actual_error.line_num == expected_error.line_num
724 && actual_error.kind == expected_error.kind
725 && actual_error.msg.contains(&expected_error.msg)
726 });
727
728 match opt_index {
729 Some(index) => {
730 assert!(!found[index]);
732 found[index] = true;
733 }
734
735 None => {
736 if actual_error.require_annotation
737 && expected_kinds.contains(&actual_error.kind)
738 && !self.props.dont_require_annotations.contains(&actual_error.kind)
739 {
740 unexpected.push(actual_error);
741 } else {
742 unimportant.push(actual_error);
743 }
744 }
745 }
746 }
747
748 unexpected.sort_by_key(|e| (e.line_num, e.column_num));
749 unimportant.sort_by_key(|e| (e.line_num, e.column_num));
750
751 let mut not_found = Vec::new();
754 for (index, expected_error) in expected_errors.iter().enumerate() {
756 if !found[index] {
757 not_found.push(expected_error);
758 }
759 }
760
761 if !unexpected.is_empty() || !not_found.is_empty() {
762 let file_name = self
765 .testpaths
766 .file
767 .strip_prefix(self.config.src_root.as_str())
768 .unwrap_or(&self.testpaths.file)
769 .to_string()
770 .replace(r"\", "/");
771 let line_str = |e: &Error| {
772 let line_num = e.line_num.map_or("?".to_string(), |line_num| line_num.to_string());
773 let opt_col_num = match e.column_num {
775 Some(col_num) if line_num != "?" => format!(":{col_num}"),
776 _ => "".to_string(),
777 };
778 format!("{file_name}:{line_num}{opt_col_num}")
779 };
780 let print_error =
781 |e| writeln!(self.stdout, "{}: {}: {}", line_str(e), e.kind, e.msg.cyan());
782 let push_suggestion =
783 |suggestions: &mut Vec<_>, e: &Error, kind, line, msg, color, rank| {
784 let mut ret = String::new();
785 if kind {
786 ret += &format!("{} {}", "with different kind:".color(color), e.kind);
787 }
788 if line {
789 if !ret.is_empty() {
790 ret.push(' ');
791 }
792 ret += &format!("{} {}", "on different line:".color(color), line_str(e));
793 }
794 if msg {
795 if !ret.is_empty() {
796 ret.push(' ');
797 }
798 ret +=
799 &format!("{} {}", "with different message:".color(color), e.msg.cyan());
800 }
801 suggestions.push((ret, rank));
802 };
803 let show_suggestions = |mut suggestions: Vec<_>, prefix: &str, color| {
804 suggestions.sort_by_key(|(_, rank)| *rank);
806 if let Some(&(_, top_rank)) = suggestions.first() {
807 for (suggestion, rank) in suggestions {
808 if rank == top_rank {
809 writeln!(self.stdout, " {} {suggestion}", prefix.color(color));
810 }
811 }
812 }
813 };
814
815 if !unexpected.is_empty() {
822 writeln!(
823 self.stdout,
824 "\n{prefix}: {n} diagnostics reported in JSON output but not expected in test file",
825 prefix = self.error_prefix(),
826 n = unexpected.len(),
827 );
828 for error in &unexpected {
829 print_error(error);
830 let mut suggestions = Vec::new();
831 for candidate in ¬_found {
832 let kind_mismatch = candidate.kind != error.kind;
833 let mut push_red_suggestion = |line, msg, rank| {
834 push_suggestion(
835 &mut suggestions,
836 candidate,
837 kind_mismatch,
838 line,
839 msg,
840 Color::Red,
841 rank,
842 )
843 };
844 if error.msg.contains(&candidate.msg) {
845 push_red_suggestion(candidate.line_num != error.line_num, false, 0);
846 } else if candidate.line_num.is_some()
847 && candidate.line_num == error.line_num
848 {
849 push_red_suggestion(false, true, if kind_mismatch { 2 } else { 1 });
850 }
851 }
852
853 show_suggestions(suggestions, "expected", Color::Red);
854 }
855 }
856 if !not_found.is_empty() {
857 writeln!(
858 self.stdout,
859 "\n{prefix}: {n} diagnostics expected in test file but not reported in JSON output",
860 prefix = self.error_prefix(),
861 n = not_found.len(),
862 );
863
864 if let Some(human_format) = self.props.compile_flags.iter().find(|flag| {
867 flag.contains("error-format")
869 && (flag.contains("short") || flag.contains("human"))
870 }) {
871 let msg = format!(
872 "tests with compile flag `{}` should not have error annotations such as `//~ ERROR`",
873 human_format
874 ).color(Color::Red);
875 writeln!(self.stdout, "{}", msg);
876 }
877
878 for error in ¬_found {
879 print_error(error);
880 let mut suggestions = Vec::new();
881 for candidate in unexpected.iter().chain(&unimportant) {
882 let kind_mismatch = candidate.kind != error.kind;
883 let mut push_green_suggestion = |line, msg, rank| {
884 push_suggestion(
885 &mut suggestions,
886 candidate,
887 kind_mismatch,
888 line,
889 msg,
890 Color::Green,
891 rank,
892 )
893 };
894 if candidate.msg.contains(&error.msg) {
895 push_green_suggestion(candidate.line_num != error.line_num, false, 0);
896 } else if candidate.line_num.is_some()
897 && candidate.line_num == error.line_num
898 {
899 push_green_suggestion(false, true, if kind_mismatch { 2 } else { 1 });
900 }
901 }
902
903 show_suggestions(suggestions, "reported", Color::Green);
904 }
905 }
906 panic!(
907 "errors differ from expected\nstatus: {}\ncommand: {}\n",
908 proc_res.status, proc_res.cmdline
909 );
910 }
911 }
912
913 fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
914 self.compile_test_general(will_execute, emit, Vec::new())
915 }
916
917 fn compile_test_general(
918 &self,
919 will_execute: WillExecute,
920 emit: Emit,
921 passes: Vec<String>,
922 ) -> ProcRes {
923 let compiler_kind = self.compiler_kind_for_non_aux();
924
925 let output_file = match will_execute {
927 WillExecute::Yes => TargetLocation::ThisFile(self.make_exe_name()),
928 WillExecute::No | WillExecute::Disabled => {
929 TargetLocation::ThisDirectory(self.output_base_dir())
930 }
931 };
932
933 let allow_unused = match self.config.mode {
934 TestMode::Ui => {
935 if compiler_kind == CompilerKind::Rustc
941 && self.props.pass_fail_mode != Some(PassFailMode::RunPass)
947 {
948 AllowUnused::Yes
949 } else {
950 AllowUnused::No
951 }
952 }
953 TestMode::Incremental => AllowUnused::Yes,
954 _ => AllowUnused::No,
955 };
956
957 let rustc = self.make_compile_args(
958 compiler_kind,
959 &self.testpaths.file,
960 output_file,
961 emit,
962 allow_unused,
963 LinkToAux::Yes,
964 passes,
965 );
966
967 self.compose_and_run_compiler(rustc, None)
968 }
969
970 fn document(&self, root_out_dir: &Utf8Path, kind: DocKind) -> ProcRes {
973 self.document_inner(&self.testpaths.file, root_out_dir, kind)
974 }
975
976 fn document_inner(
980 &self,
981 file_to_doc: &Utf8Path,
982 root_out_dir: &Utf8Path,
983 kind: DocKind,
984 ) -> ProcRes {
985 if self.props.build_aux_docs {
986 assert_eq!(kind, DocKind::Html, "build-aux-docs only make sense for html output");
987
988 for rel_ab in &self.props.aux.builds {
989 let aux_path = self.resolve_aux_path(rel_ab);
990 let props_for_aux = self.props.from_aux_file(&aux_path, self.revision, self.config);
991 let aux_cx = TestCx {
992 config: self.config,
993 stdout: self.stdout,
994 stderr: self.stderr,
995 props: &props_for_aux,
996 testpaths: self.testpaths,
997 revision: self.revision,
998 };
999 create_dir_all(aux_cx.output_base_dir()).unwrap();
1001 let auxres = aux_cx.document_inner(&aux_path, &root_out_dir, kind);
1002 if !auxres.status.success() {
1003 return auxres;
1004 }
1005 }
1006 }
1007
1008 let aux_dir = self.aux_output_dir_name();
1009
1010 let rustdoc_path = self.config.rustdoc_path.as_ref().expect("--rustdoc-path not passed");
1011
1012 let out_dir: Cow<'_, Utf8Path> = if self.props.unique_doc_out_dir {
1015 let file_name = file_to_doc.file_stem().expect("file name should not be empty");
1016 let out_dir = Utf8PathBuf::from_iter([
1017 root_out_dir,
1018 Utf8Path::new("docs"),
1019 Utf8Path::new(file_name),
1020 Utf8Path::new("doc"),
1021 ]);
1022 create_dir_all(&out_dir).unwrap();
1023 Cow::Owned(out_dir)
1024 } else {
1025 Cow::Borrowed(root_out_dir)
1026 };
1027
1028 let mut rustdoc = Command::new(rustdoc_path);
1029 let current_dir = self.output_base_dir();
1030 rustdoc.current_dir(current_dir);
1031 rustdoc
1032 .arg("-L")
1033 .arg(self.config.target_run_lib_path.as_path())
1034 .arg("-L")
1035 .arg(aux_dir)
1036 .arg("-o")
1037 .arg(out_dir.as_ref())
1038 .arg("--deny")
1039 .arg("warnings")
1040 .arg(file_to_doc)
1041 .arg("-A")
1042 .arg("internal_features")
1043 .args(&self.props.compile_flags)
1044 .args(&self.props.doc_flags);
1045
1046 match kind {
1047 DocKind::Html => {}
1048 DocKind::Json => {
1049 rustdoc.arg("--output-format").arg("json").arg("-Zunstable-options");
1050 }
1051 }
1052
1053 if let Some(ref linker) = self.config.target_linker {
1054 rustdoc.arg(format!("-Clinker={}", linker));
1055 }
1056
1057 self.compose_and_run_compiler(rustdoc, None)
1058 }
1059
1060 fn exec_compiled_test(&self) -> ProcRes {
1061 self.exec_compiled_test_general(&[], true)
1062 }
1063
1064 fn exec_compiled_test_general(
1065 &self,
1066 env_extra: &[(&str, &str)],
1067 delete_after_success: bool,
1068 ) -> ProcRes {
1069 let prepare_env = |cmd: &mut Command| {
1070 for (key, val) in &self.props.exec_env {
1071 cmd.env(key, val);
1072 }
1073 for (key, val) in env_extra {
1074 cmd.env(key, val);
1075 }
1076
1077 for key in &self.props.unset_exec_env {
1078 cmd.env_remove(key);
1079 }
1080 };
1081
1082 let proc_res = match &*self.config.target {
1083 _ if self.config.remote_test_client.is_some() => {
1100 let aux_dir = self.aux_output_dir_name();
1101 let ProcArgs { prog, args } = self.make_run_args();
1102 let mut support_libs = Vec::new();
1103 if let Ok(entries) = aux_dir.read_dir() {
1104 for entry in entries {
1105 let entry = entry.unwrap();
1106 if !entry.path().is_file() {
1107 continue;
1108 }
1109 support_libs.push(entry.path());
1110 }
1111 }
1112 let mut test_client =
1113 Command::new(self.config.remote_test_client.as_ref().unwrap());
1114 test_client
1115 .args(&["run", &support_libs.len().to_string()])
1116 .arg(&prog)
1117 .args(support_libs)
1118 .args(args);
1119
1120 prepare_env(&mut test_client);
1121
1122 self.compose_and_run(
1123 test_client,
1124 self.config.target_run_lib_path.as_path(),
1125 Some(aux_dir.as_path()),
1126 None,
1127 )
1128 }
1129 _ if self.config.target.contains("vxworks") => {
1130 let aux_dir = self.aux_output_dir_name();
1131 let ProcArgs { prog, args } = self.make_run_args();
1132 let mut wr_run = Command::new("wr-run");
1133 wr_run.args(&[&prog]).args(args);
1134
1135 prepare_env(&mut wr_run);
1136
1137 self.compose_and_run(
1138 wr_run,
1139 self.config.target_run_lib_path.as_path(),
1140 Some(aux_dir.as_path()),
1141 None,
1142 )
1143 }
1144 _ => {
1145 let aux_dir = self.aux_output_dir_name();
1146 let ProcArgs { prog, args } = self.make_run_args();
1147 let mut program = Command::new(&prog);
1148 program.args(args).current_dir(&self.output_base_dir());
1149
1150 prepare_env(&mut program);
1151
1152 self.compose_and_run(
1153 program,
1154 self.config.target_run_lib_path.as_path(),
1155 Some(aux_dir.as_path()),
1156 None,
1157 )
1158 }
1159 };
1160
1161 if delete_after_success && proc_res.status.success() {
1162 let _ = fs::remove_file(self.make_exe_name());
1165 }
1166
1167 proc_res
1168 }
1169
1170 fn resolve_aux_path(&self, relative_aux_path: &str) -> Utf8PathBuf {
1173 let aux_path = self
1174 .testpaths
1175 .file
1176 .parent()
1177 .expect("test file path has no parent")
1178 .join("auxiliary")
1179 .join(relative_aux_path);
1180 if !aux_path.exists() {
1181 self.fatal(&format!(
1182 "auxiliary source file `{relative_aux_path}` not found at `{aux_path}`"
1183 ));
1184 }
1185
1186 aux_path
1187 }
1188
1189 fn is_vxworks_pure_static(&self) -> bool {
1190 if self.config.target.contains("vxworks") {
1191 match env::var("RUST_VXWORKS_TEST_DYLINK") {
1192 Ok(s) => s != "1",
1193 _ => true,
1194 }
1195 } else {
1196 false
1197 }
1198 }
1199
1200 fn is_vxworks_pure_dynamic(&self) -> bool {
1201 self.config.target.contains("vxworks") && !self.is_vxworks_pure_static()
1202 }
1203
1204 fn has_aux_dir(&self) -> bool {
1205 !self.props.aux.builds.is_empty()
1206 || !self.props.aux.crates.is_empty()
1207 || !self.props.aux.proc_macros.is_empty()
1208 }
1209
1210 fn aux_output_dir(&self) -> Utf8PathBuf {
1211 let aux_dir = self.aux_output_dir_name();
1212
1213 if !self.props.aux.builds.is_empty() {
1214 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1215 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1216 });
1217 }
1218
1219 if !self.props.aux.bins.is_empty() {
1220 let aux_bin_dir = self.aux_bin_output_dir_name();
1221 remove_and_create_dir_all(&aux_dir).unwrap_or_else(|e| {
1222 panic!("failed to remove and recreate output directory `{aux_dir}`: {e}")
1223 });
1224 remove_and_create_dir_all(&aux_bin_dir).unwrap_or_else(|e| {
1225 panic!("failed to remove and recreate output directory `{aux_bin_dir}`: {e}")
1226 });
1227 }
1228
1229 aux_dir
1230 }
1231
1232 fn build_all_auxiliary(&self, aux_dir: &Utf8Path, rustc: &mut Command) {
1233 for rel_ab in &self.props.aux.builds {
1234 self.build_auxiliary(rel_ab, &aux_dir, None);
1235 }
1236
1237 for rel_ab in &self.props.aux.bins {
1238 self.build_auxiliary(rel_ab, &aux_dir, Some(AuxType::Bin));
1239 }
1240
1241 let path_to_crate_name = |path: &str| -> String {
1242 path.rsplit_once('/')
1243 .map_or(path, |(_, tail)| tail)
1244 .trim_end_matches(".rs")
1245 .replace('-', "_")
1246 };
1247
1248 let add_extern = |rustc: &mut Command,
1249 extern_modifiers: Option<&str>,
1250 aux_name: &str,
1251 aux_path: &str,
1252 aux_type: AuxType| {
1253 let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1254 if let Some(lib_name) = lib_name {
1255 let modifiers_and_name = match extern_modifiers {
1256 Some(modifiers) => format!("{modifiers}:{aux_name}"),
1257 None => aux_name.to_string(),
1258 };
1259 rustc.arg("--extern").arg(format!("{modifiers_and_name}={aux_dir}/{lib_name}"));
1260 }
1261 };
1262
1263 for AuxCrate { extern_modifiers, name, path } in &self.props.aux.crates {
1264 let aux_type = self.build_auxiliary(&path, &aux_dir, None);
1265 add_extern(rustc, extern_modifiers.as_deref(), name, path, aux_type);
1266 }
1267
1268 for proc_macro in &self.props.aux.proc_macros {
1269 self.build_auxiliary(&proc_macro.path, &aux_dir, Some(AuxType::ProcMacro));
1270 let crate_name = path_to_crate_name(&proc_macro.path);
1271 add_extern(
1272 rustc,
1273 proc_macro.extern_modifiers.as_deref(),
1274 &crate_name,
1275 &proc_macro.path,
1276 AuxType::ProcMacro,
1277 );
1278 }
1279
1280 if let Some(aux_file) = &self.props.aux.codegen_backend {
1283 let aux_type = self.build_auxiliary(aux_file, aux_dir, None);
1284 if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
1285 let lib_path = aux_dir.join(&lib_name);
1286 rustc.arg(format!("-Zcodegen-backend={}", lib_path));
1287 }
1288 }
1289 }
1290
1291 fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
1294 if self.props.add_minicore {
1295 let minicore_path = self.build_minicore();
1296 rustc.arg("--extern");
1297 rustc.arg(&format!("minicore={}", minicore_path));
1298 }
1299
1300 let aux_dir = self.aux_output_dir();
1301 self.build_all_auxiliary(&aux_dir, &mut rustc);
1302
1303 rustc.envs(self.props.rustc_env.clone());
1304 self.props.unset_rustc_env.iter().fold(&mut rustc, Command::env_remove);
1305 self.compose_and_run(
1306 rustc,
1307 self.config.host_compile_lib_path.as_path(),
1308 Some(aux_dir.as_path()),
1309 input,
1310 )
1311 }
1312
1313 fn build_minicore(&self) -> Utf8PathBuf {
1316 let output_file_path = self.output_base_dir().join("libminicore.rlib");
1317 let mut rustc = self.make_compile_args(
1318 CompilerKind::Rustc,
1319 &self.config.minicore_path,
1320 TargetLocation::ThisFile(output_file_path.clone()),
1321 Emit::None,
1322 AllowUnused::Yes,
1323 LinkToAux::No,
1324 vec![],
1325 );
1326
1327 rustc.args(&["--crate-type", "rlib"]);
1328 rustc.arg("-Cpanic=abort");
1329 rustc.args(self.props.minicore_compile_flags.clone());
1330
1331 let res =
1332 self.compose_and_run(rustc, self.config.host_compile_lib_path.as_path(), None, None);
1333 if !res.status.success() {
1334 self.fatal_proc_rec(
1335 &format!("auxiliary build of {} failed to compile: ", self.config.minicore_path),
1336 &res,
1337 );
1338 }
1339
1340 output_file_path
1341 }
1342
1343 fn build_auxiliary(
1347 &self,
1348 source_path: &str,
1349 aux_dir: &Utf8Path,
1350 aux_type: Option<AuxType>,
1351 ) -> AuxType {
1352 let aux_path = self.resolve_aux_path(source_path);
1353 let mut aux_props = self.props.from_aux_file(&aux_path, self.revision, self.config);
1354 if aux_type == Some(AuxType::ProcMacro) {
1355 aux_props.force_host = true;
1356 }
1357 let mut aux_dir = aux_dir.to_path_buf();
1358 if aux_type == Some(AuxType::Bin) {
1359 aux_dir.push("bin");
1363 }
1364 let aux_output = TargetLocation::ThisDirectory(aux_dir.clone());
1365 let aux_cx = TestCx {
1366 config: self.config,
1367 stdout: self.stdout,
1368 stderr: self.stderr,
1369 props: &aux_props,
1370 testpaths: self.testpaths,
1371 revision: self.revision,
1372 };
1373 create_dir_all(aux_cx.output_base_dir()).unwrap();
1375 let mut aux_rustc = aux_cx.make_compile_args(
1376 CompilerKind::Rustc,
1378 &aux_path,
1379 aux_output,
1380 Emit::None,
1381 AllowUnused::No,
1382 LinkToAux::No,
1383 Vec::new(),
1384 );
1385 aux_cx.build_all_auxiliary(&aux_dir, &mut aux_rustc);
1386
1387 aux_rustc.envs(aux_props.rustc_env.clone());
1388 for key in &aux_props.unset_rustc_env {
1389 aux_rustc.env_remove(key);
1390 }
1391
1392 let (aux_type, crate_type) = if aux_type == Some(AuxType::Bin) {
1393 (AuxType::Bin, Some("bin"))
1394 } else if aux_type == Some(AuxType::ProcMacro) {
1395 (AuxType::ProcMacro, Some("proc-macro"))
1396 } else if aux_type.is_some() {
1397 panic!("aux_type {aux_type:?} not expected");
1398 } else if aux_props.no_prefer_dynamic {
1399 (AuxType::Lib, None)
1400 } else if self.config.target.contains("emscripten")
1401 || (self.config.target.contains("musl")
1402 && !aux_props.force_host
1403 && !self.config.host.contains("musl"))
1404 || self.config.target.contains("wasm32")
1405 || self.config.target.contains("nvptx")
1406 || self.is_vxworks_pure_static()
1407 || self.config.target.contains("bpf")
1408 || !self.config.target_cfg().dynamic_linking
1409 || matches!(self.config.mode, TestMode::CoverageMap | TestMode::CoverageRun)
1410 {
1411 (AuxType::Lib, Some("lib"))
1425 } else {
1426 (AuxType::Dylib, Some("dylib"))
1427 };
1428
1429 if let Some(crate_type) = crate_type {
1430 aux_rustc.args(&["--crate-type", crate_type]);
1431 }
1432
1433 if aux_type == AuxType::ProcMacro {
1434 aux_rustc.args(&["--extern", "proc_macro"]);
1436 }
1437
1438 aux_rustc.arg("-L").arg(&aux_dir);
1439
1440 if aux_props.add_minicore {
1441 let minicore_path = self.build_minicore();
1442 aux_rustc.arg("--extern");
1443 aux_rustc.arg(&format!("minicore={}", minicore_path));
1444 }
1445
1446 let auxres = aux_cx.compose_and_run(
1447 aux_rustc,
1448 aux_cx.config.host_compile_lib_path.as_path(),
1449 Some(aux_dir.as_path()),
1450 None,
1451 );
1452 if !auxres.status.success() {
1453 self.fatal_proc_rec(
1454 &format!("auxiliary build of {aux_path} failed to compile: "),
1455 &auxres,
1456 );
1457 }
1458 aux_type
1459 }
1460
1461 fn read2_abbreviated(&self, child: Child) -> (Output, Truncated) {
1462 let mut filter_paths_from_len = Vec::new();
1463 let mut add_path = |path: &Utf8Path| {
1464 let path = path.to_string();
1465 let windows = path.replace("\\", "\\\\");
1466 if windows != path {
1467 filter_paths_from_len.push(windows);
1468 }
1469 filter_paths_from_len.push(path);
1470 };
1471
1472 add_path(&self.config.src_test_suite_root);
1478 add_path(&self.config.build_test_suite_root);
1479
1480 read2_abbreviated(child, &filter_paths_from_len).expect("failed to read output")
1481 }
1482
1483 fn compose_and_run(
1484 &self,
1485 mut command: Command,
1486 lib_path: &Utf8Path,
1487 aux_path: Option<&Utf8Path>,
1488 input: Option<String>,
1489 ) -> ProcRes {
1490 let cmdline = {
1491 let cmdline = self.make_cmdline(&command, lib_path);
1492 self.logv(format_args!("executing {cmdline}"));
1493 cmdline
1494 };
1495
1496 command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::piped());
1497
1498 add_dylib_path(&mut command, iter::once(lib_path).chain(aux_path));
1501
1502 let mut child = disable_error_reporting(|| command.spawn())
1503 .unwrap_or_else(|e| panic!("failed to exec `{command:?}`: {e:?}"));
1504 if let Some(input) = input {
1505 child.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
1506 }
1507
1508 let (Output { status, stdout, stderr }, truncated) = self.read2_abbreviated(child);
1509
1510 let result = ProcRes {
1511 status,
1512 stdout: String::from_utf8_lossy(&stdout).into_owned(),
1513 stderr: String::from_utf8_lossy(&stderr).into_owned(),
1514 truncated,
1515 cmdline,
1516 };
1517
1518 self.dump_output(
1519 self.config.verbose || (!result.status.success() && self.config.mode != TestMode::Ui),
1520 &command.get_program().to_string_lossy(),
1521 &result.stdout,
1522 &result.stderr,
1523 );
1524
1525 result
1526 }
1527
1528 fn compiler_kind_for_non_aux(&self) -> CompilerKind {
1531 match self.config.suite {
1532 TestSuite::RustdocJs | TestSuite::RustdocJson | TestSuite::RustdocUi => {
1533 CompilerKind::Rustdoc
1534 }
1535
1536 TestSuite::AssemblyLlvm
1540 | TestSuite::BuildStd
1541 | TestSuite::CodegenLlvm
1542 | TestSuite::CodegenUnits
1543 | TestSuite::Coverage
1544 | TestSuite::CoverageRunRustdoc
1545 | TestSuite::Crashes
1546 | TestSuite::Debuginfo
1547 | TestSuite::Incremental
1548 | TestSuite::MirOpt
1549 | TestSuite::Pretty
1550 | TestSuite::RunMake
1551 | TestSuite::RunMakeCargo
1552 | TestSuite::RustdocGui
1553 | TestSuite::RustdocHtml
1554 | TestSuite::RustdocJsStd
1555 | TestSuite::Ui
1556 | TestSuite::UiFullDeps => CompilerKind::Rustc,
1557 }
1558 }
1559
1560 fn make_compile_args(
1561 &self,
1562 compiler_kind: CompilerKind,
1563 input_file: &Utf8Path,
1564 output_file: TargetLocation,
1565 emit: Emit,
1566 allow_unused: AllowUnused,
1567 link_to_aux: LinkToAux,
1568 passes: Vec<String>, ) -> Command {
1570 let mut compiler = match compiler_kind {
1573 CompilerKind::Rustc => Command::new(&self.config.rustc_path),
1574 CompilerKind::Rustdoc => {
1575 Command::new(&self.config.rustdoc_path.clone().expect("no rustdoc built yet"))
1576 }
1577 };
1578 compiler.arg(input_file);
1579
1580 compiler.arg("-Zthreads=1");
1582
1583 compiler.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
1592 compiler.arg("-Ztranslate-remapped-path-to-local-path=no");
1593
1594 compiler.arg("-Z").arg(format!(
1599 "ignore-directory-in-diagnostics-source-blocks={}",
1600 home::cargo_home().expect("failed to find cargo home").to_str().unwrap()
1601 ));
1602 compiler.arg("-Z").arg(format!(
1604 "ignore-directory-in-diagnostics-source-blocks={}",
1605 self.config.src_root.join("vendor"),
1606 ));
1607
1608 if !self.props.compile_flags.iter().any(|flag| flag.starts_with("--sysroot"))
1612 && !self.config.host_rustcflags.iter().any(|flag| flag == "--sysroot")
1613 {
1614 compiler.arg("--sysroot").arg(&self.config.sysroot_base);
1616 }
1617
1618 if let Some(ref backend) = self.config.override_codegen_backend {
1620 compiler.arg(format!("-Zcodegen-backend={}", backend));
1621 }
1622
1623 let custom_target = self.props.compile_flags.iter().any(|x| x.starts_with("--target"));
1625
1626 if !custom_target {
1627 let target =
1628 if self.props.force_host { &*self.config.host } else { &*self.config.target };
1629
1630 compiler.arg(&format!("--target={}", target));
1631 if target.ends_with(".json") {
1632 compiler.arg("-Zunstable-options");
1635 }
1636 }
1637 self.set_revision_flags(&mut compiler);
1638
1639 if compiler_kind == CompilerKind::Rustc {
1640 if let Some(ref incremental_dir) = self.props.incremental_dir {
1641 compiler.args(&["-C", &format!("incremental={}", incremental_dir)]);
1642 compiler.args(&["-Z", "incremental-verify-ich"]);
1643 }
1644
1645 if self.config.mode == TestMode::CodegenUnits {
1646 compiler.args(&["-Z", "human_readable_cgu_names"]);
1647 }
1648
1649 if self.config.mode == TestMode::DebugInfo && cfg!(target_os = "windows") {
1650 compiler.args(&["-Z", r#"crate-attr=windows_subsystem="windows""#]);
1652 }
1653 }
1654
1655 if self.config.optimize_tests && compiler_kind == CompilerKind::Rustc {
1656 match self.config.mode {
1657 TestMode::Ui => {
1658 if self.effective_pass_fail_mode() == Some(PassFailMode::RunPass)
1665 && !self
1666 .props
1667 .compile_flags
1668 .iter()
1669 .any(|arg| arg == "-O" || arg.contains("opt-level"))
1670 {
1671 compiler.arg("-O");
1672 }
1673 }
1674 TestMode::DebugInfo => { }
1675 TestMode::CoverageMap | TestMode::CoverageRun => {
1676 }
1681 _ => {
1682 compiler.arg("-O");
1683 }
1684 }
1685 }
1686
1687 let set_mir_dump_dir = |rustc: &mut Command| {
1688 let mir_dump_dir = self.output_base_dir();
1689 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1690 dir_opt.push_str(mir_dump_dir.as_str());
1691 debug!("dir_opt: {:?}", dir_opt);
1692 rustc.arg(dir_opt);
1693 };
1694
1695 match self.config.mode {
1696 TestMode::Incremental => {
1697 if self.props.error_patterns.is_empty()
1701 && self.props.regex_error_patterns.is_empty()
1702 {
1703 compiler.args(&["--error-format", "json"]);
1704 compiler.args(&["--json", "future-incompat"]);
1705 }
1706 compiler.arg("-Zui-testing");
1707 compiler.arg("-Zdeduplicate-diagnostics=no");
1708 }
1709 TestMode::Ui => {
1710 if !self.props.compile_flags.iter().any(|s| s.starts_with("--error-format")) {
1711 compiler.args(&["--error-format", "json"]);
1712 compiler.args(&["--json", "future-incompat"]);
1713 }
1714 compiler.arg("-Ccodegen-units=1");
1715 compiler.arg("-Zui-testing");
1717 compiler.arg("-Zdeduplicate-diagnostics=no");
1718 compiler.arg("-Zwrite-long-types-to-disk=no");
1719 compiler.arg("-Cstrip=debuginfo");
1721
1722 if self.config.parallel_frontend_enabled() {
1723 compiler.arg(&format!("-Zthreads={}", self.config.parallel_frontend_threads));
1728 }
1729 }
1730 TestMode::MirOpt => {
1731 let zdump_arg = if !passes.is_empty() {
1735 format!("-Zdump-mir={}", passes.join(" | "))
1736 } else {
1737 "-Zdump-mir=all".to_string()
1738 };
1739
1740 compiler.args(&[
1741 "-Copt-level=1",
1742 &zdump_arg,
1743 "-Zvalidate-mir",
1744 "-Zlint-mir",
1745 "-Zdump-mir-exclude-pass-number",
1746 "-Zmir-include-spans=false", "--crate-type=rlib",
1748 ]);
1749 if let Some(pass) = &self.props.mir_unit_test {
1750 compiler
1751 .args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]);
1752 } else {
1753 compiler.args(&[
1754 "-Zmir-opt-level=4",
1755 "-Zmir-enable-passes=+ReorderBasicBlocks,+ReorderLocals",
1756 ]);
1757 }
1758
1759 set_mir_dump_dir(&mut compiler);
1760 }
1761 TestMode::CoverageMap => {
1762 compiler.arg("-Cinstrument-coverage");
1763 compiler.arg("-Zno-profiler-runtime");
1766 compiler.arg("-Copt-level=2");
1770 }
1771 TestMode::CoverageRun => {
1772 compiler.arg("-Cinstrument-coverage");
1773 compiler.arg("-Copt-level=2");
1777 }
1778 TestMode::Assembly | TestMode::Codegen => {
1779 compiler.arg("-Cdebug-assertions=no");
1780 compiler.arg("-Zcodegen-source-order");
1784 }
1785 TestMode::Crashes => {
1786 set_mir_dump_dir(&mut compiler);
1787 }
1788 TestMode::CodegenUnits => {
1789 compiler.arg("-Zprint-mono-items");
1790 }
1791 TestMode::Pretty
1792 | TestMode::DebugInfo
1793 | TestMode::RustdocHtml
1794 | TestMode::RustdocJson
1795 | TestMode::RunMake
1796 | TestMode::RustdocJs => {
1797 }
1799 }
1800
1801 if self.props.remap_src_base {
1802 compiler.arg(format!(
1803 "--remap-path-prefix={}={}",
1804 self.config.src_test_suite_root, FAKE_SRC_BASE,
1805 ));
1806 }
1807
1808 if compiler_kind == CompilerKind::Rustc {
1809 match emit {
1810 Emit::None => {}
1811 Emit::Metadata => {
1812 compiler.args(&["--emit", "metadata"]);
1813 }
1814 Emit::LlvmIr => {
1815 compiler.args(&["--emit", "llvm-ir"]);
1816 }
1817 Emit::Mir => {
1818 compiler.args(&["--emit", "mir"]);
1819 }
1820 Emit::Asm => {
1821 compiler.args(&["--emit", "asm"]);
1822 }
1823 Emit::LinkArgsAsm => {
1824 compiler.args(&["-Clink-args=--emit=asm"]);
1825 }
1826 }
1827 }
1828
1829 if compiler_kind == CompilerKind::Rustc {
1830 if self.config.target == "wasm32-unknown-unknown" || self.is_vxworks_pure_static() {
1831 } else if !self.props.no_prefer_dynamic {
1833 compiler.args(&["-C", "prefer-dynamic"]);
1834 }
1835 }
1836
1837 match output_file {
1838 _ if self.props.compile_flags.iter().any(|flag| flag == "-o") => {}
1841 TargetLocation::ThisFile(path) => {
1842 compiler.arg("-o").arg(path);
1843 }
1844 TargetLocation::ThisDirectory(path) => match compiler_kind {
1845 CompilerKind::Rustdoc => {
1846 compiler.arg("-o").arg(path);
1848 }
1849 CompilerKind::Rustc => {
1850 compiler.arg("--out-dir").arg(path);
1851 }
1852 },
1853 }
1854
1855 match self.config.compare_mode {
1856 Some(CompareMode::Polonius) => {
1857 compiler.args(&["-Zpolonius=next"]);
1858 }
1859 Some(CompareMode::NextSolver) => {
1860 compiler.args(&["-Znext-solver"]);
1861 }
1862 Some(CompareMode::NextSolverCoherence) => {
1863 compiler.args(&["-Znext-solver=coherence"]);
1864 }
1865 Some(CompareMode::SplitDwarf) if self.config.target.contains("windows") => {
1866 compiler.args(&["-Csplit-debuginfo=unpacked", "-Zunstable-options"]);
1867 }
1868 Some(CompareMode::SplitDwarf) => {
1869 compiler.args(&["-Csplit-debuginfo=unpacked"]);
1870 }
1871 Some(CompareMode::SplitDwarfSingle) => {
1872 compiler.args(&["-Csplit-debuginfo=packed"]);
1873 }
1874 None => {}
1875 }
1876
1877 if let AllowUnused::Yes = allow_unused {
1881 compiler.args(&["-A", "unused", "-W", "unused_attributes"]);
1882 }
1883
1884 compiler.args(&["-A", "internal_features"]);
1886 compiler.args(&["-A", "incomplete_features"]);
1887
1888 compiler.args(&["-A", "unused_parens"]);
1892 compiler.args(&["-A", "unused_braces"]);
1893
1894 if self.props.force_host {
1895 self.maybe_add_external_args(&mut compiler, &self.config.host_rustcflags);
1896 if compiler_kind == CompilerKind::Rustc
1897 && let Some(ref linker) = self.config.host_linker
1898 {
1899 compiler.arg(format!("-Clinker={linker}"));
1900 }
1901 } else {
1902 self.maybe_add_external_args(&mut compiler, &self.config.target_rustcflags);
1903 if compiler_kind == CompilerKind::Rustc
1904 && let Some(ref linker) = self.config.target_linker
1905 {
1906 compiler.arg(format!("-Clinker={linker}"));
1907 }
1908 }
1909
1910 if self.config.host.contains("musl") || self.is_vxworks_pure_dynamic() {
1912 compiler.arg("-Ctarget-feature=-crt-static");
1913 }
1914
1915 if let LinkToAux::Yes = link_to_aux {
1916 if self.has_aux_dir() {
1919 compiler.arg("-L").arg(self.aux_output_dir_name());
1920 }
1921 }
1922
1923 if self.props.add_minicore {
1933 compiler.arg("-Cpanic=abort");
1934 compiler.arg("-Cforce-unwind-tables=yes");
1935 }
1936
1937 compiler.args(&self.props.compile_flags);
1938
1939 compiler
1940 }
1941
1942 fn make_exe_name(&self) -> Utf8PathBuf {
1943 let mut f = self.output_base_dir().join("a");
1948 if self.config.target.contains("emscripten") {
1950 f = f.with_extra_extension("js");
1951 } else if self.config.target.starts_with("wasm") {
1952 f = f.with_extra_extension("wasm");
1953 } else if self.config.target.contains("spirv") {
1954 f = f.with_extra_extension("spv");
1955 } else if !env::consts::EXE_SUFFIX.is_empty() {
1956 f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1957 }
1958 f
1959 }
1960
1961 fn make_run_args(&self) -> ProcArgs {
1962 let mut args = self.split_maybe_args(&self.config.runner);
1965
1966 let exe_file = self.make_exe_name();
1967
1968 args.push(exe_file.into_os_string());
1969
1970 args.extend(self.props.run_flags.iter().map(OsString::from));
1972
1973 let prog = args.remove(0);
1974 ProcArgs { prog, args }
1975 }
1976
1977 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<OsString> {
1978 match *argstr {
1979 Some(ref s) => s
1980 .split(' ')
1981 .filter_map(|s| {
1982 if s.chars().all(|c| c.is_whitespace()) {
1983 None
1984 } else {
1985 Some(OsString::from(s))
1986 }
1987 })
1988 .collect(),
1989 None => Vec::new(),
1990 }
1991 }
1992
1993 fn make_cmdline(&self, command: &Command, libpath: &Utf8Path) -> String {
1994 use crate::util;
1995
1996 if cfg!(unix) {
1998 format!("{:?}", command)
1999 } else {
2000 fn lib_path_cmd_prefix(path: &str) -> String {
2003 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
2004 }
2005
2006 format!("{} {:?}", lib_path_cmd_prefix(libpath.as_str()), command)
2007 }
2008 }
2009
2010 fn dump_output(&self, print_output: bool, proc_name: &str, out: &str, err: &str) {
2011 let revision = if let Some(r) = self.revision { format!("{}.", r) } else { String::new() };
2012
2013 self.dump_output_file(out, &format!("{}out", revision));
2014 self.dump_output_file(err, &format!("{}err", revision));
2015
2016 if !print_output {
2017 return;
2018 }
2019
2020 let path = Utf8Path::new(proc_name);
2021 let proc_name = if path.file_stem().is_some_and(|p| p == "rmake") {
2022 String::from_iter(
2023 path.parent()
2024 .unwrap()
2025 .file_name()
2026 .into_iter()
2027 .chain(Some("/"))
2028 .chain(path.file_name()),
2029 )
2030 } else {
2031 path.file_name().unwrap().into()
2032 };
2033 writeln!(self.stdout, "------{proc_name} stdout------------------------------");
2034 writeln!(self.stdout, "{}", out);
2035 writeln!(self.stdout, "------{proc_name} stderr------------------------------");
2036 writeln!(self.stdout, "{}", err);
2037 writeln!(self.stdout, "------------------------------------------");
2038 }
2039
2040 fn dump_output_file(&self, out: &str, extension: &str) {
2041 let outfile = self.make_out_name(extension);
2042 fs::write(outfile.as_std_path(), out)
2043 .unwrap_or_else(|err| panic!("failed to write {outfile}: {err:?}"));
2044 }
2045
2046 fn make_out_name(&self, extension: &str) -> Utf8PathBuf {
2049 self.output_base_name().with_extension(extension)
2050 }
2051
2052 fn aux_output_dir_name(&self) -> Utf8PathBuf {
2055 self.output_base_dir()
2056 .join("auxiliary")
2057 .with_extra_extension(self.config.mode.aux_dir_disambiguator())
2058 }
2059
2060 fn aux_bin_output_dir_name(&self) -> Utf8PathBuf {
2063 self.aux_output_dir_name().join("bin")
2064 }
2065
2066 fn safe_revision(&self) -> Option<&str> {
2069 if self.config.mode == TestMode::Incremental { None } else { self.revision }
2070 }
2071
2072 fn output_base_dir(&self) -> Utf8PathBuf {
2076 output_base_dir(self.config, self.testpaths, self.safe_revision())
2077 }
2078
2079 fn output_base_name(&self) -> Utf8PathBuf {
2083 output_base_name(self.config, self.testpaths, self.safe_revision())
2084 }
2085
2086 fn logv(&self, message: impl fmt::Display) {
2091 debug!("{message}");
2092 if self.config.verbose {
2093 writeln!(self.stdout, "{message}");
2095 }
2096 }
2097
2098 #[must_use]
2101 fn error_prefix(&self) -> String {
2102 match self.revision {
2103 Some(rev) => format!("error in revision `{rev}`"),
2104 None => format!("error"),
2105 }
2106 }
2107
2108 #[track_caller]
2109 fn fatal(&self, err: &str) -> ! {
2110 writeln!(self.stdout, "\n{prefix}: {err}", prefix = self.error_prefix());
2111 error!("fatal error, panic: {:?}", err);
2112 panic!("fatal error");
2113 }
2114
2115 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
2116 self.fatal_proc_rec_general(err, None, proc_res, || ());
2117 }
2118
2119 fn fatal_proc_rec_general(
2122 &self,
2123 err: &str,
2124 extra_note: Option<&str>,
2125 proc_res: &ProcRes,
2126 callback_before_unwind: impl FnOnce(),
2127 ) -> ! {
2128 writeln!(self.stdout, "\n{prefix}: {err}", prefix = self.error_prefix());
2129
2130 if let Some(note) = extra_note {
2132 writeln!(self.stdout, "{note}");
2133 }
2134
2135 writeln!(self.stdout, "{}", proc_res.format_info());
2137
2138 callback_before_unwind();
2140
2141 std::panic::resume_unwind(Box::new(()));
2144 }
2145
2146 fn compile_test_and_save_ir(&self) -> (ProcRes, Utf8PathBuf) {
2149 let output_path = self.output_base_name().with_extension("ll");
2150 let input_file = &self.testpaths.file;
2151 let rustc = self.make_compile_args(
2152 CompilerKind::Rustc,
2153 input_file,
2154 TargetLocation::ThisFile(output_path.clone()),
2155 Emit::LlvmIr,
2156 AllowUnused::No,
2157 LinkToAux::Yes,
2158 Vec::new(),
2159 );
2160
2161 let proc_res = self.compose_and_run_compiler(rustc, None);
2162 (proc_res, output_path)
2163 }
2164
2165 fn verify_with_filecheck(&self, output: &Utf8Path) -> ProcRes {
2166 let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
2167 filecheck.arg("--input-file").arg(output).arg(&self.testpaths.file);
2168
2169 filecheck.arg("--check-prefix=CHECK");
2171
2172 if let Some(rev) = self.revision {
2180 filecheck.arg("--check-prefix").arg(rev);
2181 }
2182
2183 filecheck.arg("--allow-unused-prefixes");
2187
2188 filecheck.args(&["--dump-input-context", "100"]);
2190
2191 filecheck.args(&self.props.filecheck_flags);
2193
2194 self.compose_and_run(filecheck, Utf8Path::new(""), None, None)
2196 }
2197
2198 fn charset() -> &'static str {
2199 if cfg!(target_os = "freebsd") { "ISO-8859-1" } else { "UTF-8" }
2201 }
2202
2203 fn get_lines(&self, path: &Utf8Path, mut other_files: Option<&mut Vec<String>>) -> Vec<usize> {
2204 let content = fs::read_to_string(path.as_std_path()).unwrap();
2205 let mut ignore = false;
2206 content
2207 .lines()
2208 .enumerate()
2209 .filter_map(|(line_nb, line)| {
2210 if (line.trim_start().starts_with("pub mod ")
2211 || line.trim_start().starts_with("mod "))
2212 && line.ends_with(';')
2213 {
2214 if let Some(ref mut other_files) = other_files {
2215 other_files.push(line.rsplit("mod ").next().unwrap().replace(';', ""));
2216 }
2217 None
2218 } else {
2219 let sline = line.rsplit("///").next().unwrap();
2220 let line = sline.trim_start();
2221 if line.starts_with("```") {
2222 if ignore {
2223 ignore = false;
2224 None
2225 } else {
2226 ignore = true;
2227 Some(line_nb + 1)
2228 }
2229 } else {
2230 None
2231 }
2232 }
2233 })
2234 .collect()
2235 }
2236
2237 fn check_rustdoc_test_option(&self, res: ProcRes) {
2242 let mut other_files = Vec::new();
2243 let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2244 let normalized = fs::canonicalize(&self.testpaths.file).expect("failed to canonicalize");
2245 let normalized = normalized.to_str().unwrap().replace('\\', "/");
2246 files.insert(normalized, self.get_lines(&self.testpaths.file, Some(&mut other_files)));
2247 for other_file in other_files {
2248 let mut path = self.testpaths.file.clone();
2249 path.set_file_name(&format!("{}.rs", other_file));
2250 let path = path.canonicalize_utf8().expect("failed to canonicalize");
2251 let normalized = path.as_str().replace('\\', "/");
2252 files.insert(normalized, self.get_lines(&path, None));
2253 }
2254
2255 let mut tested = 0;
2256 for _ in res.stdout.split('\n').filter(|s| s.starts_with("test ")).inspect(|s| {
2257 if let Some((left, right)) = s.split_once(" - ") {
2258 let path = left.rsplit("test ").next().unwrap();
2259 let path = fs::canonicalize(&path).expect("failed to canonicalize");
2260 let path = path.to_str().unwrap().replace('\\', "/");
2261 if let Some(ref mut v) = files.get_mut(&path) {
2262 tested += 1;
2263 let mut iter = right.split("(line ");
2264 iter.next();
2265 let line = iter
2266 .next()
2267 .unwrap_or(")")
2268 .split(')')
2269 .next()
2270 .unwrap_or("0")
2271 .parse()
2272 .unwrap_or(0);
2273 if let Ok(pos) = v.binary_search(&line) {
2274 v.remove(pos);
2275 } else {
2276 self.fatal_proc_rec(
2277 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2278 &res,
2279 );
2280 }
2281 }
2282 }
2283 }) {}
2284 if tested == 0 {
2285 self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2286 } else {
2287 for (entry, v) in &files {
2288 if !v.is_empty() {
2289 self.fatal_proc_rec(
2290 &format!(
2291 "Not found test at line{} \"{}\":{:?}",
2292 if v.len() > 1 { "s" } else { "" },
2293 entry,
2294 v
2295 ),
2296 &res,
2297 );
2298 }
2299 }
2300 }
2301 }
2302
2303 fn force_color_svg(&self) -> bool {
2304 self.props.compile_flags.iter().any(|s| s.contains("--color=always"))
2305 }
2306
2307 fn load_compare_outputs(
2308 &self,
2309 proc_res: &ProcRes,
2310 output_kind: TestOutput,
2311 explicit_format: bool,
2312 ) -> usize {
2313 let stderr_bits = format!("{}bit.stderr", self.config.get_pointer_width());
2314 let (stderr_kind, stdout_kind) = match output_kind {
2315 TestOutput::Compile => (
2316 if self.force_color_svg() {
2317 if self.config.target.contains("windows") {
2318 UI_WINDOWS_SVG
2321 } else {
2322 UI_SVG
2323 }
2324 } else if self.props.stderr_per_bitwidth {
2325 &stderr_bits
2326 } else {
2327 UI_STDERR
2328 },
2329 UI_STDOUT,
2330 ),
2331 TestOutput::Run => (UI_RUN_STDERR, UI_RUN_STDOUT),
2332 };
2333
2334 let expected_stderr = self.load_expected_output(stderr_kind);
2335 let expected_stdout = self.load_expected_output(stdout_kind);
2336
2337 let mut normalized_stdout =
2338 self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2339 match output_kind {
2340 TestOutput::Run if self.config.remote_test_client.is_some() => {
2341 normalized_stdout = static_regex!(
2346 "^uploaded \"\\$TEST_BUILD_DIR(/[[:alnum:]_\\-.]+)+\", waiting for result\n"
2347 )
2348 .replace(&normalized_stdout, "")
2349 .to_string();
2350 normalized_stdout = static_regex!("^died due to signal [0-9]+\n")
2353 .replace(&normalized_stdout, "")
2354 .to_string();
2355 }
2358 _ => {}
2359 };
2360
2361 let stderr;
2362 let normalized_stderr;
2363
2364 if self.force_color_svg() {
2365 let normalized = self.normalize_output(&proc_res.stderr, &self.props.normalize_stderr);
2366 stderr = anstyle_svg::Term::new().render_svg(&normalized);
2367 normalized_stderr = stderr.clone();
2368 } else {
2369 stderr = if explicit_format {
2370 proc_res.stderr.clone()
2371 } else {
2372 json::extract_rendered(&proc_res.stderr)
2373 };
2374 normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2375 }
2376
2377 let mut errors = 0;
2378 match output_kind {
2379 TestOutput::Compile => {
2380 if !self.props.dont_check_compiler_stdout {
2381 if self
2382 .compare_output(
2383 stdout_kind,
2384 &normalized_stdout,
2385 &proc_res.stdout,
2386 &expected_stdout,
2387 )
2388 .should_error()
2389 {
2390 errors += 1;
2391 }
2392 }
2393 if !self.props.dont_check_compiler_stderr {
2394 if self
2395 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2396 .should_error()
2397 {
2398 errors += 1;
2399 }
2400 }
2401 }
2402 TestOutput::Run => {
2403 if self
2404 .compare_output(
2405 stdout_kind,
2406 &normalized_stdout,
2407 &proc_res.stdout,
2408 &expected_stdout,
2409 )
2410 .should_error()
2411 {
2412 errors += 1;
2413 }
2414
2415 if self
2416 .compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2417 .should_error()
2418 {
2419 errors += 1;
2420 }
2421 }
2422 }
2423 errors
2424 }
2425
2426 fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2427 let rflags = self.props.run_flags.join(" ");
2430 let cflags = self.props.compile_flags.join(" ");
2431 let json = rflags.contains("--format json")
2432 || rflags.contains("--format=json")
2433 || cflags.contains("--error-format json")
2434 || cflags.contains("--error-format pretty-json")
2435 || cflags.contains("--error-format=json")
2436 || cflags.contains("--error-format=pretty-json")
2437 || cflags.contains("--output-format json")
2438 || cflags.contains("--output-format=json");
2439
2440 let mut normalized = output.to_string();
2441
2442 let mut normalize_path = |from: &Utf8Path, to: &str| {
2443 let from = if json { &from.as_str().replace("\\", "\\\\") } else { from.as_str() };
2444
2445 normalized = normalized.replace(from, to);
2446 };
2447
2448 let parent_dir = self.testpaths.file.parent().unwrap();
2449 normalize_path(parent_dir, "$DIR");
2450
2451 if self.props.remap_src_base {
2452 let mut remapped_parent_dir = Utf8PathBuf::from(FAKE_SRC_BASE);
2453 if self.testpaths.relative_dir != Utf8Path::new("") {
2454 remapped_parent_dir.push(&self.testpaths.relative_dir);
2455 }
2456 normalize_path(&remapped_parent_dir, "$DIR");
2457 }
2458
2459 let base_dir = Utf8Path::new("/rustc/FAKE_PREFIX");
2460 normalize_path(&base_dir.join("library"), "$SRC_DIR");
2462 normalize_path(&base_dir.join("compiler"), "$COMPILER_DIR");
2466
2467 let rust_src_dir = &self.config.sysroot_base.join("lib/rustlib/src/rust");
2469 rust_src_dir.try_exists().expect(&*format!("{} should exists", rust_src_dir));
2470 let rust_src_dir =
2471 rust_src_dir.read_link_utf8().unwrap_or_else(|_| rust_src_dir.to_path_buf());
2472 normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
2473
2474 let rustc_src_dir = &self.config.sysroot_base.join("lib/rustlib/rustc-src/rust");
2476 rustc_src_dir.try_exists().expect(&*format!("{} should exists", rustc_src_dir));
2477 let rustc_src_dir = rustc_src_dir.read_link_utf8().unwrap_or(rustc_src_dir.to_path_buf());
2478 normalize_path(&rustc_src_dir.join("compiler"), "$COMPILER_DIR_REAL");
2479
2480 normalize_path(&self.output_base_dir(), "$TEST_BUILD_DIR");
2483 normalize_path(&self.output_base_dir().canonicalize_utf8().unwrap(), "$TEST_BUILD_DIR");
2490 normalize_path(&self.config.build_root, "$BUILD_DIR");
2492
2493 if json {
2494 normalized = normalized.replace("\\n", "\n");
2499 }
2500
2501 normalized = static_regex!("SRC_DIR(.+):\\d+:\\d+(: \\d+:\\d+)?")
2506 .replace_all(&normalized, "SRC_DIR$1:LL:COL")
2507 .into_owned();
2508
2509 normalized = Self::normalize_platform_differences(&normalized);
2510
2511 normalized =
2513 static_regex!(r"\$TEST_BUILD_DIR/(?P<filename>[^\.]+).long-type-(?P<hash>\d+).txt")
2514 .replace_all(&normalized, |caps: &Captures<'_>| {
2515 format!(
2516 "$TEST_BUILD_DIR/{filename}.long-type-$LONG_TYPE_HASH.txt",
2517 filename = &caps["filename"]
2518 )
2519 })
2520 .into_owned();
2521
2522 normalized = static_regex!(r"thread '(?P<name>.*?)' \((rtid )?\d+\) panicked")
2524 .replace_all(&normalized, "thread '$name' ($$TID) panicked")
2525 .into_owned();
2526
2527 normalized = normalized.replace("\t", "\\t"); normalized =
2534 static_regex!("\\s*//(\\[.*\\])?~.*").replace_all(&normalized, "").into_owned();
2535
2536 let v0_crate_hash_prefix_re = static_regex!(r"_R.*?Cs[0-9a-zA-Z]+_");
2539 let v0_crate_hash_re = static_regex!(r"Cs[0-9a-zA-Z]+_");
2540
2541 const V0_CRATE_HASH_PLACEHOLDER: &str = r"CsCRATE_HASH_";
2542 if v0_crate_hash_prefix_re.is_match(&normalized) {
2543 normalized =
2545 v0_crate_hash_re.replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER).into_owned();
2546 }
2547
2548 let v0_back_ref_prefix_re = static_regex!(r"\(_R.*?B[0-9a-zA-Z]_");
2549 let v0_back_ref_re = static_regex!(r"B[0-9a-zA-Z]_");
2550
2551 const V0_BACK_REF_PLACEHOLDER: &str = r"B<REF>_";
2552 if v0_back_ref_prefix_re.is_match(&normalized) {
2553 normalized =
2555 v0_back_ref_re.replace_all(&normalized, V0_BACK_REF_PLACEHOLDER).into_owned();
2556 }
2557
2558 {
2565 let mut seen_allocs = indexmap::IndexSet::new();
2566
2567 normalized = static_regex!(
2569 r"╾─*a(lloc)?([0-9]+)(\+0x[0-9a-f]+)?(<imm>)?( \([0-9]+ ptr bytes\))?─*╼"
2570 )
2571 .replace_all(&normalized, |caps: &Captures<'_>| {
2572 let index = caps.get(2).unwrap().as_str().to_string();
2574 let (index, _) = seen_allocs.insert_full(index);
2575 let offset = caps.get(3).map_or("", |c| c.as_str());
2576 let imm = caps.get(4).map_or("", |c| c.as_str());
2577 format!("╾ALLOC{index}{offset}{imm}╼")
2579 })
2580 .into_owned();
2581
2582 normalized = static_regex!(r"\balloc([0-9]+)\b")
2584 .replace_all(&normalized, |caps: &Captures<'_>| {
2585 let index = caps.get(1).unwrap().as_str().to_string();
2586 let (index, _) = seen_allocs.insert_full(index);
2587 format!("ALLOC{index}")
2588 })
2589 .into_owned();
2590 }
2591
2592 for rule in custom_rules {
2594 let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
2595 normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
2596 }
2597 normalized
2598 }
2599
2600 fn normalize_platform_differences(output: &str) -> String {
2606 let output = output.replace(r"\\", r"\");
2607
2608 let re = static_regex!(
2613 r#"(?x)
2614 (?:
2615 # Match paths that don't include spaces.
2616 (?:\\[\pL\pN\.\-_']+)+\.\pL+
2617 |
2618 # If the path starts with a well-known root, then allow spaces and no file extension.
2619 \$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_'\ ]+)+
2620 )"#
2621 );
2622 re.replace_all(&output, |caps: &Captures<'_>| caps[0].replace(r"\", "/"))
2623 .replace("\r\n", "\n")
2624 }
2625
2626 fn expected_output_path(&self, kind: &str) -> Utf8PathBuf {
2627 let mut path =
2628 expected_output_path(&self.testpaths, self.revision, &self.config.compare_mode, kind);
2629
2630 if !path.exists() {
2631 if let Some(CompareMode::Polonius) = self.config.compare_mode {
2632 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2633 }
2634 }
2635
2636 if !path.exists() {
2637 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2638 }
2639
2640 path
2641 }
2642
2643 fn load_expected_output(&self, kind: &str) -> String {
2644 let path = self.expected_output_path(kind);
2645 if path.exists() {
2646 match self.load_expected_output_from_path(&path) {
2647 Ok(x) => x,
2648 Err(x) => self.fatal(&x),
2649 }
2650 } else {
2651 String::new()
2652 }
2653 }
2654
2655 fn load_expected_output_from_path(&self, path: &Utf8Path) -> Result<String, String> {
2656 fs::read_to_string(path)
2657 .map_err(|err| format!("failed to load expected output from `{}`: {}", path, err))
2658 }
2659
2660 fn delete_file(&self, file: &Utf8Path) {
2662 if let Err(e) = fs::remove_file(file.as_std_path())
2663 && e.kind() != io::ErrorKind::NotFound
2664 {
2665 self.fatal(&format!("failed to delete `{}`: {}", file, e,));
2666 }
2667 }
2668
2669 fn compare_output(
2670 &self,
2671 stream: &str,
2672 actual: &str,
2673 actual_unnormalized: &str,
2674 expected: &str,
2675 ) -> CompareOutcome {
2676 let expected_path =
2677 expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2678
2679 if self.config.bless && actual.is_empty() && expected_path.exists() {
2680 self.delete_file(&expected_path);
2681 }
2682
2683 let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
2684 (true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
2687 _ => expected != actual,
2688 };
2689 if !are_different {
2690 return CompareOutcome::Same;
2691 }
2692
2693 let compare_output_by_lines_subset = self.config.runner.is_some();
2697
2698 let compare_output_by_lines = self.props.compare_output_by_lines;
2701
2702 let tmp;
2703 let (expected, actual): (&str, &str) = if compare_output_by_lines_subset {
2704 let actual_lines: HashSet<_> = actual.lines().collect();
2705 let expected_lines: Vec<_> = expected.lines().collect();
2706 let mut used = expected_lines.clone();
2707 used.retain(|line| actual_lines.contains(line));
2708
2709 if used.len() == expected_lines.len() && (expected.is_empty() == actual.is_empty()) {
2711 return CompareOutcome::Same;
2712 }
2713 if expected_lines.is_empty() {
2714 ("", actual)
2716 } else {
2717 tmp = (expected_lines.join("\n"), used.join("\n"));
2719 (&tmp.0, &tmp.1)
2720 }
2721 } else if compare_output_by_lines {
2722 let mut actual_lines: Vec<&str> = actual.lines().filter(|l| l.trim() != "|").collect();
2724 let mut expected_lines: Vec<&str> =
2725 expected.lines().filter(|l| l.trim() != "|").collect();
2726 actual_lines.sort_unstable();
2727 expected_lines.sort_unstable();
2728 if actual_lines == expected_lines {
2729 return CompareOutcome::Same;
2730 } else {
2731 (expected, actual)
2732 }
2733 } else {
2734 (expected, actual)
2735 };
2736
2737 let actual_path = self
2739 .output_base_name()
2740 .with_extra_extension(self.revision.unwrap_or(""))
2741 .with_extra_extension(
2742 self.config.compare_mode.as_ref().map(|cm| cm.to_str()).unwrap_or(""),
2743 )
2744 .with_extra_extension(stream);
2745
2746 if let Err(err) = fs::write(&actual_path, &actual) {
2747 self.fatal(&format!("failed to write {stream} to `{actual_path}`: {err}",));
2748 }
2749 writeln!(self.stdout, "Saved the actual {stream} to `{actual_path}`");
2750
2751 if !self.config.bless {
2752 if expected.is_empty() {
2753 writeln!(self.stdout, "normalized {}:\n{}\n", stream, actual);
2754 } else {
2755 self.show_diff(
2756 stream,
2757 &expected_path,
2758 &actual_path,
2759 expected,
2760 actual,
2761 actual_unnormalized,
2762 compare_output_by_lines || compare_output_by_lines_subset,
2763 );
2764 }
2765 } else {
2766 if self.revision.is_some() {
2769 let old =
2770 expected_output_path(self.testpaths, None, &self.config.compare_mode, stream);
2771 self.delete_file(&old);
2772 }
2773
2774 if !actual.is_empty() {
2775 if let Err(err) = fs::write(&expected_path, &actual) {
2776 self.fatal(&format!("failed to write {stream} to `{expected_path}`: {err}"));
2777 }
2778 writeln!(
2779 self.stdout,
2780 "Blessing the {stream} of `{test_name}` as `{expected_path}`",
2781 test_name = self.testpaths.file
2782 );
2783 }
2784 }
2785
2786 writeln!(self.stdout, "\nThe actual {stream} differed from the expected {stream}");
2787
2788 if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
2789 }
2790
2791 fn show_diff(
2793 &self,
2794 stream: &str,
2795 expected_path: &Utf8Path,
2796 actual_path: &Utf8Path,
2797 expected: &str,
2798 actual: &str,
2799 actual_unnormalized: &str,
2800 show_diff_by_lines: bool,
2801 ) {
2802 writeln!(self.stderr, "diff of {stream}:\n");
2803 if let Some(diff_command) = self.config.diff_command.as_deref() {
2804 let mut args = diff_command.split_whitespace();
2805 let name = args.next().unwrap();
2806 match Command::new(name).args(args).args([expected_path, actual_path]).output() {
2807 Err(err) => {
2808 self.fatal(&format!(
2809 "failed to call custom diff command `{diff_command}`: {err}"
2810 ));
2811 }
2812 Ok(output) => {
2813 let output = String::from_utf8_lossy(&output.stdout);
2814 write!(self.stderr, "{output}");
2815 }
2816 }
2817 } else {
2818 write!(self.stderr, "{}", write_diff(expected, actual, 3));
2819 }
2820
2821 let diff_results = make_diff(actual, expected, 0);
2823
2824 let (mut mismatches_normalized, mut mismatch_line_nos) = (String::new(), vec![]);
2825 for hunk in diff_results {
2826 let mut line_no = hunk.line_number;
2827 for line in hunk.lines {
2828 if let DiffLine::Expected(normalized) = line {
2830 mismatches_normalized += &normalized;
2831 mismatches_normalized += "\n";
2832 mismatch_line_nos.push(line_no);
2833 line_no += 1;
2834 }
2835 }
2836 }
2837 let mut mismatches_unnormalized = String::new();
2838 let diff_normalized = make_diff(actual, actual_unnormalized, 0);
2839 for hunk in diff_normalized {
2840 if mismatch_line_nos.contains(&hunk.line_number) {
2841 for line in hunk.lines {
2842 if let DiffLine::Resulting(unnormalized) = line {
2843 mismatches_unnormalized += &unnormalized;
2844 mismatches_unnormalized += "\n";
2845 }
2846 }
2847 }
2848 }
2849
2850 let normalized_diff = make_diff(&mismatches_normalized, &mismatches_unnormalized, 0);
2851 if !normalized_diff.is_empty()
2853 && !mismatches_unnormalized.is_empty()
2854 && !mismatches_normalized.is_empty()
2855 {
2856 writeln!(
2857 self.stderr,
2858 "Note: some mismatched output was normalized before being compared"
2859 );
2860 write!(
2862 self.stderr,
2863 "{}",
2864 write_diff(&mismatches_unnormalized, &mismatches_normalized, 0)
2865 );
2866 }
2867
2868 if show_diff_by_lines {
2869 write!(self.stderr, "{}", diff_by_lines(expected, actual));
2870 }
2871 }
2872
2873 fn check_and_prune_duplicate_outputs(
2874 &self,
2875 proc_res: &ProcRes,
2876 modes: &[CompareMode],
2877 require_same_modes: &[CompareMode],
2878 ) {
2879 for kind in UI_EXTENSIONS {
2880 let canon_comparison_path =
2881 expected_output_path(&self.testpaths, self.revision, &None, kind);
2882
2883 let canon = match self.load_expected_output_from_path(&canon_comparison_path) {
2884 Ok(canon) => canon,
2885 _ => continue,
2886 };
2887 let bless = self.config.bless;
2888 let check_and_prune_duplicate_outputs = |mode: &CompareMode, require_same: bool| {
2889 let examined_path =
2890 expected_output_path(&self.testpaths, self.revision, &Some(mode.clone()), kind);
2891
2892 let examined_content = match self.load_expected_output_from_path(&examined_path) {
2894 Ok(content) => content,
2895 _ => return,
2896 };
2897
2898 let is_duplicate = canon == examined_content;
2899
2900 match (bless, require_same, is_duplicate) {
2901 (true, _, true) => {
2903 self.delete_file(&examined_path);
2904 }
2905 (_, true, false) => {
2908 self.fatal_proc_rec(
2909 &format!("`{}` should not have different output from base test!", kind),
2910 proc_res,
2911 );
2912 }
2913 _ => {}
2914 }
2915 };
2916 for mode in modes {
2917 check_and_prune_duplicate_outputs(mode, false);
2918 }
2919 for mode in require_same_modes {
2920 check_and_prune_duplicate_outputs(mode, true);
2921 }
2922 }
2923 }
2924
2925 fn create_stamp(&self) {
2926 let stamp_file_path = stamp_file_path(&self.config, self.testpaths, self.revision);
2927 fs::write(&stamp_file_path, compute_stamp_hash(&self.config)).unwrap();
2928 }
2929
2930 fn init_incremental_test(&self) {
2931 let incremental_dir = self.props.incremental_dir.as_ref().unwrap();
2938 if incremental_dir.exists() {
2939 let canonicalized = incremental_dir.canonicalize().unwrap();
2942 fs::remove_dir_all(canonicalized).unwrap();
2943 }
2944 fs::create_dir_all(&incremental_dir).unwrap();
2945
2946 if self.config.verbose {
2947 writeln!(self.stdout, "init_incremental_test: incremental_dir={incremental_dir}");
2948 }
2949 }
2950}
2951
2952struct ProcArgs {
2953 prog: OsString,
2954 args: Vec<OsString>,
2955}
2956
2957#[derive(Debug)]
2958pub(crate) struct ProcRes {
2959 status: ExitStatus,
2960 stdout: String,
2961 stderr: String,
2962 truncated: Truncated,
2963 cmdline: String,
2964}
2965
2966impl ProcRes {
2967 #[must_use]
2968 pub(crate) fn format_info(&self) -> String {
2969 fn render(name: &str, contents: &str) -> String {
2970 let contents = json::extract_rendered(contents);
2971 let contents = contents.trim_end();
2972 if contents.is_empty() {
2973 format!("{name}: none")
2974 } else {
2975 format!(
2976 "\
2977 --- {name} -------------------------------\n\
2978 {contents}\n\
2979 ------------------------------------------",
2980 )
2981 }
2982 }
2983
2984 format!(
2985 "status: {}\ncommand: {}\n{}\n{}\n",
2986 self.status,
2987 self.cmdline,
2988 render("stdout", &self.stdout),
2989 render("stderr", &self.stderr),
2990 )
2991 }
2992}
2993
2994#[derive(Debug)]
2995enum TargetLocation {
2996 ThisFile(Utf8PathBuf),
2997 ThisDirectory(Utf8PathBuf),
2998}
2999
3000enum AllowUnused {
3001 Yes,
3002 No,
3003}
3004
3005enum LinkToAux {
3006 Yes,
3007 No,
3008}
3009
3010#[derive(Debug, PartialEq)]
3011enum AuxType {
3012 Bin,
3013 Lib,
3014 Dylib,
3015 ProcMacro,
3016}
3017
3018#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3021enum CompareOutcome {
3022 Same,
3024 Blessed,
3026 Differed,
3028}
3029
3030#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3031enum DocKind {
3032 Html,
3033 Json,
3034}
3035
3036impl CompareOutcome {
3037 fn should_error(&self) -> bool {
3038 matches!(self, CompareOutcome::Differed)
3039 }
3040}