2020-06-23 16:58:45 +00:00
|
|
|
//! Defines `Fixture` -- a convenient way to describe the initial state of
|
|
|
|
//! rust-analyzer database from a single string.
|
2021-04-06 18:42:09 +00:00
|
|
|
//!
|
|
|
|
//! Fixtures are strings containing rust source code with optional metadata.
|
|
|
|
//! A fixture without metadata is parsed into a single source file.
|
|
|
|
//! Use this to test functionality local to one file.
|
|
|
|
//!
|
|
|
|
//! Simple Example:
|
|
|
|
//! ```
|
|
|
|
//! r#"
|
|
|
|
//! fn main() {
|
|
|
|
//! println!("Hello World")
|
|
|
|
//! }
|
|
|
|
//! "#
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Metadata can be added to a fixture after a `//-` comment.
|
|
|
|
//! The basic form is specifying filenames,
|
|
|
|
//! which is also how to define multiple files in a single test fixture
|
|
|
|
//!
|
|
|
|
//! Example using two files in the same crate:
|
|
|
|
//! ```
|
|
|
|
//! "
|
|
|
|
//! //- /main.rs
|
|
|
|
//! mod foo;
|
|
|
|
//! fn main() {
|
|
|
|
//! foo::bar();
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! //- /foo.rs
|
|
|
|
//! pub fn bar() {}
|
|
|
|
//! "
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Example using two crates with one file each, with one crate depending on the other:
|
|
|
|
//! ```
|
|
|
|
//! r#"
|
|
|
|
//! //- /main.rs crate:a deps:b
|
|
|
|
//! fn main() {
|
|
|
|
//! b::foo();
|
|
|
|
//! }
|
|
|
|
//! //- /lib.rs crate:b
|
|
|
|
//! pub fn b() {
|
|
|
|
//! println!("Hello World")
|
|
|
|
//! }
|
|
|
|
//! "#
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Metadata allows specifying all settings and variables
|
2023-07-17 13:49:15 +00:00
|
|
|
//! that are available in a real rust project. See [`Fixture`]
|
|
|
|
//! for the syntax.
|
2021-04-06 18:42:09 +00:00
|
|
|
//!
|
2023-07-17 13:49:15 +00:00
|
|
|
//! Example using some available metadata:
|
2021-04-06 18:42:09 +00:00
|
|
|
//! ```
|
|
|
|
//! "
|
|
|
|
//! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
|
|
|
//! fn insert_source_code_here() {}
|
|
|
|
//! "
|
|
|
|
//! ```
|
2020-06-23 16:58:45 +00:00
|
|
|
|
2022-10-18 06:12:49 +00:00
|
|
|
use std::iter;
|
|
|
|
|
2020-06-23 15:59:56 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-05-04 11:10:49 +00:00
|
|
|
use stdx::trim_indent;
|
2020-06-23 15:59:56 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-06-23 16:46:56 +00:00
|
|
|
pub struct Fixture {
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Specifies the path for this file. It must start with "/".
|
2020-06-23 15:59:56 +00:00
|
|
|
pub path: String,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Defines a new crate and make this file its root module.
|
|
|
|
///
|
|
|
|
/// Version and repository URL of the crate can optionally be specified; if
|
|
|
|
/// either one is specified, the other must also be specified.
|
|
|
|
///
|
|
|
|
/// Syntax:
|
|
|
|
/// - `crate:my_awesome_lib`
|
|
|
|
/// - `crate:my_awesome_lib@0.0.1,https://example.com/repo.git`
|
2020-06-24 08:22:02 +00:00
|
|
|
pub krate: Option<String>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Specifies dependencies of this crate. This must be used with `crate` meta.
|
|
|
|
///
|
|
|
|
/// Syntax: `deps:hir-def,ide-assists`
|
2020-06-23 15:59:56 +00:00
|
|
|
pub deps: Vec<String>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Limits crates in the extern prelude. The set of crate names must be a
|
|
|
|
/// subset of `deps`. This must be used with `crate` meta.
|
|
|
|
///
|
|
|
|
/// If this is not specified, all the dependencies will be in the extern prelude.
|
|
|
|
///
|
|
|
|
/// Syntax: `extern-prelude:hir-def,ide-assists`
|
2021-09-28 19:23:46 +00:00
|
|
|
pub extern_prelude: Option<Vec<String>>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Specifies configuration options to be enabled. Options may have associated
|
|
|
|
/// values.
|
|
|
|
///
|
|
|
|
/// Syntax: `cfg:test,dbg=false,opt_level=2`
|
|
|
|
pub cfgs: Vec<(String, Option<String>)>,
|
|
|
|
/// Specifies the edition of this crate. This must be used with `crate` meta. If
|
|
|
|
/// this is not specified, ([`base_db::input::Edition::CURRENT`]) will be used.
|
|
|
|
/// This must be used with `crate` meta.
|
|
|
|
///
|
|
|
|
/// Syntax: `edition:2021`
|
2020-06-23 15:59:56 +00:00
|
|
|
pub edition: Option<String>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Specifies environment variables.
|
|
|
|
///
|
|
|
|
/// Syntax: `env:PATH=/bin,RUST_LOG=debug`
|
2020-06-23 15:59:56 +00:00
|
|
|
pub env: FxHashMap<String, String>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Introduces a new [source root](base_db::input::SourceRoot). This file **and
|
|
|
|
/// the following files** will belong the new source root. This must be used
|
|
|
|
/// with `crate` meta.
|
|
|
|
///
|
|
|
|
/// Use this if you want to test something that uses `SourceRoot::is_library()`
|
|
|
|
/// to check editability.
|
|
|
|
///
|
|
|
|
/// Note that files before the first fixture with `new_source_root` meta will
|
|
|
|
/// belong to an implicitly defined local source root.
|
|
|
|
///
|
|
|
|
/// Syntax:
|
|
|
|
/// - `new_source_root:library`
|
|
|
|
/// - `new_source_root:local`
|
2021-07-23 17:57:16 +00:00
|
|
|
pub introduce_new_source_root: Option<String>,
|
2023-07-17 13:49:15 +00:00
|
|
|
/// Explicitly declares this crate as a library outside current workspace. This
|
|
|
|
/// must be used with `crate` meta.
|
|
|
|
///
|
|
|
|
/// This is implied if this file belongs to a library source root.
|
|
|
|
///
|
|
|
|
/// Use this if you want to test something that checks if a crate is a workspace
|
|
|
|
/// member via [`CrateOrigin`](base_db::input::CrateOrigin).
|
|
|
|
///
|
|
|
|
/// Syntax: `library`
|
|
|
|
pub library: bool,
|
|
|
|
/// Actual file contents. All meta comments are stripped.
|
|
|
|
pub text: String,
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 18:02:40 +00:00
|
|
|
pub struct MiniCore {
|
|
|
|
activated_flags: Vec<String>,
|
|
|
|
valid_flags: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
pub struct FixtureWithProjectMeta {
|
2021-09-13 16:30:04 +00:00
|
|
|
pub fixture: Vec<Fixture>,
|
|
|
|
pub mini_core: Option<MiniCore>,
|
|
|
|
pub proc_macro_names: Vec<String>,
|
2021-07-23 17:57:16 +00:00
|
|
|
pub toolchain: Option<String>,
|
2024-02-16 13:48:25 +00:00
|
|
|
/// Specifies LLVM data layout to be used.
|
|
|
|
///
|
|
|
|
/// You probably don't want to manually specify this. See LLVM manual for the
|
2024-06-14 00:29:10 +00:00
|
|
|
/// syntax, if you must: <https://llvm.org/docs/LangRef.html#data-layout>
|
2024-02-16 13:48:25 +00:00
|
|
|
pub target_data_layout: String,
|
2023-06-05 09:04:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
impl FixtureWithProjectMeta {
|
|
|
|
/// Parses text which looks like this:
|
|
|
|
///
|
2024-06-14 00:29:10 +00:00
|
|
|
/// ```text
|
2020-06-23 16:46:56 +00:00
|
|
|
/// //- some meta
|
|
|
|
/// line 1
|
|
|
|
/// line 2
|
2021-04-06 18:42:09 +00:00
|
|
|
/// //- other meta
|
2020-06-23 16:46:56 +00:00
|
|
|
/// ```
|
2021-06-15 18:02:40 +00:00
|
|
|
///
|
2021-09-13 16:30:04 +00:00
|
|
|
/// Fixture can also start with a proc_macros and minicore declaration (in that order):
|
2021-06-15 18:02:40 +00:00
|
|
|
///
|
2024-06-14 00:29:10 +00:00
|
|
|
/// ```text
|
2021-06-15 18:02:40 +00:00
|
|
|
/// //- toolchain: nightly
|
2021-09-13 16:30:04 +00:00
|
|
|
/// //- proc_macros: identity
|
2021-06-15 18:02:40 +00:00
|
|
|
/// //- minicore: sized
|
|
|
|
/// ```
|
|
|
|
///
|
2021-09-13 16:30:04 +00:00
|
|
|
/// That will set toolchain to nightly and include predefined proc macros and a subset of
|
2021-06-15 18:02:40 +00:00
|
|
|
/// `libcore` into the fixture, see `minicore.rs` for what's available. Note that toolchain
|
2021-09-13 16:30:04 +00:00
|
|
|
/// defaults to stable.
|
|
|
|
pub fn parse(ra_fixture: &str) -> Self {
|
2020-06-23 20:27:24 +00:00
|
|
|
let fixture = trim_indent(ra_fixture);
|
2021-06-15 18:02:40 +00:00
|
|
|
let mut fixture = fixture.as_str();
|
2021-07-23 17:57:16 +00:00
|
|
|
let mut toolchain = None;
|
2024-02-16 13:48:25 +00:00
|
|
|
let mut target_data_layout =
|
|
|
|
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128".to_owned();
|
2021-06-15 18:02:40 +00:00
|
|
|
let mut mini_core = None;
|
2020-06-23 16:46:56 +00:00
|
|
|
let mut res: Vec<Fixture> = Vec::new();
|
2021-09-13 16:30:04 +00:00
|
|
|
let mut proc_macro_names = vec![];
|
2023-06-05 09:04:23 +00:00
|
|
|
|
2021-09-13 16:30:04 +00:00
|
|
|
if let Some(meta) = fixture.strip_prefix("//- toolchain:") {
|
|
|
|
let (meta, remain) = meta.split_once('\n').unwrap();
|
2024-02-09 15:58:06 +00:00
|
|
|
toolchain = Some(meta.trim().to_owned());
|
2021-09-13 16:30:04 +00:00
|
|
|
fixture = remain;
|
2023-06-05 09:04:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 13:48:25 +00:00
|
|
|
if let Some(meta) = fixture.strip_prefix("//- target_data_layout:") {
|
|
|
|
let (meta, remain) = meta.split_once('\n').unwrap();
|
2024-03-22 10:38:18 +00:00
|
|
|
meta.trim().clone_into(&mut target_data_layout);
|
2024-02-16 13:48:25 +00:00
|
|
|
fixture = remain;
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:30:04 +00:00
|
|
|
if let Some(meta) = fixture.strip_prefix("//- proc_macros:") {
|
|
|
|
let (meta, remain) = meta.split_once('\n').unwrap();
|
2024-02-09 15:58:06 +00:00
|
|
|
proc_macro_names = meta.split(',').map(|it| it.trim().to_owned()).collect();
|
2021-09-13 16:30:04 +00:00
|
|
|
fixture = remain;
|
|
|
|
}
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2021-06-15 18:02:40 +00:00
|
|
|
if let Some(meta) = fixture.strip_prefix("//- minicore:") {
|
|
|
|
let (meta, remain) = meta.split_once('\n').unwrap();
|
|
|
|
mini_core = Some(MiniCore::parse(meta));
|
|
|
|
fixture = remain;
|
|
|
|
}
|
|
|
|
|
|
|
|
let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") };
|
2020-06-23 22:30:34 +00:00
|
|
|
|
2021-05-04 11:10:49 +00:00
|
|
|
for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
|
2020-06-23 20:27:24 +00:00
|
|
|
if line.contains("//-") {
|
|
|
|
assert!(
|
|
|
|
line.starts_with("//-"),
|
2023-01-09 18:36:22 +00:00
|
|
|
"Metadata line {ix} has invalid indentation. \
|
2020-06-23 20:27:24 +00:00
|
|
|
All metadata lines need to have the same indentation.\n\
|
2023-01-09 18:36:22 +00:00
|
|
|
The offending line: {line:?}"
|
2020-06-23 20:27:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
if line.starts_with("//-") {
|
2020-06-23 19:45:40 +00:00
|
|
|
let meta = Self::parse_meta_line(line);
|
2021-10-03 12:39:43 +00:00
|
|
|
res.push(meta);
|
2021-06-16 07:30:29 +00:00
|
|
|
} else {
|
|
|
|
if line.starts_with("// ")
|
2021-06-18 11:40:51 +00:00
|
|
|
&& line.contains(':')
|
2021-06-16 07:30:29 +00:00
|
|
|
&& !line.contains("::")
|
2021-12-17 15:46:20 +00:00
|
|
|
&& !line.contains('.')
|
2021-06-16 07:30:29 +00:00
|
|
|
&& line.chars().all(|it| !it.is_uppercase())
|
|
|
|
{
|
2023-01-09 18:36:22 +00:00
|
|
|
panic!("looks like invalid metadata line: {line:?}");
|
2021-06-16 07:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(entry) = res.last_mut() {
|
|
|
|
entry.text.push_str(line);
|
|
|
|
}
|
2020-06-23 16:46:56 +00:00
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2024-02-16 13:48:25 +00:00
|
|
|
Self { fixture: res, mini_core, proc_macro_names, toolchain, target_data_layout }
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
2020-06-24 08:22:02 +00:00
|
|
|
fn parse_meta_line(meta: &str) -> Fixture {
|
2020-06-23 18:38:36 +00:00
|
|
|
assert!(meta.starts_with("//-"));
|
|
|
|
let meta = meta["//-".len()..].trim();
|
2023-07-17 13:49:15 +00:00
|
|
|
let mut components = meta.split_ascii_whitespace();
|
2020-06-23 16:46:56 +00:00
|
|
|
|
2024-02-09 15:58:06 +00:00
|
|
|
let path = components.next().expect("fixture meta must start with a path").to_owned();
|
2023-01-09 18:36:22 +00:00
|
|
|
assert!(path.starts_with('/'), "fixture path does not start with `/`: {path:?}");
|
2020-06-23 16:46:56 +00:00
|
|
|
|
|
|
|
let mut krate = None;
|
|
|
|
let mut deps = Vec::new();
|
2021-09-28 19:23:46 +00:00
|
|
|
let mut extern_prelude = None;
|
2020-06-23 16:46:56 +00:00
|
|
|
let mut edition = None;
|
2023-07-17 13:49:15 +00:00
|
|
|
let mut cfgs = Vec::new();
|
2020-06-23 16:46:56 +00:00
|
|
|
let mut env = FxHashMap::default();
|
2021-07-23 17:57:16 +00:00
|
|
|
let mut introduce_new_source_root = None;
|
2023-07-17 13:49:15 +00:00
|
|
|
let mut library = false;
|
|
|
|
for component in components {
|
|
|
|
if component == "library" {
|
|
|
|
library = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:36:22 +00:00
|
|
|
let (key, value) =
|
|
|
|
component.split_once(':').unwrap_or_else(|| panic!("invalid meta line: {meta:?}"));
|
2020-06-23 16:46:56 +00:00
|
|
|
match key {
|
2024-02-09 15:58:06 +00:00
|
|
|
"crate" => krate = Some(value.to_owned()),
|
|
|
|
"deps" => deps = value.split(',').map(|it| it.to_owned()).collect(),
|
2021-09-28 19:23:46 +00:00
|
|
|
"extern-prelude" => {
|
|
|
|
if value.is_empty() {
|
|
|
|
extern_prelude = Some(Vec::new());
|
|
|
|
} else {
|
|
|
|
extern_prelude =
|
2024-02-09 15:58:06 +00:00
|
|
|
Some(value.split(',').map(|it| it.to_owned()).collect::<Vec<_>>());
|
2021-09-28 19:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 15:58:06 +00:00
|
|
|
"edition" => edition = Some(value.to_owned()),
|
2020-06-23 16:46:56 +00:00
|
|
|
"cfg" => {
|
2020-06-23 16:56:26 +00:00
|
|
|
for entry in value.split(',') {
|
2021-05-04 11:10:49 +00:00
|
|
|
match entry.split_once('=') {
|
2024-02-09 15:58:06 +00:00
|
|
|
Some((k, v)) => cfgs.push((k.to_owned(), Some(v.to_owned()))),
|
|
|
|
None => cfgs.push((entry.to_owned(), None)),
|
2020-06-23 16:46:56 +00:00
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 16:46:56 +00:00
|
|
|
"env" => {
|
|
|
|
for key in value.split(',') {
|
2021-05-04 11:10:49 +00:00
|
|
|
if let Some((k, v)) = key.split_once('=') {
|
2020-06-23 16:46:56 +00:00
|
|
|
env.insert(k.into(), v.into());
|
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 15:58:06 +00:00
|
|
|
"new_source_root" => introduce_new_source_root = Some(value.to_owned()),
|
2023-01-09 18:36:22 +00:00
|
|
|
_ => panic!("bad component: {component:?}"),
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:23:46 +00:00
|
|
|
for prelude_dep in extern_prelude.iter().flatten() {
|
|
|
|
assert!(
|
|
|
|
deps.contains(prelude_dep),
|
2023-01-09 18:36:22 +00:00
|
|
|
"extern-prelude {extern_prelude:?} must be a subset of deps {deps:?}"
|
2021-09-28 19:23:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:28:02 +00:00
|
|
|
Fixture {
|
|
|
|
path,
|
|
|
|
text: String::new(),
|
|
|
|
krate,
|
|
|
|
deps,
|
2021-09-28 19:23:46 +00:00
|
|
|
extern_prelude,
|
2023-07-17 13:49:15 +00:00
|
|
|
cfgs,
|
2021-03-16 14:28:02 +00:00
|
|
|
edition,
|
|
|
|
env,
|
|
|
|
introduce_new_source_root,
|
2023-07-17 13:49:15 +00:00
|
|
|
library,
|
2021-03-16 14:28:02 +00:00
|
|
|
}
|
2020-06-23 16:46:56 +00:00
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 18:02:40 +00:00
|
|
|
impl MiniCore {
|
2023-08-21 20:43:04 +00:00
|
|
|
const RAW_SOURCE: &'static str = include_str!("./minicore.rs");
|
2023-06-05 09:04:23 +00:00
|
|
|
|
2021-06-15 18:02:40 +00:00
|
|
|
fn has_flag(&self, flag: &str) -> bool {
|
|
|
|
self.activated_flags.iter().any(|it| it == flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_flags<'a>(flags: impl IntoIterator<Item = &'a str>) -> Self {
|
|
|
|
MiniCore {
|
|
|
|
activated_flags: flags.into_iter().map(|x| x.to_owned()).collect(),
|
|
|
|
valid_flags: Vec::new(),
|
2023-06-05 09:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:59:51 +00:00
|
|
|
#[track_caller]
|
2021-06-15 18:02:40 +00:00
|
|
|
fn assert_valid_flag(&self, flag: &str) {
|
|
|
|
if !self.valid_flags.iter().any(|it| it == flag) {
|
2023-01-09 18:36:22 +00:00
|
|
|
panic!("invalid flag: {flag:?}, valid flags: {:?}", self.valid_flags);
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(line: &str) -> MiniCore {
|
|
|
|
let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() };
|
|
|
|
|
|
|
|
for entry in line.trim().split(", ") {
|
|
|
|
if res.has_flag(entry) {
|
2023-01-09 18:36:22 +00:00
|
|
|
panic!("duplicate minicore flag: {entry:?}");
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
2022-10-18 06:12:49 +00:00
|
|
|
res.activated_flags.push(entry.to_owned());
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn available_flags() -> impl Iterator<Item = &'static str> {
|
|
|
|
let lines = MiniCore::RAW_SOURCE.split_inclusive('\n');
|
2023-06-05 09:04:23 +00:00
|
|
|
lines
|
2022-10-18 06:12:49 +00:00
|
|
|
.map_while(|x| x.strip_prefix("//!"))
|
|
|
|
.skip_while(|line| !line.contains("Available flags:"))
|
2023-06-05 09:04:23 +00:00
|
|
|
.skip(1)
|
2022-10-18 06:12:49 +00:00
|
|
|
.map(|x| x.split_once(':').unwrap().0.trim())
|
2023-06-05 09:04:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 18:02:40 +00:00
|
|
|
/// Strips parts of minicore.rs which are flagged by inactive flags.
|
|
|
|
///
|
|
|
|
/// This is probably over-engineered to support flags dependencies.
|
|
|
|
pub fn source_code(mut self) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut lines = MiniCore::RAW_SOURCE.split_inclusive('\n');
|
|
|
|
|
|
|
|
let mut implications = Vec::new();
|
|
|
|
|
|
|
|
// Parse `//!` preamble and extract flags and dependencies.
|
2022-10-18 06:12:49 +00:00
|
|
|
let trim_doc: fn(&str) -> Option<&str> = |line| match line.strip_prefix("//!") {
|
|
|
|
Some(it) => Some(it),
|
|
|
|
None => {
|
|
|
|
assert!(line.trim().is_empty(), "expected empty line after minicore header");
|
|
|
|
None
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
2022-10-18 06:12:49 +00:00
|
|
|
};
|
|
|
|
for line in lines
|
|
|
|
.by_ref()
|
|
|
|
.map_while(trim_doc)
|
|
|
|
.skip_while(|line| !line.contains("Available flags:"))
|
|
|
|
.skip(1)
|
|
|
|
{
|
|
|
|
let (flag, deps) = line.split_once(':').unwrap();
|
|
|
|
let flag = flag.trim();
|
|
|
|
|
2024-02-09 15:58:06 +00:00
|
|
|
self.valid_flags.push(flag.to_owned());
|
2022-10-18 06:12:49 +00:00
|
|
|
implications.extend(
|
|
|
|
iter::repeat(flag)
|
|
|
|
.zip(deps.split(", ").map(str::trim).filter(|dep| !dep.is_empty())),
|
|
|
|
);
|
|
|
|
}
|
2021-06-15 18:02:40 +00:00
|
|
|
|
2022-10-18 06:12:49 +00:00
|
|
|
for (_, dep) in &implications {
|
|
|
|
self.assert_valid_flag(dep);
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for flag in &self.activated_flags {
|
|
|
|
self.assert_valid_flag(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixed point loop to compute transitive closure of flags.
|
|
|
|
loop {
|
|
|
|
let mut changed = false;
|
2021-10-03 12:45:08 +00:00
|
|
|
for &(u, v) in &implications {
|
2021-06-15 18:02:40 +00:00
|
|
|
if self.has_flag(u) && !self.has_flag(v) {
|
2024-02-09 15:58:06 +00:00
|
|
|
self.activated_flags.push(v.to_owned());
|
2021-06-15 18:02:40 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !changed {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:30:29 +00:00
|
|
|
let mut active_regions = Vec::new();
|
2021-06-15 19:38:21 +00:00
|
|
|
let mut seen_regions = Vec::new();
|
2021-06-15 18:02:40 +00:00
|
|
|
for line in lines {
|
|
|
|
let trimmed = line.trim();
|
|
|
|
if let Some(region) = trimmed.strip_prefix("// region:") {
|
2021-06-16 07:30:29 +00:00
|
|
|
active_regions.push(region);
|
2021-06-15 18:02:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(region) = trimmed.strip_prefix("// endregion:") {
|
2021-06-16 07:30:29 +00:00
|
|
|
let prev = active_regions.pop().unwrap();
|
2022-10-18 06:12:49 +00:00
|
|
|
assert_eq!(prev, region, "unbalanced region pairs");
|
2021-06-15 18:02:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:30:29 +00:00
|
|
|
let mut line_region = false;
|
2021-06-15 18:02:40 +00:00
|
|
|
if let Some(idx) = trimmed.find("// :") {
|
2021-06-16 07:30:29 +00:00
|
|
|
line_region = true;
|
|
|
|
active_regions.push(&trimmed[idx + "// :".len()..]);
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 07:30:29 +00:00
|
|
|
let mut keep = true;
|
|
|
|
for ®ion in &active_regions {
|
2023-01-09 18:36:22 +00:00
|
|
|
assert!(!region.starts_with(' '), "region marker starts with a space: {region:?}");
|
2021-06-16 07:30:29 +00:00
|
|
|
self.assert_valid_flag(region);
|
|
|
|
seen_regions.push(region);
|
|
|
|
keep &= self.has_flag(region);
|
|
|
|
}
|
2021-06-15 18:02:40 +00:00
|
|
|
|
2021-06-16 07:30:29 +00:00
|
|
|
if keep {
|
2021-10-03 12:39:43 +00:00
|
|
|
buf.push_str(line);
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
2021-06-16 07:30:29 +00:00
|
|
|
if line_region {
|
|
|
|
active_regions.pop().unwrap();
|
|
|
|
}
|
2021-06-15 18:02:40 +00:00
|
|
|
}
|
2021-06-15 19:38:21 +00:00
|
|
|
|
2023-06-19 06:14:04 +00:00
|
|
|
if !active_regions.is_empty() {
|
2024-05-30 23:18:49 +00:00
|
|
|
panic!("unclosed regions: {active_regions:?} Add an `endregion` comment");
|
2023-06-19 06:14:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 19:38:21 +00:00
|
|
|
for flag in &self.valid_flags {
|
|
|
|
if !seen_regions.iter().any(|it| it == flag) {
|
2023-01-09 18:36:22 +00:00
|
|
|
panic!("unused minicore flag: {flag:?}");
|
2021-06-15 19:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-15 18:02:40 +00:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 15:59:56 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_fixture_checks_further_indented_metadata() {
|
2020-06-23 16:56:26 +00:00
|
|
|
FixtureWithProjectMeta::parse(
|
2020-06-23 15:59:56 +00:00
|
|
|
r"
|
|
|
|
//- /lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
fn foo() {}
|
|
|
|
//- /bar.rs
|
|
|
|
pub fn baz() {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fixture_gets_full_meta() {
|
2024-02-16 13:48:25 +00:00
|
|
|
let FixtureWithProjectMeta {
|
|
|
|
fixture: parsed,
|
|
|
|
mini_core,
|
|
|
|
proc_macro_names,
|
|
|
|
toolchain,
|
|
|
|
target_data_layout: _,
|
|
|
|
} = FixtureWithProjectMeta::parse(
|
|
|
|
r#"
|
2023-06-05 09:04:23 +00:00
|
|
|
//- toolchain: nightly
|
2021-09-13 16:30:04 +00:00
|
|
|
//- proc_macros: identity
|
2021-06-15 18:02:40 +00:00
|
|
|
//- minicore: coerce_unsized
|
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
|
|
|
|
mod m;
|
|
|
|
"#,
|
2024-02-16 13:48:25 +00:00
|
|
|
);
|
2024-02-09 15:58:06 +00:00
|
|
|
assert_eq!(toolchain, Some("nightly".to_owned()));
|
|
|
|
assert_eq!(proc_macro_names, vec!["identity".to_owned()]);
|
|
|
|
assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_owned()]);
|
2020-06-23 15:59:56 +00:00
|
|
|
assert_eq!(1, parsed.len());
|
|
|
|
|
2020-06-23 16:34:50 +00:00
|
|
|
let meta = &parsed[0];
|
2020-06-23 20:27:24 +00:00
|
|
|
assert_eq!("mod m;\n", meta.text);
|
2020-06-23 15:59:56 +00:00
|
|
|
|
2020-06-24 08:22:02 +00:00
|
|
|
assert_eq!("foo", meta.krate.as_ref().unwrap());
|
2020-06-23 16:34:50 +00:00
|
|
|
assert_eq!("/lib.rs", meta.path);
|
|
|
|
assert_eq!(2, meta.env.len());
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|