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};
11use crate::directives::auxiliary::{AuxProps, parse_and_update_aux};
12use crate::directives::directive_names::{
13 KNOWN_DIRECTIVE_NAMES, KNOWN_HTMLDOCCK_DIRECTIVE_NAMES, KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
14};
15pub(crate) use crate::directives::file::FileDirectives;
16use crate::directives::line::{DirectiveLine, line_directive};
17use crate::directives::needs::CachedNeedsConditions;
18use crate::edition::{Edition, parse_edition};
19use crate::errors::ErrorKind;
20use crate::executor::{CollectedTestDesc, ShouldPanic};
21use crate::util::static_regex;
22use crate::{fatal, help};
23
24pub(crate) mod auxiliary;
25mod cfg;
26mod directive_names;
27mod file;
28mod line;
29mod needs;
30#[cfg(test)]
31mod tests;
32
33pub struct DirectivesCache {
34 needs: CachedNeedsConditions,
35}
36
37impl DirectivesCache {
38 pub fn load(config: &Config) -> Self {
39 Self { needs: CachedNeedsConditions::load(config) }
40 }
41}
42
43#[derive(Default)]
46pub(crate) struct EarlyProps {
47 pub(crate) aux: AuxProps,
51 pub(crate) revisions: Vec<String>,
52}
53
54impl EarlyProps {
55 pub(crate) fn from_file_directives(
56 config: &Config,
57 file_directives: &FileDirectives<'_>,
58 ) -> Self {
59 let testfile = file_directives.path;
60 let mut props = EarlyProps::default();
61 let mut poisoned = false;
62
63 iter_directives(
64 config.mode,
65 &mut poisoned,
66 file_directives,
67 &mut |ln: &DirectiveLine<'_>| {
69 parse_and_update_aux(config, ln, testfile, &mut props.aux);
70 config.parse_and_update_revisions(testfile, ln, &mut props.revisions);
71 },
72 );
73
74 if poisoned {
75 eprintln!("errors encountered during EarlyProps parsing: {}", testfile);
76 panic!("errors encountered during EarlyProps parsing");
77 }
78
79 props
80 }
81}
82
83#[derive(Clone, Debug)]
84pub struct TestProps {
85 pub error_patterns: Vec<String>,
87 pub regex_error_patterns: Vec<String>,
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_core_stubs: bool,
204 pub core_stubs_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_CORE_STUBS: &'static str = "add-core-stubs";
258 pub const CORE_STUBS_COMPILE_FLAGS: &'static str = "core-stubs-compile-flags";
259 pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
261 pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
262 pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
263}
264
265impl TestProps {
266 pub fn new() -> Self {
267 TestProps {
268 error_patterns: vec![],
269 regex_error_patterns: vec![],
270 compile_flags: vec![],
271 run_flags: vec![],
272 doc_flags: vec![],
273 pp_exact: None,
274 aux: Default::default(),
275 revisions: vec![],
276 rustc_env: vec![
277 ("RUSTC_ICE".to_string(), "0".to_string()),
278 ("RUST_BACKTRACE".to_string(), "short".to_string()),
279 ],
280 unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
281 exec_env: vec![],
282 unset_exec_env: vec![],
283 build_aux_docs: false,
284 unique_doc_out_dir: false,
285 force_host: false,
286 check_stdout: false,
287 check_run_results: false,
288 dont_check_compiler_stdout: false,
289 dont_check_compiler_stderr: false,
290 no_prefer_dynamic: false,
291 pretty_mode: "normal".to_string(),
292 pretty_compare_only: false,
293 forbid_output: vec![],
294 incremental_dir: None,
295 incremental: false,
296 known_bug: false,
297 pass_mode: None,
298 fail_mode: None,
299 ignore_pass: false,
300 check_test_line_numbers_match: false,
301 normalize_stdout: vec![],
302 normalize_stderr: vec![],
303 failure_status: None,
304 dont_check_failure_status: false,
305 run_rustfix: false,
306 rustfix_only_machine_applicable: false,
307 assembly_output: None,
308 should_ice: false,
309 stderr_per_bitwidth: false,
310 mir_unit_test: None,
311 remap_src_base: false,
312 llvm_cov_flags: vec![],
313 filecheck_flags: vec![],
314 no_auto_check_cfg: false,
315 add_core_stubs: false,
316 core_stubs_compile_flags: vec![],
317 dont_require_annotations: Default::default(),
318 disable_gdb_pretty_printers: false,
319 compare_output_by_lines: false,
320 }
321 }
322
323 pub fn from_aux_file(
324 &self,
325 testfile: &Utf8Path,
326 revision: Option<&str>,
327 config: &Config,
328 ) -> Self {
329 let mut props = TestProps::new();
330
331 props.incremental_dir = self.incremental_dir.clone();
333 props.ignore_pass = true;
334 props.load_from(testfile, revision, config);
335
336 props
337 }
338
339 pub fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
340 let mut props = TestProps::new();
341 props.load_from(testfile, revision, config);
342 props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
343
344 match (props.pass_mode, props.fail_mode) {
345 (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
346 (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
347 _ => {}
348 }
349
350 props
351 }
352
353 fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
358 let mut has_edition = false;
359 if !testfile.is_dir() {
360 let file_contents = fs::read_to_string(testfile).unwrap();
361 let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
362
363 let mut poisoned = false;
364
365 iter_directives(
366 config.mode,
367 &mut poisoned,
368 &file_directives,
369 &mut |ln: &DirectiveLine<'_>| {
370 if !ln.applies_to_test_revision(test_revision) {
371 return;
372 }
373
374 use directives::*;
375
376 config.push_name_value_directive(
377 ln,
378 ERROR_PATTERN,
379 testfile,
380 &mut self.error_patterns,
381 |r| r,
382 );
383 config.push_name_value_directive(
384 ln,
385 REGEX_ERROR_PATTERN,
386 testfile,
387 &mut self.regex_error_patterns,
388 |r| r,
389 );
390
391 config.push_name_value_directive(
392 ln,
393 DOC_FLAGS,
394 testfile,
395 &mut self.doc_flags,
396 |r| r,
397 );
398
399 fn split_flags(flags: &str) -> Vec<String> {
400 flags
403 .split('\'')
404 .enumerate()
405 .flat_map(|(i, f)| {
406 if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() }
407 })
408 .map(move |s| s.to_owned())
409 .collect::<Vec<_>>()
410 }
411
412 if let Some(flags) =
413 config.parse_name_value_directive(ln, COMPILE_FLAGS, testfile)
414 {
415 let flags = split_flags(&flags);
416 for (i, flag) in flags.iter().enumerate() {
417 if flag == "--edition" || flag.starts_with("--edition=") {
418 panic!("you must use `//@ edition` to configure the edition");
419 }
420 if (flag == "-C"
421 && flags.get(i + 1).is_some_and(|v| v.starts_with("incremental=")))
422 || flag.starts_with("-Cincremental=")
423 {
424 panic!(
425 "you must use `//@ incremental` to enable incremental compilation"
426 );
427 }
428 }
429 self.compile_flags.extend(flags);
430 }
431 if config
432 .parse_name_value_directive(ln, INCORRECT_COMPILER_FLAGS, testfile)
433 .is_some()
434 {
435 panic!("`compiler-flags` directive should be spelled `compile-flags`");
436 }
437
438 if let Some(range) = parse_edition_range(config, ln, testfile) {
439 self.compile_flags.insert(
442 0,
443 format!("--edition={}", range.edition_to_test(config.edition)),
444 );
445 has_edition = true;
446 }
447
448 config.parse_and_update_revisions(testfile, ln, &mut self.revisions);
449
450 if let Some(flags) = config.parse_name_value_directive(ln, RUN_FLAGS, testfile)
451 {
452 self.run_flags.extend(split_flags(&flags));
453 }
454
455 if self.pp_exact.is_none() {
456 self.pp_exact = config.parse_pp_exact(ln, testfile);
457 }
458
459 config.set_name_directive(ln, SHOULD_ICE, &mut self.should_ice);
460 config.set_name_directive(ln, BUILD_AUX_DOCS, &mut self.build_aux_docs);
461 config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut self.unique_doc_out_dir);
462
463 config.set_name_directive(ln, FORCE_HOST, &mut self.force_host);
464 config.set_name_directive(ln, CHECK_STDOUT, &mut self.check_stdout);
465 config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut self.check_run_results);
466 config.set_name_directive(
467 ln,
468 DONT_CHECK_COMPILER_STDOUT,
469 &mut self.dont_check_compiler_stdout,
470 );
471 config.set_name_directive(
472 ln,
473 DONT_CHECK_COMPILER_STDERR,
474 &mut self.dont_check_compiler_stderr,
475 );
476 config.set_name_directive(ln, NO_PREFER_DYNAMIC, &mut self.no_prefer_dynamic);
477
478 if let Some(m) = config.parse_name_value_directive(ln, PRETTY_MODE, testfile) {
479 self.pretty_mode = m;
480 }
481
482 config.set_name_directive(
483 ln,
484 PRETTY_COMPARE_ONLY,
485 &mut self.pretty_compare_only,
486 );
487
488 parse_and_update_aux(config, ln, testfile, &mut self.aux);
490
491 config.push_name_value_directive(
492 ln,
493 EXEC_ENV,
494 testfile,
495 &mut self.exec_env,
496 Config::parse_env,
497 );
498 config.push_name_value_directive(
499 ln,
500 UNSET_EXEC_ENV,
501 testfile,
502 &mut self.unset_exec_env,
503 |r| r.trim().to_owned(),
504 );
505 config.push_name_value_directive(
506 ln,
507 RUSTC_ENV,
508 testfile,
509 &mut self.rustc_env,
510 Config::parse_env,
511 );
512 config.push_name_value_directive(
513 ln,
514 UNSET_RUSTC_ENV,
515 testfile,
516 &mut self.unset_rustc_env,
517 |r| r.trim().to_owned(),
518 );
519 config.push_name_value_directive(
520 ln,
521 FORBID_OUTPUT,
522 testfile,
523 &mut self.forbid_output,
524 |r| r,
525 );
526 config.set_name_directive(
527 ln,
528 CHECK_TEST_LINE_NUMBERS_MATCH,
529 &mut self.check_test_line_numbers_match,
530 );
531
532 self.update_pass_mode(ln, test_revision, config);
533 self.update_fail_mode(ln, config);
534
535 config.set_name_directive(ln, IGNORE_PASS, &mut self.ignore_pass);
536
537 if let Some(NormalizeRule { kind, regex, replacement }) =
538 config.parse_custom_normalization(ln)
539 {
540 let rule_tuple = (regex, replacement);
541 match kind {
542 NormalizeKind::Stdout => self.normalize_stdout.push(rule_tuple),
543 NormalizeKind::Stderr => self.normalize_stderr.push(rule_tuple),
544 NormalizeKind::Stderr32bit => {
545 if config.target_cfg().pointer_width == 32 {
546 self.normalize_stderr.push(rule_tuple);
547 }
548 }
549 NormalizeKind::Stderr64bit => {
550 if config.target_cfg().pointer_width == 64 {
551 self.normalize_stderr.push(rule_tuple);
552 }
553 }
554 }
555 }
556
557 if let Some(code) = config
558 .parse_name_value_directive(ln, FAILURE_STATUS, testfile)
559 .and_then(|code| code.trim().parse::<i32>().ok())
560 {
561 self.failure_status = Some(code);
562 }
563
564 config.set_name_directive(
565 ln,
566 DONT_CHECK_FAILURE_STATUS,
567 &mut self.dont_check_failure_status,
568 );
569
570 config.set_name_directive(ln, RUN_RUSTFIX, &mut self.run_rustfix);
571 config.set_name_directive(
572 ln,
573 RUSTFIX_ONLY_MACHINE_APPLICABLE,
574 &mut self.rustfix_only_machine_applicable,
575 );
576 config.set_name_value_directive(
577 ln,
578 ASSEMBLY_OUTPUT,
579 testfile,
580 &mut self.assembly_output,
581 |r| r.trim().to_string(),
582 );
583 config.set_name_directive(
584 ln,
585 STDERR_PER_BITWIDTH,
586 &mut self.stderr_per_bitwidth,
587 );
588 config.set_name_directive(ln, INCREMENTAL, &mut self.incremental);
589
590 if let Some(known_bug) =
593 config.parse_name_value_directive(ln, KNOWN_BUG, testfile)
594 {
595 let known_bug = known_bug.trim();
596 if known_bug == "unknown"
597 || known_bug.split(',').all(|issue_ref| {
598 issue_ref
599 .trim()
600 .split_once('#')
601 .filter(|(_, number)| {
602 number.chars().all(|digit| digit.is_numeric())
603 })
604 .is_some()
605 })
606 {
607 self.known_bug = true;
608 } else {
609 panic!(
610 "Invalid known-bug value: {known_bug}\nIt requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
611 );
612 }
613 } else if config.parse_name_directive(ln, KNOWN_BUG) {
614 panic!(
615 "Invalid known-bug attribute, requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
616 );
617 }
618
619 config.set_name_value_directive(
620 ln,
621 TEST_MIR_PASS,
622 testfile,
623 &mut self.mir_unit_test,
624 |s| s.trim().to_string(),
625 );
626 config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
627
628 if let Some(flags) =
629 config.parse_name_value_directive(ln, LLVM_COV_FLAGS, testfile)
630 {
631 self.llvm_cov_flags.extend(split_flags(&flags));
632 }
633
634 if let Some(flags) =
635 config.parse_name_value_directive(ln, FILECHECK_FLAGS, testfile)
636 {
637 self.filecheck_flags.extend(split_flags(&flags));
638 }
639
640 config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg);
641
642 self.update_add_core_stubs(ln, config);
643
644 if let Some(flags) =
645 config.parse_name_value_directive(ln, CORE_STUBS_COMPILE_FLAGS, testfile)
646 {
647 let flags = split_flags(&flags);
648 for flag in &flags {
649 if flag == "--edition" || flag.starts_with("--edition=") {
650 panic!("you must use `//@ edition` to configure the edition");
651 }
652 }
653 self.core_stubs_compile_flags.extend(flags);
654 }
655
656 if let Some(err_kind) =
657 config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS, testfile)
658 {
659 self.dont_require_annotations
660 .insert(ErrorKind::expect_from_user_str(err_kind.trim()));
661 }
662
663 config.set_name_directive(
664 ln,
665 DISABLE_GDB_PRETTY_PRINTERS,
666 &mut self.disable_gdb_pretty_printers,
667 );
668 config.set_name_directive(
669 ln,
670 COMPARE_OUTPUT_BY_LINES,
671 &mut self.compare_output_by_lines,
672 );
673 },
674 );
675
676 if poisoned {
677 eprintln!("errors encountered during TestProps parsing: {}", testfile);
678 panic!("errors encountered during TestProps parsing");
679 }
680 }
681
682 if self.should_ice {
683 self.failure_status = Some(101);
684 }
685
686 if config.mode == TestMode::Incremental {
687 self.incremental = true;
688 }
689
690 if config.mode == TestMode::Crashes {
691 self.rustc_env = vec![
695 ("RUST_BACKTRACE".to_string(), "0".to_string()),
696 ("RUSTC_ICE".to_string(), "0".to_string()),
697 ];
698 }
699
700 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
701 if let Ok(val) = env::var(key) {
702 if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
703 self.exec_env.push(((*key).to_owned(), val))
704 }
705 }
706 }
707
708 if let (Some(edition), false) = (&config.edition, has_edition) {
709 self.compile_flags.insert(0, format!("--edition={}", edition));
712 }
713 }
714
715 fn update_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
716 let check_ui = |mode: &str| {
717 if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
719 panic!("`{}-fail` directive is only supported in UI tests", mode);
720 }
721 };
722 if config.mode == TestMode::Ui && config.parse_name_directive(ln, "compile-fail") {
723 panic!("`compile-fail` directive is useless in UI tests");
724 }
725 let fail_mode = if config.parse_name_directive(ln, "check-fail") {
726 check_ui("check");
727 Some(FailMode::Check)
728 } else if config.parse_name_directive(ln, "build-fail") {
729 check_ui("build");
730 Some(FailMode::Build)
731 } else if config.parse_name_directive(ln, "run-fail") {
732 check_ui("run");
733 Some(FailMode::Run(RunFailMode::Fail))
734 } else if config.parse_name_directive(ln, "run-crash") {
735 check_ui("run");
736 Some(FailMode::Run(RunFailMode::Crash))
737 } else if config.parse_name_directive(ln, "run-fail-or-crash") {
738 check_ui("run");
739 Some(FailMode::Run(RunFailMode::FailOrCrash))
740 } else {
741 None
742 };
743 match (self.fail_mode, fail_mode) {
744 (None, Some(_)) => self.fail_mode = fail_mode,
745 (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
746 (_, None) => {}
747 }
748 }
749
750 fn update_pass_mode(
751 &mut self,
752 ln: &DirectiveLine<'_>,
753 revision: Option<&str>,
754 config: &Config,
755 ) {
756 let check_no_run = |s| match (config.mode, s) {
757 (TestMode::Ui, _) => (),
758 (TestMode::Crashes, _) => (),
759 (TestMode::Codegen, "build-pass") => (),
760 (TestMode::Incremental, _) => {
761 if revision.is_some() && !self.revisions.iter().all(|r| r.starts_with("cfail")) {
762 panic!("`{s}` directive is only supported in `cfail` incremental tests")
763 }
764 }
765 (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
766 };
767 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
768 check_no_run("check-pass");
769 Some(PassMode::Check)
770 } else if config.parse_name_directive(ln, "build-pass") {
771 check_no_run("build-pass");
772 Some(PassMode::Build)
773 } else if config.parse_name_directive(ln, "run-pass") {
774 check_no_run("run-pass");
775 Some(PassMode::Run)
776 } else {
777 None
778 };
779 match (self.pass_mode, pass_mode) {
780 (None, Some(_)) => self.pass_mode = pass_mode,
781 (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
782 (_, None) => {}
783 }
784 }
785
786 pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
787 if !self.ignore_pass && self.fail_mode.is_none() {
788 if let mode @ Some(_) = config.force_pass_mode {
789 return mode;
790 }
791 }
792 self.pass_mode
793 }
794
795 pub fn local_pass_mode(&self) -> Option<PassMode> {
797 self.pass_mode
798 }
799
800 fn update_add_core_stubs(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
801 let add_core_stubs = config.parse_name_directive(ln, directives::ADD_CORE_STUBS);
802 if add_core_stubs {
803 if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
804 panic!(
805 "`add-core-stubs` is currently only supported for ui, codegen and assembly test modes"
806 );
807 }
808
809 if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
812 panic!("`add-core-stubs` cannot be used to run the test binary");
815 }
816
817 self.add_core_stubs = add_core_stubs;
818 }
819 }
820}
821
822pub(crate) struct CheckDirectiveResult<'ln> {
823 is_known_directive: bool,
824 trailing_directive: Option<&'ln str>,
825}
826
827fn check_directive<'a>(
828 directive_ln: &DirectiveLine<'a>,
829 mode: TestMode,
830) -> CheckDirectiveResult<'a> {
831 let &DirectiveLine { name: directive_name, .. } = directive_ln;
832
833 let is_known_directive = KNOWN_DIRECTIVE_NAMES.contains(&directive_name)
834 || match mode {
835 TestMode::Rustdoc => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
836 TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
837 _ => false,
838 };
839
840 let trailing_directive = directive_ln
844 .remark_after_space()
845 .map(|remark| remark.trim_start().split(' ').next().unwrap())
846 .filter(|token| KNOWN_DIRECTIVE_NAMES.contains(token));
847
848 CheckDirectiveResult { is_known_directive, trailing_directive }
849}
850
851fn iter_directives(
852 mode: TestMode,
853 poisoned: &mut bool,
854 file_directives: &FileDirectives<'_>,
855 it: &mut dyn FnMut(&DirectiveLine<'_>),
856) {
857 let testfile = file_directives.path;
858
859 if mode == TestMode::CoverageRun {
864 let extra_directives: &[&str] = &[
865 "//@ needs-profiler-runtime",
866 "//@ ignore-cross-compile",
870 ];
871 for directive_str in extra_directives {
873 let directive_line = line_directive(0, directive_str)
874 .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
875 it(&directive_line);
876 }
877 }
878
879 for directive_line @ &DirectiveLine { line_number, .. } in &file_directives.lines {
880 if testfile.extension() == Some("rs") {
882 let CheckDirectiveResult { is_known_directive, trailing_directive } =
883 check_directive(directive_line, mode);
884
885 if !is_known_directive {
886 *poisoned = true;
887
888 error!(
889 "{testfile}:{line_number}: detected unknown compiletest test directive `{}`",
890 directive_line.display(),
891 );
892
893 return;
894 }
895
896 if let Some(trailing_directive) = &trailing_directive {
897 *poisoned = true;
898
899 error!(
900 "{testfile}:{line_number}: detected trailing compiletest test directive `{}`",
901 trailing_directive,
902 );
903 help!("put the trailing directive in its own line: `//@ {}`", trailing_directive);
904
905 return;
906 }
907 }
908
909 it(directive_line);
910 }
911}
912
913impl Config {
914 fn parse_and_update_revisions(
915 &self,
916 testfile: &Utf8Path,
917 line: &DirectiveLine<'_>,
918 existing: &mut Vec<String>,
919 ) {
920 const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
921 "true", "false",
925 ];
926
927 const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
928 ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
929
930 if let Some(raw) = self.parse_name_value_directive(line, "revisions", testfile) {
931 if self.mode == TestMode::RunMake {
932 panic!("`run-make` mode tests do not support revisions: {}", testfile);
933 }
934
935 let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
936 for revision in raw.split_whitespace() {
937 if !duplicates.insert(revision.to_string()) {
938 panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
939 }
940
941 if FORBIDDEN_REVISION_NAMES.contains(&revision) {
942 panic!(
943 "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
944 revision, raw, testfile
945 );
946 }
947
948 if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
949 && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
950 {
951 panic!(
952 "revision name `{revision}` is not permitted in a test suite that uses \
953 `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
954 prefix: `{revision}` in line `{}`: {}",
955 raw, testfile
956 );
957 }
958
959 existing.push(revision.to_string());
960 }
961 }
962 }
963
964 fn parse_env(nv: String) -> (String, String) {
965 let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
969 let name = name.trim();
972 (name.to_owned(), value.to_owned())
973 }
974
975 fn parse_pp_exact(&self, line: &DirectiveLine<'_>, testfile: &Utf8Path) -> Option<Utf8PathBuf> {
976 if let Some(s) = self.parse_name_value_directive(line, "pp-exact", testfile) {
977 Some(Utf8PathBuf::from(&s))
978 } else if self.parse_name_directive(line, "pp-exact") {
979 testfile.file_name().map(Utf8PathBuf::from)
980 } else {
981 None
982 }
983 }
984
985 fn parse_custom_normalization(&self, line: &DirectiveLine<'_>) -> Option<NormalizeRule> {
986 let &DirectiveLine { name, .. } = line;
987
988 let kind = match name {
989 "normalize-stdout" => NormalizeKind::Stdout,
990 "normalize-stderr" => NormalizeKind::Stderr,
991 "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
992 "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
993 _ => return None,
994 };
995
996 let Some((regex, replacement)) = line.value_after_colon().and_then(parse_normalize_rule)
997 else {
998 error!("couldn't parse custom normalization rule: `{}`", line.display());
999 help!("expected syntax is: `{name}: \"REGEX\" -> \"REPLACEMENT\"`");
1000 panic!("invalid normalization rule detected");
1001 };
1002 Some(NormalizeRule { kind, regex, replacement })
1003 }
1004
1005 fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool {
1006 line.name == directive
1010 }
1011
1012 fn parse_name_value_directive(
1013 &self,
1014 line: &DirectiveLine<'_>,
1015 directive: &str,
1016 testfile: &Utf8Path,
1017 ) -> Option<String> {
1018 let &DirectiveLine { line_number, .. } = line;
1019
1020 if line.name != directive {
1021 return None;
1022 };
1023
1024 let value = line.value_after_colon()?;
1028 debug!("{}: {}", directive, value);
1029 let value = expand_variables(value.to_owned(), self);
1030
1031 if value.is_empty() {
1032 error!("{testfile}:{line_number}: empty value for directive `{directive}`");
1033 help!("expected syntax is: `{directive}: value`");
1034 panic!("empty directive value detected");
1035 }
1036
1037 Some(value)
1038 }
1039
1040 fn set_name_directive(&self, line: &DirectiveLine<'_>, directive: &str, value: &mut bool) {
1041 *value = *value || self.parse_name_directive(line, directive);
1043 }
1044
1045 fn set_name_value_directive<T>(
1046 &self,
1047 line: &DirectiveLine<'_>,
1048 directive: &str,
1049 testfile: &Utf8Path,
1050 value: &mut Option<T>,
1051 parse: impl FnOnce(String) -> T,
1052 ) {
1053 if value.is_none() {
1054 *value = self.parse_name_value_directive(line, directive, testfile).map(parse);
1055 }
1056 }
1057
1058 fn push_name_value_directive<T>(
1059 &self,
1060 line: &DirectiveLine<'_>,
1061 directive: &str,
1062 testfile: &Utf8Path,
1063 values: &mut Vec<T>,
1064 parse: impl FnOnce(String) -> T,
1065 ) {
1066 if let Some(value) = self.parse_name_value_directive(line, directive, testfile).map(parse) {
1067 values.push(value);
1068 }
1069 }
1070}
1071
1072fn expand_variables(mut value: String, config: &Config) -> String {
1074 const CWD: &str = "{{cwd}}";
1075 const SRC_BASE: &str = "{{src-base}}";
1076 const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
1077 const RUST_SRC_BASE: &str = "{{rust-src-base}}";
1078 const SYSROOT_BASE: &str = "{{sysroot-base}}";
1079 const TARGET_LINKER: &str = "{{target-linker}}";
1080 const TARGET: &str = "{{target}}";
1081
1082 if value.contains(CWD) {
1083 let cwd = env::current_dir().unwrap();
1084 value = value.replace(CWD, &cwd.to_str().unwrap());
1085 }
1086
1087 if value.contains(SRC_BASE) {
1088 value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
1089 }
1090
1091 if value.contains(TEST_SUITE_BUILD_BASE) {
1092 value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
1093 }
1094
1095 if value.contains(SYSROOT_BASE) {
1096 value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
1097 }
1098
1099 if value.contains(TARGET_LINKER) {
1100 value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
1101 }
1102
1103 if value.contains(TARGET) {
1104 value = value.replace(TARGET, &config.target);
1105 }
1106
1107 if value.contains(RUST_SRC_BASE) {
1108 let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
1109 src_base.try_exists().expect(&*format!("{} should exists", src_base));
1110 let src_base = src_base.read_link_utf8().unwrap_or(src_base);
1111 value = value.replace(RUST_SRC_BASE, &src_base.as_str());
1112 }
1113
1114 value
1115}
1116
1117struct NormalizeRule {
1118 kind: NormalizeKind,
1119 regex: String,
1120 replacement: String,
1121}
1122
1123enum NormalizeKind {
1124 Stdout,
1125 Stderr,
1126 Stderr32bit,
1127 Stderr64bit,
1128}
1129
1130fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
1135 let captures = static_regex!(
1137 r#"(?x) # (verbose mode regex)
1138 ^
1139 \s* # (leading whitespace)
1140 "(?<regex>[^"]*)" # "REGEX"
1141 \s+->\s+ # ->
1142 "(?<replacement>[^"]*)" # "REPLACEMENT"
1143 $
1144 "#
1145 )
1146 .captures(raw_value)?;
1147 let regex = captures["regex"].to_owned();
1148 let replacement = captures["replacement"].to_owned();
1149 let replacement = replacement.replace("\\n", "\n");
1153 Some((regex, replacement))
1154}
1155
1156pub fn extract_llvm_version(version: &str) -> Version {
1166 let version = version.trim();
1169 let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
1170 let version_without_suffix = match version.split_once(uninterested) {
1171 Some((prefix, _suffix)) => prefix,
1172 None => version,
1173 };
1174
1175 let components: Vec<u64> = version_without_suffix
1176 .split('.')
1177 .map(|s| s.parse().expect("llvm version component should consist of only digits"))
1178 .collect();
1179
1180 match &components[..] {
1181 [major] => Version::new(*major, 0, 0),
1182 [major, minor] => Version::new(*major, *minor, 0),
1183 [major, minor, patch] => Version::new(*major, *minor, *patch),
1184 _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
1185 }
1186}
1187
1188pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
1189 let output = Command::new(binary_path).arg("--version").output().ok()?;
1190 if !output.status.success() {
1191 return None;
1192 }
1193 let version = String::from_utf8(output.stdout).ok()?;
1194 for line in version.lines() {
1195 if let Some(version) = line.split("LLVM version ").nth(1) {
1196 return Some(extract_llvm_version(version));
1197 }
1198 }
1199 None
1200}
1201
1202pub fn llvm_has_libzstd(config: &Config) -> bool {
1206 fn is_zstd_in_config(llvm_bin_dir: &Utf8Path) -> Option<()> {
1214 let llvm_config_path = llvm_bin_dir.join("llvm-config");
1215 let output = Command::new(llvm_config_path).arg("--system-libs").output().ok()?;
1216 assert!(output.status.success(), "running llvm-config --system-libs failed");
1217
1218 let libs = String::from_utf8(output.stdout).ok()?;
1219 for lib in libs.split_whitespace() {
1220 if lib.ends_with("libzstd.a") && Utf8Path::new(lib).exists() {
1221 return Some(());
1222 }
1223 }
1224
1225 None
1226 }
1227
1228 #[cfg(unix)]
1238 fn is_lld_built_with_zstd(llvm_bin_dir: &Utf8Path) -> Option<()> {
1239 let lld_path = llvm_bin_dir.join("lld");
1240 if lld_path.exists() {
1241 let lld_symlink_path = llvm_bin_dir.join("ld.lld");
1244 if !lld_symlink_path.exists() {
1245 std::os::unix::fs::symlink(lld_path, &lld_symlink_path).ok()?;
1246 }
1247
1248 let output = Command::new(&lld_symlink_path)
1251 .arg("--compress-debug-sections=zstd")
1252 .output()
1253 .ok()?;
1254 assert!(!output.status.success());
1255
1256 let stderr = String::from_utf8(output.stderr).ok()?;
1259 let zstd_available = !stderr.contains("LLVM was not built with LLVM_ENABLE_ZSTD");
1260
1261 std::fs::remove_file(lld_symlink_path).ok()?;
1264
1265 if zstd_available {
1266 return Some(());
1267 }
1268 }
1269
1270 None
1271 }
1272
1273 #[cfg(not(unix))]
1274 fn is_lld_built_with_zstd(_llvm_bin_dir: &Utf8Path) -> Option<()> {
1275 None
1276 }
1277
1278 if let Some(llvm_bin_dir) = &config.llvm_bin_dir {
1279 if is_zstd_in_config(llvm_bin_dir).is_some() {
1281 return true;
1282 }
1283
1284 if config.target == "x86_64-unknown-linux-gnu"
1292 && config.host == config.target
1293 && is_lld_built_with_zstd(llvm_bin_dir).is_some()
1294 {
1295 return true;
1296 }
1297 }
1298
1299 false
1301}
1302
1303fn extract_version_range<'a, F, VersionTy: Clone>(
1309 line: &'a str,
1310 parse: F,
1311) -> Option<(VersionTy, VersionTy)>
1312where
1313 F: Fn(&'a str) -> Option<VersionTy>,
1314{
1315 let mut splits = line.splitn(2, "- ").map(str::trim);
1316 let min = splits.next().unwrap();
1317 if min.ends_with('-') {
1318 return None;
1319 }
1320
1321 let max = splits.next();
1322
1323 if min.is_empty() {
1324 return None;
1325 }
1326
1327 let min = parse(min)?;
1328 let max = match max {
1329 Some("") => return None,
1330 Some(max) => parse(max)?,
1331 _ => min.clone(),
1332 };
1333
1334 Some((min, max))
1335}
1336
1337pub(crate) fn make_test_description(
1338 config: &Config,
1339 cache: &DirectivesCache,
1340 name: String,
1341 path: &Utf8Path,
1342 filterable_path: &Utf8Path,
1343 file_directives: &FileDirectives<'_>,
1344 test_revision: Option<&str>,
1345 poisoned: &mut bool,
1346) -> CollectedTestDesc {
1347 let mut ignore = false;
1348 let mut ignore_message = None;
1349 let mut should_fail = false;
1350
1351 let mut local_poisoned = false;
1352
1353 iter_directives(
1355 config.mode,
1356 &mut local_poisoned,
1357 file_directives,
1358 &mut |ln @ &DirectiveLine { line_number, .. }| {
1359 if !ln.applies_to_test_revision(test_revision) {
1360 return;
1361 }
1362
1363 macro_rules! decision {
1364 ($e:expr) => {
1365 match $e {
1366 IgnoreDecision::Ignore { reason } => {
1367 ignore = true;
1368 ignore_message = Some(reason.into());
1369 }
1370 IgnoreDecision::Error { message } => {
1371 error!("{path}:{line_number}: {message}");
1372 *poisoned = true;
1373 return;
1374 }
1375 IgnoreDecision::Continue => {}
1376 }
1377 };
1378 }
1379
1380 decision!(cfg::handle_ignore(config, ln));
1381 decision!(cfg::handle_only(config, ln));
1382 decision!(needs::handle_needs(&cache.needs, config, ln));
1383 decision!(ignore_llvm(config, path, ln));
1384 decision!(ignore_backends(config, path, ln));
1385 decision!(needs_backends(config, path, ln));
1386 decision!(ignore_cdb(config, ln));
1387 decision!(ignore_gdb(config, ln));
1388 decision!(ignore_lldb(config, ln));
1389
1390 if config.target == "wasm32-unknown-unknown"
1391 && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1392 {
1393 decision!(IgnoreDecision::Ignore {
1394 reason: "ignored on WASM as the run results cannot be checked there".into(),
1395 });
1396 }
1397
1398 should_fail |= config.parse_name_directive(ln, "should-fail");
1399 },
1400 );
1401
1402 if local_poisoned {
1403 eprintln!("errors encountered when trying to make test description: {}", path);
1404 panic!("errors encountered when trying to make test description");
1405 }
1406
1407 let should_panic = match config.mode {
1411 TestMode::Pretty => ShouldPanic::No,
1412 _ if should_fail => ShouldPanic::Yes,
1413 _ => ShouldPanic::No,
1414 };
1415
1416 CollectedTestDesc {
1417 name,
1418 filterable_path: filterable_path.to_owned(),
1419 ignore,
1420 ignore_message,
1421 should_panic,
1422 }
1423}
1424
1425fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1426 if config.debugger != Some(Debugger::Cdb) {
1427 return IgnoreDecision::Continue;
1428 }
1429
1430 if let Some(actual_version) = config.cdb_version {
1431 if line.name == "min-cdb-version"
1432 && let Some(rest) = line.value_after_colon().map(str::trim)
1433 {
1434 let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1435 panic!("couldn't parse version range: {:?}", rest);
1436 });
1437
1438 if actual_version < min_version {
1441 return IgnoreDecision::Ignore {
1442 reason: format!("ignored when the CDB version is lower than {rest}"),
1443 };
1444 }
1445 }
1446 }
1447 IgnoreDecision::Continue
1448}
1449
1450fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1451 if config.debugger != Some(Debugger::Gdb) {
1452 return IgnoreDecision::Continue;
1453 }
1454
1455 if let Some(actual_version) = config.gdb_version {
1456 if line.name == "min-gdb-version"
1457 && let Some(rest) = line.value_after_colon().map(str::trim)
1458 {
1459 let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1460 .unwrap_or_else(|| {
1461 panic!("couldn't parse version range: {:?}", rest);
1462 });
1463
1464 if start_ver != end_ver {
1465 panic!("Expected single GDB version")
1466 }
1467 if actual_version < start_ver {
1470 return IgnoreDecision::Ignore {
1471 reason: format!("ignored when the GDB version is lower than {rest}"),
1472 };
1473 }
1474 } else if line.name == "ignore-gdb-version"
1475 && let Some(rest) = line.value_after_colon().map(str::trim)
1476 {
1477 let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1478 .unwrap_or_else(|| {
1479 panic!("couldn't parse version range: {:?}", rest);
1480 });
1481
1482 if max_version < min_version {
1483 panic!("Malformed GDB version range: max < min")
1484 }
1485
1486 if actual_version >= min_version && actual_version <= max_version {
1487 if min_version == max_version {
1488 return IgnoreDecision::Ignore {
1489 reason: format!("ignored when the GDB version is {rest}"),
1490 };
1491 } else {
1492 return IgnoreDecision::Ignore {
1493 reason: format!("ignored when the GDB version is between {rest}"),
1494 };
1495 }
1496 }
1497 }
1498 }
1499 IgnoreDecision::Continue
1500}
1501
1502fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1503 if config.debugger != Some(Debugger::Lldb) {
1504 return IgnoreDecision::Continue;
1505 }
1506
1507 if let Some(actual_version) = config.lldb_version {
1508 if line.name == "min-lldb-version"
1509 && let Some(rest) = line.value_after_colon().map(str::trim)
1510 {
1511 let min_version = rest.parse().unwrap_or_else(|e| {
1512 panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1513 });
1514 if actual_version < min_version {
1517 return IgnoreDecision::Ignore {
1518 reason: format!("ignored when the LLDB version is {rest}"),
1519 };
1520 }
1521 }
1522 }
1523 IgnoreDecision::Continue
1524}
1525
1526fn ignore_backends(config: &Config, path: &Utf8Path, line: &DirectiveLine<'_>) -> IgnoreDecision {
1527 if let Some(backends_to_ignore) =
1528 config.parse_name_value_directive(line, "ignore-backends", path)
1529 {
1530 for backend in backends_to_ignore.split_whitespace().map(|backend| {
1531 match CodegenBackend::try_from(backend) {
1532 Ok(backend) => backend,
1533 Err(error) => {
1534 panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1535 }
1536 }
1537 }) {
1538 if config.default_codegen_backend == backend {
1539 return IgnoreDecision::Ignore {
1540 reason: format!("{} backend is marked as ignore", backend.as_str()),
1541 };
1542 }
1543 }
1544 }
1545 IgnoreDecision::Continue
1546}
1547
1548fn needs_backends(config: &Config, path: &Utf8Path, line: &DirectiveLine<'_>) -> IgnoreDecision {
1549 if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends", path) {
1550 if !needed_backends
1551 .split_whitespace()
1552 .map(|backend| match CodegenBackend::try_from(backend) {
1553 Ok(backend) => backend,
1554 Err(error) => {
1555 panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1556 }
1557 })
1558 .any(|backend| config.default_codegen_backend == backend)
1559 {
1560 return IgnoreDecision::Ignore {
1561 reason: format!(
1562 "{} backend is not part of required backends",
1563 config.default_codegen_backend.as_str()
1564 ),
1565 };
1566 }
1567 }
1568 IgnoreDecision::Continue
1569}
1570
1571fn ignore_llvm(config: &Config, path: &Utf8Path, line: &DirectiveLine<'_>) -> IgnoreDecision {
1572 if let Some(needed_components) =
1573 config.parse_name_value_directive(line, "needs-llvm-components", path)
1574 {
1575 let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1576 if let Some(missing_component) = needed_components
1577 .split_whitespace()
1578 .find(|needed_component| !components.contains(needed_component))
1579 {
1580 if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1581 panic!(
1582 "missing LLVM component {}, and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {}",
1583 missing_component, path
1584 );
1585 }
1586 return IgnoreDecision::Ignore {
1587 reason: format!("ignored when the {missing_component} LLVM component is missing"),
1588 };
1589 }
1590 }
1591 if let Some(actual_version) = &config.llvm_version {
1592 if let Some(version_string) =
1595 config.parse_name_value_directive(line, "min-llvm-version", path)
1596 {
1597 let min_version = extract_llvm_version(&version_string);
1598 if *actual_version < min_version {
1600 return IgnoreDecision::Ignore {
1601 reason: format!(
1602 "ignored when the LLVM version {actual_version} is older than {min_version}"
1603 ),
1604 };
1605 }
1606 } else if let Some(version_string) =
1607 config.parse_name_value_directive(line, "max-llvm-major-version", path)
1608 {
1609 let max_version = extract_llvm_version(&version_string);
1610 if actual_version.major > max_version.major {
1612 return IgnoreDecision::Ignore {
1613 reason: format!(
1614 "ignored when the LLVM version ({actual_version}) is newer than major\
1615 version {}",
1616 max_version.major
1617 ),
1618 };
1619 }
1620 } else if let Some(version_string) =
1621 config.parse_name_value_directive(line, "min-system-llvm-version", path)
1622 {
1623 let min_version = extract_llvm_version(&version_string);
1624 if config.system_llvm && *actual_version < min_version {
1627 return IgnoreDecision::Ignore {
1628 reason: format!(
1629 "ignored when the system LLVM version {actual_version} is older than {min_version}"
1630 ),
1631 };
1632 }
1633 } else if let Some(version_range) =
1634 config.parse_name_value_directive(line, "ignore-llvm-version", path)
1635 {
1636 let (v_min, v_max) =
1638 extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1639 .unwrap_or_else(|| {
1640 panic!("couldn't parse version range: \"{version_range}\"");
1641 });
1642 if v_max < v_min {
1643 panic!("malformed LLVM version range where {v_max} < {v_min}")
1644 }
1645 if *actual_version >= v_min && *actual_version <= v_max {
1647 if v_min == v_max {
1648 return IgnoreDecision::Ignore {
1649 reason: format!("ignored when the LLVM version is {actual_version}"),
1650 };
1651 } else {
1652 return IgnoreDecision::Ignore {
1653 reason: format!(
1654 "ignored when the LLVM version is between {v_min} and {v_max}"
1655 ),
1656 };
1657 }
1658 }
1659 } else if let Some(version_string) =
1660 config.parse_name_value_directive(line, "exact-llvm-major-version", path)
1661 {
1662 let version = extract_llvm_version(&version_string);
1664 if actual_version.major != version.major {
1665 return IgnoreDecision::Ignore {
1666 reason: format!(
1667 "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1668 actual_version.major, version.major
1669 ),
1670 };
1671 }
1672 }
1673 }
1674 IgnoreDecision::Continue
1675}
1676
1677enum IgnoreDecision {
1678 Ignore { reason: String },
1679 Continue,
1680 Error { message: String },
1681}
1682
1683fn parse_edition_range(
1684 config: &Config,
1685 line: &DirectiveLine<'_>,
1686 testfile: &Utf8Path,
1687) -> Option<EditionRange> {
1688 let raw = config.parse_name_value_directive(line, "edition", testfile)?;
1689 let line_number = line.line_number;
1690
1691 if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1693 Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1694 (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1695 fatal!(
1696 "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1697 );
1698 }
1699 (Some(lower_bound), Some(upper_bound)) => {
1700 EditionRange::Range { lower_bound, upper_bound }
1701 }
1702 (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1703 (None, Some(_)) => {
1704 fatal!(
1705 "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1706 );
1707 }
1708 (None, None) => {
1709 fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1710 }
1711 })
1712 } else {
1713 match maybe_parse_edition(&raw) {
1714 Some(edition) => Some(EditionRange::Exact(edition)),
1715 None => {
1716 fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1717 }
1718 }
1719 }
1720}
1721
1722fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1723 input = input.trim();
1724 if input.is_empty() {
1725 return None;
1726 }
1727 Some(parse_edition(input))
1728}
1729
1730#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1731enum EditionRange {
1732 Exact(Edition),
1733 RangeFrom(Edition),
1734 Range {
1736 lower_bound: Edition,
1737 upper_bound: Edition,
1738 },
1739}
1740
1741impl EditionRange {
1742 fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1743 let min_edition = Edition::Year(2015);
1744 let requested = requested.into().unwrap_or(min_edition);
1745
1746 match *self {
1747 EditionRange::Exact(exact) => exact,
1748 EditionRange::RangeFrom(lower_bound) => {
1749 if requested >= lower_bound {
1750 requested
1751 } else {
1752 lower_bound
1753 }
1754 }
1755 EditionRange::Range { lower_bound, upper_bound } => {
1756 if requested >= lower_bound && requested < upper_bound {
1757 requested
1758 } else {
1759 lower_bound
1760 }
1761 }
1762 }
1763 }
1764}