]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/base-db/src/fixture.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / base-db / src / fixture.rs
1 //! A set of high-level utility fixture methods to use in tests.
2 use std::{mem, str::FromStr, sync::Arc};
3
4 use cfg::CfgOptions;
5 use rustc_hash::FxHashMap;
6 use test_utils::{
7 extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
8 };
9 use tt::Subtree;
10 use vfs::{file_set::FileSet, VfsPath};
11
12 use crate::{
13 input::{CrateName, CrateOrigin, LangCrateOrigin},
14 Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
15 FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, SourceDatabaseExt,
16 SourceRoot, SourceRootId,
17 };
18
19 pub const WORKSPACE: SourceRootId = SourceRootId(0);
20
21 pub trait WithFixture: Default + SourceDatabaseExt + 'static {
22 fn with_single_file(ra_fixture: &str) -> (Self, FileId) {
23 let fixture = ChangeFixture::parse(ra_fixture);
24 let mut db = Self::default();
25 fixture.change.apply(&mut db);
26 assert_eq!(fixture.files.len(), 1);
27 (db, fixture.files[0])
28 }
29
30 fn with_many_files(ra_fixture: &str) -> (Self, Vec<FileId>) {
31 let fixture = ChangeFixture::parse(ra_fixture);
32 let mut db = Self::default();
33 fixture.change.apply(&mut db);
34 assert!(fixture.file_position.is_none());
35 (db, fixture.files)
36 }
37
38 fn with_files(ra_fixture: &str) -> Self {
39 let fixture = ChangeFixture::parse(ra_fixture);
40 let mut db = Self::default();
41 fixture.change.apply(&mut db);
42 assert!(fixture.file_position.is_none());
43 db
44 }
45
46 fn with_files_extra_proc_macros(
47 ra_fixture: &str,
48 proc_macros: Vec<(String, ProcMacro)>,
49 ) -> Self {
50 let fixture = ChangeFixture::parse_with_proc_macros(ra_fixture, proc_macros);
51 let mut db = Self::default();
52 fixture.change.apply(&mut db);
53 assert!(fixture.file_position.is_none());
54 db
55 }
56
57 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
58 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
59 let offset = range_or_offset.expect_offset();
60 (db, FilePosition { file_id, offset })
61 }
62
63 fn with_range(ra_fixture: &str) -> (Self, FileRange) {
64 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
65 let range = range_or_offset.expect_range();
66 (db, FileRange { file_id, range })
67 }
68
69 fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
70 let fixture = ChangeFixture::parse(ra_fixture);
71 let mut db = Self::default();
72 fixture.change.apply(&mut db);
73 let (file_id, range_or_offset) = fixture
74 .file_position
75 .expect("Could not find file position in fixture. Did you forget to add an `$0`?");
76 (db, file_id, range_or_offset)
77 }
78
79 fn test_crate(&self) -> CrateId {
80 let crate_graph = self.crate_graph();
81 let mut it = crate_graph.iter();
82 let res = it.next().unwrap();
83 assert!(it.next().is_none());
84 res
85 }
86 }
87
88 impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
89
90 pub struct ChangeFixture {
91 pub file_position: Option<(FileId, RangeOrOffset)>,
92 pub files: Vec<FileId>,
93 pub change: Change,
94 }
95
96 impl ChangeFixture {
97 pub fn parse(ra_fixture: &str) -> ChangeFixture {
98 Self::parse_with_proc_macros(ra_fixture, Vec::new())
99 }
100
101 pub fn parse_with_proc_macros(
102 ra_fixture: &str,
103 mut proc_macros: Vec<(String, ProcMacro)>,
104 ) -> ChangeFixture {
105 let (mini_core, proc_macro_names, fixture) = Fixture::parse(ra_fixture);
106 let mut change = Change::new();
107
108 let mut files = Vec::new();
109 let mut crate_graph = CrateGraph::default();
110 let mut crates = FxHashMap::default();
111 let mut crate_deps = Vec::new();
112 let mut default_crate_root: Option<FileId> = None;
113 let mut default_cfg = CfgOptions::default();
114
115 let mut file_set = FileSet::default();
116 let mut current_source_root_kind = SourceRootKind::Local;
117 let source_root_prefix = "/".to_string();
118 let mut file_id = FileId(0);
119 let mut roots = Vec::new();
120
121 let mut file_position = None;
122
123 for entry in fixture {
124 let text = if entry.text.contains(CURSOR_MARKER) {
125 if entry.text.contains(ESCAPED_CURSOR_MARKER) {
126 entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
127 } else {
128 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
129 assert!(file_position.is_none());
130 file_position = Some((file_id, range_or_offset));
131 text
132 }
133 } else {
134 entry.text.clone()
135 };
136
137 let meta = FileMeta::from(entry);
138 assert!(meta.path.starts_with(&source_root_prefix));
139 if !meta.deps.is_empty() {
140 assert!(meta.krate.is_some(), "can't specify deps without naming the crate")
141 }
142
143 if let Some(kind) = &meta.introduce_new_source_root {
144 let root = match current_source_root_kind {
145 SourceRootKind::Local => SourceRoot::new_local(mem::take(&mut file_set)),
146 SourceRootKind::Library => SourceRoot::new_library(mem::take(&mut file_set)),
147 };
148 roots.push(root);
149 current_source_root_kind = *kind;
150 }
151
152 if let Some((krate, origin, version)) = meta.krate {
153 let crate_name = CrateName::normalize_dashes(&krate);
154 let crate_id = crate_graph.add_crate_root(
155 file_id,
156 meta.edition,
157 Some(crate_name.clone().into()),
158 version,
159 meta.cfg.clone(),
160 meta.cfg,
161 meta.env,
162 Ok(Vec::new()),
163 false,
164 origin,
165 meta.target_data_layout.as_deref().map(Arc::from),
166 );
167 let prev = crates.insert(crate_name.clone(), crate_id);
168 assert!(prev.is_none());
169 for dep in meta.deps {
170 let prelude = meta.extern_prelude.contains(&dep);
171 let dep = CrateName::normalize_dashes(&dep);
172 crate_deps.push((crate_name.clone(), dep, prelude))
173 }
174 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
175 assert!(default_crate_root.is_none());
176 default_crate_root = Some(file_id);
177 default_cfg = meta.cfg;
178 }
179
180 change.change_file(file_id, Some(Arc::new(text)));
181 let path = VfsPath::new_virtual_path(meta.path);
182 file_set.insert(file_id, path);
183 files.push(file_id);
184 file_id.0 += 1;
185 }
186
187 if crates.is_empty() {
188 let crate_root = default_crate_root
189 .expect("missing default crate root, specify a main.rs or lib.rs");
190 crate_graph.add_crate_root(
191 crate_root,
192 Edition::CURRENT,
193 Some(CrateName::new("test").unwrap().into()),
194 None,
195 default_cfg.clone(),
196 default_cfg,
197 Env::default(),
198 Ok(Vec::new()),
199 false,
200 CrateOrigin::CratesIo { repo: None, name: None },
201 None,
202 );
203 } else {
204 for (from, to, prelude) in crate_deps {
205 let from_id = crates[&from];
206 let to_id = crates[&to];
207 crate_graph
208 .add_dep(
209 from_id,
210 Dependency::with_prelude(CrateName::new(&to).unwrap(), to_id, prelude),
211 )
212 .unwrap();
213 }
214 }
215 let target_layout =
216 crate_graph.iter().next().and_then(|it| crate_graph[it].target_layout.clone());
217
218 if let Some(mini_core) = mini_core {
219 let core_file = file_id;
220 file_id.0 += 1;
221
222 let mut fs = FileSet::default();
223 fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
224 roots.push(SourceRoot::new_library(fs));
225
226 change.change_file(core_file, Some(Arc::new(mini_core.source_code())));
227
228 let all_crates = crate_graph.crates_in_topological_order();
229
230 let core_crate = crate_graph.add_crate_root(
231 core_file,
232 Edition::Edition2021,
233 Some(CrateDisplayName::from_canonical_name("core".to_string())),
234 None,
235 CfgOptions::default(),
236 CfgOptions::default(),
237 Env::default(),
238 Ok(Vec::new()),
239 false,
240 CrateOrigin::Lang(LangCrateOrigin::Core),
241 target_layout.clone(),
242 );
243
244 for krate in all_crates {
245 crate_graph
246 .add_dep(krate, Dependency::new(CrateName::new("core").unwrap(), core_crate))
247 .unwrap();
248 }
249 }
250
251 if !proc_macro_names.is_empty() {
252 let proc_lib_file = file_id;
253 file_id.0 += 1;
254
255 proc_macros.extend(default_test_proc_macros());
256 let (proc_macro, source) = filter_test_proc_macros(&proc_macro_names, proc_macros);
257 let mut fs = FileSet::default();
258 fs.insert(
259 proc_lib_file,
260 VfsPath::new_virtual_path("/sysroot/proc_macros/lib.rs".to_string()),
261 );
262 roots.push(SourceRoot::new_library(fs));
263
264 change.change_file(proc_lib_file, Some(Arc::new(source)));
265
266 let all_crates = crate_graph.crates_in_topological_order();
267
268 let proc_macros_crate = crate_graph.add_crate_root(
269 proc_lib_file,
270 Edition::Edition2021,
271 Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())),
272 None,
273 CfgOptions::default(),
274 CfgOptions::default(),
275 Env::default(),
276 Ok(proc_macro),
277 true,
278 CrateOrigin::CratesIo { repo: None, name: None },
279 target_layout,
280 );
281
282 for krate in all_crates {
283 crate_graph
284 .add_dep(
285 krate,
286 Dependency::new(CrateName::new("proc_macros").unwrap(), proc_macros_crate),
287 )
288 .unwrap();
289 }
290 }
291
292 let root = match current_source_root_kind {
293 SourceRootKind::Local => SourceRoot::new_local(mem::take(&mut file_set)),
294 SourceRootKind::Library => SourceRoot::new_library(mem::take(&mut file_set)),
295 };
296 roots.push(root);
297 change.set_roots(roots);
298 change.set_crate_graph(crate_graph);
299
300 ChangeFixture { file_position, files, change }
301 }
302 }
303
304 fn default_test_proc_macros() -> [(String, ProcMacro); 4] {
305 [
306 (
307 r#"
308 #[proc_macro_attribute]
309 pub fn identity(_attr: TokenStream, item: TokenStream) -> TokenStream {
310 item
311 }
312 "#
313 .into(),
314 ProcMacro {
315 name: "identity".into(),
316 kind: crate::ProcMacroKind::Attr,
317 expander: Arc::new(IdentityProcMacroExpander),
318 },
319 ),
320 (
321 r#"
322 #[proc_macro_derive(DeriveIdentity)]
323 pub fn derive_identity(item: TokenStream) -> TokenStream {
324 item
325 }
326 "#
327 .into(),
328 ProcMacro {
329 name: "DeriveIdentity".into(),
330 kind: crate::ProcMacroKind::CustomDerive,
331 expander: Arc::new(IdentityProcMacroExpander),
332 },
333 ),
334 (
335 r#"
336 #[proc_macro_attribute]
337 pub fn input_replace(attr: TokenStream, _item: TokenStream) -> TokenStream {
338 attr
339 }
340 "#
341 .into(),
342 ProcMacro {
343 name: "input_replace".into(),
344 kind: crate::ProcMacroKind::Attr,
345 expander: Arc::new(AttributeInputReplaceProcMacroExpander),
346 },
347 ),
348 (
349 r#"
350 #[proc_macro]
351 pub fn mirror(input: TokenStream) -> TokenStream {
352 input
353 }
354 "#
355 .into(),
356 ProcMacro {
357 name: "mirror".into(),
358 kind: crate::ProcMacroKind::FuncLike,
359 expander: Arc::new(MirrorProcMacroExpander),
360 },
361 ),
362 ]
363 }
364
365 fn filter_test_proc_macros(
366 proc_macro_names: &[String],
367 proc_macro_defs: Vec<(String, ProcMacro)>,
368 ) -> (Vec<ProcMacro>, String) {
369 // The source here is only required so that paths to the macros exist and are resolvable.
370 let mut source = String::new();
371 let mut proc_macros = Vec::new();
372
373 for (c, p) in proc_macro_defs {
374 if !proc_macro_names.iter().any(|name| name == &stdx::to_lower_snake_case(&p.name)) {
375 continue;
376 }
377 proc_macros.push(p);
378 source += &c;
379 }
380
381 (proc_macros, source)
382 }
383
384 #[derive(Debug, Clone, Copy)]
385 enum SourceRootKind {
386 Local,
387 Library,
388 }
389
390 #[derive(Debug)]
391 struct FileMeta {
392 path: String,
393 krate: Option<(String, CrateOrigin, Option<String>)>,
394 deps: Vec<String>,
395 extern_prelude: Vec<String>,
396 cfg: CfgOptions,
397 edition: Edition,
398 env: Env,
399 introduce_new_source_root: Option<SourceRootKind>,
400 target_data_layout: Option<String>,
401 }
402
403 fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
404 if let Some((a, b)) = crate_str.split_once('@') {
405 let (version, origin) = match b.split_once(':') {
406 Some(("CratesIo", data)) => match data.split_once(',') {
407 Some((version, url)) => {
408 (version, CrateOrigin::CratesIo { repo: Some(url.to_owned()), name: None })
409 }
410 _ => panic!("Bad crates.io parameter: {data}"),
411 },
412 _ => panic!("Bad string for crate origin: {b}"),
413 };
414 (a.to_owned(), origin, Some(version.to_string()))
415 } else {
416 let crate_origin = match &*crate_str {
417 "std" => CrateOrigin::Lang(LangCrateOrigin::Std),
418 "core" => CrateOrigin::Lang(LangCrateOrigin::Core),
419 _ => CrateOrigin::CratesIo { repo: None, name: None },
420 };
421 (crate_str, crate_origin, None)
422 }
423 }
424
425 impl From<Fixture> for FileMeta {
426 fn from(f: Fixture) -> FileMeta {
427 let mut cfg = CfgOptions::default();
428 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
429 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
430 let deps = f.deps;
431 FileMeta {
432 path: f.path,
433 krate: f.krate.map(parse_crate),
434 extern_prelude: f.extern_prelude.unwrap_or_else(|| deps.clone()),
435 deps,
436 cfg,
437 edition: f.edition.as_ref().map_or(Edition::CURRENT, |v| Edition::from_str(v).unwrap()),
438 env: f.env.into_iter().collect(),
439 introduce_new_source_root: f.introduce_new_source_root.map(|kind| match &*kind {
440 "local" => SourceRootKind::Local,
441 "library" => SourceRootKind::Library,
442 invalid => panic!("invalid source root kind '{invalid}'"),
443 }),
444 target_data_layout: f.target_data_layout,
445 }
446 }
447 }
448
449 // Identity mapping
450 #[derive(Debug)]
451 struct IdentityProcMacroExpander;
452 impl ProcMacroExpander for IdentityProcMacroExpander {
453 fn expand(
454 &self,
455 subtree: &Subtree,
456 _: Option<&Subtree>,
457 _: &Env,
458 ) -> Result<Subtree, ProcMacroExpansionError> {
459 Ok(subtree.clone())
460 }
461 }
462
463 // Pastes the attribute input as its output
464 #[derive(Debug)]
465 struct AttributeInputReplaceProcMacroExpander;
466 impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
467 fn expand(
468 &self,
469 _: &Subtree,
470 attrs: Option<&Subtree>,
471 _: &Env,
472 ) -> Result<Subtree, ProcMacroExpansionError> {
473 attrs
474 .cloned()
475 .ok_or_else(|| ProcMacroExpansionError::Panic("Expected attribute input".into()))
476 }
477 }
478
479 #[derive(Debug)]
480 struct MirrorProcMacroExpander;
481 impl ProcMacroExpander for MirrorProcMacroExpander {
482 fn expand(
483 &self,
484 input: &Subtree,
485 _: Option<&Subtree>,
486 _: &Env,
487 ) -> Result<Subtree, ProcMacroExpansionError> {
488 fn traverse(input: &Subtree) -> Subtree {
489 let mut res = Subtree::default();
490 res.delimiter = input.delimiter;
491 for tt in input.token_trees.iter().rev() {
492 let tt = match tt {
493 tt::TokenTree::Leaf(leaf) => tt::TokenTree::Leaf(leaf.clone()),
494 tt::TokenTree::Subtree(sub) => tt::TokenTree::Subtree(traverse(sub)),
495 };
496 res.token_trees.push(tt);
497 }
498 res
499 }
500 Ok(traverse(input))
501 }
502 }