1use std::collections::HashSet;
2use std::process::Command;
3use std::{env, fs};
4
5use camino::{Utf8Path, Utf8PathBuf};
6use semver::Version;
7use tracing::*;
8
9use crate::common::{CodegenBackend, Config, Debugger, FailMode, PassMode, RunFailMode, TestMode};
10use crate::debuggers::{extract_cdb_version, extract_gdb_version};
11pub(crate) use crate::directives::auxiliary::AuxProps;
12use crate::directives::auxiliary::parse_and_update_aux;
13use crate::directives::directive_names::{
14 KNOWN_DIRECTIVE_NAMES_SET, KNOWN_HTMLDOCCK_DIRECTIVE_NAMES, KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
15};
16pub(crate) use crate::directives::file::FileDirectives;
17use crate::directives::handlers::DIRECTIVE_HANDLERS_MAP;
18use crate::directives::line::DirectiveLine;
19use crate::directives::needs::CachedNeedsConditions;
20use crate::edition::{Edition, parse_edition};
21use crate::errors::ErrorKind;
22use crate::executor::{CollectedTestDesc, ShouldFail};
23use crate::util::static_regex;
24use crate::{fatal, help};
25
26mod auxiliary;
27mod cfg;
28mod directive_names;
29mod file;
30mod handlers;
31mod line;
32pub(crate) use line::line_directive;
33mod line_number;
34pub(crate) use line_number::LineNumber;
35mod needs;
36#[cfg(test)]
37mod tests;
38
39pub struct DirectivesCache {
40 cfg_conditions: cfg::PreparedConditions,
43 needs: CachedNeedsConditions,
44}
45
46impl DirectivesCache {
47 pub fn load(config: &Config) -> Self {
48 Self {
49 cfg_conditions: cfg::prepare_conditions(config),
50 needs: CachedNeedsConditions::load(config),
51 }
52 }
53}
54
55#[derive(Default)]
58pub(crate) struct EarlyProps {
59 pub(crate) revisions: Vec<String>,
60}
61
62impl EarlyProps {
63 pub(crate) fn from_file_directives(
64 config: &Config,
65 file_directives: &FileDirectives<'_>,
66 ) -> Self {
67 let mut props = EarlyProps::default();
68
69 iter_directives(
70 config.mode,
71 file_directives,
72 &mut |ln: &DirectiveLine<'_>| {
74 config.parse_and_update_revisions(ln, &mut props.revisions);
75 },
76 );
77
78 props
79 }
80}
81
82#[derive(Clone, Debug)]
83pub(crate) struct TestProps {
84 pub error_patterns: Vec<String>,
86 pub regex_error_patterns: Vec<String>,
88 pub edition: Option<Edition>,
92 pub compile_flags: Vec<String>,
94 pub run_flags: Vec<String>,
96 pub doc_flags: Vec<String>,
98 pub pp_exact: Option<Utf8PathBuf>,
101 pub(crate) aux: AuxProps,
103 pub rustc_env: Vec<(String, String)>,
105 pub unset_rustc_env: Vec<String>,
108 pub exec_env: Vec<(String, String)>,
110 pub unset_exec_env: Vec<String>,
113 pub build_aux_docs: bool,
115 pub unique_doc_out_dir: bool,
118 pub force_host: bool,
120 pub check_stdout: bool,
122 pub check_run_results: bool,
124 pub dont_check_compiler_stdout: bool,
126 pub dont_check_compiler_stderr: bool,
128 pub no_prefer_dynamic: bool,
134 pub pretty_mode: String,
136 pub pretty_compare_only: bool,
138 pub forbid_output: Vec<String>,
140 pub revisions: Vec<String>,
142 pub incremental_dir: Option<Utf8PathBuf>,
147 pub incremental: bool,
162 pub known_bug: bool,
168 pass_mode: Option<PassMode>,
170 ignore_pass: bool,
172 pub fail_mode: Option<FailMode>,
174 pub check_test_line_numbers_match: bool,
176 pub normalize_stdout: Vec<(String, String)>,
178 pub normalize_stderr: Vec<(String, String)>,
179 pub failure_status: Option<i32>,
180 pub dont_check_failure_status: bool,
182 pub run_rustfix: bool,
185 pub rustfix_only_machine_applicable: bool,
187 pub assembly_output: Option<String>,
188 pub should_ice: bool,
190 pub stderr_per_bitwidth: bool,
192 pub mir_unit_test: Option<String>,
194 pub remap_src_base: bool,
197 pub llvm_cov_flags: Vec<String>,
200 pub filecheck_flags: Vec<String>,
202 pub no_auto_check_cfg: bool,
204 pub add_minicore: bool,
207 pub minicore_compile_flags: Vec<String>,
209 pub dont_require_annotations: HashSet<ErrorKind>,
211 pub disable_gdb_pretty_printers: bool,
213 pub compare_output_by_lines: bool,
215}
216
217mod directives {
218 pub const ERROR_PATTERN: &'static str = "error-pattern";
219 pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
220 pub const COMPILE_FLAGS: &'static str = "compile-flags";
221 pub const RUN_FLAGS: &'static str = "run-flags";
222 pub const DOC_FLAGS: &'static str = "doc-flags";
223 pub const SHOULD_ICE: &'static str = "should-ice";
224 pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
225 pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
226 pub const FORCE_HOST: &'static str = "force-host";
227 pub const CHECK_STDOUT: &'static str = "check-stdout";
228 pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
229 pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
230 pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
231 pub const DONT_REQUIRE_ANNOTATIONS: &'static str = "dont-require-annotations";
232 pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
233 pub const PRETTY_MODE: &'static str = "pretty-mode";
234 pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
235 pub const AUX_BIN: &'static str = "aux-bin";
236 pub const AUX_BUILD: &'static str = "aux-build";
237 pub const AUX_CRATE: &'static str = "aux-crate";
238 pub const PROC_MACRO: &'static str = "proc-macro";
239 pub const AUX_CODEGEN_BACKEND: &'static str = "aux-codegen-backend";
240 pub const EXEC_ENV: &'static str = "exec-env";
241 pub const RUSTC_ENV: &'static str = "rustc-env";
242 pub const UNSET_EXEC_ENV: &'static str = "unset-exec-env";
243 pub const UNSET_RUSTC_ENV: &'static str = "unset-rustc-env";
244 pub const FORBID_OUTPUT: &'static str = "forbid-output";
245 pub const CHECK_TEST_LINE_NUMBERS_MATCH: &'static str = "check-test-line-numbers-match";
246 pub const IGNORE_PASS: &'static str = "ignore-pass";
247 pub const FAILURE_STATUS: &'static str = "failure-status";
248 pub const DONT_CHECK_FAILURE_STATUS: &'static str = "dont-check-failure-status";
249 pub const RUN_RUSTFIX: &'static str = "run-rustfix";
250 pub const RUSTFIX_ONLY_MACHINE_APPLICABLE: &'static str = "rustfix-only-machine-applicable";
251 pub const ASSEMBLY_OUTPUT: &'static str = "assembly-output";
252 pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
253 pub const INCREMENTAL: &'static str = "incremental";
254 pub const KNOWN_BUG: &'static str = "known-bug";
255 pub const TEST_MIR_PASS: &'static str = "test-mir-pass";
256 pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
257 pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags";
258 pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
259 pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
260 pub const ADD_MINICORE: &'static str = "add-minicore";
261 pub const MINICORE_COMPILE_FLAGS: &'static str = "minicore-compile-flags";
262 pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
263 pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
264}
265
266impl TestProps {
267 pub fn new() -> Self {
268 TestProps {
269 error_patterns: vec![],
270 regex_error_patterns: vec![],
271 edition: None,
272 compile_flags: vec![],
273 run_flags: vec![],
274 doc_flags: vec![],
275 pp_exact: None,
276 aux: Default::default(),
277 revisions: vec![],
278 rustc_env: vec![
279 ("RUSTC_ICE".to_string(), "0".to_string()),
280 ("RUST_BACKTRACE".to_string(), "short".to_string()),
281 ],
282 unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
283 exec_env: vec![],
284 unset_exec_env: vec![],
285 build_aux_docs: false,
286 unique_doc_out_dir: false,
287 force_host: false,
288 check_stdout: false,
289 check_run_results: false,
290 dont_check_compiler_stdout: false,
291 dont_check_compiler_stderr: false,
292 no_prefer_dynamic: false,
293 pretty_mode: "normal".to_string(),
294 pretty_compare_only: false,
295 forbid_output: vec![],
296 incremental_dir: None,
297 incremental: false,
298 known_bug: false,
299 pass_mode: None,
300 fail_mode: None,
301 ignore_pass: false,
302 check_test_line_numbers_match: false,
303 normalize_stdout: vec![],
304 normalize_stderr: vec![],
305 failure_status: None,
306 dont_check_failure_status: false,
307 run_rustfix: false,
308 rustfix_only_machine_applicable: false,
309 assembly_output: None,
310 should_ice: false,
311 stderr_per_bitwidth: false,
312 mir_unit_test: None,
313 remap_src_base: false,
314 llvm_cov_flags: vec![],
315 filecheck_flags: vec![],
316 no_auto_check_cfg: false,
317 add_minicore: false,
318 minicore_compile_flags: vec![],
319 dont_require_annotations: Default::default(),
320 disable_gdb_pretty_printers: false,
321 compare_output_by_lines: false,
322 }
323 }
324
325 pub fn from_aux_file(
326 &self,
327 testfile: &Utf8Path,
328 revision: Option<&str>,
329 config: &Config,
330 ) -> Self {
331 let mut props = TestProps::new();
332
333 props.incremental_dir = self.incremental_dir.clone();
335 props.ignore_pass = true;
336 props.load_from(testfile, revision, config);
337
338 props
339 }
340
341 pub fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
342 let mut props = TestProps::new();
343 props.load_from(testfile, revision, config);
344 props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
345
346 match (props.pass_mode, props.fail_mode) {
347 (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
348 (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
349 _ => {}
350 }
351
352 props
353 }
354
355 fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
360 if !testfile.is_dir() {
361 let file_contents = fs::read_to_string(testfile).unwrap();
362 let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
363
364 iter_directives(
365 config.mode,
366 &file_directives,
367 &mut |ln: &DirectiveLine<'_>| {
369 if !ln.applies_to_test_revision(test_revision) {
370 return;
371 }
372
373 if let Some(handler) = DIRECTIVE_HANDLERS_MAP.get(ln.name) {
374 handler.handle(config, ln, self);
375 }
376 },
377 );
378 }
379
380 if self.should_ice {
381 self.failure_status = Some(101);
382 }
383
384 if config.mode == TestMode::Incremental {
385 self.incremental = true;
386 }
387
388 if config.mode == TestMode::Crashes {
389 self.rustc_env = vec![
393 ("RUST_BACKTRACE".to_string(), "0".to_string()),
394 ("RUSTC_ICE".to_string(), "0".to_string()),
395 ];
396 }
397
398 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
399 if let Ok(val) = env::var(key) {
400 if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
401 self.exec_env.push(((*key).to_owned(), val))
402 }
403 }
404 }
405
406 if let Some(edition) = self.edition.or(config.edition) {
407 self.compile_flags.insert(0, format!("--edition={edition}"));
410 }
411 }
412
413 fn update_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
414 let check_ui = |mode: &str| {
415 if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
417 panic!("`{}-fail` directive is only supported in UI tests", mode);
418 }
419 };
420 let fail_mode = if config.parse_name_directive(ln, "check-fail") {
421 check_ui("check");
422 Some(FailMode::Check)
423 } else if config.parse_name_directive(ln, "build-fail") {
424 check_ui("build");
425 Some(FailMode::Build)
426 } else if config.parse_name_directive(ln, "run-fail") {
427 check_ui("run");
428 Some(FailMode::Run(RunFailMode::Fail))
429 } else if config.parse_name_directive(ln, "run-crash") {
430 check_ui("run");
431 Some(FailMode::Run(RunFailMode::Crash))
432 } else if config.parse_name_directive(ln, "run-fail-or-crash") {
433 check_ui("run");
434 Some(FailMode::Run(RunFailMode::FailOrCrash))
435 } else {
436 None
437 };
438 match (self.fail_mode, fail_mode) {
439 (None, Some(_)) => self.fail_mode = fail_mode,
440 (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
441 (_, None) => {}
442 }
443 }
444
445 fn update_pass_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
446 let check_no_run = |s| match (config.mode, s) {
447 (TestMode::Ui, _) => (),
448 (TestMode::Crashes, _) => (),
449 (TestMode::Codegen, "build-pass") => (),
450 (TestMode::Incremental, _) => {
451 if self.revisions.iter().any(|r| !r.starts_with("cfail")) {
454 panic!("`{s}` directive is only supported in `cfail` incremental tests")
455 }
456 }
457 (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
458 };
459 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
460 check_no_run("check-pass");
461 Some(PassMode::Check)
462 } else if config.parse_name_directive(ln, "build-pass") {
463 check_no_run("build-pass");
464 Some(PassMode::Build)
465 } else if config.parse_name_directive(ln, "run-pass") {
466 check_no_run("run-pass");
467 Some(PassMode::Run)
468 } else {
469 None
470 };
471 match (self.pass_mode, pass_mode) {
472 (None, Some(_)) => self.pass_mode = pass_mode,
473 (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
474 (_, None) => {}
475 }
476 }
477
478 pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
479 if !self.ignore_pass && self.fail_mode.is_none() {
480 if let mode @ Some(_) = config.force_pass_mode {
481 return mode;
482 }
483 }
484 self.pass_mode
485 }
486
487 pub fn local_pass_mode(&self) -> Option<PassMode> {
489 self.pass_mode
490 }
491
492 fn update_add_minicore(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
493 let add_minicore = config.parse_name_directive(ln, directives::ADD_MINICORE);
494 if add_minicore {
495 if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
496 panic!(
497 "`add-minicore` is currently only supported for ui, codegen and assembly test modes"
498 );
499 }
500
501 if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
504 panic!("`add-minicore` cannot be used to run the test binary");
507 }
508
509 self.add_minicore = add_minicore;
510 }
511 }
512}
513
514pub(crate) fn do_early_directives_check(
515 mode: TestMode,
516 file_directives: &FileDirectives<'_>,
517) -> Result<(), String> {
518 let testfile = file_directives.path;
519
520 for directive_line @ DirectiveLine { line_number, .. } in &file_directives.lines {
521 let CheckDirectiveResult { is_known_directive, trailing_directive } =
522 check_directive(directive_line, mode);
523
524 if !is_known_directive {
525 return Err(format!(
526 "ERROR: unknown compiletest directive `{directive}` at {testfile}:{line_number}",
527 directive = directive_line.display(),
528 ));
529 }
530
531 if let Some(trailing_directive) = &trailing_directive {
532 return Err(format!(
533 "ERROR: detected trailing compiletest directive `{trailing_directive}` at {testfile}:{line_number}\n\
534 HELP: put the directive on its own line: `//@ {trailing_directive}`"
535 ));
536 }
537 }
538
539 Ok(())
540}
541
542pub(crate) struct CheckDirectiveResult<'ln> {
543 is_known_directive: bool,
544 trailing_directive: Option<&'ln str>,
545}
546
547fn check_directive<'a>(
548 directive_ln: &DirectiveLine<'a>,
549 mode: TestMode,
550) -> CheckDirectiveResult<'a> {
551 let &DirectiveLine { name: directive_name, .. } = directive_ln;
552
553 let is_known_directive = KNOWN_DIRECTIVE_NAMES_SET.contains(&directive_name)
554 || match mode {
555 TestMode::RustdocHtml => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
556 TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
557 _ => false,
558 };
559
560 let trailing_directive = directive_ln
564 .remark_after_space()
565 .map(|remark| remark.trim_start().split(' ').next().unwrap())
566 .filter(|token| KNOWN_DIRECTIVE_NAMES_SET.contains(token));
567
568 CheckDirectiveResult { is_known_directive, trailing_directive }
574}
575
576fn iter_directives(
577 mode: TestMode,
578 file_directives: &FileDirectives<'_>,
579 it: &mut dyn FnMut(&DirectiveLine<'_>),
580) {
581 let testfile = file_directives.path;
582
583 if mode == TestMode::CoverageRun {
588 let extra_directives: &[&str] = &[
589 "//@ needs-profiler-runtime",
590 "//@ ignore-cross-compile",
594 ];
595 for directive_str in extra_directives {
597 let directive_line = line_directive(testfile, LineNumber::ZERO, directive_str)
598 .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
599 it(&directive_line);
600 }
601 }
602
603 for directive_line in &file_directives.lines {
604 it(directive_line);
605 }
606}
607
608impl Config {
609 fn parse_and_update_revisions(&self, line: &DirectiveLine<'_>, existing: &mut Vec<String>) {
610 const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
611 "true", "false",
615 ];
616
617 const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
618 ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
619
620 if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
621 let &DirectiveLine { file_path: testfile, .. } = line;
622
623 if self.mode == TestMode::RunMake {
624 panic!("`run-make` mode tests do not support revisions: {}", testfile);
625 }
626
627 let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
628 for revision in raw.split_whitespace() {
629 if !duplicates.insert(revision.to_string()) {
630 panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
631 }
632
633 if FORBIDDEN_REVISION_NAMES.contains(&revision) {
634 panic!(
635 "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
636 revision, raw, testfile
637 );
638 }
639
640 if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
641 && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
642 {
643 panic!(
644 "revision name `{revision}` is not permitted in a test suite that uses \
645 `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
646 prefix: `{revision}` in line `{}`: {}",
647 raw, testfile
648 );
649 }
650
651 existing.push(revision.to_string());
652 }
653 }
654 }
655
656 fn parse_env(nv: String) -> (String, String) {
657 let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
661 let name = name.trim();
664 (name.to_owned(), value.to_owned())
665 }
666
667 fn parse_pp_exact(&self, line: &DirectiveLine<'_>) -> Option<Utf8PathBuf> {
668 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
669 Some(Utf8PathBuf::from(&s))
670 } else if self.parse_name_directive(line, "pp-exact") {
671 line.file_path.file_name().map(Utf8PathBuf::from)
672 } else {
673 None
674 }
675 }
676
677 fn parse_custom_normalization(&self, line: &DirectiveLine<'_>) -> Option<NormalizeRule> {
678 let &DirectiveLine { name, .. } = line;
679
680 let kind = match name {
681 "normalize-stdout" => NormalizeKind::Stdout,
682 "normalize-stderr" => NormalizeKind::Stderr,
683 "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
684 "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
685 _ => return None,
686 };
687
688 let Some((regex, replacement)) = line.value_after_colon().and_then(parse_normalize_rule)
689 else {
690 error!("couldn't parse custom normalization rule: `{}`", line.display());
691 help!("expected syntax is: `{name}: \"REGEX\" -> \"REPLACEMENT\"`");
692 panic!("invalid normalization rule detected");
693 };
694 Some(NormalizeRule { kind, regex, replacement })
695 }
696
697 fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool {
698 line.name == directive
702 }
703
704 fn parse_name_value_directive(
705 &self,
706 line: &DirectiveLine<'_>,
707 directive: &str,
708 ) -> Option<String> {
709 let &DirectiveLine { file_path, line_number, .. } = line;
710
711 if line.name != directive {
712 return None;
713 };
714
715 let value = line.value_after_colon()?;
719 debug!("{}: {}", directive, value);
720 let value = expand_variables(value.to_owned(), self);
721
722 if value.is_empty() {
723 error!("{file_path}:{line_number}: empty value for directive `{directive}`");
724 help!("expected syntax is: `{directive}: value`");
725 panic!("empty directive value detected");
726 }
727
728 Some(value)
729 }
730
731 fn set_name_directive(&self, line: &DirectiveLine<'_>, directive: &str, value: &mut bool) {
732 *value = *value || self.parse_name_directive(line, directive);
734 }
735
736 fn set_name_value_directive<T>(
737 &self,
738 line: &DirectiveLine<'_>,
739 directive: &str,
740 value: &mut Option<T>,
741 parse: impl FnOnce(String) -> T,
742 ) {
743 if value.is_none() {
744 *value = self.parse_name_value_directive(line, directive).map(parse);
745 }
746 }
747
748 fn push_name_value_directive<T>(
749 &self,
750 line: &DirectiveLine<'_>,
751 directive: &str,
752 values: &mut Vec<T>,
753 parse: impl FnOnce(String) -> T,
754 ) {
755 if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
756 values.push(value);
757 }
758 }
759}
760
761fn expand_variables(mut value: String, config: &Config) -> String {
763 const CWD: &str = "{{cwd}}";
764 const SRC_BASE: &str = "{{src-base}}";
765 const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
766 const RUST_SRC_BASE: &str = "{{rust-src-base}}";
767 const SYSROOT_BASE: &str = "{{sysroot-base}}";
768 const TARGET_LINKER: &str = "{{target-linker}}";
769 const TARGET: &str = "{{target}}";
770
771 if value.contains(CWD) {
772 let cwd = env::current_dir().unwrap();
773 value = value.replace(CWD, &cwd.to_str().unwrap());
774 }
775
776 if value.contains(SRC_BASE) {
777 value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
778 }
779
780 if value.contains(TEST_SUITE_BUILD_BASE) {
781 value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
782 }
783
784 if value.contains(SYSROOT_BASE) {
785 value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
786 }
787
788 if value.contains(TARGET_LINKER) {
789 value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
790 }
791
792 if value.contains(TARGET) {
793 value = value.replace(TARGET, &config.target);
794 }
795
796 if value.contains(RUST_SRC_BASE) {
797 let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
798 src_base.try_exists().expect(&*format!("{} should exists", src_base));
799 let src_base = src_base.read_link_utf8().unwrap_or(src_base);
800 value = value.replace(RUST_SRC_BASE, &src_base.as_str());
801 }
802
803 value
804}
805
806struct NormalizeRule {
807 kind: NormalizeKind,
808 regex: String,
809 replacement: String,
810}
811
812enum NormalizeKind {
813 Stdout,
814 Stderr,
815 Stderr32bit,
816 Stderr64bit,
817}
818
819fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
824 let captures = static_regex!(
826 r#"(?x) # (verbose mode regex)
827 ^
828 \s* # (leading whitespace)
829 "(?<regex>[^"]*)" # "REGEX"
830 \s+->\s+ # ->
831 "(?<replacement>[^"]*)" # "REPLACEMENT"
832 $
833 "#
834 )
835 .captures(raw_value)?;
836 let regex = captures["regex"].to_owned();
837 let replacement = captures["replacement"].to_owned();
838 let replacement = replacement.replace("\\n", "\n");
842 Some((regex, replacement))
843}
844
845pub fn extract_llvm_version(version: &str) -> Version {
855 let version = version.trim();
858 let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
859 let version_without_suffix = match version.split_once(uninterested) {
860 Some((prefix, _suffix)) => prefix,
861 None => version,
862 };
863
864 let components: Vec<u64> = version_without_suffix
865 .split('.')
866 .map(|s| s.parse().expect("llvm version component should consist of only digits"))
867 .collect();
868
869 match &components[..] {
870 [major] => Version::new(*major, 0, 0),
871 [major, minor] => Version::new(*major, *minor, 0),
872 [major, minor, patch] => Version::new(*major, *minor, *patch),
873 _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
874 }
875}
876
877pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
878 let output = Command::new(binary_path).arg("--version").output().ok()?;
879 if !output.status.success() {
880 return None;
881 }
882 let version = String::from_utf8(output.stdout).ok()?;
883 for line in version.lines() {
884 if let Some(version) = line.split("LLVM version ").nth(1) {
885 return Some(extract_llvm_version(version));
886 }
887 }
888 None
889}
890
891fn extract_version_range<'a, F, VersionTy: Clone>(
897 line: &'a str,
898 parse: F,
899) -> Option<(VersionTy, VersionTy)>
900where
901 F: Fn(&'a str) -> Option<VersionTy>,
902{
903 let mut splits = line.splitn(2, "- ").map(str::trim);
904 let min = splits.next().unwrap();
905 if min.ends_with('-') {
906 return None;
907 }
908
909 let max = splits.next();
910
911 if min.is_empty() {
912 return None;
913 }
914
915 let min = parse(min)?;
916 let max = match max {
917 Some("") => return None,
918 Some(max) => parse(max)?,
919 _ => min.clone(),
920 };
921
922 Some((min, max))
923}
924
925pub(crate) fn make_test_description(
926 config: &Config,
927 cache: &DirectivesCache,
928 name: String,
929 path: &Utf8Path,
930 filterable_path: &Utf8Path,
931 file_directives: &FileDirectives<'_>,
932 test_revision: Option<&str>,
933 poisoned: &mut bool,
934 aux_props: &mut AuxProps,
935) -> CollectedTestDesc {
936 let mut ignore = false;
937 let mut ignore_message = None;
938 let mut should_fail = false;
939
940 iter_directives(
942 config.mode,
943 file_directives,
944 &mut |ln @ &DirectiveLine { line_number, .. }| {
945 if !ln.applies_to_test_revision(test_revision) {
946 return;
947 }
948
949 parse_and_update_aux(config, ln, aux_props);
951
952 macro_rules! decision {
953 ($e:expr) => {
954 match $e {
955 IgnoreDecision::Ignore { reason } => {
956 ignore = true;
957 ignore_message = Some(reason.into());
958 }
959 IgnoreDecision::Error { message } => {
960 error!("{path}:{line_number}: {message}");
961 *poisoned = true;
962 return;
963 }
964 IgnoreDecision::Continue => {}
965 }
966 };
967 }
968
969 decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
970 decision!(cfg::handle_only(&cache.cfg_conditions, ln));
971 decision!(needs::handle_needs(&cache.needs, config, ln));
972 decision!(ignore_llvm(config, ln));
973 decision!(ignore_backends(config, ln));
974 decision!(needs_backends(config, ln));
975 decision!(ignore_cdb(config, ln));
976 decision!(ignore_gdb(config, ln));
977 decision!(ignore_lldb(config, ln));
978
979 if config.target == "wasm32-unknown-unknown"
980 && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
981 {
982 decision!(IgnoreDecision::Ignore {
983 reason: "ignored on WASM as the run results cannot be checked there".into(),
984 });
985 }
986
987 should_fail |= config.parse_name_directive(ln, "should-fail");
988 },
989 );
990
991 let should_fail = if should_fail && config.mode != TestMode::Pretty {
995 ShouldFail::Yes
996 } else {
997 ShouldFail::No
998 };
999
1000 CollectedTestDesc {
1001 name,
1002 filterable_path: filterable_path.to_owned(),
1003 ignore,
1004 ignore_message,
1005 should_fail,
1006 }
1007}
1008
1009fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1010 if config.debugger != Some(Debugger::Cdb) {
1011 return IgnoreDecision::Continue;
1012 }
1013
1014 if let Some(actual_version) = config.cdb_version {
1015 if line.name == "min-cdb-version"
1016 && let Some(rest) = line.value_after_colon().map(str::trim)
1017 {
1018 let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1019 panic!("couldn't parse version range: {:?}", rest);
1020 });
1021
1022 if actual_version < min_version {
1025 return IgnoreDecision::Ignore {
1026 reason: format!("ignored when the CDB version is lower than {rest}"),
1027 };
1028 }
1029 }
1030 }
1031 IgnoreDecision::Continue
1032}
1033
1034fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1035 if config.debugger != Some(Debugger::Gdb) {
1036 return IgnoreDecision::Continue;
1037 }
1038
1039 if let Some(actual_version) = config.gdb_version {
1040 if line.name == "min-gdb-version"
1041 && let Some(rest) = line.value_after_colon().map(str::trim)
1042 {
1043 let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1044 .unwrap_or_else(|| {
1045 panic!("couldn't parse version range: {:?}", rest);
1046 });
1047
1048 if start_ver != end_ver {
1049 panic!("Expected single GDB version")
1050 }
1051 if actual_version < start_ver {
1054 return IgnoreDecision::Ignore {
1055 reason: format!("ignored when the GDB version is lower than {rest}"),
1056 };
1057 }
1058 } else if line.name == "ignore-gdb-version"
1059 && let Some(rest) = line.value_after_colon().map(str::trim)
1060 {
1061 let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1062 .unwrap_or_else(|| {
1063 panic!("couldn't parse version range: {:?}", rest);
1064 });
1065
1066 if max_version < min_version {
1067 panic!("Malformed GDB version range: max < min")
1068 }
1069
1070 if actual_version >= min_version && actual_version <= max_version {
1071 if min_version == max_version {
1072 return IgnoreDecision::Ignore {
1073 reason: format!("ignored when the GDB version is {rest}"),
1074 };
1075 } else {
1076 return IgnoreDecision::Ignore {
1077 reason: format!("ignored when the GDB version is between {rest}"),
1078 };
1079 }
1080 }
1081 }
1082 }
1083 IgnoreDecision::Continue
1084}
1085
1086fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1087 if config.debugger != Some(Debugger::Lldb) {
1088 return IgnoreDecision::Continue;
1089 }
1090
1091 if let Some(actual_version) = config.lldb_version {
1092 if line.name == "min-lldb-version"
1093 && let Some(rest) = line.value_after_colon().map(str::trim)
1094 {
1095 let min_version = rest.parse().unwrap_or_else(|e| {
1096 panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1097 });
1098 if actual_version < min_version {
1101 return IgnoreDecision::Ignore {
1102 reason: format!("ignored when the LLDB version is {rest}"),
1103 };
1104 }
1105 }
1106 }
1107 IgnoreDecision::Continue
1108}
1109
1110fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1111 let path = line.file_path;
1112 if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1113 for backend in backends_to_ignore.split_whitespace().map(|backend| {
1114 match CodegenBackend::try_from(backend) {
1115 Ok(backend) => backend,
1116 Err(error) => {
1117 panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1118 }
1119 }
1120 }) {
1121 if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1122 return IgnoreDecision::Ignore {
1123 reason: format!("{} backend is marked as ignore", backend.as_str()),
1124 };
1125 }
1126 }
1127 }
1128 IgnoreDecision::Continue
1129}
1130
1131fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1132 let path = line.file_path;
1133 if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1134 if !needed_backends
1135 .split_whitespace()
1136 .map(|backend| match CodegenBackend::try_from(backend) {
1137 Ok(backend) => backend,
1138 Err(error) => {
1139 panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1140 }
1141 })
1142 .any(|backend| config.default_codegen_backend == backend)
1143 {
1144 return IgnoreDecision::Ignore {
1145 reason: format!(
1146 "{} backend is not part of required backends",
1147 config.default_codegen_backend.as_str()
1148 ),
1149 };
1150 }
1151 }
1152 IgnoreDecision::Continue
1153}
1154
1155fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1156 let path = line.file_path;
1157 if let Some(needed_components) =
1158 config.parse_name_value_directive(line, "needs-llvm-components")
1159 {
1160 let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1161 if let Some(missing_component) = needed_components
1162 .split_whitespace()
1163 .find(|needed_component| !components.contains(needed_component))
1164 {
1165 if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1166 panic!(
1167 "missing LLVM component {missing_component}, \
1168 and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1169 );
1170 }
1171 return IgnoreDecision::Ignore {
1172 reason: format!("ignored when the {missing_component} LLVM component is missing"),
1173 };
1174 }
1175 }
1176 if let Some(actual_version) = &config.llvm_version {
1177 if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1180 let min_version = extract_llvm_version(&version_string);
1181 if *actual_version < min_version {
1183 return IgnoreDecision::Ignore {
1184 reason: format!(
1185 "ignored when the LLVM version {actual_version} is older than {min_version}"
1186 ),
1187 };
1188 }
1189 } else if let Some(version_string) =
1190 config.parse_name_value_directive(line, "max-llvm-major-version")
1191 {
1192 let max_version = extract_llvm_version(&version_string);
1193 if actual_version.major > max_version.major {
1195 return IgnoreDecision::Ignore {
1196 reason: format!(
1197 "ignored when the LLVM version ({actual_version}) is newer than major\
1198 version {}",
1199 max_version.major
1200 ),
1201 };
1202 }
1203 } else if let Some(version_string) =
1204 config.parse_name_value_directive(line, "min-system-llvm-version")
1205 {
1206 let min_version = extract_llvm_version(&version_string);
1207 if config.system_llvm && *actual_version < min_version {
1210 return IgnoreDecision::Ignore {
1211 reason: format!(
1212 "ignored when the system LLVM version {actual_version} is older than {min_version}"
1213 ),
1214 };
1215 }
1216 } else if let Some(version_range) =
1217 config.parse_name_value_directive(line, "ignore-llvm-version")
1218 {
1219 let (v_min, v_max) =
1221 extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1222 .unwrap_or_else(|| {
1223 panic!("couldn't parse version range: \"{version_range}\"");
1224 });
1225 if v_max < v_min {
1226 panic!("malformed LLVM version range where {v_max} < {v_min}")
1227 }
1228 if *actual_version >= v_min && *actual_version <= v_max {
1230 if v_min == v_max {
1231 return IgnoreDecision::Ignore {
1232 reason: format!("ignored when the LLVM version is {actual_version}"),
1233 };
1234 } else {
1235 return IgnoreDecision::Ignore {
1236 reason: format!(
1237 "ignored when the LLVM version is between {v_min} and {v_max}"
1238 ),
1239 };
1240 }
1241 }
1242 } else if let Some(version_string) =
1243 config.parse_name_value_directive(line, "exact-llvm-major-version")
1244 {
1245 let version = extract_llvm_version(&version_string);
1247 if actual_version.major != version.major {
1248 return IgnoreDecision::Ignore {
1249 reason: format!(
1250 "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1251 actual_version.major, version.major
1252 ),
1253 };
1254 }
1255 }
1256 }
1257 IgnoreDecision::Continue
1258}
1259
1260enum IgnoreDecision {
1261 Ignore { reason: String },
1262 Continue,
1263 Error { message: String },
1264}
1265
1266fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1267 let raw = config.parse_name_value_directive(line, "edition")?;
1268 let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1269
1270 if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1272 Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1273 (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1274 fatal!(
1275 "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1276 );
1277 }
1278 (Some(lower_bound), Some(upper_bound)) => {
1279 EditionRange::Range { lower_bound, upper_bound }
1280 }
1281 (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1282 (None, Some(_)) => {
1283 fatal!(
1284 "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1285 );
1286 }
1287 (None, None) => {
1288 fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1289 }
1290 })
1291 } else {
1292 match maybe_parse_edition(&raw) {
1293 Some(edition) => Some(EditionRange::Exact(edition)),
1294 None => {
1295 fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1296 }
1297 }
1298 }
1299}
1300
1301fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1302 input = input.trim();
1303 if input.is_empty() {
1304 return None;
1305 }
1306 Some(parse_edition(input))
1307}
1308
1309#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1310enum EditionRange {
1311 Exact(Edition),
1312 RangeFrom(Edition),
1313 Range {
1315 lower_bound: Edition,
1316 upper_bound: Edition,
1317 },
1318}
1319
1320impl EditionRange {
1321 fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1322 let min_edition = Edition::Year(2015);
1323 let requested = requested.into().unwrap_or(min_edition);
1324
1325 match *self {
1326 EditionRange::Exact(exact) => exact,
1327 EditionRange::RangeFrom(lower_bound) => {
1328 if requested >= lower_bound {
1329 requested
1330 } else {
1331 lower_bound
1332 }
1333 }
1334 EditionRange::Range { lower_bound, upper_bound } => {
1335 if requested >= lower_bound && requested < upper_bound {
1336 requested
1337 } else {
1338 lower_bound
1339 }
1340 }
1341 }
1342 }
1343}
1344
1345fn split_flags(flags: &str) -> Vec<String> {
1346 flags
1351 .split('\'')
1352 .enumerate()
1353 .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1354 .map(move |s| s.to_owned())
1355 .collect::<Vec<_>>()
1356}