compiletest/
directives.rs

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