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