compiletest/
directives.rs

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