diff options
-rw-r--r-- | crates/ra_batch/src/lib.rs | 8 | ||||
-rw-r--r-- | crates/ra_cfg/src/lib.rs | 24 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 3 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/world.rs | 8 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 36 |
7 files changed, 59 insertions, 40 deletions
diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index a5fc2a23e..602beb439 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs | |||
@@ -43,8 +43,12 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId, | |||
43 | ); | 43 | ); |
44 | 44 | ||
45 | // FIXME: cfg options? | 45 | // FIXME: cfg options? |
46 | let default_cfg_options = | 46 | let default_cfg_options = { |
47 | get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); | 47 | let mut opts = get_rustc_cfg_options(); |
48 | opts.insert_atom("test".into()); | ||
49 | opts.insert_atom("debug_assertion".into()); | ||
50 | opts | ||
51 | }; | ||
48 | 52 | ||
49 | let (crate_graph, _crate_names) = | 53 | let (crate_graph, _crate_names) = |
50 | ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { | 54 | ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { |
diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs index e1c92fbba..1bee3eb99 100644 --- a/crates/ra_cfg/src/lib.rs +++ b/crates/ra_cfg/src/lib.rs | |||
@@ -36,26 +36,20 @@ impl CfgOptions { | |||
36 | self.check(&parse_cfg(attr)) | 36 | self.check(&parse_cfg(attr)) |
37 | } | 37 | } |
38 | 38 | ||
39 | pub fn atom(mut self, name: SmolStr) -> CfgOptions { | 39 | pub fn insert_atom(&mut self, key: SmolStr) { |
40 | self.atoms.insert(name); | 40 | self.atoms.insert(key); |
41 | self | ||
42 | } | 41 | } |
43 | 42 | ||
44 | pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { | 43 | pub fn remove_atom(&mut self, name: &str) { |
45 | self.key_values.insert((key, value)); | 44 | self.atoms.remove(name); |
46 | self | ||
47 | } | 45 | } |
48 | 46 | ||
49 | /// Shortcut to set features | 47 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { |
50 | pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions { | 48 | self.key_values.insert((key, value)); |
51 | for feat in iter { | ||
52 | self = self.key_value("feature".into(), feat); | ||
53 | } | ||
54 | self | ||
55 | } | 49 | } |
56 | 50 | ||
57 | pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { | 51 | /// Shortcut to set features |
58 | self.atoms.remove(name); | 52 | pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) { |
59 | self | 53 | iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat)); |
60 | } | 54 | } |
61 | } | 55 | } |
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 34dd79574..208c2f16f 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -561,12 +561,14 @@ fn cfg_test() { | |||
561 | "#, | 561 | "#, |
562 | crate_graph! { | 562 | crate_graph! { |
563 | "main": ("/main.rs", ["std"]), | 563 | "main": ("/main.rs", ["std"]), |
564 | "std": ("/lib.rs", [], CfgOptions::default() | 564 | "std": ("/lib.rs", [], { |
565 | .atom("test".into()) | 565 | let mut opts = CfgOptions::default(); |
566 | .key_value("feature".into(), "foo".into()) | 566 | opts.insert_atom("test".into()); |
567 | .key_value("feature".into(), "bar".into()) | 567 | opts.insert_key_value("feature".into(), "foo".into()); |
568 | .key_value("opt".into(), "42".into()) | 568 | opts.insert_key_value("feature".into(), "bar".into()); |
569 | ), | 569 | opts.insert_key_value("opt".into(), "42".into()); |
570 | opts | ||
571 | }), | ||
570 | }, | 572 | }, |
571 | ); | 573 | ); |
572 | 574 | ||
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 25dad81eb..b16b48258 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -62,7 +62,11 @@ impl S { | |||
62 | "#, | 62 | "#, |
63 | ); | 63 | ); |
64 | db.set_crate_graph_from_fixture(crate_graph! { | 64 | db.set_crate_graph_from_fixture(crate_graph! { |
65 | "main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), | 65 | "main": ("/main.rs", ["foo"], { |
66 | let mut opts = CfgOptions::default(); | ||
67 | opts.insert_atom("test".into()); | ||
68 | opts | ||
69 | }), | ||
66 | "foo": ("/foo.rs", []), | 70 | "foo": ("/foo.rs", []), |
67 | }); | 71 | }); |
68 | assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); | 72 | assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); |
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 24f1b91f6..2d92fe1c5 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -325,7 +325,8 @@ impl Analysis { | |||
325 | let file_id = FileId(0); | 325 | let file_id = FileId(0); |
326 | // FIXME: cfg options | 326 | // FIXME: cfg options |
327 | // Default to enable test for single file. | 327 | // Default to enable test for single file. |
328 | let cfg_options = CfgOptions::default().atom("test".into()); | 328 | let mut cfg_options = CfgOptions::default(); |
329 | cfg_options.insert_atom("test".into()); | ||
329 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 330 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); |
330 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); | 331 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); |
331 | change.set_crate_graph(crate_graph); | 332 | change.set_crate_graph(crate_graph); |
diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 27da751ab..0eb684de5 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs | |||
@@ -98,8 +98,12 @@ impl WorldState { | |||
98 | } | 98 | } |
99 | 99 | ||
100 | // FIXME: Read default cfgs from config | 100 | // FIXME: Read default cfgs from config |
101 | let default_cfg_options = | 101 | let default_cfg_options = { |
102 | get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); | 102 | let mut opts = get_rustc_cfg_options(); |
103 | opts.insert_atom("test".into()); | ||
104 | opts.insert_atom("debug_assertion".into()); | ||
105 | opts | ||
106 | }; | ||
103 | 107 | ||
104 | // Create crate graph from all the workspaces | 108 | // Create crate graph from all the workspaces |
105 | let mut crate_graph = CrateGraph::default(); | 109 | let mut crate_graph = CrateGraph::default(); |
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 640a5ebd3..a4d117e34 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -134,13 +134,16 @@ impl ProjectWorkspace { | |||
134 | json_project::Edition::Edition2015 => Edition::Edition2015, | 134 | json_project::Edition::Edition2015 => Edition::Edition2015, |
135 | json_project::Edition::Edition2018 => Edition::Edition2018, | 135 | json_project::Edition::Edition2018 => Edition::Edition2018, |
136 | }; | 136 | }; |
137 | let mut cfg_options = default_cfg_options.clone(); | 137 | let cfg_options = { |
138 | for name in &krate.atom_cfgs { | 138 | let mut opts = default_cfg_options.clone(); |
139 | cfg_options = cfg_options.atom(name.into()); | 139 | for name in &krate.atom_cfgs { |
140 | } | 140 | opts.insert_atom(name.into()); |
141 | for (key, value) in &krate.key_value_cfgs { | 141 | } |
142 | cfg_options = cfg_options.key_value(key.into(), value.into()); | 142 | for (key, value) in &krate.key_value_cfgs { |
143 | } | 143 | opts.insert_key_value(key.into(), value.into()); |
144 | } | ||
145 | opts | ||
146 | }; | ||
144 | crates.insert( | 147 | crates.insert( |
145 | crate_id, | 148 | crate_id, |
146 | crate_graph.add_crate_root(file_id, edition, cfg_options), | 149 | crate_graph.add_crate_root(file_id, edition, cfg_options), |
@@ -171,7 +174,12 @@ impl ProjectWorkspace { | |||
171 | for krate in sysroot.crates() { | 174 | for krate in sysroot.crates() { |
172 | if let Some(file_id) = load(krate.root(&sysroot)) { | 175 | if let Some(file_id) = load(krate.root(&sysroot)) { |
173 | // Crates from sysroot have `cfg(test)` disabled | 176 | // Crates from sysroot have `cfg(test)` disabled |
174 | let cfg_options = default_cfg_options.clone().remove_atom(&"test".into()); | 177 | let cfg_options = { |
178 | let mut opts = default_cfg_options.clone(); | ||
179 | opts.remove_atom("test"); | ||
180 | opts | ||
181 | }; | ||
182 | |||
175 | let crate_id = | 183 | let crate_id = |
176 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 184 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); |
177 | sysroot_crates.insert(krate, crate_id); | 185 | sysroot_crates.insert(krate, crate_id); |
@@ -202,9 +210,11 @@ impl ProjectWorkspace { | |||
202 | let root = tgt.root(&cargo); | 210 | let root = tgt.root(&cargo); |
203 | if let Some(file_id) = load(root) { | 211 | if let Some(file_id) = load(root) { |
204 | let edition = pkg.edition(&cargo); | 212 | let edition = pkg.edition(&cargo); |
205 | let cfg_options = default_cfg_options | 213 | let cfg_options = { |
206 | .clone() | 214 | let mut opts = default_cfg_options.clone(); |
207 | .features(pkg.features(&cargo).iter().map(Into::into)); | 215 | opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); |
216 | opts | ||
217 | }; | ||
208 | let crate_id = | 218 | let crate_id = |
209 | crate_graph.add_crate_root(file_id, edition, cfg_options); | 219 | crate_graph.add_crate_root(file_id, edition, cfg_options); |
210 | names.insert(crate_id, pkg.name(&cargo).to_string()); | 220 | names.insert(crate_id, pkg.name(&cargo).to_string()); |
@@ -321,11 +331,11 @@ pub fn get_rustc_cfg_options() -> CfgOptions { | |||
321 | Ok(rustc_cfgs) => { | 331 | Ok(rustc_cfgs) => { |
322 | for line in rustc_cfgs.lines() { | 332 | for line in rustc_cfgs.lines() { |
323 | match line.find('=') { | 333 | match line.find('=') { |
324 | None => cfg_options = cfg_options.atom(line.into()), | 334 | None => cfg_options.insert_atom(line.into()), |
325 | Some(pos) => { | 335 | Some(pos) => { |
326 | let key = &line[..pos]; | 336 | let key = &line[..pos]; |
327 | let value = line[pos + 1..].trim_matches('"'); | 337 | let value = line[pos + 1..].trim_matches('"'); |
328 | cfg_options = cfg_options.key_value(key.into(), value.into()); | 338 | cfg_options.insert_key_value(key.into(), value.into()); |
329 | } | 339 | } |
330 | } | 340 | } |
331 | } | 341 | } |