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