Skip to main content

compiletest/directives/
handlers.rs

1use std::collections::HashMap;
2use std::sync::{Arc, LazyLock};
3
4use crate::common::Config;
5use crate::directives::{
6    DirectiveLine, NormalizeKind, NormalizeRule, TestProps, parse_and_update_aux,
7    parse_edition_range, split_flags,
8};
9use crate::errors::ErrorKind;
10
11pub(crate) static DIRECTIVE_HANDLERS_MAP: LazyLock<HashMap<&str, Handler>> =
12    LazyLock::new(make_directive_handlers_map);
13
14#[derive(Clone)]
15pub(crate) struct Handler {
16    handler_fn: Arc<dyn Fn(HandlerArgs<'_>) + Send + Sync>,
17}
18
19impl Handler {
20    pub(crate) fn handle(&self, config: &Config, line: &DirectiveLine<'_>, props: &mut TestProps) {
21        (self.handler_fn)(HandlerArgs { config, line, props })
22    }
23}
24
25struct HandlerArgs<'a> {
26    config: &'a Config,
27    line: &'a DirectiveLine<'a>,
28    props: &'a mut TestProps,
29}
30
31/// Intermediate data structure, used for defining a list of handlers.
32struct NamedHandler {
33    names: Vec<&'static str>,
34    handler: Handler,
35}
36
37/// Helper function to create a simple handler, so that changes can be made
38/// to the handler struct without disturbing existing handler declarations.
39fn handler(
40    name: &'static str,
41    handler_fn: impl Fn(&Config, &DirectiveLine<'_>, &mut TestProps) + Send + Sync + 'static,
42) -> NamedHandler {
43    multi_handler(&[name], handler_fn)
44}
45
46/// Associates the same handler function with multiple directive names.
47fn multi_handler(
48    names: &[&'static str],
49    handler_fn: impl Fn(&Config, &DirectiveLine<'_>, &mut TestProps) + Send + Sync + 'static,
50) -> NamedHandler {
51    NamedHandler {
52        names: names.to_owned(),
53        handler: Handler {
54            handler_fn: Arc::new(move |args| handler_fn(args.config, args.line, args.props)),
55        },
56    }
57}
58
59fn make_directive_handlers_map() -> HashMap<&'static str, Handler> {
60    use crate::directives::directives::*;
61
62    // FIXME(Zalathar): Now that most directive-processing has been extracted
63    // into individual handlers, there should be many opportunities to simplify
64    // these handlers, e.g. by getting rid of now-redundant name checks.
65
66    let handlers: Vec<NamedHandler> = vec![
67        handler(ERROR_PATTERN, |config, ln, props| {
68            config.push_name_value_directive(ln, ERROR_PATTERN, &mut props.error_patterns, |r| r);
69        }),
70        handler(REGEX_ERROR_PATTERN, |config, ln, props| {
71            config.push_name_value_directive(
72                ln,
73                REGEX_ERROR_PATTERN,
74                &mut props.regex_error_patterns,
75                |r| r,
76            );
77        }),
78        handler(DOC_FLAGS, |config, ln, props| {
79            config.push_name_value_directive(ln, DOC_FLAGS, &mut props.doc_flags, |r| r);
80        }),
81        handler(COMPILE_FLAGS, |config, ln, props| {
82            if let Some(flags) = config.parse_name_value_directive(ln, COMPILE_FLAGS) {
83                let flags = split_flags(&flags);
84                // FIXME(#147955): Extract and unify this with other handlers that
85                // check compiler flags, e.g. MINICORE_COMPILE_FLAGS.
86                for (i, flag) in flags.iter().enumerate() {
87                    if flag == "--edition" || flag.starts_with("--edition=") {
88                        panic!("you must use `//@ edition` to configure the edition");
89                    }
90                    if (flag == "-C"
91                        && flags.get(i + 1).is_some_and(|v| v.starts_with("incremental=")))
92                        || flag.starts_with("-Cincremental=")
93                    {
94                        panic!("you must use `//@ incremental` to enable incremental compilation");
95                    }
96                }
97                props.compile_flags.extend(flags);
98            }
99        }),
100        handler("edition", |config, ln, props| {
101            if let Some(range) = parse_edition_range(config, ln) {
102                props.edition = Some(range.edition_to_test(config.edition));
103            }
104        }),
105        handler("revisions", |config, ln, props| {
106            config.parse_and_update_revisions(ln, &mut props.revisions);
107        }),
108        handler(RUN_FLAGS, |config, ln, props| {
109            if let Some(flags) = config.parse_name_value_directive(ln, RUN_FLAGS) {
110                props.run_flags.extend(split_flags(&flags));
111            }
112        }),
113        handler("pp-exact", |config, ln, props| {
114            if props.pp_exact.is_none() {
115                props.pp_exact = config.parse_pp_exact(ln);
116            }
117        }),
118        handler(BUILD_AUX_DOCS, |config, ln, props| {
119            config.set_name_directive(ln, BUILD_AUX_DOCS, &mut props.build_aux_docs);
120        }),
121        handler(UNIQUE_DOC_OUT_DIR, |config, ln, props| {
122            config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut props.unique_doc_out_dir);
123        }),
124        handler(FORCE_HOST, |config, ln, props| {
125            config.set_name_directive(ln, FORCE_HOST, &mut props.force_host);
126        }),
127        handler(CHECK_STDOUT, |config, ln, props| {
128            config.set_name_directive(ln, CHECK_STDOUT, &mut props.check_stdout);
129        }),
130        handler(CHECK_RUN_RESULTS, |config, ln, props| {
131            config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut props.check_run_results);
132        }),
133        handler(DONT_CHECK_COMPILER_STDOUT, |config, ln, props| {
134            config.set_name_directive(
135                ln,
136                DONT_CHECK_COMPILER_STDOUT,
137                &mut props.dont_check_compiler_stdout,
138            );
139        }),
140        handler(DONT_CHECK_COMPILER_STDERR, |config, ln, props| {
141            config.set_name_directive(
142                ln,
143                DONT_CHECK_COMPILER_STDERR,
144                &mut props.dont_check_compiler_stderr,
145            );
146        }),
147        handler(NO_PREFER_DYNAMIC, |config, ln, props| {
148            config.set_name_directive(ln, NO_PREFER_DYNAMIC, &mut props.no_prefer_dynamic);
149        }),
150        handler(PRETTY_MODE, |config, ln, props| {
151            if let Some(m) = config.parse_name_value_directive(ln, PRETTY_MODE) {
152                props.pretty_mode = m;
153            }
154        }),
155        handler(PRETTY_COMPARE_ONLY, |config, ln, props| {
156            config.set_name_directive(ln, PRETTY_COMPARE_ONLY, &mut props.pretty_compare_only);
157        }),
158        multi_handler(
159            &[AUX_BUILD, AUX_BIN, AUX_CRATE, PROC_MACRO, AUX_CODEGEN_BACKEND],
160            |config, ln, props| {
161                // Call a helper method to deal with aux-related directives.
162                parse_and_update_aux(config, ln, &mut props.aux);
163            },
164        ),
165        handler(EXEC_ENV, |config, ln, props| {
166            config.push_name_value_directive(ln, EXEC_ENV, &mut props.exec_env, Config::parse_env);
167        }),
168        handler(UNSET_EXEC_ENV, |config, ln, props| {
169            config.push_name_value_directive(ln, UNSET_EXEC_ENV, &mut props.unset_exec_env, |r| {
170                r.trim().to_owned()
171            });
172        }),
173        handler(RUSTC_ENV, |config, ln, props| {
174            config.push_name_value_directive(
175                ln,
176                RUSTC_ENV,
177                &mut props.rustc_env,
178                Config::parse_env,
179            );
180        }),
181        handler(UNSET_RUSTC_ENV, |config, ln, props| {
182            config.push_name_value_directive(
183                ln,
184                UNSET_RUSTC_ENV,
185                &mut props.unset_rustc_env,
186                |r| r.trim().to_owned(),
187            );
188        }),
189        handler(FORBID_OUTPUT, |config, ln, props| {
190            config.push_name_value_directive(ln, FORBID_OUTPUT, &mut props.forbid_output, |r| r);
191        }),
192        handler(CHECK_TEST_LINE_NUMBERS_MATCH, |config, ln, props| {
193            config.set_name_directive(
194                ln,
195                CHECK_TEST_LINE_NUMBERS_MATCH,
196                &mut props.check_test_line_numbers_match,
197            );
198        }),
199        multi_handler(&["check-pass", "build-pass", "run-pass"], |config, ln, props| {
200            props.update_pass_mode(ln, config);
201        }),
202        multi_handler(
203            &["check-fail", "build-fail", "run-fail", "run-crash", "run-fail-or-crash"],
204            |config, ln, props| {
205                props.update_fail_mode(ln, config);
206            },
207        ),
208        handler(IGNORE_PASS, |config, ln, props| {
209            config.set_name_directive(ln, IGNORE_PASS, &mut props.ignore_pass);
210        }),
211        multi_handler(
212            &[
213                "normalize-stdout",
214                "normalize-stderr",
215                "normalize-stderr-32bit",
216                "normalize-stderr-64bit",
217            ],
218            |config, ln, props| {
219                if let Some(NormalizeRule { kind, regex, replacement }) =
220                    config.parse_custom_normalization(ln)
221                {
222                    let rule_tuple = (regex, replacement);
223                    match kind {
224                        NormalizeKind::Stdout => props.normalize_stdout.push(rule_tuple),
225                        NormalizeKind::Stderr => props.normalize_stderr.push(rule_tuple),
226                        NormalizeKind::Stderr32bit => {
227                            if config.target_cfg().pointer_width == 32 {
228                                props.normalize_stderr.push(rule_tuple);
229                            }
230                        }
231                        NormalizeKind::Stderr64bit => {
232                            if config.target_cfg().pointer_width == 64 {
233                                props.normalize_stderr.push(rule_tuple);
234                            }
235                        }
236                    }
237                }
238            },
239        ),
240        handler(FAILURE_STATUS, |config, ln, props| {
241            if let Some(code) = config
242                .parse_name_value_directive(ln, FAILURE_STATUS)
243                .and_then(|code| code.trim().parse::<i32>().ok())
244            {
245                props.failure_status = Some(code);
246            }
247        }),
248        handler(DONT_CHECK_FAILURE_STATUS, |config, ln, props| {
249            config.set_name_directive(
250                ln,
251                DONT_CHECK_FAILURE_STATUS,
252                &mut props.dont_check_failure_status,
253            );
254        }),
255        handler(RUN_RUSTFIX, |config, ln, props| {
256            config.set_name_directive(ln, RUN_RUSTFIX, &mut props.run_rustfix);
257        }),
258        handler(RUSTFIX_ONLY_MACHINE_APPLICABLE, |config, ln, props| {
259            config.set_name_directive(
260                ln,
261                RUSTFIX_ONLY_MACHINE_APPLICABLE,
262                &mut props.rustfix_only_machine_applicable,
263            );
264        }),
265        handler(ASSEMBLY_OUTPUT, |config, ln, props| {
266            config.set_name_value_directive(ln, ASSEMBLY_OUTPUT, &mut props.assembly_output, |r| {
267                r.trim().to_string()
268            });
269        }),
270        handler(STDERR_PER_BITWIDTH, |config, ln, props| {
271            config.set_name_directive(ln, STDERR_PER_BITWIDTH, &mut props.stderr_per_bitwidth);
272        }),
273        handler(INCREMENTAL, |config, ln, props| {
274            config.set_name_directive(ln, INCREMENTAL, &mut props.incremental);
275        }),
276        handler(KNOWN_BUG, |config, ln, props| {
277            // Unlike the other `name_value_directive`s this needs to be handled manually,
278            // because it sets a `bool` flag.
279            if let Some(known_bug) = config.parse_name_value_directive(ln, KNOWN_BUG) {
280                let known_bug = known_bug.trim();
281                if known_bug == "unknown"
282                    || known_bug.split(',').all(|issue_ref| {
283                        issue_ref
284                            .trim()
285                            .split_once('#')
286                            .filter(|(_, number)| number.chars().all(|digit| digit.is_numeric()))
287                            .is_some()
288                    })
289                {
290                    props.known_bug = true;
291                } else {
292                    panic!(
293                        "Invalid known-bug value: {known_bug}\nIt requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
294                    );
295                }
296            } else if config.parse_name_directive(ln, KNOWN_BUG) {
297                panic!(
298                    "Invalid known-bug attribute, requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
299                );
300            }
301        }),
302        handler(TEST_MIR_PASS, |config, ln, props| {
303            config.set_name_value_directive(ln, TEST_MIR_PASS, &mut props.mir_unit_test, |s| {
304                s.trim().to_string()
305            });
306        }),
307        handler(REMAP_SRC_BASE, |config, ln, props| {
308            config.set_name_directive(ln, REMAP_SRC_BASE, &mut props.remap_src_base);
309        }),
310        handler(LLVM_COV_FLAGS, |config, ln, props| {
311            if let Some(flags) = config.parse_name_value_directive(ln, LLVM_COV_FLAGS) {
312                props.llvm_cov_flags.extend(split_flags(&flags));
313            }
314        }),
315        handler(FILECHECK_FLAGS, |config, ln, props| {
316            if let Some(flags) = config.parse_name_value_directive(ln, FILECHECK_FLAGS) {
317                props.filecheck_flags.extend(split_flags(&flags));
318            }
319        }),
320        handler(NO_AUTO_CHECK_CFG, |config, ln, props| {
321            config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut props.no_auto_check_cfg);
322        }),
323        handler(ADD_MINICORE, |config, ln, props| {
324            props.update_add_minicore(ln, config);
325        }),
326        handler(MINICORE_COMPILE_FLAGS, |config, ln, props| {
327            if let Some(flags) = config.parse_name_value_directive(ln, MINICORE_COMPILE_FLAGS) {
328                let flags = split_flags(&flags);
329                // FIXME(#147955): Extract and unify this with other handlers that
330                // check compiler flags, e.g. COMPILE_FLAGS.
331                for flag in &flags {
332                    if flag == "--edition" || flag.starts_with("--edition=") {
333                        panic!("you must use `//@ edition` to configure the edition");
334                    }
335                }
336                props.minicore_compile_flags.extend(flags);
337            }
338        }),
339        handler(DONT_REQUIRE_ANNOTATIONS, |config, ln, props| {
340            if let Some(err_kind) = config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS)
341            {
342                props
343                    .dont_require_annotations
344                    .insert(ErrorKind::expect_from_user_str(err_kind.trim()));
345            }
346        }),
347        handler(DISABLE_GDB_PRETTY_PRINTERS, |config, ln, props| {
348            config.set_name_directive(
349                ln,
350                DISABLE_GDB_PRETTY_PRINTERS,
351                &mut props.disable_gdb_pretty_printers,
352            );
353        }),
354        handler(COMPARE_OUTPUT_BY_LINES, |config, ln, props| {
355            config.set_name_directive(
356                ln,
357                COMPARE_OUTPUT_BY_LINES,
358                &mut props.compare_output_by_lines,
359            );
360        }),
361    ];
362
363    handlers
364        .into_iter()
365        .flat_map(|NamedHandler { names, handler }| {
366            names.into_iter().map(move |name| (name, Handler::clone(&handler)))
367        })
368        .collect()
369}