compiletest/
directives.rs

1use std::collections::HashSet;
2use std::env;
3use std::fs::File;
4use std::io::BufReader;
5use std::io::prelude::*;
6use std::process::Command;
7
8use camino::{Utf8Path, Utf8PathBuf};
9use semver::Version;
10use tracing::*;
11
12use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
13use crate::debuggers::{extract_cdb_version, extract_gdb_version};
14use crate::directives::auxiliary::{AuxProps, parse_and_update_aux};
15use crate::directives::needs::CachedNeedsConditions;
16use crate::errors::ErrorKind;
17use crate::executor::{CollectedTestDesc, ShouldPanic};
18use crate::help;
19use crate::util::static_regex;
20
21pub(crate) mod auxiliary;
22mod cfg;
23mod needs;
24#[cfg(test)]
25mod tests;
26
27pub struct DirectivesCache {
28    needs: CachedNeedsConditions,
29}
30
31impl DirectivesCache {
32    pub fn load(config: &Config) -> Self {
33        Self { needs: CachedNeedsConditions::load(config) }
34    }
35}
36
37/// Properties which must be known very early, before actually running
38/// the test.
39#[derive(Default)]
40pub struct EarlyProps {
41    /// Auxiliary crates that should be built and made available to this test.
42    /// Included in [`EarlyProps`] so that the indicated files can participate
43    /// in up-to-date checking. Building happens via [`TestProps::aux`] instead.
44    pub(crate) aux: AuxProps,
45    pub revisions: Vec<String>,
46}
47
48impl EarlyProps {
49    pub fn from_file(config: &Config, testfile: &Utf8Path) -> Self {
50        let file = File::open(testfile.as_std_path()).expect("open test file to parse earlyprops");
51        Self::from_reader(config, testfile, file)
52    }
53
54    pub fn from_reader<R: Read>(config: &Config, testfile: &Utf8Path, rdr: R) -> Self {
55        let mut props = EarlyProps::default();
56        let mut poisoned = false;
57        iter_directives(
58            config.mode,
59            &config.suite,
60            &mut poisoned,
61            testfile,
62            rdr,
63            &mut |DirectiveLine { raw_directive: ln, .. }| {
64                parse_and_update_aux(config, ln, &mut props.aux);
65                config.parse_and_update_revisions(testfile, ln, &mut props.revisions);
66            },
67        );
68
69        if poisoned {
70            eprintln!("errors encountered during EarlyProps parsing: {}", testfile);
71            panic!("errors encountered during EarlyProps parsing");
72        }
73
74        props
75    }
76}
77
78#[derive(Clone, Debug)]
79pub struct TestProps {
80    // Lines that should be expected, in order, on standard out
81    pub error_patterns: Vec<String>,
82    // Regexes that should be expected, in order, on standard out
83    pub regex_error_patterns: Vec<String>,
84    // Extra flags to pass to the compiler
85    pub compile_flags: Vec<String>,
86    // Extra flags to pass when the compiled code is run (such as --bench)
87    pub run_flags: Vec<String>,
88    /// Extra flags to pass to rustdoc but not the compiler.
89    pub doc_flags: Vec<String>,
90    // If present, the name of a file that this test should match when
91    // pretty-printed
92    pub pp_exact: Option<Utf8PathBuf>,
93    /// Auxiliary crates that should be built and made available to this test.
94    pub(crate) aux: AuxProps,
95    // Environment settings to use for compiling
96    pub rustc_env: Vec<(String, String)>,
97    // Environment variables to unset prior to compiling.
98    // Variables are unset before applying 'rustc_env'.
99    pub unset_rustc_env: Vec<String>,
100    // Environment settings to use during execution
101    pub exec_env: Vec<(String, String)>,
102    // Environment variables to unset prior to execution.
103    // Variables are unset before applying 'exec_env'
104    pub unset_exec_env: Vec<String>,
105    // Build documentation for all specified aux-builds as well
106    pub build_aux_docs: bool,
107    /// Build the documentation for each crate in a unique output directory.
108    /// Uses `<root output directory>/docs/<test name>/doc`.
109    pub unique_doc_out_dir: bool,
110    // Flag to force a crate to be built with the host architecture
111    pub force_host: bool,
112    // Check stdout for error-pattern output as well as stderr
113    pub check_stdout: bool,
114    // Check stdout & stderr for output of run-pass test
115    pub check_run_results: bool,
116    // For UI tests, allows compiler to generate arbitrary output to stdout
117    pub dont_check_compiler_stdout: bool,
118    // For UI tests, allows compiler to generate arbitrary output to stderr
119    pub dont_check_compiler_stderr: bool,
120    // Don't force a --crate-type=dylib flag on the command line
121    //
122    // Set this for example if you have an auxiliary test file that contains
123    // a proc-macro and needs `#![crate_type = "proc-macro"]`. This ensures
124    // that the aux file is compiled as a `proc-macro` and not as a `dylib`.
125    pub no_prefer_dynamic: bool,
126    // Which pretty mode are we testing with, default to 'normal'
127    pub pretty_mode: String,
128    // Only compare pretty output and don't try compiling
129    pub pretty_compare_only: bool,
130    // Patterns which must not appear in the output of a cfail test.
131    pub forbid_output: Vec<String>,
132    // Revisions to test for incremental compilation.
133    pub revisions: Vec<String>,
134    // Directory (if any) to use for incremental compilation.  This is
135    // not set by end-users; rather it is set by the incremental
136    // testing harness and used when generating compilation
137    // arguments. (In particular, it propagates to the aux-builds.)
138    pub incremental_dir: Option<Utf8PathBuf>,
139    // If `true`, this test will use incremental compilation.
140    //
141    // This can be set manually with the `incremental` directive, or implicitly
142    // by being a part of an incremental mode test. Using the `incremental`
143    // directive should be avoided if possible; using an incremental mode test is
144    // preferred. Incremental mode tests support multiple passes, which can
145    // verify that the incremental cache can be loaded properly after being
146    // created. Just setting the directive will only verify the behavior with
147    // creating an incremental cache, but doesn't check that it is created
148    // correctly.
149    //
150    // Compiletest will create the incremental directory, and ensure it is
151    // empty before the test starts. Incremental mode tests will reuse the
152    // incremental directory between passes in the same test.
153    pub incremental: bool,
154    // If `true`, this test is a known bug.
155    //
156    // When set, some requirements are relaxed. Currently, this only means no
157    // error annotations are needed, but this may be updated in the future to
158    // include other relaxations.
159    pub known_bug: bool,
160    // How far should the test proceed while still passing.
161    pass_mode: Option<PassMode>,
162    // Ignore `--pass` overrides from the command line for this test.
163    ignore_pass: bool,
164    // How far this test should proceed to start failing.
165    pub fail_mode: Option<FailMode>,
166    // rustdoc will test the output of the `--test` option
167    pub check_test_line_numbers_match: bool,
168    // customized normalization rules
169    pub normalize_stdout: Vec<(String, String)>,
170    pub normalize_stderr: Vec<(String, String)>,
171    pub failure_status: Option<i32>,
172    // For UI tests, allows compiler to exit with arbitrary failure status
173    pub dont_check_failure_status: bool,
174    // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the
175    // resulting Rust code.
176    pub run_rustfix: bool,
177    // If true, `rustfix` will only apply `MachineApplicable` suggestions.
178    pub rustfix_only_machine_applicable: bool,
179    pub assembly_output: Option<String>,
180    // If true, the test is expected to ICE
181    pub should_ice: bool,
182    // If true, the stderr is expected to be different across bit-widths.
183    pub stderr_per_bitwidth: bool,
184    // The MIR opt to unit test, if any
185    pub mir_unit_test: Option<String>,
186    // Whether to tell `rustc` to remap the "src base" directory to a fake
187    // directory.
188    pub remap_src_base: bool,
189    /// Extra flags to pass to `llvm-cov` when producing coverage reports.
190    /// Only used by the "coverage-run" test mode.
191    pub llvm_cov_flags: Vec<String>,
192    /// Extra flags to pass to LLVM's `filecheck` tool, in tests that use it.
193    pub filecheck_flags: Vec<String>,
194    /// Don't automatically insert any `--check-cfg` args
195    pub no_auto_check_cfg: bool,
196    /// Run tests which require enzyme being build
197    pub has_enzyme: bool,
198    /// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
199    /// that don't otherwise want/need `-Z build-std`.
200    pub add_core_stubs: bool,
201    /// Whether line annotatins are required for the given error kind.
202    pub dont_require_annotations: HashSet<ErrorKind>,
203}
204
205mod directives {
206    pub const ERROR_PATTERN: &'static str = "error-pattern";
207    pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
208    pub const COMPILE_FLAGS: &'static str = "compile-flags";
209    pub const RUN_FLAGS: &'static str = "run-flags";
210    pub const DOC_FLAGS: &'static str = "doc-flags";
211    pub const SHOULD_ICE: &'static str = "should-ice";
212    pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
213    pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
214    pub const FORCE_HOST: &'static str = "force-host";
215    pub const CHECK_STDOUT: &'static str = "check-stdout";
216    pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
217    pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
218    pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
219    pub const DONT_REQUIRE_ANNOTATIONS: &'static str = "dont-require-annotations";
220    pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
221    pub const PRETTY_MODE: &'static str = "pretty-mode";
222    pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
223    pub const AUX_BIN: &'static str = "aux-bin";
224    pub const AUX_BUILD: &'static str = "aux-build";
225    pub const AUX_CRATE: &'static str = "aux-crate";
226    pub const PROC_MACRO: &'static str = "proc-macro";
227    pub const AUX_CODEGEN_BACKEND: &'static str = "aux-codegen-backend";
228    pub const EXEC_ENV: &'static str = "exec-env";
229    pub const RUSTC_ENV: &'static str = "rustc-env";
230    pub const UNSET_EXEC_ENV: &'static str = "unset-exec-env";
231    pub const UNSET_RUSTC_ENV: &'static str = "unset-rustc-env";
232    pub const FORBID_OUTPUT: &'static str = "forbid-output";
233    pub const CHECK_TEST_LINE_NUMBERS_MATCH: &'static str = "check-test-line-numbers-match";
234    pub const IGNORE_PASS: &'static str = "ignore-pass";
235    pub const FAILURE_STATUS: &'static str = "failure-status";
236    pub const DONT_CHECK_FAILURE_STATUS: &'static str = "dont-check-failure-status";
237    pub const RUN_RUSTFIX: &'static str = "run-rustfix";
238    pub const RUSTFIX_ONLY_MACHINE_APPLICABLE: &'static str = "rustfix-only-machine-applicable";
239    pub const ASSEMBLY_OUTPUT: &'static str = "assembly-output";
240    pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
241    pub const INCREMENTAL: &'static str = "incremental";
242    pub const KNOWN_BUG: &'static str = "known-bug";
243    pub const TEST_MIR_PASS: &'static str = "test-mir-pass";
244    pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
245    pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags";
246    pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
247    pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
248    pub const ADD_CORE_STUBS: &'static str = "add-core-stubs";
249    // This isn't a real directive, just one that is probably mistyped often
250    pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
251}
252
253impl TestProps {
254    pub fn new() -> Self {
255        TestProps {
256            error_patterns: vec![],
257            regex_error_patterns: vec![],
258            compile_flags: vec![],
259            run_flags: vec![],
260            doc_flags: vec![],
261            pp_exact: None,
262            aux: Default::default(),
263            revisions: vec![],
264            rustc_env: vec![
265                ("RUSTC_ICE".to_string(), "0".to_string()),
266                ("RUST_BACKTRACE".to_string(), "short".to_string()),
267            ],
268            unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
269            exec_env: vec![],
270            unset_exec_env: vec![],
271            build_aux_docs: false,
272            unique_doc_out_dir: false,
273            force_host: false,
274            check_stdout: false,
275            check_run_results: false,
276            dont_check_compiler_stdout: false,
277            dont_check_compiler_stderr: false,
278            no_prefer_dynamic: false,
279            pretty_mode: "normal".to_string(),
280            pretty_compare_only: false,
281            forbid_output: vec![],
282            incremental_dir: None,
283            incremental: false,
284            known_bug: false,
285            pass_mode: None,
286            fail_mode: None,
287            ignore_pass: false,
288            check_test_line_numbers_match: false,
289            normalize_stdout: vec![],
290            normalize_stderr: vec![],
291            failure_status: None,
292            dont_check_failure_status: false,
293            run_rustfix: false,
294            rustfix_only_machine_applicable: false,
295            assembly_output: None,
296            should_ice: false,
297            stderr_per_bitwidth: false,
298            mir_unit_test: None,
299            remap_src_base: false,
300            llvm_cov_flags: vec![],
301            filecheck_flags: vec![],
302            no_auto_check_cfg: false,
303            has_enzyme: false,
304            add_core_stubs: false,
305            dont_require_annotations: Default::default(),
306        }
307    }
308
309    pub fn from_aux_file(
310        &self,
311        testfile: &Utf8Path,
312        revision: Option<&str>,
313        config: &Config,
314    ) -> Self {
315        let mut props = TestProps::new();
316
317        // copy over select properties to the aux build:
318        props.incremental_dir = self.incremental_dir.clone();
319        props.ignore_pass = true;
320        props.load_from(testfile, revision, config);
321
322        props
323    }
324
325    pub fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
326        let mut props = TestProps::new();
327        props.load_from(testfile, revision, config);
328        props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
329
330        match (props.pass_mode, props.fail_mode) {
331            (None, None) if config.mode == Mode::Ui => props.fail_mode = Some(FailMode::Check),
332            (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
333            _ => {}
334        }
335
336        props
337    }
338
339    /// Loads properties from `testfile` into `props`. If a property is
340    /// tied to a particular revision `foo` (indicated by writing
341    /// `//@[foo]`), then the property is ignored unless `test_revision` is
342    /// `Some("foo")`.
343    fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
344        let mut has_edition = false;
345        if !testfile.is_dir() {
346            let file = File::open(testfile.as_std_path()).unwrap();
347
348            let mut poisoned = false;
349
350            iter_directives(
351                config.mode,
352                &config.suite,
353                &mut poisoned,
354                testfile,
355                file,
356                &mut |directive @ DirectiveLine { raw_directive: ln, .. }| {
357                    if !directive.applies_to_test_revision(test_revision) {
358                        return;
359                    }
360
361                    use directives::*;
362
363                    config.push_name_value_directive(
364                        ln,
365                        ERROR_PATTERN,
366                        &mut self.error_patterns,
367                        |r| r,
368                    );
369                    config.push_name_value_directive(
370                        ln,
371                        REGEX_ERROR_PATTERN,
372                        &mut self.regex_error_patterns,
373                        |r| r,
374                    );
375
376                    config.push_name_value_directive(ln, DOC_FLAGS, &mut self.doc_flags, |r| r);
377
378                    fn split_flags(flags: &str) -> Vec<String> {
379                        // Individual flags can be single-quoted to preserve spaces; see
380                        // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
381                        flags
382                            .split('\'')
383                            .enumerate()
384                            .flat_map(|(i, f)| {
385                                if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() }
386                            })
387                            .map(move |s| s.to_owned())
388                            .collect::<Vec<_>>()
389                    }
390
391                    if let Some(flags) = config.parse_name_value_directive(ln, COMPILE_FLAGS) {
392                        let flags = split_flags(&flags);
393                        for flag in &flags {
394                            if flag == "--edition" || flag.starts_with("--edition=") {
395                                panic!("you must use `//@ edition` to configure the edition");
396                            }
397                        }
398                        self.compile_flags.extend(flags);
399                    }
400                    if config.parse_name_value_directive(ln, INCORRECT_COMPILER_FLAGS).is_some() {
401                        panic!("`compiler-flags` directive should be spelled `compile-flags`");
402                    }
403
404                    if let Some(edition) = config.parse_edition(ln) {
405                        // The edition is added at the start, since flags from //@compile-flags must
406                        // be passed to rustc last.
407                        self.compile_flags.insert(0, format!("--edition={}", edition.trim()));
408                        has_edition = true;
409                    }
410
411                    config.parse_and_update_revisions(testfile, ln, &mut self.revisions);
412
413                    if let Some(flags) = config.parse_name_value_directive(ln, RUN_FLAGS) {
414                        self.run_flags.extend(split_flags(&flags));
415                    }
416
417                    if self.pp_exact.is_none() {
418                        self.pp_exact = config.parse_pp_exact(ln, testfile);
419                    }
420
421                    config.set_name_directive(ln, SHOULD_ICE, &mut self.should_ice);
422                    config.set_name_directive(ln, BUILD_AUX_DOCS, &mut self.build_aux_docs);
423                    config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut self.unique_doc_out_dir);
424
425                    config.set_name_directive(ln, FORCE_HOST, &mut self.force_host);
426                    config.set_name_directive(ln, CHECK_STDOUT, &mut self.check_stdout);
427                    config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut self.check_run_results);
428                    config.set_name_directive(
429                        ln,
430                        DONT_CHECK_COMPILER_STDOUT,
431                        &mut self.dont_check_compiler_stdout,
432                    );
433                    config.set_name_directive(
434                        ln,
435                        DONT_CHECK_COMPILER_STDERR,
436                        &mut self.dont_check_compiler_stderr,
437                    );
438                    config.set_name_directive(ln, NO_PREFER_DYNAMIC, &mut self.no_prefer_dynamic);
439
440                    if let Some(m) = config.parse_name_value_directive(ln, PRETTY_MODE) {
441                        self.pretty_mode = m;
442                    }
443
444                    config.set_name_directive(
445                        ln,
446                        PRETTY_COMPARE_ONLY,
447                        &mut self.pretty_compare_only,
448                    );
449
450                    // Call a helper method to deal with aux-related directives.
451                    parse_and_update_aux(config, ln, &mut self.aux);
452
453                    config.push_name_value_directive(
454                        ln,
455                        EXEC_ENV,
456                        &mut self.exec_env,
457                        Config::parse_env,
458                    );
459                    config.push_name_value_directive(
460                        ln,
461                        UNSET_EXEC_ENV,
462                        &mut self.unset_exec_env,
463                        |r| r.trim().to_owned(),
464                    );
465                    config.push_name_value_directive(
466                        ln,
467                        RUSTC_ENV,
468                        &mut self.rustc_env,
469                        Config::parse_env,
470                    );
471                    config.push_name_value_directive(
472                        ln,
473                        UNSET_RUSTC_ENV,
474                        &mut self.unset_rustc_env,
475                        |r| r.trim().to_owned(),
476                    );
477                    config.push_name_value_directive(
478                        ln,
479                        FORBID_OUTPUT,
480                        &mut self.forbid_output,
481                        |r| r,
482                    );
483                    config.set_name_directive(
484                        ln,
485                        CHECK_TEST_LINE_NUMBERS_MATCH,
486                        &mut self.check_test_line_numbers_match,
487                    );
488
489                    self.update_pass_mode(ln, test_revision, config);
490                    self.update_fail_mode(ln, config);
491
492                    config.set_name_directive(ln, IGNORE_PASS, &mut self.ignore_pass);
493
494                    if let Some(NormalizeRule { kind, regex, replacement }) =
495                        config.parse_custom_normalization(ln)
496                    {
497                        let rule_tuple = (regex, replacement);
498                        match kind {
499                            NormalizeKind::Stdout => self.normalize_stdout.push(rule_tuple),
500                            NormalizeKind::Stderr => self.normalize_stderr.push(rule_tuple),
501                            NormalizeKind::Stderr32bit => {
502                                if config.target_cfg().pointer_width == 32 {
503                                    self.normalize_stderr.push(rule_tuple);
504                                }
505                            }
506                            NormalizeKind::Stderr64bit => {
507                                if config.target_cfg().pointer_width == 64 {
508                                    self.normalize_stderr.push(rule_tuple);
509                                }
510                            }
511                        }
512                    }
513
514                    if let Some(code) = config
515                        .parse_name_value_directive(ln, FAILURE_STATUS)
516                        .and_then(|code| code.trim().parse::<i32>().ok())
517                    {
518                        self.failure_status = Some(code);
519                    }
520
521                    config.set_name_directive(
522                        ln,
523                        DONT_CHECK_FAILURE_STATUS,
524                        &mut self.dont_check_failure_status,
525                    );
526
527                    config.set_name_directive(ln, RUN_RUSTFIX, &mut self.run_rustfix);
528                    config.set_name_directive(
529                        ln,
530                        RUSTFIX_ONLY_MACHINE_APPLICABLE,
531                        &mut self.rustfix_only_machine_applicable,
532                    );
533                    config.set_name_value_directive(
534                        ln,
535                        ASSEMBLY_OUTPUT,
536                        &mut self.assembly_output,
537                        |r| r.trim().to_string(),
538                    );
539                    config.set_name_directive(
540                        ln,
541                        STDERR_PER_BITWIDTH,
542                        &mut self.stderr_per_bitwidth,
543                    );
544                    config.set_name_directive(ln, INCREMENTAL, &mut self.incremental);
545
546                    // Unlike the other `name_value_directive`s this needs to be handled manually,
547                    // because it sets a `bool` flag.
548                    if let Some(known_bug) = config.parse_name_value_directive(ln, KNOWN_BUG) {
549                        let known_bug = known_bug.trim();
550                        if known_bug == "unknown"
551                            || known_bug.split(',').all(|issue_ref| {
552                                issue_ref
553                                    .trim()
554                                    .split_once('#')
555                                    .filter(|(_, number)| {
556                                        number.chars().all(|digit| digit.is_numeric())
557                                    })
558                                    .is_some()
559                            })
560                        {
561                            self.known_bug = true;
562                        } else {
563                            panic!(
564                                "Invalid known-bug value: {known_bug}\nIt requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
565                            );
566                        }
567                    } else if config.parse_name_directive(ln, KNOWN_BUG) {
568                        panic!(
569                            "Invalid known-bug attribute, requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
570                        );
571                    }
572
573                    config.set_name_value_directive(
574                        ln,
575                        TEST_MIR_PASS,
576                        &mut self.mir_unit_test,
577                        |s| s.trim().to_string(),
578                    );
579                    config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
580
581                    if let Some(flags) = config.parse_name_value_directive(ln, LLVM_COV_FLAGS) {
582                        self.llvm_cov_flags.extend(split_flags(&flags));
583                    }
584
585                    if let Some(flags) = config.parse_name_value_directive(ln, FILECHECK_FLAGS) {
586                        self.filecheck_flags.extend(split_flags(&flags));
587                    }
588
589                    config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg);
590
591                    self.update_add_core_stubs(ln, config);
592
593                    if let Some(err_kind) =
594                        config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS)
595                    {
596                        self.dont_require_annotations
597                            .insert(ErrorKind::expect_from_user_str(err_kind.trim()));
598                    }
599                },
600            );
601
602            if poisoned {
603                eprintln!("errors encountered during TestProps parsing: {}", testfile);
604                panic!("errors encountered during TestProps parsing");
605            }
606        }
607
608        if self.should_ice {
609            self.failure_status = Some(101);
610        }
611
612        if config.mode == Mode::Incremental {
613            self.incremental = true;
614        }
615
616        if config.mode == Mode::Crashes {
617            // we don't want to pollute anything with backtrace-files
618            // also turn off backtraces in order to save some execution
619            // time on the tests; we only need to know IF it crashes
620            self.rustc_env = vec![
621                ("RUST_BACKTRACE".to_string(), "0".to_string()),
622                ("RUSTC_ICE".to_string(), "0".to_string()),
623            ];
624        }
625
626        for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
627            if let Ok(val) = env::var(key) {
628                if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
629                    self.exec_env.push(((*key).to_owned(), val))
630                }
631            }
632        }
633
634        if let (Some(edition), false) = (&config.edition, has_edition) {
635            // The edition is added at the start, since flags from //@compile-flags must be passed
636            // to rustc last.
637            self.compile_flags.insert(0, format!("--edition={}", edition));
638        }
639    }
640
641    fn update_fail_mode(&mut self, ln: &str, config: &Config) {
642        let check_ui = |mode: &str| {
643            // Mode::Crashes may need build-fail in order to trigger llvm errors or stack overflows
644            if config.mode != Mode::Ui && config.mode != Mode::Crashes {
645                panic!("`{}-fail` directive is only supported in UI tests", mode);
646            }
647        };
648        if config.mode == Mode::Ui && config.parse_name_directive(ln, "compile-fail") {
649            panic!("`compile-fail` directive is useless in UI tests");
650        }
651        let fail_mode = if config.parse_name_directive(ln, "check-fail") {
652            check_ui("check");
653            Some(FailMode::Check)
654        } else if config.parse_name_directive(ln, "build-fail") {
655            check_ui("build");
656            Some(FailMode::Build)
657        } else if config.parse_name_directive(ln, "run-fail") {
658            check_ui("run");
659            Some(FailMode::Run)
660        } else {
661            None
662        };
663        match (self.fail_mode, fail_mode) {
664            (None, Some(_)) => self.fail_mode = fail_mode,
665            (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
666            (_, None) => {}
667        }
668    }
669
670    fn update_pass_mode(&mut self, ln: &str, revision: Option<&str>, config: &Config) {
671        let check_no_run = |s| match (config.mode, s) {
672            (Mode::Ui, _) => (),
673            (Mode::Crashes, _) => (),
674            (Mode::Codegen, "build-pass") => (),
675            (Mode::Incremental, _) => {
676                if revision.is_some() && !self.revisions.iter().all(|r| r.starts_with("cfail")) {
677                    panic!("`{s}` directive is only supported in `cfail` incremental tests")
678                }
679            }
680            (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
681        };
682        let pass_mode = if config.parse_name_directive(ln, "check-pass") {
683            check_no_run("check-pass");
684            Some(PassMode::Check)
685        } else if config.parse_name_directive(ln, "build-pass") {
686            check_no_run("build-pass");
687            Some(PassMode::Build)
688        } else if config.parse_name_directive(ln, "run-pass") {
689            check_no_run("run-pass");
690            Some(PassMode::Run)
691        } else {
692            None
693        };
694        match (self.pass_mode, pass_mode) {
695            (None, Some(_)) => self.pass_mode = pass_mode,
696            (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
697            (_, None) => {}
698        }
699    }
700
701    pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
702        if !self.ignore_pass && self.fail_mode.is_none() {
703            if let mode @ Some(_) = config.force_pass_mode {
704                return mode;
705            }
706        }
707        self.pass_mode
708    }
709
710    // does not consider CLI override for pass mode
711    pub fn local_pass_mode(&self) -> Option<PassMode> {
712        self.pass_mode
713    }
714
715    pub fn update_add_core_stubs(&mut self, ln: &str, config: &Config) {
716        let add_core_stubs = config.parse_name_directive(ln, directives::ADD_CORE_STUBS);
717        if add_core_stubs {
718            if !matches!(config.mode, Mode::Ui | Mode::Codegen | Mode::Assembly) {
719                panic!(
720                    "`add-core-stubs` is currently only supported for ui, codegen and assembly test modes"
721                );
722            }
723
724            // FIXME(jieyouxu): this check is currently order-dependent, but we should probably
725            // collect all directives in one go then perform a validation pass after that.
726            if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
727                // `minicore` can only be used with non-run modes, because it's `core` prelude stubs
728                // and can't run.
729                panic!("`add-core-stubs` cannot be used to run the test binary");
730            }
731
732            self.add_core_stubs = add_core_stubs;
733        }
734    }
735}
736
737/// If the given line begins with the appropriate comment prefix for a directive,
738/// returns a struct containing various parts of the directive.
739fn line_directive<'line>(
740    line_number: usize,
741    original_line: &'line str,
742) -> Option<DirectiveLine<'line>> {
743    // Ignore lines that don't start with the comment prefix.
744    let after_comment =
745        original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
746
747    let revision;
748    let raw_directive;
749
750    if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
751        // A comment like `//@[foo]` only applies to revision `foo`.
752        let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
753            panic!(
754                "malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
755            )
756        };
757
758        revision = Some(line_revision);
759        raw_directive = after_close_bracket.trim_start();
760    } else {
761        revision = None;
762        raw_directive = after_comment;
763    };
764
765    Some(DirectiveLine { line_number, revision, raw_directive })
766}
767
768// To prevent duplicating the list of directives between `compiletest`,`htmldocck` and `jsondocck`,
769// we put it into a common file which is included in rust code and parsed here.
770// FIXME: This setup is temporary until we figure out how to improve this situation.
771//        See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
772include!("directive-list.rs");
773
774const KNOWN_HTMLDOCCK_DIRECTIVE_NAMES: &[&str] = &[
775    "count",
776    "!count",
777    "files",
778    "!files",
779    "has",
780    "!has",
781    "has-dir",
782    "!has-dir",
783    "hasraw",
784    "!hasraw",
785    "matches",
786    "!matches",
787    "matchesraw",
788    "!matchesraw",
789    "snapshot",
790    "!snapshot",
791];
792
793const KNOWN_JSONDOCCK_DIRECTIVE_NAMES: &[&str] =
794    &["count", "!count", "has", "!has", "is", "!is", "ismany", "!ismany", "set", "!set"];
795
796/// The (partly) broken-down contents of a line containing a test directive,
797/// which [`iter_directives`] passes to its callback function.
798///
799/// For example:
800///
801/// ```text
802/// //@ compile-flags: -O
803///     ^^^^^^^^^^^^^^^^^ raw_directive
804///
805/// //@ [foo] compile-flags: -O
806///      ^^^                    revision
807///           ^^^^^^^^^^^^^^^^^ raw_directive
808/// ```
809struct DirectiveLine<'ln> {
810    line_number: usize,
811    /// Some test directives start with a revision name in square brackets
812    /// (e.g. `[foo]`), and only apply to that revision of the test.
813    /// If present, this field contains the revision name (e.g. `foo`).
814    revision: Option<&'ln str>,
815    /// The main part of the directive, after removing the comment prefix
816    /// and the optional revision specifier.
817    ///
818    /// This is "raw" because the directive's name and colon-separated value
819    /// (if present) have not yet been extracted or checked.
820    raw_directive: &'ln str,
821}
822
823impl<'ln> DirectiveLine<'ln> {
824    fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
825        self.revision.is_none() || self.revision == test_revision
826    }
827}
828
829pub(crate) struct CheckDirectiveResult<'ln> {
830    is_known_directive: bool,
831    trailing_directive: Option<&'ln str>,
832}
833
834pub(crate) fn check_directive<'a>(
835    directive_ln: &'a str,
836    mode: Mode,
837    original_line: &str,
838) -> CheckDirectiveResult<'a> {
839    let (directive_name, post) = directive_ln.split_once([':', ' ']).unwrap_or((directive_ln, ""));
840
841    let trailing = post.trim().split_once(' ').map(|(pre, _)| pre).unwrap_or(post);
842    let is_known = |s: &str| {
843        KNOWN_DIRECTIVE_NAMES.contains(&s)
844            || match mode {
845                Mode::Rustdoc | Mode::RustdocJson => {
846                    original_line.starts_with("//@")
847                        && match mode {
848                            Mode::Rustdoc => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES,
849                            Mode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
850                            _ => unreachable!(),
851                        }
852                        .contains(&s)
853                }
854                _ => false,
855            }
856    };
857    let trailing_directive = {
858        // 1. is the directive name followed by a space? (to exclude `:`)
859        matches!(directive_ln.get(directive_name.len()..), Some(s) if s.starts_with(' '))
860            // 2. is what is after that directive also a directive (ex: "only-x86 only-arm")
861            && is_known(trailing)
862    }
863    .then_some(trailing);
864
865    CheckDirectiveResult { is_known_directive: is_known(&directive_name), trailing_directive }
866}
867
868const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
869
870fn iter_directives(
871    mode: Mode,
872    _suite: &str,
873    poisoned: &mut bool,
874    testfile: &Utf8Path,
875    rdr: impl Read,
876    it: &mut dyn FnMut(DirectiveLine<'_>),
877) {
878    if testfile.is_dir() {
879        return;
880    }
881
882    // Coverage tests in coverage-run mode always have these extra directives, without needing to
883    // specify them manually in every test file.
884    //
885    // FIXME(jieyouxu): I feel like there's a better way to do this, leaving for later.
886    if mode == Mode::CoverageRun {
887        let extra_directives: &[&str] = &[
888            "needs-profiler-runtime",
889            // FIXME(pietroalbini): this test currently does not work on cross-compiled targets
890            // because remote-test is not capable of sending back the *.profraw files generated by
891            // the LLVM instrumentation.
892            "ignore-cross-compile",
893        ];
894        // Process the extra implied directives, with a dummy line number of 0.
895        for raw_directive in extra_directives {
896            it(DirectiveLine { line_number: 0, revision: None, raw_directive });
897        }
898    }
899
900    let mut rdr = BufReader::with_capacity(1024, rdr);
901    let mut ln = String::new();
902    let mut line_number = 0;
903
904    loop {
905        line_number += 1;
906        ln.clear();
907        if rdr.read_line(&mut ln).unwrap() == 0 {
908            break;
909        }
910        let ln = ln.trim();
911
912        let Some(directive_line) = line_directive(line_number, ln) else {
913            continue;
914        };
915
916        // Perform unknown directive check on Rust files.
917        if testfile.extension().map(|e| e == "rs").unwrap_or(false) {
918            let CheckDirectiveResult { is_known_directive, trailing_directive } =
919                check_directive(directive_line.raw_directive, mode, ln);
920
921            if !is_known_directive {
922                *poisoned = true;
923
924                error!(
925                    "{testfile}:{line_number}: detected unknown compiletest test directive `{}`",
926                    directive_line.raw_directive,
927                );
928
929                return;
930            }
931
932            if let Some(trailing_directive) = &trailing_directive {
933                *poisoned = true;
934
935                error!(
936                    "{testfile}:{line_number}: detected trailing compiletest test directive `{}`",
937                    trailing_directive,
938                );
939                help!("put the trailing directive in it's own line: `//@ {}`", trailing_directive);
940
941                return;
942            }
943        }
944
945        it(directive_line);
946    }
947}
948
949impl Config {
950    fn parse_and_update_revisions(
951        &self,
952        testfile: &Utf8Path,
953        line: &str,
954        existing: &mut Vec<String>,
955    ) {
956        const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
957            // `//@ revisions: true false` Implying `--cfg=true` and `--cfg=false` makes it very
958            // weird for the test, since if the test writer wants a cfg of the same revision name
959            // they'd have to use `cfg(r#true)` and `cfg(r#false)`.
960            "true", "false",
961        ];
962
963        const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
964            ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
965
966        if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
967            if self.mode == Mode::RunMake {
968                panic!("`run-make` tests do not support revisions: {}", testfile);
969            }
970
971            let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
972            for revision in raw.split_whitespace() {
973                if !duplicates.insert(revision.to_string()) {
974                    panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
975                }
976
977                if FORBIDDEN_REVISION_NAMES.contains(&revision) {
978                    panic!(
979                        "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
980                        revision, raw, testfile
981                    );
982                }
983
984                if matches!(self.mode, Mode::Assembly | Mode::Codegen | Mode::MirOpt)
985                    && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
986                {
987                    panic!(
988                        "revision name `{revision}` is not permitted in a test suite that uses \
989                        `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
990                        prefix: `{revision}` in line `{}`: {}",
991                        raw, testfile
992                    );
993                }
994
995                existing.push(revision.to_string());
996            }
997        }
998    }
999
1000    fn parse_env(nv: String) -> (String, String) {
1001        // nv is either FOO or FOO=BAR
1002        // FIXME(Zalathar): The form without `=` seems to be unused; should
1003        // we drop support for it?
1004        let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
1005        // Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
1006        // sees the name as `FOO` and not ` FOO`.
1007        let name = name.trim();
1008        (name.to_owned(), value.to_owned())
1009    }
1010
1011    fn parse_pp_exact(&self, line: &str, testfile: &Utf8Path) -> Option<Utf8PathBuf> {
1012        if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
1013            Some(Utf8PathBuf::from(&s))
1014        } else if self.parse_name_directive(line, "pp-exact") {
1015            testfile.file_name().map(Utf8PathBuf::from)
1016        } else {
1017            None
1018        }
1019    }
1020
1021    fn parse_custom_normalization(&self, raw_directive: &str) -> Option<NormalizeRule> {
1022        // FIXME(Zalathar): Integrate name/value splitting into `DirectiveLine`
1023        // instead of doing it here.
1024        let (directive_name, raw_value) = raw_directive.split_once(':')?;
1025
1026        let kind = match directive_name {
1027            "normalize-stdout" => NormalizeKind::Stdout,
1028            "normalize-stderr" => NormalizeKind::Stderr,
1029            "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
1030            "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
1031            _ => return None,
1032        };
1033
1034        let Some((regex, replacement)) = parse_normalize_rule(raw_value) else {
1035            error!("couldn't parse custom normalization rule: `{raw_directive}`");
1036            help!("expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`");
1037            panic!("invalid normalization rule detected");
1038        };
1039        Some(NormalizeRule { kind, regex, replacement })
1040    }
1041
1042    fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
1043        // Ensure the directive is a whole word. Do not match "ignore-x86" when
1044        // the line says "ignore-x86_64".
1045        line.starts_with(directive)
1046            && matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
1047    }
1048
1049    fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
1050        line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
1051    }
1052
1053    pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
1054        let colon = directive.len();
1055        if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
1056            let value = line[(colon + 1)..].to_owned();
1057            debug!("{}: {}", directive, value);
1058            Some(expand_variables(value, self))
1059        } else {
1060            None
1061        }
1062    }
1063
1064    fn parse_edition(&self, line: &str) -> Option<String> {
1065        self.parse_name_value_directive(line, "edition")
1066    }
1067
1068    fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
1069        match value {
1070            true => {
1071                if self.parse_negative_name_directive(line, directive) {
1072                    *value = false;
1073                }
1074            }
1075            false => {
1076                if self.parse_name_directive(line, directive) {
1077                    *value = true;
1078                }
1079            }
1080        }
1081    }
1082
1083    fn set_name_value_directive<T>(
1084        &self,
1085        line: &str,
1086        directive: &str,
1087        value: &mut Option<T>,
1088        parse: impl FnOnce(String) -> T,
1089    ) {
1090        if value.is_none() {
1091            *value = self.parse_name_value_directive(line, directive).map(parse);
1092        }
1093    }
1094
1095    fn push_name_value_directive<T>(
1096        &self,
1097        line: &str,
1098        directive: &str,
1099        values: &mut Vec<T>,
1100        parse: impl FnOnce(String) -> T,
1101    ) {
1102        if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
1103            values.push(value);
1104        }
1105    }
1106}
1107
1108// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
1109fn expand_variables(mut value: String, config: &Config) -> String {
1110    const CWD: &str = "{{cwd}}";
1111    const SRC_BASE: &str = "{{src-base}}";
1112    const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
1113    const RUST_SRC_BASE: &str = "{{rust-src-base}}";
1114    const SYSROOT_BASE: &str = "{{sysroot-base}}";
1115    const TARGET_LINKER: &str = "{{target-linker}}";
1116    const TARGET: &str = "{{target}}";
1117
1118    if value.contains(CWD) {
1119        let cwd = env::current_dir().unwrap();
1120        value = value.replace(CWD, &cwd.to_str().unwrap());
1121    }
1122
1123    if value.contains(SRC_BASE) {
1124        value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
1125    }
1126
1127    if value.contains(TEST_SUITE_BUILD_BASE) {
1128        value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
1129    }
1130
1131    if value.contains(SYSROOT_BASE) {
1132        value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
1133    }
1134
1135    if value.contains(TARGET_LINKER) {
1136        value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
1137    }
1138
1139    if value.contains(TARGET) {
1140        value = value.replace(TARGET, &config.target);
1141    }
1142
1143    if value.contains(RUST_SRC_BASE) {
1144        let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
1145        src_base.try_exists().expect(&*format!("{} should exists", src_base));
1146        let src_base = src_base.read_link_utf8().unwrap_or(src_base);
1147        value = value.replace(RUST_SRC_BASE, &src_base.as_str());
1148    }
1149
1150    value
1151}
1152
1153struct NormalizeRule {
1154    kind: NormalizeKind,
1155    regex: String,
1156    replacement: String,
1157}
1158
1159enum NormalizeKind {
1160    Stdout,
1161    Stderr,
1162    Stderr32bit,
1163    Stderr64bit,
1164}
1165
1166/// Parses the regex and replacement values of a `//@ normalize-*` directive, in the format:
1167/// ```text
1168/// "REGEX" -> "REPLACEMENT"
1169/// ```
1170fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
1171    // FIXME: Support escaped double-quotes in strings.
1172    let captures = static_regex!(
1173        r#"(?x) # (verbose mode regex)
1174        ^
1175        \s*                     # (leading whitespace)
1176        "(?<regex>[^"]*)"       # "REGEX"
1177        \s+->\s+                # ->
1178        "(?<replacement>[^"]*)" # "REPLACEMENT"
1179        $
1180        "#
1181    )
1182    .captures(raw_value)?;
1183    let regex = captures["regex"].to_owned();
1184    let replacement = captures["replacement"].to_owned();
1185    // A `\n` sequence in the replacement becomes an actual newline.
1186    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
1187    // backslashes and double-quotes.
1188    let replacement = replacement.replace("\\n", "\n");
1189    Some((regex, replacement))
1190}
1191
1192/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
1193/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
1194/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
1195/// having to write `//@ min-llvm-version: 19.0.0`.
1196///
1197/// Currently panics if the input string is malformed, though we really should not use panic as an
1198/// error handling strategy.
1199///
1200/// FIXME(jieyouxu): improve error handling
1201pub fn extract_llvm_version(version: &str) -> Version {
1202    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
1203    // fancy suffix like `-rc1` or `meow`.
1204    let version = version.trim();
1205    let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
1206    let version_without_suffix = match version.split_once(uninterested) {
1207        Some((prefix, _suffix)) => prefix,
1208        None => version,
1209    };
1210
1211    let components: Vec<u64> = version_without_suffix
1212        .split('.')
1213        .map(|s| s.parse().expect("llvm version component should consist of only digits"))
1214        .collect();
1215
1216    match &components[..] {
1217        [major] => Version::new(*major, 0, 0),
1218        [major, minor] => Version::new(*major, *minor, 0),
1219        [major, minor, patch] => Version::new(*major, *minor, *patch),
1220        _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
1221    }
1222}
1223
1224pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
1225    let output = Command::new(binary_path).arg("--version").output().ok()?;
1226    if !output.status.success() {
1227        return None;
1228    }
1229    let version = String::from_utf8(output.stdout).ok()?;
1230    for line in version.lines() {
1231        if let Some(version) = line.split("LLVM version ").nth(1) {
1232            return Some(extract_llvm_version(version));
1233        }
1234    }
1235    None
1236}
1237
1238/// For tests using the `needs-llvm-zstd` directive:
1239/// - for local LLVM builds, try to find the static zstd library in the llvm-config system libs.
1240/// - for `download-ci-llvm`, see if `lld` was built with zstd support.
1241pub fn llvm_has_libzstd(config: &Config) -> bool {
1242    // Strategy 1: works for local builds but not with `download-ci-llvm`.
1243    //
1244    // We check whether `llvm-config` returns the zstd library. Bootstrap's `llvm.libzstd` will only
1245    // ask to statically link it when building LLVM, so we only check if the list of system libs
1246    // contains a path to that static lib, and that it exists.
1247    //
1248    // See compiler/rustc_llvm/build.rs for more details and similar expectations.
1249    fn is_zstd_in_config(llvm_bin_dir: &Utf8Path) -> Option<()> {
1250        let llvm_config_path = llvm_bin_dir.join("llvm-config");
1251        let output = Command::new(llvm_config_path).arg("--system-libs").output().ok()?;
1252        assert!(output.status.success(), "running llvm-config --system-libs failed");
1253
1254        let libs = String::from_utf8(output.stdout).ok()?;
1255        for lib in libs.split_whitespace() {
1256            if lib.ends_with("libzstd.a") && Utf8Path::new(lib).exists() {
1257                return Some(());
1258            }
1259        }
1260
1261        None
1262    }
1263
1264    // Strategy 2: `download-ci-llvm`'s `llvm-config --system-libs` will not return any libs to
1265    // use.
1266    //
1267    // The CI artifacts also don't contain the bootstrap config used to build them: otherwise we
1268    // could have looked at the `llvm.libzstd` config.
1269    //
1270    // We infer whether `LLVM_ENABLE_ZSTD` was used to build LLVM as a byproduct of testing whether
1271    // `lld` supports it. If not, an error will be emitted: "LLVM was not built with
1272    // LLVM_ENABLE_ZSTD or did not find zstd at build time".
1273    #[cfg(unix)]
1274    fn is_lld_built_with_zstd(llvm_bin_dir: &Utf8Path) -> Option<()> {
1275        let lld_path = llvm_bin_dir.join("lld");
1276        if lld_path.exists() {
1277            // We can't call `lld` as-is, it expects to be invoked by a compiler driver using a
1278            // different name. Prepare a temporary symlink to do that.
1279            let lld_symlink_path = llvm_bin_dir.join("ld.lld");
1280            if !lld_symlink_path.exists() {
1281                std::os::unix::fs::symlink(lld_path, &lld_symlink_path).ok()?;
1282            }
1283
1284            // Run `lld` with a zstd flag. We expect this command to always error here, we don't
1285            // want to link actual files and don't pass any.
1286            let output = Command::new(&lld_symlink_path)
1287                .arg("--compress-debug-sections=zstd")
1288                .output()
1289                .ok()?;
1290            assert!(!output.status.success());
1291
1292            // Look for a specific error caused by LLVM not being built with zstd support. We could
1293            // also look for the "no input files" message, indicating the zstd flag was accepted.
1294            let stderr = String::from_utf8(output.stderr).ok()?;
1295            let zstd_available = !stderr.contains("LLVM was not built with LLVM_ENABLE_ZSTD");
1296
1297            // We don't particularly need to clean the link up (so the previous commands could fail
1298            // in theory but won't in practice), but we can try.
1299            std::fs::remove_file(lld_symlink_path).ok()?;
1300
1301            if zstd_available {
1302                return Some(());
1303            }
1304        }
1305
1306        None
1307    }
1308
1309    #[cfg(not(unix))]
1310    fn is_lld_built_with_zstd(_llvm_bin_dir: &Utf8Path) -> Option<()> {
1311        None
1312    }
1313
1314    if let Some(llvm_bin_dir) = &config.llvm_bin_dir {
1315        // Strategy 1: for local LLVM builds.
1316        if is_zstd_in_config(llvm_bin_dir).is_some() {
1317            return true;
1318        }
1319
1320        // Strategy 2: for LLVM artifacts built on CI via `download-ci-llvm`.
1321        //
1322        // It doesn't work for cases where the artifacts don't contain the linker, but it's
1323        // best-effort: CI has `llvm.libzstd` and `lld` enabled on the x64 linux artifacts, so it
1324        // will at least work there.
1325        //
1326        // If this can be improved and expanded to less common cases in the future, it should.
1327        if config.target == "x86_64-unknown-linux-gnu"
1328            && config.host == config.target
1329            && is_lld_built_with_zstd(llvm_bin_dir).is_some()
1330        {
1331            return true;
1332        }
1333    }
1334
1335    // Otherwise, all hope is lost.
1336    false
1337}
1338
1339/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
1340/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
1341///
1342/// If the `<version2>` part is omitted, the second component of the tuple is the same as
1343/// `<version1>`.
1344fn extract_version_range<'a, F, VersionTy: Clone>(
1345    line: &'a str,
1346    parse: F,
1347) -> Option<(VersionTy, VersionTy)>
1348where
1349    F: Fn(&'a str) -> Option<VersionTy>,
1350{
1351    let mut splits = line.splitn(2, "- ").map(str::trim);
1352    let min = splits.next().unwrap();
1353    if min.ends_with('-') {
1354        return None;
1355    }
1356
1357    let max = splits.next();
1358
1359    if min.is_empty() {
1360        return None;
1361    }
1362
1363    let min = parse(min)?;
1364    let max = match max {
1365        Some("") => return None,
1366        Some(max) => parse(max)?,
1367        _ => min.clone(),
1368    };
1369
1370    Some((min, max))
1371}
1372
1373pub(crate) fn make_test_description<R: Read>(
1374    config: &Config,
1375    cache: &DirectivesCache,
1376    name: String,
1377    path: &Utf8Path,
1378    src: R,
1379    test_revision: Option<&str>,
1380    poisoned: &mut bool,
1381) -> CollectedTestDesc {
1382    let mut ignore = false;
1383    let mut ignore_message = None;
1384    let mut should_fail = false;
1385
1386    let mut local_poisoned = false;
1387
1388    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
1389    iter_directives(
1390        config.mode,
1391        &config.suite,
1392        &mut local_poisoned,
1393        path,
1394        src,
1395        &mut |directive @ DirectiveLine { line_number, raw_directive: ln, .. }| {
1396            if !directive.applies_to_test_revision(test_revision) {
1397                return;
1398            }
1399
1400            macro_rules! decision {
1401                ($e:expr) => {
1402                    match $e {
1403                        IgnoreDecision::Ignore { reason } => {
1404                            ignore = true;
1405                            ignore_message = Some(reason.into());
1406                        }
1407                        IgnoreDecision::Error { message } => {
1408                            error!("{path}:{line_number}: {message}");
1409                            *poisoned = true;
1410                            return;
1411                        }
1412                        IgnoreDecision::Continue => {}
1413                    }
1414                };
1415            }
1416
1417            decision!(cfg::handle_ignore(config, ln));
1418            decision!(cfg::handle_only(config, ln));
1419            decision!(needs::handle_needs(&cache.needs, config, ln));
1420            decision!(ignore_llvm(config, path, ln));
1421            decision!(ignore_cdb(config, ln));
1422            decision!(ignore_gdb(config, ln));
1423            decision!(ignore_lldb(config, ln));
1424
1425            if config.target == "wasm32-unknown-unknown"
1426                && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1427            {
1428                decision!(IgnoreDecision::Ignore {
1429                    reason: "ignored on WASM as the run results cannot be checked there".into(),
1430                });
1431            }
1432
1433            should_fail |= config.parse_name_directive(ln, "should-fail");
1434        },
1435    );
1436
1437    if local_poisoned {
1438        eprintln!("errors encountered when trying to make test description: {}", path);
1439        panic!("errors encountered when trying to make test description");
1440    }
1441
1442    // The `should-fail` annotation doesn't apply to pretty tests,
1443    // since we run the pretty printer across all tests by default.
1444    // If desired, we could add a `should-fail-pretty` annotation.
1445    let should_panic = match config.mode {
1446        crate::common::Pretty => ShouldPanic::No,
1447        _ if should_fail => ShouldPanic::Yes,
1448        _ => ShouldPanic::No,
1449    };
1450
1451    CollectedTestDesc { name, ignore, ignore_message, should_panic }
1452}
1453
1454fn ignore_cdb(config: &Config, line: &str) -> IgnoreDecision {
1455    if config.debugger != Some(Debugger::Cdb) {
1456        return IgnoreDecision::Continue;
1457    }
1458
1459    if let Some(actual_version) = config.cdb_version {
1460        if let Some(rest) = line.strip_prefix("min-cdb-version:").map(str::trim) {
1461            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1462                panic!("couldn't parse version range: {:?}", rest);
1463            });
1464
1465            // Ignore if actual version is smaller than the minimum
1466            // required version
1467            if actual_version < min_version {
1468                return IgnoreDecision::Ignore {
1469                    reason: format!("ignored when the CDB version is lower than {rest}"),
1470                };
1471            }
1472        }
1473    }
1474    IgnoreDecision::Continue
1475}
1476
1477fn ignore_gdb(config: &Config, line: &str) -> IgnoreDecision {
1478    if config.debugger != Some(Debugger::Gdb) {
1479        return IgnoreDecision::Continue;
1480    }
1481
1482    if let Some(actual_version) = config.gdb_version {
1483        if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
1484            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1485                .unwrap_or_else(|| {
1486                    panic!("couldn't parse version range: {:?}", rest);
1487                });
1488
1489            if start_ver != end_ver {
1490                panic!("Expected single GDB version")
1491            }
1492            // Ignore if actual version is smaller than the minimum
1493            // required version
1494            if actual_version < start_ver {
1495                return IgnoreDecision::Ignore {
1496                    reason: format!("ignored when the GDB version is lower than {rest}"),
1497                };
1498            }
1499        } else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
1500            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1501                .unwrap_or_else(|| {
1502                    panic!("couldn't parse version range: {:?}", rest);
1503                });
1504
1505            if max_version < min_version {
1506                panic!("Malformed GDB version range: max < min")
1507            }
1508
1509            if actual_version >= min_version && actual_version <= max_version {
1510                if min_version == max_version {
1511                    return IgnoreDecision::Ignore {
1512                        reason: format!("ignored when the GDB version is {rest}"),
1513                    };
1514                } else {
1515                    return IgnoreDecision::Ignore {
1516                        reason: format!("ignored when the GDB version is between {rest}"),
1517                    };
1518                }
1519            }
1520        }
1521    }
1522    IgnoreDecision::Continue
1523}
1524
1525fn ignore_lldb(config: &Config, line: &str) -> IgnoreDecision {
1526    if config.debugger != Some(Debugger::Lldb) {
1527        return IgnoreDecision::Continue;
1528    }
1529
1530    if let Some(actual_version) = config.lldb_version {
1531        if let Some(rest) = line.strip_prefix("min-lldb-version:").map(str::trim) {
1532            let min_version = rest.parse().unwrap_or_else(|e| {
1533                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1534            });
1535            // Ignore if actual version is smaller the minimum required
1536            // version
1537            if actual_version < min_version {
1538                return IgnoreDecision::Ignore {
1539                    reason: format!("ignored when the LLDB version is {rest}"),
1540                };
1541            }
1542        }
1543    }
1544    IgnoreDecision::Continue
1545}
1546
1547fn ignore_llvm(config: &Config, path: &Utf8Path, line: &str) -> IgnoreDecision {
1548    if let Some(needed_components) =
1549        config.parse_name_value_directive(line, "needs-llvm-components")
1550    {
1551        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1552        if let Some(missing_component) = needed_components
1553            .split_whitespace()
1554            .find(|needed_component| !components.contains(needed_component))
1555        {
1556            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1557                panic!(
1558                    "missing LLVM component {}, and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {}",
1559                    missing_component, path
1560                );
1561            }
1562            return IgnoreDecision::Ignore {
1563                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1564            };
1565        }
1566    }
1567    if let Some(actual_version) = &config.llvm_version {
1568        // Note that these `min` versions will check for not just major versions.
1569
1570        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1571            let min_version = extract_llvm_version(&version_string);
1572            // Ignore if actual version is smaller than the minimum required version.
1573            if *actual_version < min_version {
1574                return IgnoreDecision::Ignore {
1575                    reason: format!(
1576                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1577                    ),
1578                };
1579            }
1580        } else if let Some(version_string) =
1581            config.parse_name_value_directive(line, "max-llvm-major-version")
1582        {
1583            let max_version = extract_llvm_version(&version_string);
1584            // Ignore if actual major version is larger than the maximum required major version.
1585            if actual_version.major > max_version.major {
1586                return IgnoreDecision::Ignore {
1587                    reason: format!(
1588                        "ignored when the LLVM version ({actual_version}) is newer than major\
1589                        version {}",
1590                        max_version.major
1591                    ),
1592                };
1593            }
1594        } else if let Some(version_string) =
1595            config.parse_name_value_directive(line, "min-system-llvm-version")
1596        {
1597            let min_version = extract_llvm_version(&version_string);
1598            // Ignore if using system LLVM and actual version
1599            // is smaller the minimum required version
1600            if config.system_llvm && *actual_version < min_version {
1601                return IgnoreDecision::Ignore {
1602                    reason: format!(
1603                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1604                    ),
1605                };
1606            }
1607        } else if let Some(version_range) =
1608            config.parse_name_value_directive(line, "ignore-llvm-version")
1609        {
1610            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1611            let (v_min, v_max) =
1612                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1613                    .unwrap_or_else(|| {
1614                        panic!("couldn't parse version range: \"{version_range}\"");
1615                    });
1616            if v_max < v_min {
1617                panic!("malformed LLVM version range where {v_max} < {v_min}")
1618            }
1619            // Ignore if version lies inside of range.
1620            if *actual_version >= v_min && *actual_version <= v_max {
1621                if v_min == v_max {
1622                    return IgnoreDecision::Ignore {
1623                        reason: format!("ignored when the LLVM version is {actual_version}"),
1624                    };
1625                } else {
1626                    return IgnoreDecision::Ignore {
1627                        reason: format!(
1628                            "ignored when the LLVM version is between {v_min} and {v_max}"
1629                        ),
1630                    };
1631                }
1632            }
1633        } else if let Some(version_string) =
1634            config.parse_name_value_directive(line, "exact-llvm-major-version")
1635        {
1636            // Syntax is "exact-llvm-major-version: <version>"
1637            let version = extract_llvm_version(&version_string);
1638            if actual_version.major != version.major {
1639                return IgnoreDecision::Ignore {
1640                    reason: format!(
1641                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1642                        actual_version.major, version.major
1643                    ),
1644                };
1645            }
1646        }
1647    }
1648    IgnoreDecision::Continue
1649}
1650
1651enum IgnoreDecision {
1652    Ignore { reason: String },
1653    Continue,
1654    Error { message: String },
1655}