1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use common::CompareMode;
12 use common::{expected_output_path, UI_EXTENSIONS, UI_FIXED, UI_STDERR, UI_STDOUT};
13 use common::{output_base_dir, output_base_name, output_testname_unique};
14 use common::{Codegen, CodegenUnits, DebugInfoGdb, DebugInfoLldb, Rustdoc};
15 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
16 use common::{Config, TestPaths};
17 use common::{Incremental, MirOpt, RunMake, Ui};
19 use errors::{self, Error, ErrorKind};
20 use filetime::FileTime;
21 use header::TestProps;
24 use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
25 use util::{logv, PathBufExt};
27 use std::collections::hash_map::DefaultHasher;
28 use std::collections::{HashMap, HashSet, VecDeque};
30 use std::ffi::OsString;
32 use std::fs::{self, create_dir_all, File};
33 use std::hash::{Hash, Hasher};
34 use std::io::prelude::*;
35 use std::io::{self, BufReader};
36 use std::path::{Path, PathBuf};
37 use std::process::{Child, Command, ExitStatus, Output, Stdio};
40 use extract_gdb_version;
43 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
45 const SEM_NOGPFAULTERRORBOX: u32 = 0x0002;
47 fn SetErrorMode(mode: u32) -> u32;
51 static ref LOCK: Mutex<()> = { Mutex::new(()) };
53 // Error mode is a global variable, so lock it so only one thread will change it
54 let _lock = LOCK.lock().unwrap();
56 // Tell Windows to not show any UI on errors (such as terminating abnormally).
57 // This is important for running tests, since some of them use abnormal
58 // termination by design. This mode is inherited by all child processes.
60 let old_mode = SetErrorMode(SEM_NOGPFAULTERRORBOX); // read inherited flags
61 SetErrorMode(old_mode | SEM_NOGPFAULTERRORBOX);
63 SetErrorMode(old_mode);
69 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
73 /// The name of the environment variable that holds dynamic library locations.
74 pub fn dylib_env_var() -> &'static str {
77 } else if cfg!(target_os = "macos") {
79 } else if cfg!(target_os = "haiku") {
86 #[derive(Debug, PartialEq)]
93 #[derive(Debug, PartialEq)]
96 pub lines: Vec<DiffLine>,
100 fn new(line_number: u32) -> Mismatch {
102 line_number: line_number,
108 // Produces a diff between the expected output and actual output.
109 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
110 let mut line_number = 1;
111 let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
112 let mut lines_since_mismatch = context_size + 1;
113 let mut results = Vec::new();
114 let mut mismatch = Mismatch::new(0);
116 for result in diff::lines(expected, actual) {
118 diff::Result::Left(str) => {
119 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
120 results.push(mismatch);
121 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
124 while let Some(line) = context_queue.pop_front() {
125 mismatch.lines.push(DiffLine::Context(line.to_owned()));
128 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
130 lines_since_mismatch = 0;
132 diff::Result::Right(str) => {
133 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
134 results.push(mismatch);
135 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
138 while let Some(line) = context_queue.pop_front() {
139 mismatch.lines.push(DiffLine::Context(line.to_owned()));
142 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
143 lines_since_mismatch = 0;
145 diff::Result::Both(str, _) => {
146 if context_queue.len() >= context_size {
147 let _ = context_queue.pop_front();
150 if lines_since_mismatch < context_size {
151 mismatch.lines.push(DiffLine::Context(str.to_owned()));
152 } else if context_size > 0 {
153 context_queue.push_back(str);
157 lines_since_mismatch += 1;
162 results.push(mismatch);
168 pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
169 match &*config.target {
170 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
171 if !config.adb_device_status {
172 panic!("android device not available");
177 // android has its own gdb handling
178 if config.mode == DebugInfoGdb && config.gdb.is_none() {
179 panic!("gdb not available but debuginfo gdb debuginfo test requested");
185 // We're going to be dumping a lot of info. Start on a new line.
188 debug!("running {:?}", testpaths.file.display());
189 let props = TestProps::from_file(&testpaths.file, revision, &config);
197 create_dir_all(&cx.output_base_dir()).unwrap();
199 if config.mode == Incremental {
200 // Incremental tests are special because they cannot be run in
203 !props.revisions.is_empty(),
204 "Incremental tests require revisions."
206 cx.init_incremental_test();
207 for revision in &props.revisions {
208 let revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
209 let rev_cx = TestCx {
211 props: &revision_props,
213 revision: Some(revision),
215 rev_cx.run_revision();
224 pub fn compute_stamp_hash(config: &Config) -> String {
225 let mut hash = DefaultHasher::new();
226 config.stage_id.hash(&mut hash);
227 format!("{:x}", hash.finish())
230 struct TestCx<'test> {
231 config: &'test Config,
232 props: &'test TestProps,
233 testpaths: &'test TestPaths,
234 revision: Option<&'test str>,
237 struct DebuggerCommands {
238 commands: Vec<String>,
239 check_lines: Vec<String>,
240 breakpoint_lines: Vec<usize>,
243 impl<'test> TestCx<'test> {
244 /// Code executed for each revision in turn (or, if there are no
245 /// revisions, exactly once, with revision == None).
246 fn run_revision(&self) {
247 match self.config.mode {
248 CompileFail | ParseFail => self.run_cfail_test(),
249 RunFail => self.run_rfail_test(),
250 RunPassValgrind => self.run_valgrind_test(),
251 Pretty => self.run_pretty_test(),
252 DebugInfoGdb => self.run_debuginfo_gdb_test(),
253 DebugInfoLldb => self.run_debuginfo_lldb_test(),
254 Codegen => self.run_codegen_test(),
255 Rustdoc => self.run_rustdoc_test(),
256 CodegenUnits => self.run_codegen_units_test(),
257 Incremental => self.run_incremental_test(),
258 RunMake => self.run_rmake_test(),
259 RunPass | Ui => self.run_ui_test(),
260 MirOpt => self.run_mir_opt_test(),
264 fn should_run_successfully(&self) -> bool {
265 match self.config.mode {
266 RunPass => !self.props.skip_codegen,
267 Ui => self.props.run_pass,
268 _ => unimplemented!(),
272 fn should_compile_successfully(&self) -> bool {
273 match self.config.mode {
274 ParseFail | CompileFail => self.props.compile_pass,
276 Ui => self.props.compile_pass,
278 let revision = self.revision
279 .expect("incremental tests require a list of revisions");
280 if revision.starts_with("rpass") || revision.starts_with("rfail") {
282 } else if revision.starts_with("cfail") {
283 // FIXME: would be nice if incremental revs could start with "cpass"
284 self.props.compile_pass
286 panic!("revision name must begin with rpass, rfail, or cfail");
289 mode => panic!("unimplemented for mode {:?}", mode),
293 fn check_if_test_should_compile(&self, proc_res: &ProcRes) {
294 if self.should_compile_successfully() {
295 if !proc_res.status.success() {
296 self.fatal_proc_rec("test compilation failed although it shouldn't!", proc_res);
299 if proc_res.status.success() {
301 &format!("{} test compiled successfully!", self.config.mode)[..],
306 self.check_correct_failure_status(proc_res);
310 fn run_cfail_test(&self) {
311 let proc_res = self.compile_test();
312 self.check_if_test_should_compile(&proc_res);
313 self.check_no_compiler_crash(&proc_res);
315 let output_to_check = self.get_output(&proc_res);
316 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
317 if !expected_errors.is_empty() {
318 if !self.props.error_patterns.is_empty() {
319 self.fatal("both error pattern and expected errors specified");
321 self.check_expected_errors(expected_errors, &proc_res);
323 self.check_error_patterns(&output_to_check, &proc_res);
326 self.check_forbid_output(&output_to_check, &proc_res);
329 fn run_rfail_test(&self) {
330 let proc_res = self.compile_test();
332 if !proc_res.status.success() {
333 self.fatal_proc_rec("compilation failed!", &proc_res);
336 let proc_res = self.exec_compiled_test();
338 // The value our Makefile configures valgrind to return on failure
339 const VALGRIND_ERR: i32 = 100;
340 if proc_res.status.code() == Some(VALGRIND_ERR) {
341 self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
344 let output_to_check = self.get_output(&proc_res);
345 self.check_correct_failure_status(&proc_res);
346 self.check_error_patterns(&output_to_check, &proc_res);
349 fn get_output(&self, proc_res: &ProcRes) -> String {
350 if self.props.check_stdout {
351 format!("{}{}", proc_res.stdout, proc_res.stderr)
353 proc_res.stderr.clone()
357 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
358 let expected_status = Some(self.props.failure_status);
359 let received_status = proc_res.status.code();
361 if expected_status != received_status {
364 "Error: expected failure status ({:?}) but received status {:?}.",
365 expected_status, received_status
372 fn run_rpass_test(&self) {
373 let proc_res = self.compile_test();
375 if !proc_res.status.success() {
376 self.fatal_proc_rec("compilation failed!", &proc_res);
379 // FIXME(#41968): Move this check to tidy?
380 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
382 expected_errors.is_empty(),
383 "run-pass tests with expected warnings should be moved to ui/"
386 if !self.props.skip_codegen {
387 let proc_res = self.exec_compiled_test();
388 if !proc_res.status.success() {
389 self.fatal_proc_rec("test run failed!", &proc_res);
394 fn run_valgrind_test(&self) {
395 assert!(self.revision.is_none(), "revisions not relevant here");
397 if self.config.valgrind_path.is_none() {
398 assert!(!self.config.force_valgrind);
399 return self.run_rpass_test();
402 let mut proc_res = self.compile_test();
404 if !proc_res.status.success() {
405 self.fatal_proc_rec("compilation failed!", &proc_res);
408 let mut new_config = self.config.clone();
409 new_config.runtool = new_config.valgrind_path.clone();
410 let new_cx = TestCx {
414 proc_res = new_cx.exec_compiled_test();
416 if !proc_res.status.success() {
417 self.fatal_proc_rec("test run failed!", &proc_res);
421 fn run_pretty_test(&self) {
422 if self.props.pp_exact.is_some() {
423 logv(self.config, "testing for exact pretty-printing".to_owned());
427 "testing for converging pretty-printing".to_owned(),
431 let rounds = match self.props.pp_exact {
436 let mut src = String::new();
437 File::open(&self.testpaths.file)
439 .read_to_string(&mut src)
441 let mut srcs = vec![src];
444 while round < rounds {
448 "pretty-printing round {} revision {:?}",
452 let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
454 if !proc_res.status.success() {
457 "pretty-printing failed in round {} revision {:?}",
464 let ProcRes { stdout, .. } = proc_res;
469 let mut expected = match self.props.pp_exact {
471 let filepath = self.testpaths.file.parent().unwrap().join(file);
472 let mut s = String::new();
473 File::open(&filepath)
475 .read_to_string(&mut s)
479 None => srcs[srcs.len() - 2].clone(),
481 let mut actual = srcs[srcs.len() - 1].clone();
483 if self.props.pp_exact.is_some() {
484 // Now we have to care about line endings
485 let cr = "\r".to_owned();
486 actual = actual.replace(&cr, "").to_owned();
487 expected = expected.replace(&cr, "").to_owned();
490 self.compare_source(&expected, &actual);
492 // If we're only making sure that the output matches then just stop here
493 if self.props.pretty_compare_only {
497 // Finally, let's make sure it actually appears to remain valid code
498 let proc_res = self.typecheck_source(actual);
499 if !proc_res.status.success() {
500 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
503 if !self.props.pretty_expanded {
507 // additionally, run `--pretty expanded` and try to build it.
508 let proc_res = self.print_source(srcs[round].clone(), "expanded");
509 if !proc_res.status.success() {
510 self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
514 stdout: expanded_src,
517 let proc_res = self.typecheck_source(expanded_src);
518 if !proc_res.status.success() {
520 "pretty-printed source (expanded) does not typecheck",
526 fn print_source(&self, src: String, pretty_type: &str) -> ProcRes {
527 let aux_dir = self.aux_output_dir_name();
529 let mut rustc = Command::new(&self.config.rustc_path);
532 .args(&["-Z", &format!("unpretty={}", pretty_type)])
533 .args(&["--target", &self.config.target])
536 .args(self.split_maybe_args(&self.config.target_rustcflags))
537 .args(&self.props.compile_flags)
538 .envs(self.props.exec_env.clone());
540 self.compose_and_run(
542 self.config.compile_lib_path.to_str().unwrap(),
543 Some(aux_dir.to_str().unwrap()),
548 fn compare_source(&self, expected: &str, actual: &str) {
549 if expected != actual {
551 "pretty-printed source does not match expected source\n\
553 ------------------------------------------\n\
555 ------------------------------------------\n\
557 ------------------------------------------\n\
559 ------------------------------------------\n\
566 fn typecheck_source(&self, src: String) -> ProcRes {
567 let mut rustc = Command::new(&self.config.rustc_path);
569 let out_dir = self.output_base_name().with_extension("pretty-out");
570 let _ = fs::remove_dir_all(&out_dir);
571 create_dir_all(&out_dir).unwrap();
573 let target = if self.props.force_host {
579 let aux_dir = self.aux_output_dir_name();
586 .arg(&format!("--target={}", target))
588 .arg(&self.config.build_base)
592 if let Some(revision) = self.revision {
593 rustc.args(&["--cfg", revision]);
596 rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
597 rustc.args(&self.props.compile_flags);
599 self.compose_and_run_compiler(rustc, Some(src))
602 fn run_debuginfo_gdb_test(&self) {
603 assert!(self.revision.is_none(), "revisions not relevant here");
605 let config = Config {
606 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
607 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
608 ..self.config.clone()
611 let test_cx = TestCx {
616 test_cx.run_debuginfo_gdb_test_no_opt();
619 fn run_debuginfo_gdb_test_no_opt(&self) {
620 let prefixes = if self.config.gdb_native_rust {
622 static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
623 println!("NOTE: compiletest thinks it is using GDB with native rust support");
627 static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
628 println!("NOTE: compiletest thinks it is using GDB without native rust support");
632 let DebuggerCommands {
636 } = self.parse_debugger_commands(prefixes);
637 let mut cmds = commands.join("\n");
639 // compile test file (it should have 'compile-flags:-g' in the header)
640 let compiler_run_result = self.compile_test();
641 if !compiler_run_result.status.success() {
642 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
645 let exe_file = self.make_exe_name();
647 let debugger_run_result;
648 match &*self.config.target {
649 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
650 cmds = cmds.replace("run", "continue");
652 let tool_path = match self.config.android_cross_path.to_str() {
653 Some(x) => x.to_owned(),
654 None => self.fatal("cannot find android cross path"),
657 // write debugger script
658 let mut script_str = String::with_capacity(2048);
659 script_str.push_str(&format!("set charset {}\n", Self::charset()));
660 script_str.push_str(&format!("set sysroot {}\n", tool_path));
661 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
662 script_str.push_str("target remote :5039\n");
663 script_str.push_str(&format!(
664 "set solib-search-path \
665 ./{}/stage2/lib/rustlib/{}/lib/\n",
666 self.config.host, self.config.target
668 for line in &breakpoint_lines {
672 self.testpaths.file.file_name().unwrap().to_string_lossy(),
677 script_str.push_str(&cmds);
678 script_str.push_str("\nquit\n");
680 debug!("script_str = {}", script_str);
681 self.dump_output_file(&script_str, "debugger.script");
683 let adb_path = &self.config.adb_path;
685 Command::new(adb_path)
688 .arg(&self.config.adb_test_dir)
690 .expect(&format!("failed to exec `{:?}`", adb_path));
692 Command::new(adb_path)
693 .args(&["forward", "tcp:5039", "tcp:5039"])
695 .expect(&format!("failed to exec `{:?}`", adb_path));
697 let adb_arg = format!(
698 "export LD_LIBRARY_PATH={}; \
699 gdbserver{} :5039 {}/{}",
700 self.config.adb_test_dir.clone(),
701 if self.config.target.contains("aarch64") {
706 self.config.adb_test_dir.clone(),
707 exe_file.file_name().unwrap().to_str().unwrap()
710 debug!("adb arg: {}", adb_arg);
711 let mut adb = Command::new(adb_path)
712 .args(&["shell", &adb_arg])
713 .stdout(Stdio::piped())
714 .stderr(Stdio::inherit())
716 .expect(&format!("failed to exec `{:?}`", adb_path));
718 // Wait for the gdbserver to print out "Listening on port ..."
719 // at which point we know that it's started and then we can
720 // execute the debugger below.
721 let mut stdout = BufReader::new(adb.stdout.take().unwrap());
722 let mut line = String::new();
725 stdout.read_line(&mut line).unwrap();
726 if line.starts_with("Listening on port 5039") {
732 let debugger_script = self.make_out_name("debugger.script");
733 // FIXME (#9639): This needs to handle non-utf8 paths
734 let debugger_opts = vec![
738 format!("-command={}", debugger_script.to_str().unwrap()),
741 let mut gdb_path = tool_path;
742 gdb_path.push_str("/bin/gdb");
747 } = Command::new(&gdb_path)
748 .args(&debugger_opts)
750 .expect(&format!("failed to exec `{:?}`", gdb_path));
752 let mut gdb = Command::new(&format!("{}-gdb", self.config.target));
753 gdb.args(&debugger_opts);
754 let cmdline = self.make_cmdline(&gdb, "");
755 logv(self.config, format!("executing {}", cmdline));
759 debugger_run_result = ProcRes {
761 stdout: String::from_utf8(stdout).unwrap(),
762 stderr: String::from_utf8(stderr).unwrap(),
765 if adb.kill().is_err() {
766 println!("Adb process is already finished.");
771 let rust_src_root = self
773 .find_rust_src_root()
774 .expect("Could not find Rust source root");
775 let rust_pp_module_rel_path = Path::new("./src/etc");
776 let rust_pp_module_abs_path = rust_src_root
777 .join(rust_pp_module_rel_path)
781 // write debugger script
782 let mut script_str = String::with_capacity(2048);
783 script_str.push_str(&format!("set charset {}\n", Self::charset()));
784 script_str.push_str("show version\n");
786 match self.config.gdb_version {
789 "NOTE: compiletest thinks it is using GDB version {}",
793 if version > extract_gdb_version("7.4").unwrap() {
794 // Add the directory containing the pretty printers to
795 // GDB's script auto loading safe path
796 script_str.push_str(&format!(
797 "add-auto-load-safe-path {}\n",
798 rust_pp_module_abs_path.replace(r"\", r"\\")
804 "NOTE: compiletest does not know which version of \
810 // The following line actually doesn't have to do anything with
811 // pretty printing, it just tells GDB to print values on one line:
812 script_str.push_str("set print pretty off\n");
814 // Add the pretty printer directory to GDB's source-file search path
815 script_str.push_str(&format!("directory {}\n", rust_pp_module_abs_path));
817 // Load the target executable
818 script_str.push_str(&format!(
820 exe_file.to_str().unwrap().replace(r"\", r"\\")
823 // Force GDB to print values in the Rust format.
824 if self.config.gdb_native_rust {
825 script_str.push_str("set language rust\n");
828 // Add line breakpoints
829 for line in &breakpoint_lines {
830 script_str.push_str(&format!(
832 self.testpaths.file.file_name().unwrap().to_string_lossy(),
837 script_str.push_str(&cmds);
838 script_str.push_str("\nquit\n");
840 debug!("script_str = {}", script_str);
841 self.dump_output_file(&script_str, "debugger.script");
843 let debugger_script = self.make_out_name("debugger.script");
845 // FIXME (#9639): This needs to handle non-utf8 paths
846 let debugger_opts = vec![
850 format!("-command={}", debugger_script.to_str().unwrap()),
853 let mut gdb = Command::new(self.config.gdb.as_ref().unwrap());
854 gdb.args(&debugger_opts)
855 .env("PYTHONPATH", rust_pp_module_abs_path);
857 debugger_run_result = self.compose_and_run(
859 self.config.run_lib_path.to_str().unwrap(),
866 if !debugger_run_result.status.success() {
867 self.fatal_proc_rec("gdb failed to execute", &debugger_run_result);
870 self.check_debugger_output(&debugger_run_result, &check_lines);
873 fn run_debuginfo_lldb_test(&self) {
874 assert!(self.revision.is_none(), "revisions not relevant here");
876 if self.config.lldb_python_dir.is_none() {
877 self.fatal("Can't run LLDB test because LLDB's python path is not set.");
880 let config = Config {
881 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
882 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
883 ..self.config.clone()
886 let test_cx = TestCx {
891 test_cx.run_debuginfo_lldb_test_no_opt();
894 fn run_debuginfo_lldb_test_no_opt(&self) {
895 // compile test file (it should have 'compile-flags:-g' in the header)
896 let compile_result = self.compile_test();
897 if !compile_result.status.success() {
898 self.fatal_proc_rec("compilation failed!", &compile_result);
901 let exe_file = self.make_exe_name();
903 match self.config.lldb_version {
904 Some(ref version) => {
906 "NOTE: compiletest thinks it is using LLDB version {}",
912 "NOTE: compiletest does not know which version of \
918 // Parse debugger commands etc from test files
919 let DebuggerCommands {
924 } = self.parse_debugger_commands(&["lldb"]);
926 // Write debugger script:
927 // We don't want to hang when calling `quit` while the process is still running
928 let mut script_str = String::from("settings set auto-confirm true\n");
930 // Make LLDB emit its version, so we have it documented in the test output
931 script_str.push_str("version\n");
933 // Switch LLDB into "Rust mode"
934 let rust_src_root = self
936 .find_rust_src_root()
937 .expect("Could not find Rust source root");
938 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
939 let rust_pp_module_abs_path = rust_src_root
940 .join(rust_pp_module_rel_path)
946 .push_str(&format!("command script import {}\n", &rust_pp_module_abs_path[..])[..]);
947 script_str.push_str("type summary add --no-value ");
948 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
949 script_str.push_str("-x \".*\" --category Rust\n");
950 script_str.push_str("type category enable Rust\n");
952 // Set breakpoints on every line that contains the string "#break"
953 let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
954 for line in &breakpoint_lines {
955 script_str.push_str(&format!(
956 "breakpoint set --file '{}' --line {}\n",
957 source_file_name, line
961 // Append the other commands
962 for line in &commands {
963 script_str.push_str(line);
964 script_str.push_str("\n");
967 // Finally, quit the debugger
968 script_str.push_str("\nquit\n");
970 // Write the script into a file
971 debug!("script_str = {}", script_str);
972 self.dump_output_file(&script_str, "debugger.script");
973 let debugger_script = self.make_out_name("debugger.script");
975 // Let LLDB execute the script via lldb_batchmode.py
976 let debugger_run_result = self.run_lldb(&exe_file, &debugger_script, &rust_src_root);
978 if !debugger_run_result.status.success() {
979 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
982 self.check_debugger_output(&debugger_run_result, &check_lines);
987 test_executable: &Path,
988 debugger_script: &Path,
989 rust_src_root: &Path,
991 // Prepare the lldb_batchmode which executes the debugger script
992 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
994 Command::new(&self.config.lldb_python)
995 .arg(&lldb_script_path)
996 .arg(test_executable)
997 .arg(debugger_script)
998 .env("PYTHONPATH", self.config.lldb_python_dir.as_ref().unwrap()),
1002 fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
1003 let (status, out, err) = match cmd.output() {
1010 String::from_utf8(stdout).unwrap(),
1011 String::from_utf8(stderr).unwrap(),
1013 Err(e) => self.fatal(&format!(
1014 "Failed to setup Python process for \
1020 self.dump_output(&out, &err);
1025 cmdline: format!("{:?}", cmd),
1029 fn parse_debugger_commands(&self, debugger_prefixes: &[&str]) -> DebuggerCommands {
1030 let directives = debugger_prefixes
1032 .map(|prefix| (format!("{}-command", prefix), format!("{}-check", prefix)))
1033 .collect::<Vec<_>>();
1035 let mut breakpoint_lines = vec![];
1036 let mut commands = vec![];
1037 let mut check_lines = vec![];
1038 let mut counter = 1;
1039 let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
1040 for line in reader.lines() {
1043 let line = if line.starts_with("//") {
1044 line[2..].trim_left()
1049 if line.contains("#break") {
1050 breakpoint_lines.push(counter);
1053 for &(ref command_directive, ref check_directive) in &directives {
1055 .parse_name_value_directive(&line, command_directive)
1056 .map(|cmd| commands.push(cmd));
1059 .parse_name_value_directive(&line, check_directive)
1060 .map(|cmd| check_lines.push(cmd));
1063 Err(e) => self.fatal(&format!("Error while parsing debugger commands: {}", e)),
1075 fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
1076 if options.is_none() {
1080 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
1081 let options_to_remove = ["-O".to_owned(), "-g".to_owned(), "--debuginfo".to_owned()];
1082 let new_options = self
1083 .split_maybe_args(options)
1085 .filter(|x| !options_to_remove.contains(x))
1086 .collect::<Vec<String>>();
1088 Some(new_options.join(" "))
1091 fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
1092 let num_check_lines = check_lines.len();
1094 let mut check_line_index = 0;
1095 for line in debugger_run_result.stdout.lines() {
1096 if check_line_index >= num_check_lines {
1100 if check_single_line(line, &(check_lines[check_line_index])[..]) {
1101 check_line_index += 1;
1104 if check_line_index != num_check_lines && num_check_lines > 0 {
1105 self.fatal_proc_rec(
1107 "line not found in debugger output: {}",
1108 check_lines[check_line_index]
1110 debugger_run_result,
1114 fn check_single_line(line: &str, check_line: &str) -> bool {
1115 // Allow check lines to leave parts unspecified (e.g., uninitialized
1116 // bits in the wrong case of an enum) with the notation "[...]".
1117 let line = line.trim();
1118 let check_line = check_line.trim();
1119 let can_start_anywhere = check_line.starts_with("[...]");
1120 let can_end_anywhere = check_line.ends_with("[...]");
1122 let check_fragments: Vec<&str> = check_line
1124 .filter(|frag| !frag.is_empty())
1126 if check_fragments.is_empty() {
1130 let (mut rest, first_fragment) = if can_start_anywhere {
1131 match line.find(check_fragments[0]) {
1132 Some(pos) => (&line[pos + check_fragments[0].len()..], 1),
1133 None => return false,
1139 for current_fragment in &check_fragments[first_fragment..] {
1140 match rest.find(current_fragment) {
1142 rest = &rest[pos + current_fragment.len()..];
1144 None => return false,
1148 if !can_end_anywhere && !rest.is_empty() {
1156 fn check_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
1157 debug!("check_error_patterns");
1158 if self.props.error_patterns.is_empty() {
1159 if self.props.compile_pass {
1162 self.fatal(&format!(
1163 "no error pattern specified in {:?}",
1164 self.testpaths.file.display()
1169 let mut missing_patterns: Vec<String> = Vec::new();
1171 for pattern in &self.props.error_patterns {
1172 if output_to_check.contains(pattern.trim()) {
1173 debug!("found error pattern {}", pattern);
1175 missing_patterns.push(pattern.to_string());
1179 if missing_patterns.is_empty() {
1183 if missing_patterns.len() == 1 {
1184 self.fatal_proc_rec(
1185 &format!("error pattern '{}' not found!", missing_patterns[0]),
1189 for pattern in missing_patterns {
1190 self.error(&format!("error pattern '{}' not found!", pattern));
1192 self.fatal_proc_rec("multiple error patterns not found", proc_res);
1196 fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
1197 match proc_res.status.code() {
1198 Some(101) => self.fatal_proc_rec("compiler encountered internal error", proc_res),
1199 None => self.fatal_proc_rec("compiler terminated by signal", proc_res),
1204 fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
1205 for pat in &self.props.forbid_output {
1206 if output_to_check.contains(pat) {
1207 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
1212 fn check_expected_errors(&self, expected_errors: Vec<errors::Error>, proc_res: &ProcRes) {
1213 debug!("check_expected_errors: expected_errors={:?} proc_res.status={:?}",
1214 expected_errors, proc_res.status);
1215 if proc_res.status.success()
1218 .any(|x| x.kind == Some(ErrorKind::Error))
1220 self.fatal_proc_rec("process did not return an error status", proc_res);
1223 // On Windows, keep all '\' path separators to match the paths reported in the JSON output
1224 // from the compiler
1225 let os_file_name = self.testpaths.file.display().to_string();
1227 // on windows, translate all '\' path separators to '/'
1228 let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
1230 // If the testcase being checked contains at least one expected "help"
1231 // message, then we'll ensure that all "help" messages are expected.
1232 // Otherwise, all "help" messages reported by the compiler will be ignored.
1233 // This logic also applies to "note" messages.
1234 let expect_help = expected_errors
1236 .any(|ee| ee.kind == Some(ErrorKind::Help));
1237 let expect_note = expected_errors
1239 .any(|ee| ee.kind == Some(ErrorKind::Note));
1241 // Parse the JSON output from the compiler and extract out the messages.
1242 let actual_errors = json::parse_output(&os_file_name, &proc_res.stderr, proc_res);
1243 let mut unexpected = Vec::new();
1244 let mut found = vec![false; expected_errors.len()];
1245 for actual_error in &actual_errors {
1246 let opt_index = expected_errors.iter().enumerate().position(
1247 |(index, expected_error)| {
1248 !found[index] && actual_error.line_num == expected_error.line_num
1249 && (expected_error.kind.is_none()
1250 || actual_error.kind == expected_error.kind)
1251 && actual_error.msg.contains(&expected_error.msg)
1257 // found a match, everybody is happy
1258 assert!(!found[index]);
1259 found[index] = true;
1263 if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1264 self.error(&format!(
1265 "{}:{}: unexpected {}: '{}'",
1267 actual_error.line_num,
1271 .map_or(String::from("message"), |k| k.to_string()),
1274 unexpected.push(actual_error);
1280 let mut not_found = Vec::new();
1281 // anything not yet found is a problem
1282 for (index, expected_error) in expected_errors.iter().enumerate() {
1284 self.error(&format!(
1285 "{}:{}: expected {} not found: {}",
1287 expected_error.line_num,
1291 .map_or("message".into(), |k| k.to_string()),
1294 not_found.push(expected_error);
1298 if !unexpected.is_empty() || !not_found.is_empty() {
1299 self.error(&format!(
1300 "{} unexpected errors found, {} expected errors not found",
1304 println!("status: {}\ncommand: {}", proc_res.status, proc_res.cmdline);
1305 if !unexpected.is_empty() {
1306 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1308 if !not_found.is_empty() {
1309 println!("not found errors (from test file): {:#?}\n", not_found);
1315 /// Returns true if we should report an error about `actual_error`,
1316 /// which did not match any of the expected error. We always require
1317 /// errors/warnings to be explicitly listed, but only require
1318 /// helps/notes if there are explicit helps/notes given.
1319 fn is_unexpected_compiler_message(
1321 actual_error: &Error,
1325 match actual_error.kind {
1326 Some(ErrorKind::Help) => expect_help,
1327 Some(ErrorKind::Note) => expect_note,
1328 Some(ErrorKind::Error) | Some(ErrorKind::Warning) => true,
1329 Some(ErrorKind::Suggestion) | None => false,
1333 fn compile_test(&self) -> ProcRes {
1334 let mut rustc = self.make_compile_args(
1335 &self.testpaths.file,
1336 TargetLocation::ThisFile(self.make_exe_name()),
1339 rustc.arg("-L").arg(&self.aux_output_dir_name());
1341 match self.config.mode {
1342 CompileFail | Ui => {
1343 // compile-fail and ui tests tend to have tons of unused code as
1344 // it's just testing various pieces of the compile, but we don't
1345 // want to actually assert warnings about all this code. Instead
1346 // let's just ignore unused code warnings by defaults and tests
1347 // can turn it back on if needed.
1348 if !self.config.src_base.ends_with("rustdoc-ui") {
1349 rustc.args(&["-A", "unused"]);
1355 self.compose_and_run_compiler(rustc, None)
1358 fn document(&self, out_dir: &Path) -> ProcRes {
1359 if self.props.build_aux_docs {
1360 for rel_ab in &self.props.aux_builds {
1361 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1364 .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1365 let aux_cx = TestCx {
1366 config: self.config,
1368 testpaths: &aux_testpaths,
1369 revision: self.revision,
1371 // Create the directory for the stdout/stderr files.
1372 create_dir_all(aux_cx.output_base_dir()).unwrap();
1373 let auxres = aux_cx.document(out_dir);
1374 if !auxres.status.success() {
1380 let aux_dir = self.aux_output_dir_name();
1382 let rustdoc_path = self
1386 .expect("--rustdoc-path passed");
1387 let mut rustdoc = Command::new(rustdoc_path);
1391 .arg(self.config.run_lib_path.to_str().unwrap())
1396 .arg(&self.testpaths.file)
1397 .args(&self.props.compile_flags);
1399 if let Some(ref linker) = self.config.linker {
1404 .arg("unstable-options");
1407 self.compose_and_run_compiler(rustdoc, None)
1410 fn exec_compiled_test(&self) -> ProcRes {
1411 let env = &self.props.exec_env;
1413 let proc_res = match &*self.config.target {
1414 // This is pretty similar to below, we're transforming:
1416 // program arg1 arg2
1420 // remote-test-client run program:support-lib.so arg1 arg2
1422 // The test-client program will upload `program` to the emulator
1423 // along with all other support libraries listed (in this case
1424 // `support-lib.so`. It will then execute the program on the
1425 // emulator with the arguments specified (in the environment we give
1426 // the process) and then report back the same result.
1427 _ if self.config.remote_test_client.is_some() => {
1428 let aux_dir = self.aux_output_dir_name();
1429 let ProcArgs { mut prog, args } = self.make_run_args();
1430 if let Ok(entries) = aux_dir.read_dir() {
1431 for entry in entries {
1432 let entry = entry.unwrap();
1433 if !entry.path().is_file() {
1437 prog.push_str(entry.path().to_str().unwrap());
1440 let mut test_client =
1441 Command::new(self.config.remote_test_client.as_ref().unwrap());
1443 .args(&["run", &prog])
1446 self.compose_and_run(
1448 self.config.run_lib_path.to_str().unwrap(),
1449 Some(aux_dir.to_str().unwrap()),
1454 let aux_dir = self.aux_output_dir_name();
1455 let ProcArgs { prog, args } = self.make_run_args();
1456 let mut program = Command::new(&prog);
1459 .current_dir(&self.output_base_dir())
1461 self.compose_and_run(
1463 self.config.run_lib_path.to_str().unwrap(),
1464 Some(aux_dir.to_str().unwrap()),
1470 if proc_res.status.success() {
1471 // delete the executable after running it to save space.
1472 // it is ok if the deletion failed.
1473 let _ = fs::remove_file(self.make_exe_name());
1479 /// For each `aux-build: foo/bar` annotation, we check to find the
1480 /// file in a `auxiliary` directory relative to the test itself.
1481 fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1486 .expect("test file path has no parent")
1489 if !test_ab.exists() {
1490 self.fatal(&format!(
1491 "aux-build `{}` source not found",
1501 .join(self.output_testname_unique())
1505 .expect("aux-build path has no parent")
1510 fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
1511 let aux_dir = self.aux_output_dir_name();
1513 if !self.props.aux_builds.is_empty() {
1514 let _ = fs::remove_dir_all(&aux_dir);
1515 create_dir_all(&aux_dir).unwrap();
1518 for rel_ab in &self.props.aux_builds {
1519 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1522 .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1523 let aux_output = TargetLocation::ThisDirectory(self.aux_output_dir_name());
1524 let aux_cx = TestCx {
1525 config: self.config,
1527 testpaths: &aux_testpaths,
1528 revision: self.revision,
1530 // Create the directory for the stdout/stderr files.
1531 create_dir_all(aux_cx.output_base_dir()).unwrap();
1532 let mut aux_rustc = aux_cx.make_compile_args(&aux_testpaths.file, aux_output);
1534 let crate_type = if aux_props.no_prefer_dynamic {
1536 } else if self.config.target.contains("cloudabi")
1537 || self.config.target.contains("emscripten")
1538 || (self.config.target.contains("musl") && !aux_props.force_host)
1539 || self.config.target.contains("wasm32")
1541 // We primarily compile all auxiliary libraries as dynamic libraries
1542 // to avoid code size bloat and large binaries as much as possible
1543 // for the test suite (otherwise including libstd statically in all
1544 // executables takes up quite a bit of space).
1546 // For targets like MUSL or Emscripten, however, there is no support for
1547 // dynamic libraries so we just go back to building a normal library. Note,
1548 // however, that for MUSL if the library is built with `force_host` then
1549 // it's ok to be a dylib as the host should always support dylibs.
1555 if let Some(crate_type) = crate_type {
1556 aux_rustc.args(&["--crate-type", crate_type]);
1559 aux_rustc.arg("-L").arg(&aux_dir);
1561 let auxres = aux_cx.compose_and_run(
1563 aux_cx.config.compile_lib_path.to_str().unwrap(),
1564 Some(aux_dir.to_str().unwrap()),
1567 if !auxres.status.success() {
1568 self.fatal_proc_rec(
1570 "auxiliary build of {:?} failed to compile: ",
1571 aux_testpaths.file.display()
1578 rustc.envs(self.props.rustc_env.clone());
1579 self.compose_and_run(
1581 self.config.compile_lib_path.to_str().unwrap(),
1582 Some(aux_dir.to_str().unwrap()),
1589 mut command: Command,
1591 aux_path: Option<&str>,
1592 input: Option<String>,
1595 let cmdline = self.make_cmdline(&command, lib_path);
1596 logv(self.config, format!("executing {}", cmdline));
1601 .stdout(Stdio::piped())
1602 .stderr(Stdio::piped())
1603 .stdin(Stdio::piped());
1605 // Need to be sure to put both the lib_path and the aux path in the dylib
1606 // search path for the child.
1607 let mut path = env::split_paths(&env::var_os(dylib_env_var()).unwrap_or(OsString::new()))
1608 .collect::<Vec<_>>();
1609 if let Some(p) = aux_path {
1610 path.insert(0, PathBuf::from(p))
1612 path.insert(0, PathBuf::from(lib_path));
1614 // Add the new dylib search path var
1615 let newpath = env::join_paths(&path).unwrap();
1616 command.env(dylib_env_var(), newpath);
1618 let mut child = disable_error_reporting(|| command.spawn())
1619 .expect(&format!("failed to exec `{:?}`", &command));
1620 if let Some(input) = input {
1625 .write_all(input.as_bytes())
1633 } = read2_abbreviated(child).expect("failed to read output");
1635 let result = ProcRes {
1637 stdout: String::from_utf8_lossy(&stdout).into_owned(),
1638 stderr: String::from_utf8_lossy(&stderr).into_owned(),
1642 self.dump_output(&result.stdout, &result.stderr);
1647 fn make_compile_args(&self, input_file: &Path, output_file: TargetLocation) -> Command {
1648 let is_rustdoc = self.config.src_base.ends_with("rustdoc-ui");
1649 let mut rustc = if !is_rustdoc {
1650 Command::new(&self.config.rustc_path)
1657 .expect("no rustdoc built yet"),
1660 // FIXME Why is -L here?
1661 rustc.arg(input_file); //.arg("-L").arg(&self.config.build_base);
1663 // Optionally prevent default --target if specified in test compile-flags.
1664 let custom_target = self
1668 .any(|x| x.starts_with("--target"));
1671 let target = if self.props.force_host {
1674 &*self.config.target
1677 rustc.arg(&format!("--target={}", target));
1680 if let Some(revision) = self.revision {
1681 rustc.args(&["--cfg", revision]);
1685 if let Some(ref incremental_dir) = self.props.incremental_dir {
1686 rustc.args(&["-C", &format!("incremental={}", incremental_dir.display())]);
1687 rustc.args(&["-Z", "incremental-verify-ich"]);
1688 rustc.args(&["-Z", "incremental-queries"]);
1691 if self.config.mode == CodegenUnits {
1692 rustc.args(&["-Z", "human_readable_cgu_names"]);
1696 match self.config.mode {
1697 CompileFail | ParseFail | Incremental => {
1698 // If we are extracting and matching errors in the new
1699 // fashion, then you want JSON mode. Old-skool error
1700 // patterns still match the raw compiler output.
1701 if self.props.error_patterns.is_empty() {
1702 rustc.args(&["--error-format", "json"]);
1704 if !self.props.disable_ui_testing_normalization {
1705 rustc.arg("-Zui-testing");
1713 .any(|s| s.starts_with("--error-format"))
1715 rustc.args(&["--error-format", "json"]);
1717 if !self.props.disable_ui_testing_normalization {
1718 rustc.arg("-Zui-testing");
1724 "-Zmir-opt-level=3",
1725 "-Zdump-mir-exclude-pass-number",
1728 let mir_dump_dir = self.get_mir_dump_dir();
1729 let _ = fs::remove_dir_all(&mir_dump_dir);
1730 create_dir_all(mir_dump_dir.as_path()).unwrap();
1731 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1732 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1733 debug!("dir_opt: {:?}", dir_opt);
1737 RunFail | RunPassValgrind | Pretty | DebugInfoGdb | DebugInfoLldb
1738 | Codegen | Rustdoc | RunMake | CodegenUnits => {
1739 // do not use JSON output
1743 if self.props.skip_codegen {
1749 .any(|s| s.starts_with("--emit"))
1751 rustc.args(&["--emit", "metadata"]);
1755 if self.config.target == "wasm32-unknown-unknown" {
1756 // rustc.arg("-g"); // get any backtrace at all on errors
1757 } else if !self.props.no_prefer_dynamic {
1758 rustc.args(&["-C", "prefer-dynamic"]);
1763 TargetLocation::ThisFile(path) => {
1764 rustc.arg("-o").arg(path);
1766 TargetLocation::ThisDirectory(path) => {
1767 rustc.arg("--out-dir").arg(path);
1771 match self.config.compare_mode {
1772 Some(CompareMode::Nll) => {
1773 rustc.args(&["-Zborrowck=mir", "-Ztwo-phase-borrows"]);
1775 Some(CompareMode::Polonius) => {
1776 rustc.args(&["-Zpolonius", "-Zborrowck=mir", "-Ztwo-phase-borrows"]);
1781 if self.props.force_host {
1782 rustc.args(self.split_maybe_args(&self.config.host_rustcflags));
1784 rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
1787 if let Some(ref linker) = self.config.linker {
1788 rustc.arg(format!("-Clinker={}", linker));
1792 rustc.args(&self.props.compile_flags);
1797 fn make_exe_name(&self) -> PathBuf {
1798 // Using a single letter here to keep the path length down for
1799 // Windows. Some test names get very long. rustc creates `rcgu`
1800 // files with the module name appended to it which can more than
1801 // double the length.
1802 let mut f = self.output_base_dir().join("a");
1803 // FIXME: This is using the host architecture exe suffix, not target!
1804 if self.config.target.contains("emscripten") {
1805 f = f.with_extra_extension("js");
1806 } else if self.config.target.contains("wasm32") {
1807 f = f.with_extra_extension("wasm");
1808 } else if !env::consts::EXE_SUFFIX.is_empty() {
1809 f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1814 fn make_run_args(&self) -> ProcArgs {
1815 // If we've got another tool to run under (valgrind),
1816 // then split apart its command
1817 let mut args = self.split_maybe_args(&self.config.runtool);
1819 // If this is emscripten, then run tests under nodejs
1820 if self.config.target.contains("emscripten") {
1821 if let Some(ref p) = self.config.nodejs {
1822 args.push(p.clone());
1824 self.fatal("no NodeJS binary found (--nodejs)");
1828 // If this is otherwise wasm , then run tests under nodejs with our
1830 if self.config.target.contains("wasm32") {
1831 if let Some(ref p) = self.config.nodejs {
1832 args.push(p.clone());
1834 self.fatal("no NodeJS binary found (--nodejs)");
1837 let src = self.config.src_base
1838 .parent().unwrap() // chop off `run-pass`
1839 .parent().unwrap() // chop off `test`
1840 .parent().unwrap(); // chop off `src`
1841 args.push(src.join("src/etc/wasm32-shim.js").display().to_string());
1844 let exe_file = self.make_exe_name();
1846 // FIXME (#9639): This needs to handle non-utf8 paths
1847 args.push(exe_file.to_str().unwrap().to_owned());
1849 // Add the arguments in the run_flags directive
1850 args.extend(self.split_maybe_args(&self.props.run_flags));
1852 let prog = args.remove(0);
1853 ProcArgs { prog, args }
1856 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1861 if s.chars().all(|c| c.is_whitespace()) {
1872 fn make_cmdline(&self, command: &Command, libpath: &str) -> String {
1875 // Linux and mac don't require adjusting the library search path
1877 format!("{:?}", command)
1879 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1880 // for diagnostic purposes
1881 fn lib_path_cmd_prefix(path: &str) -> String {
1884 util::lib_path_env_var(),
1885 util::make_new_path(path)
1889 format!("{} {:?}", lib_path_cmd_prefix(libpath), command)
1893 fn dump_output(&self, out: &str, err: &str) {
1894 let revision = if let Some(r) = self.revision {
1900 self.dump_output_file(out, &format!("{}out", revision));
1901 self.dump_output_file(err, &format!("{}err", revision));
1902 self.maybe_dump_to_stdout(out, err);
1905 fn dump_output_file(&self, out: &str, extension: &str) {
1906 let outfile = self.make_out_name(extension);
1907 File::create(&outfile)
1909 .write_all(out.as_bytes())
1913 /// Create a filename for output with the given extension. Example:
1914 /// /.../testname.revision.mode/testname.extension
1915 fn make_out_name(&self, extension: &str) -> PathBuf {
1916 self.output_base_name().with_extension(extension)
1919 /// Directory where auxiliary files are written. Example:
1920 /// /.../testname.revision.mode/auxiliary/
1921 fn aux_output_dir_name(&self) -> PathBuf {
1922 self.output_base_dir()
1924 .with_extra_extension(self.config.mode.disambiguator())
1927 /// Generates a unique name for the test, such as `testname.revision.mode`.
1928 fn output_testname_unique(&self) -> PathBuf {
1929 output_testname_unique(self.config, self.testpaths, self.safe_revision())
1932 /// The revision, ignored for Incremental since it wants all revisions in
1933 /// the same directory.
1934 fn safe_revision(&self) -> Option<&str> {
1935 if self.config.mode == Incremental {
1942 /// Absolute path to the directory where all output for the given
1943 /// test/revision should reside. Example:
1944 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
1945 fn output_base_dir(&self) -> PathBuf {
1946 output_base_dir(self.config, self.testpaths, self.safe_revision())
1949 /// Absolute path to the base filename used as output for the given
1950 /// test/revision. Example:
1951 /// /.../relative/testname.revision.mode/testname
1952 fn output_base_name(&self) -> PathBuf {
1953 output_base_name(self.config, self.testpaths, self.safe_revision())
1956 fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1957 if self.config.verbose {
1958 println!("------{}------------------------------", "stdout");
1959 println!("{}", out);
1960 println!("------{}------------------------------", "stderr");
1961 println!("{}", err);
1962 println!("------------------------------------------");
1966 fn error(&self, err: &str) {
1967 match self.revision {
1968 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1969 None => println!("\nerror: {}", err),
1973 fn fatal(&self, err: &str) -> ! {
1978 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1979 self.try_print_open_handles();
1981 proc_res.fatal(None);
1984 // This function is a poor man's attempt to debug rust-lang/rust#38620, if
1985 // that's closed then this should be deleted
1987 // This is a very "opportunistic" debugging attempt, so we ignore all
1989 fn try_print_open_handles(&self) {
1993 if self.config.mode != Incremental {
1997 let filename = match self.testpaths.file.file_stem() {
2002 let mut cmd = Command::new("handle.exe");
2003 cmd.arg("-a").arg("-u");
2005 cmd.arg("-nobanner");
2006 cmd.stdout(Stdio::piped());
2007 cmd.stderr(Stdio::piped());
2008 let output = match cmd.spawn().and_then(read2_abbreviated) {
2009 Ok(output) => output,
2012 println!("---------------------------------------------------");
2013 println!("ran extra command to debug rust-lang/rust#38620: ");
2014 println!("{:?}", cmd);
2015 println!("result: {}", output.status);
2016 println!("--- stdout ----------------------------------------");
2017 println!("{}", String::from_utf8_lossy(&output.stdout));
2018 println!("--- stderr ----------------------------------------");
2019 println!("{}", String::from_utf8_lossy(&output.stderr));
2020 println!("---------------------------------------------------");
2023 // codegen tests (using FileCheck)
2025 fn compile_test_and_save_ir(&self) -> ProcRes {
2026 let aux_dir = self.aux_output_dir_name();
2028 let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
2029 let mut rustc = self.make_compile_args(&self.testpaths.file, output_file);
2030 rustc.arg("-L").arg(aux_dir).arg("--emit=llvm-ir");
2032 self.compose_and_run_compiler(rustc, None)
2035 fn check_ir_with_filecheck(&self) -> ProcRes {
2036 let irfile = self.output_base_name().with_extension("ll");
2037 let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
2039 .arg("--input-file")
2041 .arg(&self.testpaths.file);
2042 self.compose_and_run(filecheck, "", None, None)
2045 fn run_codegen_test(&self) {
2046 assert!(self.revision.is_none(), "revisions not relevant here");
2048 if self.config.llvm_filecheck.is_none() {
2049 self.fatal("missing --llvm-filecheck");
2052 let mut proc_res = self.compile_test_and_save_ir();
2053 if !proc_res.status.success() {
2054 self.fatal_proc_rec("compilation failed!", &proc_res);
2057 proc_res = self.check_ir_with_filecheck();
2058 if !proc_res.status.success() {
2059 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
2063 fn charset() -> &'static str {
2064 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
2065 if cfg!(target_os = "bitrig") {
2067 } else if cfg!(target_os = "freebsd") {
2074 fn run_rustdoc_test(&self) {
2075 assert!(self.revision.is_none(), "revisions not relevant here");
2077 let out_dir = self.output_base_dir();
2078 let _ = fs::remove_dir_all(&out_dir);
2079 create_dir_all(&out_dir).unwrap();
2081 let proc_res = self.document(&out_dir);
2082 if !proc_res.status.success() {
2083 self.fatal_proc_rec("rustdoc failed!", &proc_res);
2086 if self.props.check_test_line_numbers_match {
2087 self.check_rustdoc_test_option(proc_res);
2089 let root = self.config.find_rust_src_root().unwrap();
2090 let res = self.cmd2procres(
2091 Command::new(&self.config.docck_python)
2092 .arg(root.join("src/etc/htmldocck.py"))
2094 .arg(&self.testpaths.file),
2096 if !res.status.success() {
2097 self.fatal_proc_rec("htmldocck failed!", &res);
2102 fn get_lines<P: AsRef<Path>>(
2105 mut other_files: Option<&mut Vec<String>>,
2108 fs::File::open(path).expect("markdown_test_output_check_entry File::open failed");
2109 let mut content = String::new();
2110 file.read_to_string(&mut content)
2111 .expect("markdown_test_output_check_entry read_to_string failed");
2112 let mut ignore = false;
2116 .filter_map(|(line_nb, line)| {
2117 if (line.trim_left().starts_with("pub mod ")
2118 || line.trim_left().starts_with("mod "))
2119 && line.ends_with(';')
2121 if let Some(ref mut other_files) = other_files {
2122 other_files.push(line.rsplit("mod ").next().unwrap().replace(";", ""));
2126 let sline = line.split("///").last().unwrap_or("");
2127 let line = sline.trim_left();
2128 if line.starts_with("```") {
2144 fn check_rustdoc_test_option(&self, res: ProcRes) {
2145 let mut other_files = Vec::new();
2146 let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2147 let cwd = env::current_dir().unwrap();
2152 .unwrap_or(&self.testpaths.file)
2155 .replace('\\', "/"),
2156 self.get_lines(&self.testpaths.file, Some(&mut other_files)),
2158 for other_file in other_files {
2159 let mut path = self.testpaths.file.clone();
2160 path.set_file_name(&format!("{}.rs", other_file));
2162 path.strip_prefix(&cwd)
2166 .replace('\\', "/"),
2167 self.get_lines(&path, None),
2175 .filter(|s| s.starts_with("test "))
2177 let tmp: Vec<&str> = s.split(" - ").collect();
2179 let path = tmp[0].rsplit("test ").next().unwrap();
2180 if let Some(ref mut v) = files.get_mut(&path.replace('\\', "/")) {
2182 let mut iter = tmp[1].split("(line ");
2192 if let Ok(pos) = v.binary_search(&line) {
2195 self.fatal_proc_rec(
2196 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2204 self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2206 for (entry, v) in &files {
2208 self.fatal_proc_rec(
2210 "Not found test at line{} \"{}\":{:?}",
2211 if v.len() > 1 { "s" } else { "" },
2222 fn run_codegen_units_test(&self) {
2223 assert!(self.revision.is_none(), "revisions not relevant here");
2225 let proc_res = self.compile_test();
2227 if !proc_res.status.success() {
2228 self.fatal_proc_rec("compilation failed!", &proc_res);
2231 self.check_no_compiler_crash(&proc_res);
2233 const PREFIX: &'static str = "MONO_ITEM ";
2234 const CGU_MARKER: &'static str = "@@";
2236 let actual: Vec<MonoItem> = proc_res
2239 .filter(|line| line.starts_with(PREFIX))
2240 .map(|line| str_to_mono_item(line, true))
2243 let expected: Vec<MonoItem> = errors::load_errors(&self.testpaths.file, None)
2245 .map(|e| str_to_mono_item(&e.msg[..], false))
2248 let mut missing = Vec::new();
2249 let mut wrong_cgus = Vec::new();
2251 for expected_item in &expected {
2252 let actual_item_with_same_name = actual.iter().find(|ti| ti.name == expected_item.name);
2254 if let Some(actual_item) = actual_item_with_same_name {
2255 if !expected_item.codegen_units.is_empty() &&
2256 // Also check for codegen units
2257 expected_item.codegen_units != actual_item.codegen_units
2259 wrong_cgus.push((expected_item.clone(), actual_item.clone()));
2262 missing.push(expected_item.string.clone());
2266 let unexpected: Vec<_> = actual
2268 .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
2269 .map(|acgu| acgu.string.clone())
2272 if !missing.is_empty() {
2275 println!("\nThese items should have been contained but were not:\n");
2277 for item in &missing {
2278 println!("{}", item);
2284 if !unexpected.is_empty() {
2286 let mut sorted = unexpected.clone();
2291 println!("\nThese items were contained but should not have been:\n");
2293 for item in sorted {
2294 println!("{}", item);
2300 if !wrong_cgus.is_empty() {
2301 wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
2302 println!("\nThe following items were assigned to wrong codegen units:\n");
2304 for &(ref expected_item, ref actual_item) in &wrong_cgus {
2305 println!("{}", expected_item.name);
2308 codegen_units_to_str(&expected_item.codegen_units)
2312 codegen_units_to_str(&actual_item.codegen_units)
2318 if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty()) {
2322 #[derive(Clone, Eq, PartialEq)]
2325 codegen_units: HashSet<String>,
2329 // [MONO_ITEM] name [@@ (cgu)+]
2330 fn str_to_mono_item(s: &str, cgu_has_crate_disambiguator: bool) -> MonoItem {
2331 let s = if s.starts_with(PREFIX) {
2332 (&s[PREFIX.len()..]).trim()
2337 let full_string = format!("{}{}", PREFIX, s);
2339 let parts: Vec<&str> = s
2342 .filter(|s| !s.is_empty())
2345 let name = parts[0].trim();
2347 let cgus = if parts.len() > 1 {
2348 let cgus_str = parts[1];
2353 .filter(|s| !s.is_empty())
2355 if cgu_has_crate_disambiguator {
2356 remove_crate_disambiguator_from_cgu(s)
2367 name: name.to_owned(),
2368 codegen_units: cgus,
2369 string: full_string,
2373 fn codegen_units_to_str(cgus: &HashSet<String>) -> String {
2374 let mut cgus: Vec<_> = cgus.iter().collect();
2377 let mut string = String::new();
2379 string.push_str(&cgu[..]);
2380 string.push_str(" ");
2386 // Given a cgu-name-prefix of the form <crate-name>.<crate-disambiguator> or
2387 // the form <crate-name1>.<crate-disambiguator1>-in-<crate-name2>.<crate-disambiguator2>,
2388 // remove all crate-disambiguators.
2389 fn remove_crate_disambiguator_from_cgu(cgu: &str) -> String {
2391 static ref RE: Regex = Regex::new(
2392 r"^[^\.]+(?P<d1>\.[[:alnum:]]+)(-in-[^\.]+(?P<d2>\.[[:alnum:]]+))?"
2396 let captures = RE.captures(cgu).unwrap_or_else(|| {
2397 panic!("invalid cgu name encountered: {}", cgu)
2400 let mut new_name = cgu.to_owned();
2402 if let Some(d2) = captures.name("d2") {
2403 new_name.replace_range(d2.start() .. d2.end(), "");
2406 let d1 = captures.name("d1").unwrap();
2407 new_name.replace_range(d1.start() .. d1.end(), "");
2413 fn init_incremental_test(&self) {
2414 // (See `run_incremental_test` for an overview of how incremental tests work.)
2416 // Before any of the revisions have executed, create the
2417 // incremental workproduct directory. Delete any old
2418 // incremental work products that may be there from prior
2420 let incremental_dir = self.incremental_dir();
2421 if incremental_dir.exists() {
2422 // Canonicalizing the path will convert it to the //?/ format
2423 // on Windows, which enables paths longer than 260 character
2424 let canonicalized = incremental_dir.canonicalize().unwrap();
2425 fs::remove_dir_all(canonicalized).unwrap();
2427 fs::create_dir_all(&incremental_dir).unwrap();
2429 if self.config.verbose {
2431 "init_incremental_test: incremental_dir={}",
2432 incremental_dir.display()
2437 fn run_incremental_test(&self) {
2438 // Basic plan for a test incremental/foo/bar.rs:
2439 // - load list of revisions rpass1, cfail2, rpass3
2440 // - each should begin with `rpass`, `cfail`, or `rfail`
2441 // - if `rpass`, expect compile and execution to succeed
2442 // - if `cfail`, expect compilation to fail
2443 // - if `rfail`, expect execution to fail
2444 // - create a directory build/foo/bar.incremental
2445 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
2446 // - because name of revision starts with "rpass", expect success
2447 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
2448 // - because name of revision starts with "cfail", expect an error
2449 // - load expected errors as usual, but filter for those that end in `[rfail2]`
2450 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2451 // - because name of revision starts with "rpass", expect success
2452 // - execute build/foo/bar.exe and save output
2454 // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2455 // to #[rustc_dirty] and clean tests I guess
2459 .expect("incremental tests require a list of revisions");
2461 // Incremental workproduct directory should have already been created.
2462 let incremental_dir = self.incremental_dir();
2464 incremental_dir.exists(),
2465 "init_incremental_test failed to create incremental dir"
2468 // Add an extra flag pointing at the incremental directory.
2469 let mut revision_props = self.props.clone();
2470 revision_props.incremental_dir = Some(incremental_dir);
2472 let revision_cx = TestCx {
2473 config: self.config,
2474 props: &revision_props,
2475 testpaths: self.testpaths,
2476 revision: self.revision,
2479 if self.config.verbose {
2481 "revision={:?} revision_props={:#?}",
2482 revision, revision_props
2486 if revision.starts_with("rpass") {
2487 revision_cx.run_rpass_test();
2488 } else if revision.starts_with("rfail") {
2489 revision_cx.run_rfail_test();
2490 } else if revision.starts_with("cfail") {
2491 revision_cx.run_cfail_test();
2493 revision_cx.fatal("revision name must begin with rpass, rfail, or cfail");
2497 /// Directory where incremental work products are stored.
2498 fn incremental_dir(&self) -> PathBuf {
2499 self.output_base_name().with_extension("inc")
2502 fn run_rmake_test(&self) {
2503 let cwd = env::current_dir().unwrap();
2513 let src_root = cwd.join(&src_root);
2515 let tmpdir = cwd.join(self.output_base_name());
2516 if tmpdir.exists() {
2517 self.aggressive_rm_rf(&tmpdir).unwrap();
2519 create_dir_all(&tmpdir).unwrap();
2521 let host = &self.config.host;
2522 let make = if host.contains("bitrig")
2523 || host.contains("dragonfly")
2524 || host.contains("freebsd")
2525 || host.contains("netbsd")
2526 || host.contains("openbsd")
2533 let mut cmd = Command::new(make);
2534 cmd.current_dir(&self.testpaths.file)
2535 .stdout(Stdio::piped())
2536 .stderr(Stdio::piped())
2537 .env("TARGET", &self.config.target)
2538 .env("PYTHON", &self.config.docck_python)
2540 .env("RUST_BUILD_STAGE", &self.config.stage_id)
2541 .env("RUSTC", cwd.join(&self.config.rustc_path))
2542 .env("TMPDIR", &tmpdir)
2543 .env("LD_LIB_PATH_ENVVAR", dylib_env_var())
2544 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2545 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2546 .env("LLVM_COMPONENTS", &self.config.llvm_components)
2547 .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags)
2549 // We for sure don't want these tests to run in parallel, so make
2550 // sure they don't have access to these vars if we we run via `make`
2552 .env_remove("MAKEFLAGS")
2553 .env_remove("MFLAGS")
2554 .env_remove("CARGO_MAKEFLAGS");
2556 if let Some(ref rustdoc) = self.config.rustdoc_path {
2557 cmd.env("RUSTDOC", cwd.join(rustdoc));
2560 if let Some(ref node) = self.config.nodejs {
2561 cmd.env("NODE", node);
2564 if let Some(ref linker) = self.config.linker {
2565 cmd.env("RUSTC_LINKER", linker);
2568 // We don't want RUSTFLAGS set from the outside to interfere with
2569 // compiler flags set in the test cases:
2570 cmd.env_remove("RUSTFLAGS");
2572 if self.config.target.contains("msvc") && self.config.cc != "" {
2573 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2574 // and that `lib.exe` lives next to it.
2575 let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2577 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2578 // a path and instead passes `C:\msys64\foo`, so convert all
2579 // `/`-arguments to MSVC here to `-` arguments.
2584 .map(|s| s.replace("/", "-"))
2585 .collect::<Vec<_>>()
2588 cmd.env("IS_MSVC", "1")
2589 .env("IS_WINDOWS", "1")
2590 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2591 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2592 .env("CXX", format!("'{}'", &self.config.cxx));
2594 cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2595 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags))
2596 .env("AR", &self.config.ar);
2598 if self.config.target.contains("windows") {
2599 cmd.env("IS_WINDOWS", "1");
2605 .and_then(read2_abbreviated)
2606 .expect("failed to spawn `make`");
2607 if !output.status.success() {
2609 status: output.status,
2610 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2611 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2612 cmdline: format!("{:?}", cmd),
2614 self.fatal_proc_rec("make failed", &res);
2618 fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2619 for e in path.read_dir()? {
2621 let path = entry.path();
2622 if entry.file_type()?.is_dir() {
2623 self.aggressive_rm_rf(&path)?;
2625 // Remove readonly files as well on windows (by default we can't)
2626 fs::remove_file(&path).or_else(|e| {
2627 if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2628 let mut meta = entry.metadata()?.permissions();
2629 meta.set_readonly(false);
2630 fs::set_permissions(&path, meta)?;
2631 fs::remove_file(&path)
2638 fs::remove_dir(path)
2641 fn run_ui_test(&self) {
2642 // if the user specified a format in the ui test
2643 // print the output to the stderr file, otherwise extract
2644 // the rendered error messages from json and print them
2649 .any(|s| s.contains("--error-format"));
2650 let proc_res = self.compile_test();
2651 self.check_if_test_should_compile(&proc_res);
2653 let expected_stderr = self.load_expected_output(UI_STDERR);
2654 let expected_stdout = self.load_expected_output(UI_STDOUT);
2655 let expected_fixed = self.load_expected_output(UI_FIXED);
2657 let normalized_stdout =
2658 self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2660 let stderr = if explicit {
2661 proc_res.stderr.clone()
2663 json::extract_rendered(&proc_res.stderr, &proc_res)
2666 let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2669 if !self.props.dont_check_compiler_stdout {
2670 errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2672 if !self.props.dont_check_compiler_stderr {
2673 errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2676 let modes_to_prune = vec![CompareMode::Nll];
2677 self.prune_duplicate_outputs(&modes_to_prune);
2679 if self.config.compare_mode.is_some() {
2680 // don't test rustfix with nll right now
2681 } else if self.props.run_rustfix {
2682 // Apply suggestions from rustc to the code itself
2683 let unfixed_code = self
2684 .load_expected_output_from_path(&self.testpaths.file)
2686 let suggestions = get_suggestions_from_json(
2689 if self.props.rustfix_only_machine_applicable {
2690 Filter::MachineApplicableOnly
2695 let fixed_code = apply_suggestions(&unfixed_code, &suggestions).expect(&format!(
2696 "failed to apply suggestions for {:?} with rustfix",
2700 errors += self.compare_output("fixed", &fixed_code, &expected_fixed);
2701 } else if !expected_fixed.is_empty() {
2703 "the `// run-rustfix` directive wasn't found but a `*.fixed` \
2709 println!("To update references, rerun the tests and pass the `--bless` flag");
2710 let relative_path_to_file = self
2713 .join(self.testpaths.file.file_name().unwrap());
2715 "To only update this specific test, also pass `--test-args {}`",
2716 relative_path_to_file.display(),
2718 self.fatal_proc_rec(
2719 &format!("{} errors occurred comparing output.", errors),
2724 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
2726 if self.should_run_successfully() {
2727 let proc_res = self.exec_compiled_test();
2729 if !proc_res.status.success() {
2730 self.fatal_proc_rec("test run failed!", &proc_res);
2734 debug!("run_ui_test: explicit={:?} config.compare_mode={:?} expected_errors={:?} \
2735 proc_res.status={:?} props.error_patterns={:?}",
2736 explicit, self.config.compare_mode, expected_errors, proc_res.status,
2737 self.props.error_patterns);
2738 if !explicit && self.config.compare_mode.is_none() {
2739 if !expected_errors.is_empty() && !proc_res.status.success() {
2740 // "//~ERROR comments"
2741 self.check_expected_errors(expected_errors, &proc_res);
2742 } else if !self.props.error_patterns.is_empty() && !proc_res.status.success() {
2743 // "// error-pattern" comments
2744 self.check_error_patterns(&proc_res.stderr, &proc_res);
2748 if self.props.run_rustfix && self.config.compare_mode.is_none() {
2749 // And finally, compile the fixed code and make sure it both
2750 // succeeds and has no diagnostics.
2751 let mut rustc = self.make_compile_args(
2752 &self.testpaths.file.with_extension(UI_FIXED),
2753 TargetLocation::ThisFile(self.make_exe_name()),
2755 rustc.arg("-L").arg(&self.aux_output_dir_name());
2756 let res = self.compose_and_run_compiler(rustc, None);
2757 if !res.status.success() {
2758 self.fatal_proc_rec("failed to compile fixed code", &res);
2760 if !res.stderr.is_empty() && !self.props.rustfix_only_machine_applicable {
2761 self.fatal_proc_rec("fixed code is still producing diagnostics", &res);
2766 fn run_mir_opt_test(&self) {
2767 let proc_res = self.compile_test();
2769 if !proc_res.status.success() {
2770 self.fatal_proc_rec("compilation failed!", &proc_res);
2773 let proc_res = self.exec_compiled_test();
2775 if !proc_res.status.success() {
2776 self.fatal_proc_rec("test run failed!", &proc_res);
2778 self.check_mir_dump();
2781 fn check_mir_dump(&self) {
2782 let mut test_file_contents = String::new();
2783 fs::File::open(self.testpaths.file.clone())
2785 .read_to_string(&mut test_file_contents)
2787 if let Some(idx) = test_file_contents.find("// END RUST SOURCE") {
2788 let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2789 let tests_text_str = String::from(tests_text);
2790 let mut curr_test: Option<&str> = None;
2791 let mut curr_test_contents = vec![ExpectedLine::Elision];
2792 for l in tests_text_str.lines() {
2793 debug!("line: {:?}", l);
2794 if l.starts_with("// START ") {
2795 let (_, t) = l.split_at("// START ".len());
2796 curr_test = Some(t);
2797 } else if l.starts_with("// END") {
2798 let (_, t) = l.split_at("// END ".len());
2799 if Some(t) != curr_test {
2800 panic!("mismatched START END test name");
2802 self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2804 curr_test_contents.clear();
2805 curr_test_contents.push(ExpectedLine::Elision);
2806 } else if l.is_empty() {
2808 } else if l.starts_with("//") && l.split_at("//".len()).1.trim() == "..." {
2809 curr_test_contents.push(ExpectedLine::Elision)
2810 } else if l.starts_with("// ") {
2811 let (_, test_content) = l.split_at("// ".len());
2812 curr_test_contents.push(ExpectedLine::Text(test_content));
2818 fn check_mir_test_timestamp(&self, test_name: &str, output_file: &Path) {
2819 let t = |file| FileTime::from_last_modification_time(&fs::metadata(file).unwrap());
2820 let source_file = &self.testpaths.file;
2821 let output_time = t(output_file);
2822 let source_time = t(source_file);
2823 if source_time > output_time {
2825 "source file time: {:?} output file time: {:?}",
2826 source_time, output_time
2829 "test source file `{}` is newer than potentially stale output file `{}`.",
2830 source_file.display(),
2836 fn compare_mir_test_output(&self, test_name: &str, expected_content: &[ExpectedLine<&str>]) {
2837 let mut output_file = PathBuf::new();
2838 output_file.push(self.get_mir_dump_dir());
2839 output_file.push(test_name);
2840 debug!("comparing the contests of: {:?}", output_file);
2841 debug!("with: {:?}", expected_content);
2842 if !output_file.exists() {
2844 "Output file `{}` from test does not exist",
2845 output_file.into_os_string().to_string_lossy()
2848 self.check_mir_test_timestamp(test_name, &output_file);
2850 let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2851 let mut dumped_string = String::new();
2852 dumped_file.read_to_string(&mut dumped_string).unwrap();
2853 let mut dumped_lines = dumped_string
2855 .map(|l| nocomment_mir_line(l))
2856 .filter(|l| !l.is_empty());
2857 let mut expected_lines = expected_content
2860 if let &ExpectedLine::Text(l) = l {
2868 let compare = |expected_line, dumped_line| {
2869 let e_norm = normalize_mir_line(expected_line);
2870 let d_norm = normalize_mir_line(dumped_line);
2871 debug!("found: {:?}", d_norm);
2872 debug!("expected: {:?}", e_norm);
2876 let error = |expected_line, extra_msg| {
2877 let normalize_all = dumped_string
2879 .map(nocomment_mir_line)
2880 .filter(|l| !l.is_empty())
2881 .collect::<Vec<_>>()
2883 let f = |l: &ExpectedLine<_>| match l {
2884 &ExpectedLine::Elision => "... (elided)".into(),
2885 &ExpectedLine::Text(t) => t,
2887 let expected_content = expected_content
2890 .collect::<Vec<_>>()
2893 "Did not find expected line, error: {}\n\
2894 Expected Line: {:?}\n\
2898 extra_msg, expected_line, test_name, expected_content, normalize_all
2902 // We expect each non-empty line to appear consecutively, non-consecutive lines
2903 // must be separated by at least one Elision
2904 let mut start_block_line = None;
2905 while let Some(dumped_line) = dumped_lines.next() {
2906 match expected_lines.next() {
2907 Some(&ExpectedLine::Text(expected_line)) => {
2908 let normalized_expected_line = normalize_mir_line(expected_line);
2909 if normalized_expected_line.contains(":{") {
2910 start_block_line = Some(expected_line);
2913 if !compare(expected_line, dumped_line) {
2914 error!("{:?}", start_block_line);
2918 "Mismatch in lines\n\
2919 Current block: {}\n\
2921 start_block_line.unwrap_or("None"),
2927 Some(&ExpectedLine::Elision) => {
2928 // skip any number of elisions in a row.
2929 while let Some(&&ExpectedLine::Elision) = expected_lines.peek() {
2930 expected_lines.next();
2932 if let Some(&ExpectedLine::Text(expected_line)) = expected_lines.next() {
2933 let mut found = compare(expected_line, dumped_line);
2937 while let Some(dumped_line) = dumped_lines.next() {
2938 found = compare(expected_line, dumped_line);
2944 error(expected_line, "ran out of mir dump to match against".into());
2953 fn get_mir_dump_dir(&self) -> PathBuf {
2954 let mut mir_dump_dir = PathBuf::from(self.config.build_base.as_path());
2955 debug!("input_file: {:?}", self.testpaths.file);
2956 mir_dump_dir.push(&self.testpaths.relative_dir);
2957 mir_dump_dir.push(self.testpaths.file.file_stem().unwrap());
2961 fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2962 let parent_dir = self.testpaths.file.parent().unwrap();
2963 let cflags = self.props.compile_flags.join(" ");
2964 let json = cflags.contains("--error-format json")
2965 || cflags.contains("--error-format pretty-json")
2966 || cflags.contains("--error-format=json")
2967 || cflags.contains("--error-format=pretty-json");
2968 let parent_dir_str = if json {
2969 parent_dir.display().to_string().replace("\\", "\\\\")
2971 parent_dir.display().to_string()
2974 let mut normalized = output.replace(&parent_dir_str, "$DIR");
2976 // Paths into the libstd/libcore
2977 let src_dir = self.config.src_base.parent().unwrap().parent().unwrap();
2978 let src_dir_str = if json {
2979 src_dir.display().to_string().replace("\\", "\\\\")
2981 src_dir.display().to_string()
2983 normalized = normalized.replace(&src_dir_str, "$SRC_DIR");
2985 // Paths into the build directory
2986 let test_build_dir = &self.config.build_base;
2987 let parent_build_dir = test_build_dir.parent().unwrap().parent().unwrap().parent().unwrap();
2989 // eg. /home/user/rust/build/x86_64-unknown-linux-gnu/test/ui
2990 normalized = normalized.replace(test_build_dir.to_str().unwrap(), "$TEST_BUILD_DIR");
2991 // eg. /home/user/rust/build
2992 normalized = normalized.replace(&parent_build_dir.to_str().unwrap(), "$BUILD_DIR");
2994 // Paths into lib directory.
2995 let mut lib_dir = parent_build_dir.parent().unwrap().to_path_buf();
2996 lib_dir.push("lib");
2997 normalized = normalized.replace(&lib_dir.to_str().unwrap(), "$LIB_DIR");
3000 // escaped newlines in json strings should be readable
3001 // in the stderr files. There's no point int being correct,
3002 // since only humans process the stderr files.
3003 // Thus we just turn escaped newlines back into newlines.
3004 normalized = normalized.replace("\\n", "\n");
3007 // If there are `$SRC_DIR` normalizations with line and column numbers, then replace them
3008 // with placeholders as we do not want tests needing updated when compiler source code
3010 // eg. $SRC_DIR/libcore/mem.rs:323:14 becomes $SRC_DIR/libcore/mem.rs:LL:COL
3011 normalized = Regex::new("SRC_DIR(.+):\\d+:\\d+").unwrap()
3012 .replace_all(&normalized, "SRC_DIR$1:LL:COL").into_owned();
3014 normalized = normalized.replace("\\\\", "\\") // denormalize for paths on windows
3015 .replace("\\", "/") // normalize for paths on windows
3016 .replace("\r\n", "\n") // normalize for linebreaks on windows
3017 .replace("\t", "\\t"); // makes tabs visible
3018 for rule in custom_rules {
3019 let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
3020 normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
3025 fn expected_output_path(&self, kind: &str) -> PathBuf {
3026 let mut path = expected_output_path(
3029 &self.config.compare_mode,
3034 if let Some(CompareMode::Polonius) = self.config.compare_mode {
3035 path = expected_output_path(
3038 &Some(CompareMode::Nll),
3045 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
3051 fn load_expected_output(&self, kind: &str) -> String {
3052 let path = self.expected_output_path(kind);
3054 match self.load_expected_output_from_path(&path) {
3056 Err(x) => self.fatal(&x),
3063 fn load_expected_output_from_path(&self, path: &Path) -> Result<String, String> {
3064 let mut result = String::new();
3065 match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
3066 Ok(_) => Ok(result),
3067 Err(e) => Err(format!(
3068 "failed to load expected output from `{}`: {}",
3075 fn delete_file(&self, file: &PathBuf) {
3076 if let Err(e) = ::std::fs::remove_file(file) {
3077 self.fatal(&format!(
3078 "failed to delete `{}`: {}",
3085 fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
3086 if actual == expected {
3090 if !self.config.bless {
3091 if expected.is_empty() {
3092 println!("normalized {}:\n{}\n", kind, actual);
3094 println!("diff of {}:\n", kind);
3095 let diff_results = make_diff(expected, actual, 3);
3096 for result in diff_results {
3097 let mut line_number = result.line_number;
3098 for line in result.lines {
3100 DiffLine::Expected(e) => {
3101 println!("-\t{}", e);
3104 DiffLine::Context(c) => {
3105 println!("{}\t{}", line_number, c);
3108 DiffLine::Resulting(r) => {
3109 println!("+\t{}", r);
3118 let mode = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
3119 let output_file = self
3121 .with_extra_extension(self.revision.unwrap_or(""))
3122 .with_extra_extension(mode)
3123 .with_extra_extension(kind);
3125 let mut files = vec![output_file];
3126 if self.config.bless {
3127 files.push(expected_output_path(
3130 &self.config.compare_mode,
3135 for output_file in &files {
3136 if actual.is_empty() {
3137 self.delete_file(output_file);
3139 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
3141 Err(e) => self.fatal(&format!(
3142 "failed to write {} to `{}`: {}",
3144 output_file.display(),
3151 println!("\nThe actual {0} differed from the expected {0}.", kind);
3152 for output_file in files {
3153 println!("Actual {} saved to {}", kind, output_file.display());
3155 if self.config.bless {
3162 fn prune_duplicate_output(&self, mode: CompareMode, kind: &str, canon_content: &str) {
3163 let examined_path = expected_output_path(
3170 let examined_content = self
3171 .load_expected_output_from_path(&examined_path)
3172 .unwrap_or_else(|_| String::new());
3174 if examined_path.exists() && canon_content == &examined_content {
3175 self.delete_file(&examined_path);
3179 fn prune_duplicate_outputs(&self, modes: &[CompareMode]) {
3180 if self.config.bless {
3181 for kind in UI_EXTENSIONS {
3182 let canon_comparison_path = expected_output_path(
3189 if let Ok(canon) = self.load_expected_output_from_path(&canon_comparison_path) {
3191 self.prune_duplicate_output(mode.clone(), kind, &canon);
3198 fn create_stamp(&self) {
3199 let mut f = File::create(::stamp(&self.config, self.testpaths, self.revision)).unwrap();
3200 f.write_all(compute_stamp_hash(&self.config).as_bytes())
3210 pub struct ProcRes {
3218 pub fn fatal(&self, err: Option<&str>) -> ! {
3219 if let Some(e) = err {
3220 println!("\nerror: {}", e);
3227 ------------------------------------------\n\
3229 ------------------------------------------\n\
3231 ------------------------------------------\n\
3233 ------------------------------------------\n\
3235 self.status, self.cmdline, self.stdout, self.stderr
3241 enum TargetLocation {
3243 ThisDirectory(PathBuf),
3246 #[derive(Clone, PartialEq, Eq)]
3247 enum ExpectedLine<T: AsRef<str>> {
3252 impl<T> fmt::Debug for ExpectedLine<T>
3254 T: AsRef<str> + fmt::Debug,
3256 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
3257 if let &ExpectedLine::Text(ref t) = self {
3258 write!(formatter, "{:?}", t)
3260 write!(formatter, "\"...\" (Elision)")
3265 fn normalize_mir_line(line: &str) -> String {
3266 nocomment_mir_line(line).replace(char::is_whitespace, "")
3269 fn nocomment_mir_line(line: &str) -> &str {
3270 if let Some(idx) = line.find("//") {
3271 let (l, _) = line.split_at(idx);
3278 fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
3280 use std::mem::replace;
3282 const HEAD_LEN: usize = 160 * 1024;
3283 const TAIL_LEN: usize = 256 * 1024;
3295 fn extend(&mut self, data: &[u8]) {
3296 let new_self = match *self {
3297 ProcOutput::Full(ref mut bytes) => {
3298 bytes.extend_from_slice(data);
3299 let new_len = bytes.len();
3300 if new_len <= HEAD_LEN + TAIL_LEN {
3303 let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice();
3304 let head = replace(bytes, Vec::new());
3305 let skipped = new_len - HEAD_LEN - TAIL_LEN;
3306 ProcOutput::Abbreviated {
3312 ProcOutput::Abbreviated {
3317 *skipped += data.len();
3318 if data.len() <= TAIL_LEN {
3319 tail[..data.len()].copy_from_slice(data);
3320 tail.rotate_left(data.len());
3322 tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
3330 fn into_bytes(self) -> Vec<u8> {
3332 ProcOutput::Full(bytes) => bytes,
3333 ProcOutput::Abbreviated {
3338 write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
3339 head.extend_from_slice(&tail);
3346 let mut stdout = ProcOutput::Full(Vec::new());
3347 let mut stderr = ProcOutput::Full(Vec::new());
3349 drop(child.stdin.take());
3351 child.stdout.take().unwrap(),
3352 child.stderr.take().unwrap(),
3353 &mut |is_stdout, data, _| {
3354 if is_stdout { &mut stdout } else { &mut stderr }.extend(data);
3358 let status = child.wait()?;
3362 stdout: stdout.into_bytes(),
3363 stderr: stderr.into_bytes(),