aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-30 00:38:16 +0100
committeruHOOCCOOHu <[email protected]>2019-10-02 19:28:02 +0100
commitd2ea776b8fbb5286a04dde75a9a8f8b14f12bfe9 (patch)
treed6ddac6dc88b327aaf956843dc53b3e635f699b9
parentb1ed887d813bf5775a16624694939fdf836f97b1 (diff)
Enable CfgOptions `test` for workspace crates
-rw-r--r--crates/ra_db/src/input.rs28
-rw-r--r--crates/ra_hir/src/mock.rs23
-rw-r--r--crates/ra_hir/src/nameres/tests.rs70
-rw-r--r--crates/ra_ide_api/Cargo.toml1
-rw-r--r--crates/ra_ide_api/src/lib.rs6
-rw-r--r--crates/ra_ide_api/src/mock_analysis.rs7
-rw-r--r--crates/ra_ide_api/src/parent_module.rs3
-rw-r--r--crates/ra_project_model/Cargo.toml1
-rw-r--r--crates/ra_project_model/src/lib.rs21
9 files changed, 134 insertions, 26 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 5fd6edd78..23148096c 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -114,9 +114,8 @@ struct CrateData {
114} 114}
115 115
116impl CrateData { 116impl CrateData {
117 fn new(file_id: FileId, edition: Edition) -> CrateData { 117 fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData {
118 // FIXME: cfg options 118 CrateData { file_id, edition, dependencies: Vec::new(), cfg_options }
119 CrateData { file_id, edition, dependencies: Vec::new(), cfg_options: CfgOptions::default() }
120 } 119 }
121 120
122 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { 121 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -137,9 +136,14 @@ impl Dependency {
137} 136}
138 137
139impl CrateGraph { 138impl CrateGraph {
140 pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId { 139 pub fn add_crate_root(
140 &mut self,
141 file_id: FileId,
142 edition: Edition,
143 cfg_options: CfgOptions,
144 ) -> CrateId {
141 let crate_id = CrateId(self.arena.len() as u32); 145 let crate_id = CrateId(self.arena.len() as u32);
142 let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition)); 146 let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options));
143 assert!(prev.is_none()); 147 assert!(prev.is_none());
144 crate_id 148 crate_id
145 } 149 }
@@ -228,14 +232,14 @@ impl CrateGraph {
228 232
229#[cfg(test)] 233#[cfg(test)]
230mod tests { 234mod tests {
231 use super::{CrateGraph, Edition::Edition2018, FileId, SmolStr}; 235 use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr};
232 236
233 #[test] 237 #[test]
234 fn it_should_panic_because_of_cycle_dependencies() { 238 fn it_should_panic_because_of_cycle_dependencies() {
235 let mut graph = CrateGraph::default(); 239 let mut graph = CrateGraph::default();
236 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); 240 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
237 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); 241 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
238 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); 242 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
239 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 243 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
240 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 244 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
241 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); 245 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@@ -244,9 +248,9 @@ mod tests {
244 #[test] 248 #[test]
245 fn it_works() { 249 fn it_works() {
246 let mut graph = CrateGraph::default(); 250 let mut graph = CrateGraph::default();
247 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); 251 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
248 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); 252 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
249 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); 253 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
250 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 254 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
251 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 255 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
252 } 256 }
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 50feb98fb..f750986b8 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -3,6 +3,7 @@
3use std::{panic, sync::Arc}; 3use std::{panic, sync::Arc};
4 4
5use parking_lot::Mutex; 5use parking_lot::Mutex;
6use ra_cfg::CfgOptions;
6use ra_db::{ 7use ra_db::{
7 salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot, 8 salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot,
8 SourceRootId, 9 SourceRootId,
@@ -74,13 +75,13 @@ impl MockDatabase {
74 pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) { 75 pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) {
75 let mut ids = FxHashMap::default(); 76 let mut ids = FxHashMap::default();
76 let mut crate_graph = CrateGraph::default(); 77 let mut crate_graph = CrateGraph::default();
77 for (crate_name, (crate_root, edition, _)) in graph.0.iter() { 78 for (crate_name, (crate_root, edition, cfg_options, _)) in graph.0.iter() {
78 let crate_root = self.file_id_of(&crate_root); 79 let crate_root = self.file_id_of(&crate_root);
79 let crate_id = crate_graph.add_crate_root(crate_root, *edition); 80 let crate_id = crate_graph.add_crate_root(crate_root, *edition, cfg_options.clone());
80 Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone()); 81 Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone());
81 ids.insert(crate_name, crate_id); 82 ids.insert(crate_name, crate_id);
82 } 83 }
83 for (crate_name, (_, _, deps)) in graph.0.iter() { 84 for (crate_name, (_, _, _, deps)) in graph.0.iter() {
84 let from = ids[crate_name]; 85 let from = ids[crate_name];
85 for dep in deps { 86 for dep in deps {
86 let to = ids[dep]; 87 let to = ids[dep];
@@ -184,7 +185,7 @@ impl MockDatabase {
184 185
185 if is_crate_root { 186 if is_crate_root {
186 let mut crate_graph = CrateGraph::default(); 187 let mut crate_graph = CrateGraph::default();
187 crate_graph.add_crate_root(file_id, Edition::Edition2018); 188 crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
188 self.set_crate_graph(Arc::new(crate_graph)); 189 self.set_crate_graph(Arc::new(crate_graph));
189 } 190 }
190 file_id 191 file_id
@@ -268,19 +269,27 @@ impl MockDatabase {
268} 269}
269 270
270#[derive(Default)] 271#[derive(Default)]
271pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, Vec<String>))>); 272pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, CfgOptions, Vec<String>))>);
272 273
273#[macro_export] 274#[macro_export]
274macro_rules! crate_graph { 275macro_rules! crate_graph {
275 ($($crate_name:literal: ($crate_path:literal, $($edition:literal,)? [$($dep:literal),*]),)*) => {{ 276 ($(
277 $crate_name:literal: (
278 $crate_path:literal,
279 $($edition:literal,)?
280 [$($dep:literal),*]
281 $(,$cfg:expr)?
282 ),
283 )*) => {{
276 let mut res = $crate::mock::CrateGraphFixture::default(); 284 let mut res = $crate::mock::CrateGraphFixture::default();
277 $( 285 $(
278 #[allow(unused_mut, unused_assignments)] 286 #[allow(unused_mut, unused_assignments)]
279 let mut edition = ra_db::Edition::Edition2018; 287 let mut edition = ra_db::Edition::Edition2018;
280 $(edition = ra_db::Edition::from_string($edition);)? 288 $(edition = ra_db::Edition::from_string($edition);)?
289 let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? };
281 res.0.push(( 290 res.0.push((
282 $crate_name.to_string(), 291 $crate_name.to_string(),
283 ($crate_path.to_string(), edition, vec![$($dep.to_string()),*]) 292 ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])
284 )); 293 ));
285 )* 294 )*
286 res 295 res
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index bc4b47b70..f43767e59 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -7,6 +7,7 @@ mod mod_resolution;
7use std::sync::Arc; 7use std::sync::Arc;
8 8
9use insta::assert_snapshot; 9use insta::assert_snapshot;
10use ra_cfg::CfgOptions;
10use ra_db::SourceDatabase; 11use ra_db::SourceDatabase;
11use test_utils::covers; 12use test_utils::covers;
12 13
@@ -507,3 +508,72 @@ fn values_dont_shadow_extern_crates() {
507 ⋮foo: v 508 ⋮foo: v
508 "###); 509 "###);
509} 510}
511
512#[test]
513fn cfg_not_test() {
514 let map = def_map_with_crate_graph(
515 r#"
516 //- /main.rs
517 use {Foo, Bar, Baz};
518 //- /lib.rs
519 #[prelude_import]
520 pub use self::prelude::*;
521 mod prelude {
522 #[cfg(test)]
523 pub struct Foo;
524 #[cfg(not(test))]
525 pub struct Bar;
526 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
527 pub struct Baz;
528 }
529 "#,
530 crate_graph! {
531 "main": ("/main.rs", ["std"]),
532 "std": ("/lib.rs", []),
533 },
534 );
535
536 assert_snapshot!(map, @r###"
537 ⋮crate
538 ⋮Bar: t v
539 ⋮Baz: _
540 ⋮Foo: _
541 "###);
542}
543
544#[test]
545fn cfg_test() {
546 let map = def_map_with_crate_graph(
547 r#"
548 //- /main.rs
549 use {Foo, Bar, Baz};
550 //- /lib.rs
551 #[prelude_import]
552 pub use self::prelude::*;
553 mod prelude {
554 #[cfg(test)]
555 pub struct Foo;
556 #[cfg(not(test))]
557 pub struct Bar;
558 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
559 pub struct Baz;
560 }
561 "#,
562 crate_graph! {
563 "main": ("/main.rs", ["std"]),
564 "std": ("/lib.rs", [], CfgOptions::default()
565 .atom("test".into())
566 .feature("foo".into())
567 .feature("bar".into())
568 .option("opt".into(), "42".into())
569 ),
570 },
571 );
572
573 assert_snapshot!(map, @r###"
574 ⋮crate
575 ⋮Bar: _
576 ⋮Baz: t v
577 ⋮Foo: t v
578 "###);
579}
diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml
index 6bbf9d5dd..f919a2d61 100644
--- a/crates/ra_ide_api/Cargo.toml
+++ b/crates/ra_ide_api/Cargo.toml
@@ -23,6 +23,7 @@ rand = { version = "0.7.0", features = ["small_rng"] }
23ra_syntax = { path = "../ra_syntax" } 23ra_syntax = { path = "../ra_syntax" }
24ra_text_edit = { path = "../ra_text_edit" } 24ra_text_edit = { path = "../ra_text_edit" }
25ra_db = { path = "../ra_db" } 25ra_db = { path = "../ra_db" }
26ra_cfg = { path = "../ra_cfg" }
26ra_fmt = { path = "../ra_fmt" } 27ra_fmt = { path = "../ra_fmt" }
27ra_prof = { path = "../ra_prof" } 28ra_prof = { path = "../ra_prof" }
28hir = { path = "../ra_hir", package = "ra_hir" } 29hir = { path = "../ra_hir", package = "ra_hir" }
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 44d1ec77b..24f1b91f6 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -49,6 +49,7 @@ mod test_utils;
49 49
50use std::sync::Arc; 50use std::sync::Arc;
51 51
52use ra_cfg::CfgOptions;
52use ra_db::{ 53use ra_db::{
53 salsa::{self, ParallelDatabase}, 54 salsa::{self, ParallelDatabase},
54 CheckCanceled, SourceDatabase, 55 CheckCanceled, SourceDatabase,
@@ -322,7 +323,10 @@ impl Analysis {
322 change.add_root(source_root, true); 323 change.add_root(source_root, true);
323 let mut crate_graph = CrateGraph::default(); 324 let mut crate_graph = CrateGraph::default();
324 let file_id = FileId(0); 325 let file_id = FileId(0);
325 crate_graph.add_crate_root(file_id, Edition::Edition2018); 326 // FIXME: cfg options
327 // Default to enable test for single file.
328 let cfg_options = CfgOptions::default().atom("test".into());
329 crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
326 change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); 330 change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
327 change.set_crate_graph(crate_graph); 331 change.set_crate_graph(crate_graph);
328 host.apply_change(change); 332 host.apply_change(change);
diff --git a/crates/ra_ide_api/src/mock_analysis.rs b/crates/ra_ide_api/src/mock_analysis.rs
index 16870c7ae..13258b63d 100644
--- a/crates/ra_ide_api/src/mock_analysis.rs
+++ b/crates/ra_ide_api/src/mock_analysis.rs
@@ -2,6 +2,7 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_cfg::CfgOptions;
5use relative_path::RelativePathBuf; 6use relative_path::RelativePathBuf;
6use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER}; 7use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
7 8
@@ -93,10 +94,12 @@ impl MockAnalysis {
93 assert!(path.starts_with('/')); 94 assert!(path.starts_with('/'));
94 let path = RelativePathBuf::from_path(&path[1..]).unwrap(); 95 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
95 let file_id = FileId(i as u32 + 1); 96 let file_id = FileId(i as u32 + 1);
97 // FIXME: cfg options
98 let cfg_options = CfgOptions::default();
96 if path == "/lib.rs" || path == "/main.rs" { 99 if path == "/lib.rs" || path == "/main.rs" {
97 root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018)); 100 root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options));
98 } else if path.ends_with("/lib.rs") { 101 } else if path.ends_with("/lib.rs") {
99 let other_crate = crate_graph.add_crate_root(file_id, Edition2018); 102 let other_crate = crate_graph.add_crate_root(file_id, Edition2018, cfg_options);
100 let crate_name = path.parent().unwrap().file_name().unwrap(); 103 let crate_name = path.parent().unwrap().file_name().unwrap();
101 if let Some(root_crate) = root_crate { 104 if let Some(root_crate) = root_crate {
102 crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap(); 105 crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap();
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs
index c85f1d0d0..566509849 100644
--- a/crates/ra_ide_api/src/parent_module.rs
+++ b/crates/ra_ide_api/src/parent_module.rs
@@ -41,6 +41,7 @@ mod tests {
41 AnalysisChange, CrateGraph, 41 AnalysisChange, CrateGraph,
42 Edition::Edition2018, 42 Edition::Edition2018,
43 }; 43 };
44 use ra_cfg::CfgOptions;
44 45
45 #[test] 46 #[test]
46 fn test_resolve_parent_module() { 47 fn test_resolve_parent_module() {
@@ -88,7 +89,7 @@ mod tests {
88 assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); 89 assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
89 90
90 let mut crate_graph = CrateGraph::default(); 91 let mut crate_graph = CrateGraph::default();
91 let crate_id = crate_graph.add_crate_root(root_file, Edition2018); 92 let crate_id = crate_graph.add_crate_root(root_file, Edition2018, CfgOptions::default());
92 let mut change = AnalysisChange::new(); 93 let mut change = AnalysisChange::new();
93 change.set_crate_graph(crate_graph); 94 change.set_crate_graph(crate_graph);
94 host.apply_change(change); 95 host.apply_change(change);
diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml
index ae6b91aa6..a65100031 100644
--- a/crates/ra_project_model/Cargo.toml
+++ b/crates/ra_project_model/Cargo.toml
@@ -12,6 +12,7 @@ cargo_metadata = "0.8.2"
12 12
13ra_arena = { path = "../ra_arena" } 13ra_arena = { path = "../ra_arena" }
14ra_db = { path = "../ra_db" } 14ra_db = { path = "../ra_db" }
15ra_cfg = { path = "../ra_cfg" }
15 16
16serde = { version = "1.0.89", features = ["derive"] } 17serde = { version = "1.0.89", features = ["derive"] }
17serde_json = "1.0.39" 18serde_json = "1.0.39"
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 5d3078598..5ff3971e0 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -11,6 +11,7 @@ use std::{
11 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
12}; 12};
13 13
14use ra_cfg::CfgOptions;
14use ra_db::{CrateGraph, CrateId, Edition, FileId}; 15use ra_db::{CrateGraph, CrateId, Edition, FileId};
15use rustc_hash::FxHashMap; 16use rustc_hash::FxHashMap;
16use serde_json::from_reader; 17use serde_json::from_reader;
@@ -131,7 +132,13 @@ impl ProjectWorkspace {
131 json_project::Edition::Edition2015 => Edition::Edition2015, 132 json_project::Edition::Edition2015 => Edition::Edition2015,
132 json_project::Edition::Edition2018 => Edition::Edition2018, 133 json_project::Edition::Edition2018 => Edition::Edition2018,
133 }; 134 };
134 crates.insert(crate_id, crate_graph.add_crate_root(file_id, edition)); 135 // FIXME: cfg options
136 // Default to enable test for workspace crates.
137 let cfg_options = CfgOptions::default().atom("test".into());
138 crates.insert(
139 crate_id,
140 crate_graph.add_crate_root(file_id, edition, cfg_options),
141 );
135 } 142 }
136 } 143 }
137 144
@@ -157,7 +164,11 @@ impl ProjectWorkspace {
157 let mut sysroot_crates = FxHashMap::default(); 164 let mut sysroot_crates = FxHashMap::default();
158 for krate in sysroot.crates() { 165 for krate in sysroot.crates() {
159 if let Some(file_id) = load(krate.root(&sysroot)) { 166 if let Some(file_id) = load(krate.root(&sysroot)) {
160 let crate_id = crate_graph.add_crate_root(file_id, Edition::Edition2018); 167 // FIXME: cfg options
168 // Crates from sysroot have `cfg(test)` disabled
169 let cfg_options = CfgOptions::default();
170 let crate_id =
171 crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
161 sysroot_crates.insert(krate, crate_id); 172 sysroot_crates.insert(krate, crate_id);
162 names.insert(crate_id, krate.name(&sysroot).to_string()); 173 names.insert(crate_id, krate.name(&sysroot).to_string());
163 } 174 }
@@ -186,7 +197,11 @@ impl ProjectWorkspace {
186 let root = tgt.root(&cargo); 197 let root = tgt.root(&cargo);
187 if let Some(file_id) = load(root) { 198 if let Some(file_id) = load(root) {
188 let edition = pkg.edition(&cargo); 199 let edition = pkg.edition(&cargo);
189 let crate_id = crate_graph.add_crate_root(file_id, edition); 200 // FIXME: cfg options
201 // Default to enable test for workspace crates.
202 let cfg_options = CfgOptions::default().atom("test".into());
203 let crate_id =
204 crate_graph.add_crate_root(file_id, edition, cfg_options);
190 names.insert(crate_id, pkg.name(&cargo).to_string()); 205 names.insert(crate_id, pkg.name(&cargo).to_string());
191 if tgt.kind(&cargo) == TargetKind::Lib { 206 if tgt.kind(&cargo) == TargetKind::Lib {
192 lib_tgt = Some(crate_id); 207 lib_tgt = Some(crate_id);