• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Defines `Fixture` -- a convenient way to describe the initial state of
2 //! rust-analyzer database from a single string.
3 //!
4 //! Fixtures are strings containing rust source code with optional metadata.
5 //! A fixture without metadata is parsed into a single source file.
6 //! Use this to test functionality local to one file.
7 //!
8 //! Simple Example:
9 //! ```
10 //! r#"
11 //! fn main() {
12 //!     println!("Hello World")
13 //! }
14 //! "#
15 //! ```
16 //!
17 //! Metadata can be added to a fixture after a `//-` comment.
18 //! The basic form is specifying filenames,
19 //! which is also how to define multiple files in a single test fixture
20 //!
21 //! Example using two files in the same crate:
22 //! ```
23 //! "
24 //! //- /main.rs
25 //! mod foo;
26 //! fn main() {
27 //!     foo::bar();
28 //! }
29 //!
30 //! //- /foo.rs
31 //! pub fn bar() {}
32 //! "
33 //! ```
34 //!
35 //! Example using two crates with one file each, with one crate depending on the other:
36 //! ```
37 //! r#"
38 //! //- /main.rs crate:a deps:b
39 //! fn main() {
40 //!     b::foo();
41 //! }
42 //! //- /lib.rs crate:b
43 //! pub fn b() {
44 //!     println!("Hello World")
45 //! }
46 //! "#
47 //! ```
48 //!
49 //! Metadata allows specifying all settings and variables
50 //! that are available in a real rust project:
51 //! - crate names via `crate:cratename`
52 //! - dependencies via `deps:dep1,dep2`
53 //! - configuration settings via `cfg:dbg=false,opt_level=2`
54 //! - environment variables via `env:PATH=/bin,RUST_LOG=debug`
55 //!
56 //! Example using all available metadata:
57 //! ```
58 //! "
59 //! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
60 //! fn insert_source_code_here() {}
61 //! "
62 //! ```
63 
64 use std::iter;
65 
66 use rustc_hash::FxHashMap;
67 use stdx::trim_indent;
68 
69 #[derive(Debug, Eq, PartialEq)]
70 pub struct Fixture {
71     pub path: String,
72     pub text: String,
73     pub krate: Option<String>,
74     pub deps: Vec<String>,
75     pub extern_prelude: Option<Vec<String>>,
76     pub cfg_atoms: Vec<String>,
77     pub cfg_key_values: Vec<(String, String)>,
78     pub edition: Option<String>,
79     pub env: FxHashMap<String, String>,
80     pub introduce_new_source_root: Option<String>,
81     pub target_data_layout: Option<String>,
82 }
83 
84 pub struct MiniCore {
85     activated_flags: Vec<String>,
86     valid_flags: Vec<String>,
87 }
88 
89 pub struct FixtureWithProjectMeta {
90     pub fixture: Vec<Fixture>,
91     pub mini_core: Option<MiniCore>,
92     pub proc_macro_names: Vec<String>,
93     pub toolchain: Option<String>,
94 }
95 
96 impl FixtureWithProjectMeta {
97     /// Parses text which looks like this:
98     ///
99     ///  ```not_rust
100     ///  //- some meta
101     ///  line 1
102     ///  line 2
103     ///  //- other meta
104     ///  ```
105     ///
106     /// Fixture can also start with a proc_macros and minicore declaration (in that order):
107     ///
108     /// ```
109     /// //- toolchain: nightly
110     /// //- proc_macros: identity
111     /// //- minicore: sized
112     /// ```
113     ///
114     /// That will set toolchain to nightly and include predefined proc macros and a subset of
115     /// `libcore` into the fixture, see `minicore.rs` for what's available. Note that toolchain
116     /// defaults to stable.
parse(ra_fixture: &str) -> Self117     pub fn parse(ra_fixture: &str) -> Self {
118         let fixture = trim_indent(ra_fixture);
119         let mut fixture = fixture.as_str();
120         let mut toolchain = None;
121         let mut mini_core = None;
122         let mut res: Vec<Fixture> = Vec::new();
123         let mut proc_macro_names = vec![];
124 
125         if let Some(meta) = fixture.strip_prefix("//- toolchain:") {
126             let (meta, remain) = meta.split_once('\n').unwrap();
127             toolchain = Some(meta.trim().to_string());
128             fixture = remain;
129         }
130 
131         if let Some(meta) = fixture.strip_prefix("//- proc_macros:") {
132             let (meta, remain) = meta.split_once('\n').unwrap();
133             proc_macro_names = meta.split(',').map(|it| it.trim().to_string()).collect();
134             fixture = remain;
135         }
136 
137         if let Some(meta) = fixture.strip_prefix("//- minicore:") {
138             let (meta, remain) = meta.split_once('\n').unwrap();
139             mini_core = Some(MiniCore::parse(meta));
140             fixture = remain;
141         }
142 
143         let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") };
144 
145         for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
146             if line.contains("//-") {
147                 assert!(
148                     line.starts_with("//-"),
149                     "Metadata line {ix} has invalid indentation. \
150                      All metadata lines need to have the same indentation.\n\
151                      The offending line: {line:?}"
152                 );
153             }
154 
155             if line.starts_with("//-") {
156                 let meta = Self::parse_meta_line(line);
157                 res.push(meta);
158             } else {
159                 if line.starts_with("// ")
160                     && line.contains(':')
161                     && !line.contains("::")
162                     && !line.contains('.')
163                     && line.chars().all(|it| !it.is_uppercase())
164                 {
165                     panic!("looks like invalid metadata line: {line:?}");
166                 }
167 
168                 if let Some(entry) = res.last_mut() {
169                     entry.text.push_str(line);
170                 }
171             }
172         }
173 
174         Self { fixture: res, mini_core, proc_macro_names, toolchain }
175     }
176 
177     //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
parse_meta_line(meta: &str) -> Fixture178     fn parse_meta_line(meta: &str) -> Fixture {
179         assert!(meta.starts_with("//-"));
180         let meta = meta["//-".len()..].trim();
181         let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
182 
183         let path = components[0].to_string();
184         assert!(path.starts_with('/'), "fixture path does not start with `/`: {path:?}");
185 
186         let mut krate = None;
187         let mut deps = Vec::new();
188         let mut extern_prelude = None;
189         let mut edition = None;
190         let mut cfg_atoms = Vec::new();
191         let mut cfg_key_values = Vec::new();
192         let mut env = FxHashMap::default();
193         let mut introduce_new_source_root = None;
194         let mut target_data_layout = Some(
195             "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128".to_string(),
196         );
197         for component in components[1..].iter() {
198             let (key, value) =
199                 component.split_once(':').unwrap_or_else(|| panic!("invalid meta line: {meta:?}"));
200             match key {
201                 "crate" => krate = Some(value.to_string()),
202                 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
203                 "extern-prelude" => {
204                     if value.is_empty() {
205                         extern_prelude = Some(Vec::new());
206                     } else {
207                         extern_prelude =
208                             Some(value.split(',').map(|it| it.to_string()).collect::<Vec<_>>());
209                     }
210                 }
211                 "edition" => edition = Some(value.to_string()),
212                 "cfg" => {
213                     for entry in value.split(',') {
214                         match entry.split_once('=') {
215                             Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
216                             None => cfg_atoms.push(entry.to_string()),
217                         }
218                     }
219                 }
220                 "env" => {
221                     for key in value.split(',') {
222                         if let Some((k, v)) = key.split_once('=') {
223                             env.insert(k.into(), v.into());
224                         }
225                     }
226                 }
227                 "new_source_root" => introduce_new_source_root = Some(value.to_string()),
228                 "target_data_layout" => target_data_layout = Some(value.to_string()),
229                 _ => panic!("bad component: {component:?}"),
230             }
231         }
232 
233         for prelude_dep in extern_prelude.iter().flatten() {
234             assert!(
235                 deps.contains(prelude_dep),
236                 "extern-prelude {extern_prelude:?} must be a subset of deps {deps:?}"
237             );
238         }
239 
240         Fixture {
241             path,
242             text: String::new(),
243             krate,
244             deps,
245             extern_prelude,
246             cfg_atoms,
247             cfg_key_values,
248             edition,
249             env,
250             introduce_new_source_root,
251             target_data_layout,
252         }
253     }
254 }
255 
256 impl MiniCore {
257     const RAW_SOURCE: &str = include_str!("./minicore.rs");
258 
has_flag(&self, flag: &str) -> bool259     fn has_flag(&self, flag: &str) -> bool {
260         self.activated_flags.iter().any(|it| it == flag)
261     }
262 
from_flags<'a>(flags: impl IntoIterator<Item = &'a str>) -> Self263     pub fn from_flags<'a>(flags: impl IntoIterator<Item = &'a str>) -> Self {
264         MiniCore {
265             activated_flags: flags.into_iter().map(|x| x.to_owned()).collect(),
266             valid_flags: Vec::new(),
267         }
268     }
269 
270     #[track_caller]
assert_valid_flag(&self, flag: &str)271     fn assert_valid_flag(&self, flag: &str) {
272         if !self.valid_flags.iter().any(|it| it == flag) {
273             panic!("invalid flag: {flag:?}, valid flags: {:?}", self.valid_flags);
274         }
275     }
276 
parse(line: &str) -> MiniCore277     fn parse(line: &str) -> MiniCore {
278         let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() };
279 
280         for entry in line.trim().split(", ") {
281             if res.has_flag(entry) {
282                 panic!("duplicate minicore flag: {entry:?}");
283             }
284             res.activated_flags.push(entry.to_owned());
285         }
286 
287         res
288     }
289 
available_flags() -> impl Iterator<Item = &'static str>290     pub fn available_flags() -> impl Iterator<Item = &'static str> {
291         let lines = MiniCore::RAW_SOURCE.split_inclusive('\n');
292         lines
293             .map_while(|x| x.strip_prefix("//!"))
294             .skip_while(|line| !line.contains("Available flags:"))
295             .skip(1)
296             .map(|x| x.split_once(':').unwrap().0.trim())
297     }
298 
299     /// Strips parts of minicore.rs which are flagged by inactive flags.
300     ///
301     /// This is probably over-engineered to support flags dependencies.
source_code(mut self) -> String302     pub fn source_code(mut self) -> String {
303         let mut buf = String::new();
304         let mut lines = MiniCore::RAW_SOURCE.split_inclusive('\n');
305 
306         let mut implications = Vec::new();
307 
308         // Parse `//!` preamble and extract flags and dependencies.
309         let trim_doc: fn(&str) -> Option<&str> = |line| match line.strip_prefix("//!") {
310             Some(it) => Some(it),
311             None => {
312                 assert!(line.trim().is_empty(), "expected empty line after minicore header");
313                 None
314             }
315         };
316         for line in lines
317             .by_ref()
318             .map_while(trim_doc)
319             .skip_while(|line| !line.contains("Available flags:"))
320             .skip(1)
321         {
322             let (flag, deps) = line.split_once(':').unwrap();
323             let flag = flag.trim();
324 
325             self.valid_flags.push(flag.to_string());
326             implications.extend(
327                 iter::repeat(flag)
328                     .zip(deps.split(", ").map(str::trim).filter(|dep| !dep.is_empty())),
329             );
330         }
331 
332         for (_, dep) in &implications {
333             self.assert_valid_flag(dep);
334         }
335 
336         for flag in &self.activated_flags {
337             self.assert_valid_flag(flag);
338         }
339 
340         // Fixed point loop to compute transitive closure of flags.
341         loop {
342             let mut changed = false;
343             for &(u, v) in &implications {
344                 if self.has_flag(u) && !self.has_flag(v) {
345                     self.activated_flags.push(v.to_string());
346                     changed = true;
347                 }
348             }
349             if !changed {
350                 break;
351             }
352         }
353 
354         let mut active_regions = Vec::new();
355         let mut seen_regions = Vec::new();
356         for line in lines {
357             let trimmed = line.trim();
358             if let Some(region) = trimmed.strip_prefix("// region:") {
359                 active_regions.push(region);
360                 continue;
361             }
362             if let Some(region) = trimmed.strip_prefix("// endregion:") {
363                 let prev = active_regions.pop().unwrap();
364                 assert_eq!(prev, region, "unbalanced region pairs");
365                 continue;
366             }
367 
368             let mut line_region = false;
369             if let Some(idx) = trimmed.find("// :") {
370                 line_region = true;
371                 active_regions.push(&trimmed[idx + "// :".len()..]);
372             }
373 
374             let mut keep = true;
375             for &region in &active_regions {
376                 assert!(!region.starts_with(' '), "region marker starts with a space: {region:?}");
377                 self.assert_valid_flag(region);
378                 seen_regions.push(region);
379                 keep &= self.has_flag(region);
380             }
381 
382             if keep {
383                 buf.push_str(line);
384             }
385             if line_region {
386                 active_regions.pop().unwrap();
387             }
388         }
389 
390         if !active_regions.is_empty() {
391             panic!("unclosed regions: {:?} Add an `endregion` comment", active_regions);
392         }
393 
394         for flag in &self.valid_flags {
395             if !seen_regions.iter().any(|it| it == flag) {
396                 panic!("unused minicore flag: {flag:?}");
397             }
398         }
399         buf
400     }
401 }
402 
403 #[test]
404 #[should_panic]
parse_fixture_checks_further_indented_metadata()405 fn parse_fixture_checks_further_indented_metadata() {
406     FixtureWithProjectMeta::parse(
407         r"
408         //- /lib.rs
409           mod bar;
410 
411           fn foo() {}
412           //- /bar.rs
413           pub fn baz() {}
414           ",
415     );
416 }
417 
418 #[test]
parse_fixture_gets_full_meta()419 fn parse_fixture_gets_full_meta() {
420     let FixtureWithProjectMeta { fixture: parsed, mini_core, proc_macro_names, toolchain } =
421         FixtureWithProjectMeta::parse(
422             r#"
423 //- toolchain: nightly
424 //- proc_macros: identity
425 //- minicore: coerce_unsized
426 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
427 mod m;
428 "#,
429         );
430     assert_eq!(toolchain, Some("nightly".to_string()));
431     assert_eq!(proc_macro_names, vec!["identity".to_string()]);
432     assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]);
433     assert_eq!(1, parsed.len());
434 
435     let meta = &parsed[0];
436     assert_eq!("mod m;\n", meta.text);
437 
438     assert_eq!("foo", meta.krate.as_ref().unwrap());
439     assert_eq!("/lib.rs", meta.path);
440     assert_eq!(2, meta.env.len());
441 }
442