Skip to main content

compiletest/directives/
file.rs

1use camino::Utf8Path;
2
3use crate::directives::LineNumber;
4use crate::directives::line::{DirectiveLine, line_directive};
5
6pub(crate) struct FileDirectives<'a> {
7    pub(crate) path: &'a Utf8Path,
8    pub(crate) lines: Vec<DirectiveLine<'a>>,
9
10    /// Whether the test source file contains an explicit `#![no_std]`/`#![no_core]` attribute.
11    pub(crate) has_explicit_no_std_core_attribute: bool,
12}
13
14impl<'a> FileDirectives<'a> {
15    pub(crate) fn from_file_contents(path: &'a Utf8Path, file_contents: &'a str) -> Self {
16        let mut lines = vec![];
17        let mut has_explicit_no_std_core_attribute = false;
18
19        for (line_number, ln) in LineNumber::enumerate().zip(file_contents.lines()) {
20            let ln = ln.trim();
21
22            // Perform a naive check for lines starting with `#![no_std]`/`#![no_core]`, which
23            // suppresses the implied `//@ needs-target-std` in codegen tests. This ignores
24            // occurrences in ordinary comments.
25            //
26            // This check is imperfect in some edge cases, but we can generally trust our own test
27            // suite to not hit those edge cases (e.g. `#![no_std]`/`#![no_core]` in multi-line
28            // comments or string literals). Tests can write `//@ needs-target-std` manually if
29            // needed.
30            if ln.starts_with("#![no_std]") || ln.starts_with("#![no_core]") {
31                has_explicit_no_std_core_attribute = true;
32                continue;
33            }
34
35            if let Some(directive_line) = line_directive(path, line_number, ln) {
36                lines.push(directive_line);
37            }
38        }
39
40        Self { path, lines, has_explicit_no_std_core_attribute }
41    }
42}